Summary: Atwood’s law states that any application that can be implemented in Javascript will eventually be implemented in Javascript. The server is the home of Python, the hottest field of machine learning, but what about mobile? Python is not available on Android or iOS by default. But where there are browsers, there are JS, and now there is a new scenario – applets.

Source: Alibaba F2E By Xu Lun

Atwood’s law states that any application that can be implemented in Javascript will eventually be implemented in Javascript. The server is the home of Python, the hottest field of machine learning, but what about mobile? Python is not available on Android or iOS by default. But where there are browsers, there are JS, and now there is a new scenario – applets.

In addition, there are Native frameworks such as React Native for training without networking.

So wherever there is a front end, there is a framework for machine learning.

The only problem with JS is that it’s changing so fast, with so many new libraries coming out every year, and so many older libraries that have been declared unmaintainable. However, the tools themselves often change, but their types are very stable.

Methodology for selecting machine learning tools

What kind of tools do we need to write machine learning algorithms?

Machine learning tools can be divided into the following four levels:

Level 1: frameworks that directly serve specific domains

First we need frameworks that directly serve specific domains, such as those dealing with CV, NLP, recommendation algorithms, etc.

NLP. Js, for example, was last released in October 2020. The code for nlP.js looks like this:

const { NlpManager } = require('node-nlp'); const manager = new NlpManager({ languages: ['en'], forceNER: true }); // Adds the utterances and intents for the NLP manager.addDocument('en', 'goodbye for now', 'greetings.bye'); manager.addDocument('en', 'bye bye take care', 'greetings.bye'); manager.addDocument('en', 'okay see you later', 'greetings.bye'); manager.addDocument('en', 'bye for now', 'greetings.bye'); manager.addDocument('en', 'i must go', 'greetings.bye'); manager.addDocument('en', 'hello', 'greetings.hello'); manager.addDocument('en', 'hi', 'greetings.hello'); manager.addDocument('en', 'howdy', 'greetings.hello'); // Train also the NLG manager.addAnswer('en', 'greetings.bye', 'Till next time'); manager.addAnswer('en', 'greetings.bye', 'see you soon! '); manager.addAnswer('en', 'greetings.hello', 'Hey there! '); manager.addAnswer('en', 'greetings.hello', 'Greetings! '); // Train and save the model. (async() => { await manager.train(); manager.save(); const response = await manager.process('en', 'I should go now'); console.log(response); }) ();Copy the code

It’s easy to run, just install a library:

npm install node-nlp
Copy the code

The speed of training is also fast:

Epoch 1 Loss 0.4629286907733636 time 1ms Epoch 2 loss 0.2818764774939686 time 0ms Epoch 3 Loss 0.16872372018062168 time 0ms Epoch 4 Loss 0.11241683507408215 time 0ms... Epoch 31 Loss 0.00004645272306535786 time 0msCopy the code

The output looks something like this:

{ locale: 'en', utterance: 'I should go now', settings: undefined, languageGuessed: false, localeIso2: 'en', language: 'English', nluAnswer: { classifications: [ [Object] ], entities: undefined, explanation: undefined }, classifications: [ { intent: 'greetings.bye', score: 1 } ], intent: 'greetings.bye', score: 1, domain: 'default', sourceEntities: [ { start: 12, end: 14, resolution: [Object], text: 'now', typeName: 'datetimeV2.datetime' } ], entities: [ { start: 12, end: 14, len: 3, accuracy: 0.95, sourceText: 'now', utteranceText: 'now', entity: 'datetime', resolution: [Object] } ], answers: [ { answer: 'Till next time', opts: undefined }, { answer: 'see you soon!', opts: undefined } ], answer: 'see you soon! [], sentiment: {score: 0.5, numWords: 4, numHits: 1, average: 0.125, type: 'senticon', locale: 'en', vote: 'positive' } }Copy the code

Level 2: Deep learning framework

The second is our core content, the deep learning framework.

When it comes to Javascript deep learning, tensorflow.js is still dominant. Let’s take a look at a classic example of reinforcement learning:

Using the browser’s Local storage and IndexDB as storage, training while showing the training effect process, very front-end style.

Let’s look at another example from Microsoft, which supports webGL and WASM, but is not easy to use in a browser:

It is also important to note that the framework used for the front end is not a simple port of native or Python frameworks, for example to deal with compatibility issues:

Many of you are familiar with Tensorflow, so let’s write a webpage and tune the TF API in it:

<! DOCTYPE html> <html> <head> <meta encoding="UTF-8"/> <script SRC = "https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js" > < / script > < / head > < body > < div Id = "tf - display" > < / div > < script > let a = tf. Tensor1d ([1.0]); let d1 = document.getElementById("tf-display"); d1.innerText = a; </script> </body> </html>Copy the code

Level 3: Machine learning framework

The third is the framework of machine learning. Understanding deep learning is not enough. Traditional machine learning may work better when it is closer to the business and saves computing resources. For example, you can use the MLJS library at github.com/mljs/ml

For example, if we want to make a k-means cluster, we can use the ML-Kmeans library of MLJS framework:

const kmeans = require('ml-kmeans'); Let data = [[1, 1, 1], [1, 2, 1], [1, 1, 1], [1, 1, 1.5]]. let centers = [[1, 2, 1], [-1, -1, -1]]; let ans = kmeans(data, 2, { initialization: centers }); console.log(ans);Copy the code

Pack a bag and play:

npm i ml-kmeans
Copy the code

The running results are as follows:

KMeansResult {clusters: [0, 0, 1, 1], Centroids: [{CentroID: [Array], Error: 0.25, size: 2}, {Centroid: Converged: true, Iterations: 2, [Symbol(distance)]: [Function: squaredEuclidean]}Copy the code

We can also use it directly in web pages. For example, let’s write a k-nearest neighbor example:

<! DOCTYPE html> <html> <head> <meta encoding="UTF-8" /> <script SRC = "https://www.lactame.com/lib/ml/4.0.0/ml.min.js" > < / script > < / head > < body > < div id = "ml - display" > < / div > < script > const  train_dataset = [ [0, 0, 0], [0, 1, 1], [1, 1, 0], [2, 2, 2], [1, 2, 2], [2, 1, 2], ]; const train_labels = [0, 0, 0, 1, 1, 1]; let knn = new ML.KNN(train_dataset, train_labels, { k: 2 }); // consider 2 neighbor const test_dataset = [[0.9, 0.9, 0.9], [1.1, 1.1, 1.1], [1.1, 1.1, 1.2], [1.2, 1.2, 1.2]]; let ans = knn.predict(test_dataset); let d1 = document.getElementById("ml-display"); d1.innerText = ans; </script> </body> </html>Copy the code

Finally, we take a decision tree example, using the Iris data set of MLJS. Ml-dataset – Iris and ML-CART need to be installed by NPM:

const irisDataset = require('ml-dataset-iris');
const DecisionTreeClassifier = require('ml-cart');

const trainingSet = irisDataset.getNumbers();
const predictions = irisDataset
  .getClasses()
  .map((elem) => irisDataset.getDistinctClasses().indexOf(elem));

const options = {
  gainFunction: 'gini',
  maxDepth: 10,
  minNumSamples: 3,
};

const classifier = new DecisionTreeClassifier.DecisionTreeClassifier(options);
classifier.train(trainingSet, predictions);
const result = classifier.predict(trainingSet);

console.log(result);
Copy the code

The following output is displayed:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ... 50 more items ]Copy the code

Level 4: Math and statistics library

The fourth level is the math and statistics library. Doing statistics and math is often the best way to understand a business. At this time we can not empty hands, also need tools.

IO/stdlib. IO/stdlib. IO/stdlib. IO

Let’s install it for the next experiment:

npm install @stdlib/stdlib
Copy the code

Like various mathematical functions:

Or take various random distributions:

Let’s take a normal distribution as an example and see how stdlib describes the distribution:

const Normal = require( '@stdlib/stats/base/dists/normal' ).Normal;
let dist1 = new Normal( 0, 1 );
console.log(dist1);
let m1 = dist1.mean;
console.log(m1);
let v1 = dist1.variance;
console.log(v1);
Copy the code

The two parameters used to construct Normal are mean and variance.

The output is as follows:

Normal { mu: [Getter/Setter], sigma: [Getter/Setter] }
0
1
Copy the code

More than 50 datasets are supported by this unseemly standard library. Here’s a quick example:

const capitals = require( '@stdlib/datasets/us-states-capitals' );
const data_c = capitals();
console.log(data_c);
Copy the code

The following output is displayed:

[
  'Montgomery',     'Juneau',         'Phoenix',
  'Little Rock',    'Sacramento',     'Denver',
  'Hartford',       'Dover',          'Tallahassee',
  'Atlanta',        'Honolulu',       'Boise',
  'Springfield',    'Indianapolis',   'Des Moines',
  'Topeka',         'Frankfort',      'Baton Rouge',
  'Augusta',        'Annapolis',      'Boston',
  'Lansing',        'Saint Paul',     'Jackson',
  'Jefferson City', 'Helena',         'Lincoln',
  'Carson City',    'Concord',        'Trenton',
  'Santa Fe',       'Albany',         'Raleigh',
  'Bismarck',       'Columbus',       'Oklahoma City',
  'Salem',          'Harrisburg',     'Providence',
  'Columbia',       'Pierre',         'Nashville',
  'Austin',         'Salt Lake City', 'Montpelier',
  'Richmond',       'Olympia',        'Charleston',
  'Madison',        'Cheyenne'
]
Copy the code

conclusion

To sum up, if you want to do business from 0 to 1, use level 1 tools as much as possible, which will help you get off the ground quickly. But if you want to do incremental, especially difficult growth, the third and fourth levels are preferred because they help you understand the data more deeply.

The original link

This article is the original content of Aliyun and shall not be reproduced without permission.