A brief introduction TensorFlow. Js
TensorFlow is the second generation of ARTIFICIAL intelligence learning system developed by Google based on DistBelief. Its name comes from its operating principle. Tensor means n-dimensional arrays, Flow means calculations based on data Flow diagrams, TensorFlow is the calculation of tensors flowing from one end of a Flow diagram to the other.
TensorFlow is a system that transmits complex data structures to an artificial intelligence neural network for analysis and processing. It is widely used in many machine learning and deep learning fields, such as speech recognition or image recognition, and can run on devices ranging from a smartphone to thousands of data center servers.
The data flow operation model of TensorFlow is shown below:
Tensorflow.js is an open source WebGL-accelerated javascript library for developing machine learning projects. CNN(convolutional neural network), RNN(recurrent neural network) and so on can be created in the browser with it, and these models can be trained with the GPU processing power of the terminal.
So, before we study machine learning, we need to understand some of these concepts.
We solve a problem in two ways: One is called Model Driven, by studying the physical and chemical mechanism models of the object, modeling the object to solve problems. For example, we are familiar with Newton’s three laws. For the formula above, we know the input X and mechanism model F (), and need to solve the Y we want to get. The other is called data driven. As people encounter more and more complex problems, it is more and more expensive to search for object mechanism model, while the cost of data acquisition is less and less. Therefore, researchers begin to think about problems from another perspective, whether we can get what we want through analysis of these data. That is, I know some samples (x,y) or I only know x, and I want to analyze them to get the model f() of the object, so that when I have an X again, I can get the Y THAT I want. If not strictly speaking, all such data analysis methods can be classified as machine learning.
So a machine learning should usually include basic elements: training data, parametric model, loss function, training algorithm. Needless to say, training data is useful; The parametric model is used to approximate f(); Loss function is an index to measure the merits and demerits of a model, such as the accuracy of model recognition and classification. The training algorithm can also be called the optimization function, which is used to constantly update the parameters of the model to minimize the loss function and get a better model, or it can be called the learning machine. The following will introduce some basic concepts in machine learning, which may not be very coherent.
model
A model is a description of things in the problem domain in the real world, not a description of software design. In machine learning, the model is similar to a set of parameters with some parameters to be trained for approximating f() mentioned above. In the parameter space, f() is just a point, and the model I mentioned is also a point, and since the parameters can change, all I have to do is make this point of my model as close as possible to the point of the real f().
There are many model algorithms for machine learning, but the more commonly used models can be summarized as three kinds:
- Network based model: the most typical is neural network, the model has several layers, each layer has a number of nodes, each two nodes have a variable parameter between, through a large number of nonlinear neurons, neural network can approach any function.
- Model based on kernel method: Typical ones are SVM and Gaussian Process. SVM maps input vector to high-dimensional space through a kernel and then finds several hyperplanes to divide data into several categories. The kernel of SVM can be adjusted.
- Model based on statistical learning: The simplest example is Bayesian learning machine, statistical learning method is to use mathematical statistics mathematical tools to realize the training of learning machine, usually the parameters in the model are some statistical characteristics such as mean variance, and finally make the expectation of correct probability to reach the maximum.
In real life, models are everywhere, such as world maps, charts and so on. To illustrate what the model is, let’s take an example: Barcelona house prices change with the number of rooms.
- With only five samples, it’s not reliable enough;
- There are only two parameters, but in fact there are more factors affecting the price of a house, such as geographical location, age of the house, etc.
For the first problem, we can solve it by adding a sample number, such as 1 million data. For the second problem, we can add more axes. In 2D we can draw a straight line, in 3D we can draw a plane.
The neural network
In biology, a typical neural network consists of dendritic processes, axons, and synapses.
- Dendrites: Places where data is entered.
- Axon: Output end.
- Synapse: Structure of communication between nerves. It carries electrical signals from the end of axons to the dendrites of nearby nerves. These synaptic structures are key to learning because they increase or decrease the activity of electrical signals during use.
In machine learning. The simplified nerve looks like this:
In machine learning, the following parts are required:
- Input: Input parameter.
- Weight: Like synapses, they adjust neural activity by increasing or decreasing in order to achieve better linear regression.
- Linear function: Each nerve acts like a Linear regression function, and so far a Linear regression function requires only one nerve.
- Activation functions: We can provide Activation functions to change from one Scalar to another non-linear function. For example: Sigmoid, RELU, TANH.
- Output: the Output result calculated by the activation function.
The use of activation function is very useful, it is the essence of neural network. Neural networks cannot be intelligent without activation functions. The reason is that although you might have a lot of nerves in a network, the output of a neural network is always a linear regression. We need some mechanism to change this independent linear regression to nonlinear in order to solve nonlinear problems. The following figure shows the transition from a linear function to a nonlinear function:
Training model
In the 2D linear regression example above, drawing a line on a graph is enough to start predicting new data. The purpose of “deep learning,” however, is for our neural networks to learn to draw that line. Refer to the following links to learn: Training Models
To draw a simple line we just need a very simple neural network that includes a single nerve which is a relatively simple case for training models, but other models are much more complicated, such as categorizing two sets of data. For example, “train to learn” how to draw the following image:
In order to understand gDA, we need to know that each algorithm (linear regression, logistic regression, etc.) has a different cost function to measure these errors.
The cost function always converges to some point, and it may be convex or non-convex. The lowest point of convergence will be found at 0% error, which is our goal.
But when we use gradient descent, we start at a random point, but we don’t know where it is. Imagine you’re on a mountain, completely blind, and you need to make your way down, step by step, to the lowest position. If the terrain is complex (like a non-convex function), the descent will be more complicated.
I’m not going to go into the details of what gradient descent is. All you need to remember is that it’s an optimization algorithm that trains the AI model to minimize predictive errors. This algorithm requires time and GPU to compute matrix multiplication. Convergence point is usually difficult to achieve in the first round of execution, so we need to tune some hyperparameters such as learning rate, or add some regularization.
After repeated gradient descent, we get very close to the convergence point, and the error rate is close to 0%. At this point, our model has been created and we can start making predictions.
TensorFlow. Use js
Tensorflow. js provides a simple way to create a neural network. First, we’ll create a LinearModel class and add the trainModel method. For this type of model I will use a sequential model, which means that the outputs of one layer are the inputs of the next, such as when the topology of the model is a simple stack, with no branching and skipping.
In the trainModel method we will define the layers (only one needs to be used, which is sufficient for linear regression problems) :
import * as tf from '@tensorflow/tfjs';
/**
* Linear model class
*/
export default class LinearModel {
/**
* Train model
*/
async trainModel(xs, ys){
const layers = tf.layers.dense({
units: 1, // Dimensionality of the output space
inputShape: [1], // Only one param
});
const lossAndOptimizer = {
loss: 'meanSquaredError',
optimizer: 'sgd', // Stochastic gradient descent
};
this.linearModel = tf.sequential();
this.linearModel.add(layers); // Add the layer
this.linearModel.compile(lossAndOptimizer);
// Start the model training!
await this.linearModel.fit(
tf.tensor1d(xs),
tf.tensor1d(ys),
);
}
...more
}
Copy the code
The usage of this class is as follows:
const model = new LinearModel();
// xs and ys -> array of numbers (x-axis and y-axis)
Copy the code
##2, the prediction part using tensorflow. js is usually simpler. The training model needs to define some hyperparameters. We’ll add this method to the LinearRegressor class:
import * as tf from '@tensorflow/tfjs';
exportdefault class LinearModel { ... trainingCode predict(value){return Array.from(
this.linearModel
.predict(tf.tensor2d([value], [1, 1]))
.dataSync()
)
}
}
Copy the code
We then call this function to fill in the parameters to predict:
const prediction = model.predict(500); // Predict forThe number 500 console.log(prediction) // => 420.423Copy the code
Stackblitz.com/edit/linear…
3. Use trained models
There are many models available in tensorflow.js, and you can create models using TensorFlow or Keras and import them into tensorflow.js. For example, you can use the Posenet model (real-time human pose simulation) to do something fun:
Github.com/aralroca/po…
import * as posenet from '@tensorflow-models/posenet';
// Constants
const imageScaleFactor = 0.5;
const outputStride = 16;
const flipHorizontal = true;
const weight = 0.5;
// Load the model
const net = await posenet.load(weight);
// Do predictions
const poses = await net
.estimateSinglePose(
imageElement,
imageScaleFactor,
flipHorizontal,
outputStride
);
Copy the code
Then add test data to the model:
{
"score": 0.32371445304906."keypoints": [{"position": {
"y": 76.291801452637."x": 253.36747741699},"part": "nose"."score": 0.99539834260941}, {"position": {
"y": 71.10383605957."x": 253.54365539551},"part": "leftEye"."score": 0.98781454563141}, // Andfor: rightEye, leftEar, rightEar, leftShoulder, rightShoulder
// leftElbow, rightElbow, leftWrist, rightWrist, leftHip, rightHip,
// leftKnee, rightKnee, leftAnkle, rightAnkle
]
}
Copy the code
Here is the sample code: github.com/aralroca/fi… We can import models from outside to tensorflow.js. In the following example, we will use a Keras model for number recognition (file format h5). To do this, we need to use TFJS_Converter.
pip install tensorflowjs
Copy the code
Then, use the transform tool:
tensorflowjs_converter --input_format keras keras/cnn.h5 src/assets
Copy the code
Now you can import the model into your JS code:
// Load model
const model = await tf.loadModel('./assets/model.json');
// Prepare image
let img = tf.fromPixels(imageData, 1);
img = img.reshape([1, 28, 28, 1]);
img = tf.cast(img, 'float32');
// Predict
const output = model.predict(img);
Copy the code
With just a few lines of code, you can use the number recognition model in Keras. Of course, we can also add some more playful logic, such as adding a canvas to draw a number and then capturing the image to identify the number.
Github.com/aralroca/MN…
If the hardware doesn’t work, training the model on the browser can be very inefficient. Tensorflow.js uses the WebGL interface to speed up training, but even then it is 1.5-2 times slower than the TensorFlow Python version.
However, before Tensorflow.js, it was almost impossible to use machine learning models in browsers without API interaction. Now we can train and use models offline in our application. Also, not having to interact with the server makes forecasting faster.
Original link: aralroca.com/2018/08/24/… Reference: Machine learning basic concepts tensorflow.js basic concepts