What is a Tensor?
Math Concepts
Scalar, Vector, Matrix and Tensor
Matrix
means a matrix with rows and columns. Also called by matrix.
Matrix have 2 dimensions.
In Geometry, A Matrix is like a plane.
In Python, to declare a matrix :
1 | # get numpy library |
Vector
Vector is a matrix that have only 1 row and many columns; or only 1 column and many rows.
The number of elements in a vector is called length.
Example :
- Row Vector
- Column Vector
Vector have 1 dimensions.
In Geometry, A Vector is like a line. It has direction.
In Python, to declare a vector :
1 | # get numpy library |
Note you can use the reshape
method to change the shape of vector.
1 | v.reshape(3,1) |
Scalar
Scalar is a matrix with 1 row and 1 column. Also called 1 by 1 matrix.
A matrix only single element is called a scalar.
Scalar have 0 dimensions.
In Geometry, A Scalar is like a point. It have no direction nor size.
In Python, any integer or float is a scalar.
Note a scalar does not have shape because it have 0 dimensions.
Tensor
- 0D array is a scalar (Rank 0 Tensor)
- 1D array is a vector (Rank 1 Tensor)
- 2D array is a matrix (Rank 2 Tensor)
- 3D array is a tensor (Rank 3 Tensor)
- nD array for n>2 is a tensor (Rank n Tensor)
To create a Rank 3 Tensor in python, you can use np.array([matrix1, matrix2])
to make a 3D array.
1 | m1 = np.array([[5,12,6],[-3,0,14]]) |
In above example the shape of t is (2,2,3).
Multiplication
Vector Multiplication
Condition: They must have the same length.
There are 2 Types of Output we can get:
- Dot Product (inner product)
- Tensor Product (outer product)
Dot Product is heavily used.
Example of Dot Product:
Vector Vector = Scalar
Matrix Multiplication
Condition: We can only mulitply an with an matrix
Example of Dot Product:
Tensorflow
One of the biggest advantages of tensorflow is it uses not only the CPU of the computer but also as GPU. Recently Google furthered this trend by introducing TPU (tensor processing units) which improves performance even further.
Tensorflow is a good fit for neural network but sklearn is a better fit in clustering and regressions, especially preprocessing.
Note:
- Tensorflow doesn’t work with csv or xlxs files. Instead it work with Tensors (npz files) which store ndarray.
- In other words, data need to be preprocessed and save in .npz file.
Minimal example with TensorFlow 2.0
Import the relevant libraries
1 | import numpy as np |
Data generation
1 | # First, we should declare a variable containing the size of the training set we want to generate. |
Solving with TensorFlow
1 | # Load the training data from the NPZ |
1 | # Declare a variable where we will store the input size of our model |
Extract the weights and bias
1 | # Extracting the weights and biases is achieved quite easily |
1 | # We can save the weights and biases in separate variables for easier examination |
1 | # We can save the weights and biases in separate variables for easier examination |
Extract the outputs (make predictions)
1 | # We can predict new values in order to actually make use of the model |
1 | # If we display our targets (actual observed values), we can manually compare the outputs and the targets |
Plotting the data
1 | # The model is optimized, so the outputs are calculated based on the last form of the model |
Reference
The Data Science Course 2020: Complete Data Science Bootcamp