Exercises on matrix operations in TensorFlow 1.4
Practice makes perfect
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
Exercise 1. Please define a tensor: constant = vector [1, 2, 3] in format int16¶
$ begin{bmatrix}
1 \
2 \
3
end{bmatrix}$
vector1 = tf.constant ([1,3,5], tf.int16)
print(vector1)
Exercise 2. Please define a tensor: constant = matrix [1, 2, 3, 4] in format int16¶
$begin{bmatrix}
1 & 2 \
3 & 4
end{bmatrix}$
matrix1 = tf.constant ([[1, 2],[3, 4]], tf.int16)
print (matrix1)
Exercise 3. Please define a tensor: constant = matrix [1, 2, 3, 4, 5, 6] in format int16¶
$begin{bmatrix}
1 & 2 \
3 & 4 \
5 & 6
end{bmatrix}$
matrix2 = tf.constant ([[[1, 2], [3, 4], [5, 6]]], tf.int16)
print (matrix2)
matrix2.shape
Exercise 4. Create a tensor in the form of a vector of a specific shape 5, filled with zeros¶
$begin{bmatrix}
0 \
0 \
0 \
0 \
0
end{bmatrix}$
kot = tf.zeros(5)
print(kot)
kot.shape
Exercise 5. Create a tensor in the form of a matrix with a specific 4×4 shape, filled with only ones¶
$begin{bmatrix}
1 & 1 & 1 & 1\
1 & 1 & 1 & 1\
1 & 1 & 1 & 1\
1 & 1 & 1 & 1
end{bmatrix}$
fok = tf.ones ([4, 4])
print(fok)
fok.shape
Exercise 6. Matrix adding¶
$ begin{eqnarray}
nonumber
begin{bmatrix}
2 & 5\
4 & 5\
end{bmatrix}
&=&
begin{bmatrix}
1 & 3\
2 & 4\
end{bmatrix}+
begin{bmatrix}
1 & 2\
2 & 1\
end{bmatrix}
end{eqnarray} $
To add two matrices, their dimensions must be equal. That is, the number of rows and the number of columns of the first and second matrices must be equal.
matrix3 = tf.constant ([[[1, 3], [2, 4]]], tf.int16)
matrix4 = tf.constant ([[[1, 2], [2, 1]]], tf.int16)
PZU = tf.add(matrix3, matrix4)
sess = tf.Session()
print(sess.run(PZU))
Exercise 7. Matrix subtraction¶
$ begin{eqnarray}¶
nonumber
begin{bmatrix}
0 & 1\
0 & 3\
end{bmatrix}
&=&
begin{bmatrix}
1 & 3\
2 & 4\
end{bmatrix}-
begin{bmatrix}
1 & 2\
2 & 1\
end{bmatrix}
end{eqnarray} $
To subtract two matrices their dimensions must be equal. That is, the number of rows and the number of columns of the first and second matrices must be equal.
PZU = tf.subtract(matrix3, matrix4)
sess = tf.Session()
print(sess.run(PZU))
Exercise 8. Multiply the matrix by a number¶
$ begin{eqnarray}¶
nonumber
begin{bmatrix}
5 & 10 \
15 & 20 \
end{bmatrix}
&=&
begin{bmatrix}
1 & 2 \
3 & 4 \
end{bmatrix}*
end{eqnarray}$ 5
matrix1 = tf.constant ([[1, 2],[3, 4]], tf.int16)
FOK = tf.multiply (matrix1, 5)
sess = tf.Session()
print(sess.run(FOK))
Exercise 9. Multiply the matrix by a number TensorFlow¶
$ begin{eqnarray}¶
nonumber
begin{bmatrix}
10 & 35 & 20\
25 & 10 & 5\
30 & 15 & 20\
end{bmatrix}
&=&
begin{bmatrix}
2 & 7 & 4\
5 & 2 & 1\
6 & 3 & 4\
end{bmatrix}*
end{eqnarray}$ 5
matrix5 = tf.constant ([[2,7,4],[5,2,1],[6,3,4]], tf.int16)
ZHP = tf.multiply (matrix1, 5)
sess = tf.Session()
print(sess.run(ZHP))
$ begin{eqnarray}¶
nonumber
begin{bmatrix}
33 & 40\
86 & 66\
end{bmatrix}
&=&
begin{bmatrix}
1 & 4 & 6\
8 & 2 & 4\
end{bmatrix}*
begin{bmatrix}
9 & 6\
3 & 7\
2 & 1\
end{bmatrix}
end{eqnarray} $
matrix6 = tf.constant([[1,4,6],[8,2,4]],tf.int32)
matrix7 = tf.constant([[9,6],[3,7],[2,1]],tf.int32)
KPU = tf.matmul(matrix6,matrix7)
sess = tf.Session()
print(sess.run(KPU))
Exercise 11. Multiply the matrix by a matrix TensorFlow¶
Matrix multiplication is not alternating.
KPU = tf.matmul(matrix7,matrix6)
sess = tf.Session()
print(sess.run(KPU))
Exercise 11. Multiplication of C⋅D square matrices¶
matrix8 = tf.constant([[5,6,1],[8,7,9],[1,5,2]],tf.int32)
matrix9 = tf.constant([[4,6,7],[2,5,1],[0,3,9]],tf.int32)
PKS = tf.matmul(matrix8,matrix9)
sess = tf.Session()
print(sess.run(PKS))
Exercise 11.Transpose the matrix¶
$A=begin{bmatrix}
1 & 3 & 9 \
7 & 2 & 5 \
end{bmatrix}$
$A^T=begin{bmatrix}
1 & 7 \
3 & 2 \
9 & 5 \
end{bmatrix}$
x = tf.constant([[1, 3, 9], [7, 2, 5]])
GAP = tf.transpose(x)
sess = tf.Session()
print(sess.run(GAP))
Exercise 12. Determinant of the matrix¶
$A = begin{bmatrix}
1 & 2 \
3 & 4
end{bmatrix}$
$|A| = (1*4)-(3*2)= -2$
matrix1 = tf.constant ([[1, 2],[3, 4]], tf.float32)
PKO = tf.matrix_determinant(matrix1)
sess = tf.Session()
print(sess.run(PKO))
Exercise 13. Diagonal matrix¶
Diagonal matrix – a matrix, usually square [a], whose all coefficients lying outside the main diagonal (main diagonal) are zero. In other words, it is an upper- and lower-triangular matrix at the same time.
PPS = tf.diag([1.2, 1.5, 1.0, 7.1, 2, 8.3])
sess = tf.Session()
print(sess.run(PPS))
Exercise 14. Outputs random values from a truncated normal distribution¶
https://docs.w3cub.com/tensorflow~python/tf/truncated_normal/
The generated values follow a normal distribution with specified mean and standard deviation, except that values whose magnitude is more than 2 standard deviations from the mean are dropped and re-picked.
ABC = tf.truncated_normal([2, 3])
sess = tf.Session()
print(sess.run(ABC))
print(ABC.shape)
Random values for the mean: 7 and standard deviation: 2. Generate matrix: 3 by 5, the values should be in the format: float32.
KSU = tf.truncated_normal([3,5],mean=7,stddev=2.0,dtype=tf.float32)
sess = tf.Session()
Exercise 15. Creates a tensor filled with a scalar value.¶
This operation creates a tensor of shape dims and fills it with value.
https://docs.w3cub.com/tensorflow~python/tf/fill/
print(sess.run(tf.fill([4,2],4)))
Macierz 4×2 wypełniona wartościami 4
Exercise 16. Outputs random values from a uniform distribution.¶
The generated values follow a uniform distribution in the range [minval, maxval). The lower bound minval is included in the range, while the upper bound maxval is excluded.
GAD = tf.random_uniform([3,7], minval=4, maxval=8, dtype=tf.float32)
sess = tf.Session()
print(sess.run(GAD))
Exercise 17. Transforming the matrix into a Tensorflow matrix¶
We have some kind of matrix and we want to include it in the tensorflow model.
import numpy as np
KAT = np.array([[1., 2., 3.],[-3., -7., -1.],[0., 5., -2.]])
KAT
DOK = tf.convert_to_tensor(KAT)
sess = tf.Session()
print(sess.run(DOK))
