Shock!!!!! Birthday!! The front end can also write machine learning!! First JS in the universe!!
As a “front-end less than”, see well-known furnaces
TensorFlow JS version unexpectedly, my heart is very chicken frozen.
Ha ha ha ha, usually in the school that help alchemy of always look down on our drawing page, today let you see our front also to machine learning skills!!
Well, I’m just playing a little too much. Since tensorflow. js has just come out, there are few Chinese materials on the website, so I plan to sort out the translation of articles on tensorflow. js for readers.
This is the first translation of the tutorial, original address:
TensorFlow.jsjs.tensorflow.org
Introduction to the
Tensorflow.js is a browser-based machine learning library that uses WebGL to accelerate machine learning. It provides a high-level JavaScript API interface. It brings high-performance machine learning building blocks to your fingertips, enabling you to train neural networks in a browser or run pre-trained models in inference mode. For guidance on installing/configuring tensorflow.js, see the Getting Started Guide.
Tensors (tensor)
The core representation of data in tensorflow.js is a tensor: a one-dimensional or multidimensional array of values. Every instance of Tensor has a shape property that defines the shape of the dimensions of the array — that is, how many dimensions the array has, and how many values each dimension has. The main tensor constructor is tf.tensor:
// 2x3 tensor const Shape = [2, 3]; // Linear a = tf.tensor([1.0, 2.0, 3.0, 10.0, 20.0, 30.0], shape); a.print(); / / / / output of tensor value output: [[1, 2, 3], / / [10, 20, 30]] / / tensor dimensions can be speculated that shape: Const b = tf. Tensor ([[1.0, 2.0, 3.0], [10.0, 20.0, 30.0]]). b.print(); // Output: [[1, 2, 3], // [10, 20, 30]Copy the code
However, if you only need to construct low-dimensional tensors, we recommend using the following functions instead of TF.scalar, TF.tensor1d, TF.tensor2d, TF.tensor3D, and TF.tensor4d to enhance the readability of your code.
The following example uses tf.tensor2d to create the same tensor as the above example:
Const c = tf. Tensor2d ([[1.0, 2.0, 3.0], [10.0, 20.0, 30.0]]). c.print(); // Output: [[1, 2, 3], // [10, 20, 30]Copy the code
Tensorflow.js also provides some tensor constructors with initialization functions, such as creating tf.zeros with all values of 0 or tf.ones with values of 1:
// Const zeros = tf.zeros([3, 5]); // Const zeros = tf.zeros([3, 5]); / / output: [[0, 0, 0, 0, 0], / / [0, 0, 0, 0, 0], / / [0, 0, 0, 0, 0]]Copy the code
In tensorflow.js, tensors are immutable. Once they’re created, you can’t change their values; But you can generate new tensors by performing operations on them.
Variables
Variables are initialized from the values of tensors. Unlike tensors, however, their values are variable. You can assign a new tensor to an existing variable using the assign method:
const initialValues = tf.zeros([5]); const biases = tf.variable(initialValues); // Initialize biases. Print (); // Output: [0, 0, 0, 0, 0] const updatedValues = tf.tensor1d([0, 1, 0, 1, 0]); biases.assign(updatedValues); // Update the value of biases. Print (); // Output: [0, 1, 0, 1, 0]Copy the code
Variables are primarily used to store and update values during model training.
Operations (Operations)
When you use tensors to store data, you can use operations (OPS) to perform data operations. Tensorflow.js provides multiple OPS for linear algebra and machine learning. Because tensors are immutable, these operations do not modify their values; Instead, OPS returns a new tensor.
There are many operations available, including unary operations such as square:
Const d = tf.tensor2d([[1.0, 2.0], [3.0, 4.0]]); const d_squared = d.square(); d_squared.print(); // Output: [1, 4], // [9, 16]]Copy the code
There are also binary operations such as add, sub, and mul:
Const e = tf.tensor2d([[1.0, 2.0], [3.0, 4.0]]); Const f = tf.tensor2d([[5.0, 6.0], [7.0, 8.0]]); const e_plus_f = e.add(f); e_plus_f.print(); // Output: [[6, 8], // [10, 12]]Copy the code
The TENsorflow. js API supports chain calls, so you can continue calling OPS on the result of ops:
const sq_sum = e.add(f).square(); sq_sum.print(); // Output: [[36, 64], // [100, 144]] // All operators are public in the main namespace, // so you can do this like 👇 : const sq_sum = tf.square(tf.add(e, f));Copy the code
Models and Layers
Conceptually, a model is a function that, given some input, produces some desired output. There are two ways to create a model in tensorflow.js. You can use OPS directly to represent what the model does. Such as:
Y = 2x^2 + 4x + 8 const A = tf.scalar(2); const b = tf.scalar(4); const c = tf.scalar(8); Function predict(input) {// y = a * x ^ 2 + b * x + c return tf.tidy(() => {const x = tf.scalar(input); const ax2 = a.mul(x.square()); const bx = b.mul(x); const y = ax2.add(bx).add(c); return y; }); } // predict the output const result = predict(2); Result.print () // Output: 24Copy the code
You can also use the more abstract API Tf.Model to build multi-level models, which is a popular abstraction in deep learning. The following code builds a tF.sequential model:
const model = tf.sequential();
model.add(
tf.layers.simpleRNN({
units: 20,
recurrentInitializer: 'GlorotNormal',
inputShape: [80, 4]
})
);
const optimizer = tf.train.sgd(LEARNING_RATE);
model.compile({optimizer, loss: 'categoricalCrossentropy'});
model.fit({x: data, y: labels)});
Copy the code
There are many different types of layers in tensorflow. js, such as tf.layers.simpleRNN, tf.layers.gru and tf.layers. LSTM.
Memory management: Dispose and Tf.Tidy
Because Tensorflow.js uses the GPU to speed up mathematical operations, it is necessary to manage GPU memory when working with tensors and variables.
Tensorflow.js provides two functions that help solve this problem: Dispose and tf.Tidy.
dispose
You can call Dispose on tensors or variables to empty occupied GPU memory:
Const x = tf.tensor2d([[0.0, 2.0], [4.0, 6.0]]); const x_squared = x.square(); x.dispose(); x_squared.dispose();Copy the code
tf.tidy
After a large number of tensor operations, it can be cumbersome to call dispose one by one. Therefore, tensorflow.js provides another function tf.Tidy, which is somewhat similar to the “scope” in JavaScript, but is oriented towards “GPU-backed tensors”. Tf.tidy clears all intermediate tensors and frees their GPU memory when executed. But it does not clear the return value of the inner function.
/ / tf. Tidy takes a function to tidy up after const business = tf, tidy, () = > {/ / tf. Tidy by tensor using this function will remove all the GPU memory / / But it does not clear the tensor that is returned // //. Even in short operations like the one below, // tensorflow.js creates intermediate tensors. Therefore, // putting your math operations in a tf.Tidy is a great choice! Const y = tf.tensor1d([1.0, 2.0, 3.0, 4.0]); const z = tf.ones([4]); return y.sub(z).square().mean(); }); Average. Print () // Output: 3.5Copy the code
Proper use of TF.Tidy will help mitigate memory leaks in your application and also help control when memory is reclaimed.
Two important tips
- Passed to the
tf.tidy
The function should be synchronous and should not return promises. We recommend placing asynchronous code such as UI updates or network requests intf.tidy
Outside the function. tf.tidy
No cleanup of variables. Variables usually exist throughout the lifecycle of a machine learning model, so tensorflow.js does not clean them up, even though they were created ontf.tidy
. However, you can call it manuallydispose
Get rid of them.