Machine learning is essentially glorified linear algebra combined with probability. Google makes this easy with the Tensor Flow Library.
Number Types
It’s important to refresh ourselves on the basic number types:
Scalar – single number
Vector – 1 x N or N x 1 numbers
Matrix – N x N numbers
Tensors are just numbers that reflect these types.
Shape
This refers to the dimensions of the tensor. Example:
5 6 8
3 4 1
The above matrix has a shape of 2, 3. Count rows first then columns.
If I have 200 tensors of dimensions 2, 3, then the shape of all of the data is:
200, 2, 3
We can use basic tf.tensor function to create tensors, but we can also use shape-specific functions. These include:
tf.scalar
tf.tensor1f
tf.tensor2d
Using the generic tensor creator, we can enter a one dimensional array and specify the shape:
const tense = tf.tensor([1,2,3,4,5,6,7,8], [2,2,2],'int32');
console.log(tense.toString());
Here we are telling the generic tensor creator to output 2 matrices of ints (default is float. Boolean can also be selected). Each matrix will be 2 x 2 in dimension. The output is:
Tensor [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
Immutability
Tensors are immutable.
3 Core Element Types
In short there are three important elements when creating a tensor using the tensor flow library:
- The data values
- The shape
- The data type
Operations
Since tensors are just standard number types, we can perform linear algebra operations on tensors, such as matrix multiplication.
Recall when performing matrix multiplication the number of columns in Matrix A must equal the number of rows in Matrix B.
We can use the matMul function to multiply matrices. Note that matrix multiplication will only work on 2 dimensional tensors n X n. For example, the code below will multiply these two matrices:
[[1, 2, 3],
[4, 5, 6]]
[[1, 2],
[3, 4],
[5, 6]]
const tense = tf.tensor([1,2,3,4,5, 6], [2,3]);
const tenseb = tf.tensor([1,2,3,4,5, 6], [3,2]);
const tenseMul = tense.matMul(tenseb);
console.log(tenseMul.toString());
This code yields the following matrix product:
Tensor
[[22, 28],
[49, 64]]
Memory Management
When working with large tensors, memory management can be an issue. Unused tensors can be removed to free-up memory. Tensors can be unassigned in this way:
tensorVar.dispose();
We can also perform memory management with the tidy() function. The code which runs inside the arrow function is now memory-managed. The tensors will be de-allocated after the function has finished running.
tf.tidy( ()=> {
let tense = tf.tensor([1,2,3,4,5,6], [2,3]);
});