### Tensors (tensors)

` `

` `// 2x3 Tensor
const shape = [2, 3]; // 2 rows, 3 columns
const a = tf.tensor([1.0, 2.0, 3.0, 10.0, 20.0, 30.0], shape);
a.print(); // print Tensor values
// Output: [[1 , 2 , 3 ],
// [10, 20, 30]]
// The shape can also be inferred:
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]]

` `

` `

` `

` `

` `

` `

` `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]]

` `

` `

` `// 3x5 Tensor with all values set to 0
const zeros = tf.zeros([3, 5]);
// Output: [[0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0]]

### Variables (variables)

` `

` `const initialValues = tf.zeros([5]);
const biases = tf.variable(initialValues); // initialize biases
biases.print(); // output: [0, 0, 0, 0, 0]
const updatedValues = tf.tensor1d([0, 1, 0, 1, 0]);
biases.assign(updatedValues); // update values of biases
biases.print(); // output: [0, 1, 0, 1, 0]

### Operations(Ops)

` `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]]

` `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]]

` `const sq_ sum = e.add(f).square();
sq_ sum.print();
// Output: [[36 , 64 ],
// [100, 144]]
// All operations are also exposed as functions in the main namespace,
// so you could also do the following:
const sq_ sum = tf.square(tf.add(e, f));

### Models and layers

` `// Define function
function predict(input) {
// y = a * x ^ 2 + b * x + c
// More on tf.tidy in the next section
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;
});
}
// Define constants: y = 2x^2 + 4x + 8
const a = tf.scalar(2);
const b = tf.scalar(4);
const c = tf.scalar(8);
// Predict output for input of 2
const result = predict(2);
result.print() // Output: 24

` `

` `

` `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)});

` `

` `

` `

### Memory Management

` `

` `

#### dispose

` `

` `const x = tf.tensor2d([[0.0, 2.0], [4.0, 6.0]]);
const x_ squared = x.square();
x.dispose();
x_ squared.dispose();

#### tf.tidy

` `

` `

` `

` `// tf.tidy takes a function to tidy up after
const average = tf.tidy(() => {
// tf.tidy will clean up all the GPU memory used by tensors inside
// this function, other than the tensor that is returned.
//
// Even in a short sequence of operations like the one below, a number
// of intermediate tensors get created. So it is a good practice to
// put your math ops in a tidy!
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.5

` `

#### Two important tips

tf.tidy Use is synchronous and does not return promise objects. It is recommended to keep the UI code updated, or tf.tidy The remote request is initiated after calling; tf.tidy Variables are not cleaned up. Variables are retained throughout the life cycle of the machine learning model, so tensorflow. JS does not clean them up, even if they are created in a clean environment. But you can use it dispose Manual cleaning.

## Other information