Tensorflow tutorial, Basic operations on tensors

In this tutorial, I am going to learn the fundamentals of tensors.

Install TensorFlow

bash
pip install tensorflow[and-cuda]

GPU setup

text
nvidia-smi

Install on CPU or GPU

bash
# For GPU users
pip install tensorflow[and-cuda]# For CPU users
pip install tensorflow

Verify the installation

python
Verify the CPU setup:python3 -c "
import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000,
1000])))"If a tensor is returned, you've installed TensorFlow successfully.Verify the GPU setup:python3 -c "
import tensorflow as tf;
print(tf.config.list_physical_devices('GPU'))"If a list of GPU devices is returned, you've installed TensorFlow successfully.
Tensorflow tutorial, Basic operations on tensors

Test

python
import tensorflow as tfprint(tf.__version__)

Basic

python
import osos.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import tensorflow as tf

# Initialization

python
x = tf.constant(4.0)<tf.Tensor: shape=(), dtype=float32, numpy=4.0>
python
x = tf.constant(4.0, shape=(1,1))<tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[4.]],
dtype=float32)>
python
x = tf.constant(4.0, shape=(1,1), dtype=tf.float32)<tf.Tensor: shape=(1, 1), dtype=float32,
numpy=array([[4.]], dtype=float32)>
python
x = tf.constant([[1,2,3],[4,5,6]])<tf.Tensor: shape=(2, 3), dtype=int32, numpy=array([[1, 2, 3],
[4, 5, 6]], dtype=int32)>
python
x = tf.ones((3,3))<tf.Tensor: shape=(3, 3), dtype=float32, numpy=array([[1., 1., 1.],       [1., 1.,
1.],       [1., 1., 1.]], dtype=float32)>
python
x = tf.zeros((3,2))<tf.Tensor: shape=(3, 2), dtype=float32, numpy=array([[0., 0.],       [0., 0.],
[0., 0.]], dtype=float32)>
python
x = tf.eye(3)<tf.Tensor: shape=(3, 3), dtype=float32, numpy=array([[1., 0., 0.],       [0., 1., 0.],
[0., 0., 1.]], dtype=float32)>

Normal distribution

python
x = tf.random.normal((3,3), mean=0 , stddev=1)<tf.Tensor: shape=(3, 3), dtype=float32,
numpy=array([[ 0.8438105 , -1.3465139 ,  0.3282705 ],       [-1.3032753 ,  0.15576549,  2.9160573 ],
[-0.57551485,  0.25881144, -1.4114394 ]], dtype=float32)>

Uniform distribution

python
x = tf.random.uniform((5,3) , minval=0, maxval=1)2.16.1<tf.Tensor: shape=(5, 3), dtype=float32,
numpy=array([[0.14925945, 0.68959737, 0.01986969],       [0.0124476 , 0.8936583 , 0.03025806],
[0.22682643, 0.3770913 , 0.15388298],       [0.55487454, 0.16352475, 0.5743084 ],       [0.54566336,
0.6106169 , 0.2642449 ]], dtype=float32)>

Range

python
x = tf.range(9)<tf.Tensor: shape=(9,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8],
dtype=int32)>
python
x = tf.range(start=1, limit=20, delta=2)<tf.Tensor: shape=(10,), dtype=int32, numpy=array([ 1,  3,
5,  7,  9, 11, 13, 15, 17, 19], dtype=int32)>

Cast

python
x = tf.cast(x , dtype=tf.float64)<tf.Tensor: shape=(10,), dtype=float64, numpy=array([ 1.,  3.,  5.,
7.,  9., 11., 13., 15., 17., 19.])>

Mathematics

python
x = tf.constant([1,2,3])y = tf.constant([9,8,7])z  = tf.add(x,y)z  = x + y<tf.Tensor: shape=(3,),
dtype=int32, numpy=array([10, 10, 10], dtype=int32)>
python
z  = x - y<tf.Tensor: shape=(3,), dtype=int32, numpy=array([-8, -6, -4], dtype=int32)>z  = x /
y<tf.Tensor: shape=(3,), dtype=float64, numpy=array([0.11111111, 0.25      , 0.42857143])>z  = x *
yz = tf.multiply(x, y)<tf.Tensor: shape=(3,), dtype=int32, numpy=array([ 9, 16, 21], dtype=int32)>z
= tf.tensordot(x, y , axes=1)<tf.Tensor: shape=(), dtype=int32, numpy=46>x = tf.reduce_sum(x*y ,
axis=0)<tf.Tensor: shape=(), dtype=int32, numpy=46>z = x ** 5<tf.Tensor: shape=(3,), dtype=int32,
numpy=array([  1,  32, 243], dtype=int32)>

Matrix Multiply

python
x = tf.random.normal((2,3))y = tf.random.normal((3,4))<tf.Tensor: shape=(2, 3), dtype=float32,
numpy=array([[ 1.396877  , -1.4690485 , -0.5183508 ],       [ 0.01694403,  1.6056603 ,
0.76242363]], dtype=float32)><tf.Tensor: shape=(3, 4), dtype=float32, numpy=array([[-1.6349603 ,
0.2476521 , -0.2984849 ,  0.16962565],       [-0.4788278 , -1.9370278 ,  1.0966969 , -2.8167336 ],
[-0.9770468 ,  0.02774622, -1.6080935 , -0.03016708]],      dtype=float32)>z = x @ yz = tf.matmul(x,
y)<tf.Tensor: shape=(2, 4), dtype=float32, numpy=array([[-1.0739641,  3.177145 , -1.194491 ,
4.3905015],       [-1.5414612, -3.0848582,  0.5298166, -4.5428433]], dtype=float32)>

Indexing

python
x = tf.constant([2,3,4,5,6,3,34])print(x[:])tf.Tensor([ 2  3  4  5  6  3 34], shape=(7,),
dtype=int32)print(x[::2])tf.Tensor([ 2  4  6 34], shape=(4,),
dtype=int32)print(x[::-1])tf.Tensor([34  3  6  5  4  3  2], shape=(7,), dtype=int32)x_ind =
tf.gather(x , [3,4])<tf.Tensor: shape=(2,), dtype=int32, numpy=array([5, 6], dtype=int32)>

Reshape

python
x = tf.constant([2,3,4,5,6,3,34])y = tf.reshape(x , (3,2))2.16.1<tf.Tensor: shape=(3, 2),
dtype=int32, numpy=array([[ 2,  3],       [ 4,  5],       [ 6, 34]], dtype=int32)>

Number of dimensions

python
x = tf.constant([2,3,4,5,6,34])x.ndim =>1x = tf.constant([[2,3,4] , [0,4,5]])x.ndim => 2

Changeable Tensor

python
x = tf.Variable([[2,3,4] , [0,4,5]])<tf.Variable 'Variable:0' shape=(2, 3) dtype=int32,
numpy=array([[2, 3, 4],       [0, 4, 5]], dtype=int32)>
python
x[0].assign([[9,21,31]])<tf.Variable 'UnreadVariable' shape=(2, 3) dtype=int32, numpy=array([[ 9,
21, 31],       [ 0,  4,  5]], dtype=int32)>

Random Generator

python
random_1 = tf.random.Generator.from_seed(42)random_1 = random_1.normal(shape=(3,2))<tf.Tensor:
shape=(3, 2), dtype=float32, numpy=array([[-0.7565803 , -0.06854702],       [ 0.07595026, -1.2573844
],       [-0.23193763, -1.8107855 ]], dtype=float32)>
python
random_2 = tf.random.Generator.from_seed(42)random_2 = random_2.normal(shape=(3,2))<tf.Tensor:
shape=(3, 2), dtype=float32, numpy=array([[-0.7565803 , -0.06854702],       [ 0.07595026, -1.2573844
],       [-0.23193763, -1.8107855 ]], dtype=float32)>

Shuffle a tensor

python
not_shuffled = tf.constant([ [10 ,20], [40,50], [90,100]])<tf.Tensor: shape=(3, 2), dtype=int32,
numpy=array([[ 10,  20],       [ 40,  50],       [ 90, 100]],
dtype=int32)>tf.random.shuffle(not_shuffled)<tf.Tensor: shape=(3, 2), dtype=int32, numpy=array([[
40,  50],       [ 90, 100],       [ 10,  20]], dtype=int32)>
python
tf.random.set_seed(42) # global level random seedtf.random.shuffle(not_shuffled , seed=
42)<tf.Tensor: shape=(3, 2), dtype=int32, numpy=array([[ 10,  20],       [ 40,  50],       [ 90,
100]], dtype=int32)><tf.Tensor: shape=(3, 2), dtype=int32, numpy=array([[ 10,  20],       [ 40,
50],       [ 90, 100]], dtype=int32)>
Tensorflow tutorial, Basic operations on tensors

Trun Numpy into tensor

python
import numpy as npnumpay_A = np.arange(1,25, dtype=np.int32numpay_Aarray([ 1,  2,  3,  4,  5,  6,
7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,       18, 19, 20, 21, 22, 23, 24], dtype=int32)A =
tf.constant(numpay_A)<tf.Tensor: shape=(24,), dtype=int32, numpy=array([ 1,  2,  3,  4,  5,  6,  7,
8,  9, 10, 11, 12, 13, 14, 15, 16, 17,       18, 19, 20, 21, 22, 23, 24], dtype=int32)>A =
tf.constant(numpay_A , shape=(2,12))<tf.Tensor: shape=(2, 12), dtype=int32, numpy=array([[ 1,  2,
3,  4,  5,  6,  7,  8,  9, 10, 11, 12],       [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]],
dtype=int32)>

Rank

python
rank_4_tensor = tf.zeros(shape=[2,3,4,5])
python
<tf.Tensor: shape=(2, 3, 4, 5), dtype=float32, numpy=array([[[[0., 0., 0., 0., 0.],         [0., 0.,
0., 0., 0.],         [0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.]],        [[0., 0., 0., 0.,
0.],         [0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.]],
[[0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.],         [0., 0.,
0., 0., 0.]]],       [[[0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.],         [0., 0., 0., 0.,
0.],         [0., 0., 0., 0., 0.]],        [[0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.]],        [[0., 0., 0., 0., 0.],         [0., 0.,
0., 0., 0.],         [0., 0., 0., 0., 0.],         [0., 0., 0., 0., 0.]]]], dtype=float32)>
text
rank_4_tensor[0]
python
<tf.Tensor: shape=(3, 4, 5), dtype=float32, numpy=array([[[0., 0., 0., 0., 0.],        [0., 0., 0.,
0., 0.],        [0., 0., 0., 0., 0.],        [0., 0., 0., 0., 0.]],       [[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],        [0., 0., 0., 0., 0.],        [0., 0., 0., 0., 0.]],       [[0., 0., 0.,
0., 0.],        [0., 0., 0., 0., 0.],        [0., 0., 0., 0., 0.],        [0., 0., 0., 0., 0.]]],
dtype=float32)>
text
rank_4_tensor.shape
text
TensorShape([2, 3, 4, 5])
text
rank_4_tensor.ndim4
python
tf.size(rank_4_tensor)<tf.Tensor: shape=(), dtype=int32, numpy=120>

Add Extra Dimension to exist tensor

python
rank_3 = tf.constant(np.random.randint(0,100, size=(2,3,4)))<tf.Tensor: shape=(2, 3, 4),
dtype=int64, numpy=array([[[15, 43,  0, 82],        [68, 26, 52,  6],        [22,  6, 18, 30]],
[[56,  8, 42, 58],        [12,  7, 81, 55],        [18, 46, 21, 80]]])>

‍‍

python
rank_4 = rank_3[... , tf.newaxis]<tf.Tensor: shape=(2, 3, 4, 1), dtype=int64, numpy=array([[[[15],
[43],         [ 0],         [82]],        [[68],         [26],         [52],         [ 6]],
[[22],         [ 6],         [18],         [30]]],       [[[56],         [ 8],         [42],
[58]],        [[12],         [ 7],         [81],         [55]],        [[18],         [46],
[21],         [80]]]])>
python
rank_4 = rank_3[ tf.newaxis , ... ]<tf.Tensor: shape=(1, 2, 3, 4), dtype=int64, numpy=array([[[[15,
43,  0, 82],         [68, 26, 52,  6],         [22,  6, 18, 30]],        [[56,  8, 42, 58],
[12,  7, 81, 55],         [18, 46, 21, 80]]]])>
Tensorflow tutorial, Basic operations on tensors
python
rank_4 = rank_3[ : , tf.newaxis , : , :  ]<tf.Tensor: shape=(2, 1, 3, 4), dtype=int64,
numpy=array([[[[15, 43,  0, 82],         [68, 26, 52,  6],         [22,  6, 18, 30]]],       [[[56,
8, 42, 58],         [12,  7, 81, 55],         [18, 46, 21, 80]]]])>
python
rank_4 = tf.expand_dims(rank_3 , axis=1)<tf.Tensor: shape=(2, 1, 3, 4), dtype=int64,
numpy=array([[[[15, 43,  0, 82],         [68, 26, 52,  6],         [22,  6, 18, 30]]],       [[[56,
8, 42, 58],         [12,  7, 81, 55],         [18, 46, 21, 80]]]])>

Manipulating tensor

python
tensor = tf.constant([[10,7],[3,4]])<tf.Tensor: shape=(2, 2), dtype=int32, numpy=array([[10,  7],
[ 3,  4]], dtype=int32)>
python
tensor + 10<tf.Tensor: shape=(2, 2), dtype=int32, numpy=array([[20, 17],       [13, 14]],
dtype=int32)>
python
tf.multiply(tensor , 10)<tf.Tensor: shape=(2, 2), dtype=int32, numpy=array([[100,  70],       [ 30,
40]], dtype=int32)>

Matrix manipulation

Tensorflow tutorial, Basic operations on tensors
python
tensor<tf.Tensor: shape=(2, 2), dtype=int32, numpy=array([[10,  7],       [ 3,  4]],
dtype=int32)>tf.matmul(tensor , tensor)<tf.Tensor: shape=(2, 2), dtype=int32, numpy=array([[121,
98],       [ 42,  37]], dtype=int32)>
python
tensor @ tensor<tf.Tensor: shape=(2, 2), dtype=int32, numpy=array([[121,  98],       [ 42,  37]],
dtype=int32)>

Not matrix multiply

python
tensor * tensor<tf.Tensor: shape=(2, 2), dtype=int32, numpy=array([[100,  49],       [  9,  16]],
dtype=int32)>

Transpose and Reshape

Tensorflow tutorial, Basic operations on tensors

Change dtype in tensor

python
A = tf.constant([7, 10])A.dtypetf.int32B = tf.cast(A , dtype=tf.float32)B.dtypetf.float32

Aggregating

python
tensor = tf.constant([-7 , -10])tf.abs(tensor)<tf.Tensor: shape=(2,), dtype=int32, numpy=array([ 7,
10], dtype=int32)>
python
tensor = tf.constant(np.random.randint(0,100, size=(3,3)))<tf.Tensor: shape=(3, 3), dtype=int64,
numpy=array([[78, 98,  6],       [27,  1, 92],       [31, 13, 94]])>tf.size(tensor) , tensor.shape ,
tensor.ndim(<tf.Tensor: shape=(), dtype=int32, numpy=9>, TensorShape([3, 3]), 2)
python
tf.reduce_min(tensor)<tf.Tensor: shape=(), dtype=int64, numpy=1>
python
tf.reduce_max(tensor)<tf.Tensor: shape=(), dtype=int64, numpy=98>
python
tensor = tf.cast(tensor , dtype=tf.float32)tf.math.reduce_variance(tensor)<tf.Tensor: shape=(),
dtype=float32, numpy=1206.3209>

Max Argument

python
A = tf.random.uniform(shape=[50])tf.argmax(A)<tf.Tensor: shape=(), dtype=int64,
numpy=47>A[47]<tf.Tensor: shape=(), dtype=float32, numpy=0.9869994>
python
A = tf.random.uniform(shape=[50,2])tf.argmax(A,0)<tf.Tensor: shape=(2,), dtype=int64,
numpy=array([36, 27])>A[36,0]<tf.Tensor: shape=(), dtype=float32, numpy=0.9909723>A[27 ,
1]<tf.Tensor: shape=(), dtype=float32, numpy=0.9717448>tf.reduce_max(A , 0)<tf.Tensor: shape=(2,),
dtype=float32, numpy=array([0.9909723, 0.9717448], dtype=float32)>

Squeeze

python
B = tf.constant(tf.random.uniform(shape=[20]) , shape=(1,1,1,1,20))<tf.Tensor: shape=(1, 1, 1, 1,
20), dtype=float32, numpy=array([[[[[0.803156  , 0.49777734, 0.37054038, 0.9118674 , 0.637642  ,
0.18209696, 0.63791955, 0.27701473, 0.04227114, 0.84219384,           0.90637195, 0.222556  ,
0.9198462 , 0.68789077, 0.42705178,           0.878158  , 0.6943959 , 0.46567595, 0.52925766,
0.33019018]]]]],      dtype=float32)>
python
S = tf.squeeze(B)<tf.Tensor: shape=(20,), dtype=float32, numpy=array([0.803156  , 0.49777734,
0.37054038, 0.9118674 , 0.637642  ,       0.18209696, 0.63791955, 0.27701473, 0.04227114,
0.84219384,       0.90637195, 0.222556  , 0.9198462 , 0.68789077, 0.42705178,       0.878158  ,
0.6943959 , 0.46567595, 0.52925766, 0.33019018],      dtype=float32)>

One-hot encoding

python
C = [0,1,2,3,5]tf.one_hot(C , depth=6)<tf.Tensor: shape=(5, 6), dtype=float32, numpy=array([[1., 0.,
0., 0., 0., 0.],       [0., 1., 0., 0., 0., 0.],       [0., 0., 1., 0., 0., 0.],       [0., 0., 0.,
1., 0., 0.],       [0., 0., 0., 0., 0., 1.]], dtype=float32)>
python
tf.one_hot(C , depth=6, on_value=1.0 , off_value=100.0)<tf.Tensor: shape=(5, 6), dtype=float32,
numpy=array([[  1., 100., 100., 100., 100., 100.],       [100.,   1., 100., 100., 100., 100.],
[100., 100.,   1., 100., 100., 100.],       [100., 100., 100.,   1., 100., 100.],       [100., 100.,
100., 100., 100.,   1.]], dtype=float32)>

Square

python
A = tf.range(1,10)<tf.Tensor: shape=(9,), dtype=int32, numpy=array([1, 2, 3, 4, 5, 6, 7, 8, 9],
dtype=int32)>tf.square(A)<tf.Tensor: shape=(9,), dtype=int32, numpy=array([ 1,  4,  9, 16, 25, 36,
49, 64, 81], dtype=int32)>
python
A = tf.cast(A , dtype=tf.float32)tf.sqrt(A)<tf.Tensor: shape=(9,), dtype=float32, numpy=array([1.
, 1.4142135, 1.7320508, 2.       , 2.236068 , 2.4494898,       2.6457512, 2.828427 , 3.       ],
dtype=float32)>

Finding access to GPU

python
tf.config.list_physical_devices()[PhysicalDevice(name='/physical_device:CPU:0',
device_type='CPU')]tf.config.list_physical_devices("GPU")[]!nvidia-smi

How to install TensorFlow GPU on UBUNTU 18.04

Tensorflow tutorial, Basic operations on tensors
Tensorflow tutorial, Basic operations on tensors
Tensorflow tutorial, Basic operations on tensors

Stackademic 🎓

Thank you for reading until the end. Before you go: