EN111220192126

# Exercises on matrix operations in TensorFlow 1.4

Practice makes perfect

In [1]:
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}$

In [2]:
vector1 = tf.constant ([1,3,5], tf.int16)
print(vector1)

Tensor("Const:0", shape=(3,), dtype=int16)


### Exercise 2. Please define a tensor: constant = matrix [1, 2, 3, 4] in format int16¶

$begin{bmatrix} 1 & 2 \ 3 & 4 end{bmatrix}$

In [3]:
matrix1 = tf.constant ([[1, 2],[3, 4]], tf.int16)
print (matrix1)

Tensor("Const_1:0", shape=(2, 2), dtype=int16)


### 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}$

In [4]:
matrix2 = tf.constant ([[[1, 2],  [3, 4], [5, 6]]], tf.int16)
print (matrix2)

Tensor("Const_2:0", shape=(1, 3, 2), dtype=int16)

In [5]:
matrix2.shape

Out[5]:
TensorShape([Dimension(1), Dimension(3), Dimension(2)])

### 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}$

In [6]:
kot = tf.zeros(5)
print(kot)
kot.shape

Tensor("zeros:0", shape=(5,), dtype=float32)

Out[6]:
TensorShape([Dimension(5)])

### 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}$

In [7]:
fok = tf.ones ([4, 4])
print(fok)
fok.shape

Tensor("ones:0", shape=(4, 4), dtype=float32)

Out[7]:
TensorShape([Dimension(4), Dimension(4)])

$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.

In [8]:
matrix3 = tf.constant ([[[1, 3], [2, 4]]], tf.int16)
matrix4 = tf.constant ([[[1, 2], [2, 1]]], tf.int16)

In [9]:
PZU = tf.add(matrix3, matrix4)

In [10]:
sess = tf.Session()
print(sess.run(PZU))

[[[2 5]
[4 5]]]


### $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.

In [11]:
PZU = tf.subtract(matrix3, matrix4)

In [12]:
sess = tf.Session()
print(sess.run(PZU))

[[[0 1]
[0 3]]]


### $begin{eqnarray}¶ nonumber begin{bmatrix} 5 & 10 \ 15 & 20 \ end{bmatrix} &=& begin{bmatrix} 1 & 2 \ 3 & 4 \ end{bmatrix}* end{eqnarray}$ 5

In [13]:
matrix1 = tf.constant ([[1, 2],[3, 4]], tf.int16)

In [14]:
FOK = tf.multiply (matrix1, 5)

In [15]:
sess = tf.Session()
print(sess.run(FOK))

[[ 5 10]
[15 20]]


### $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

In [16]:
matrix5 = tf.constant ([[2,7,4],[5,2,1],[6,3,4]], tf.int16)

In [17]:
ZHP = tf.multiply (matrix1, 5)

In [18]:
sess = tf.Session()
print(sess.run(ZHP))

[[ 5 10]
[15 20]]


### Exercise 10. Multiply the matrix by a matrix TensorFlow¶

Matrix multiplication is not alternating.

### $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}$

In [19]:
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)

In [20]:
sess = tf.Session()
print(sess.run(KPU))

[[33 40]
[86 66]]


### Exercise 11. Multiply the matrix by a matrix TensorFlow¶

Matrix multiplication is not alternating.

In [21]:
KPU = tf.matmul(matrix7,matrix6)

In [22]:
sess = tf.Session()
print(sess.run(KPU))

[[57 48 78]
[59 26 46]
[10 10 16]]


### Exercise 11. Multiplication of C⋅D square matrices¶

In [23]:
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)

In [24]:
PKS = tf.matmul(matrix8,matrix9)

In [25]:
sess = tf.Session()
print(sess.run(PKS))

[[ 32  63  50]
[ 46 110 144]
[ 14  37  30]]


### 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}$

In [26]:
x = tf.constant([[1, 3, 9], [7, 2, 5]])
GAP = tf.transpose(x)

In [27]:
sess = tf.Session()
print(sess.run(GAP))

[[1 7]
[3 2]
[9 5]]


### Exercise 12. Determinant of the matrix¶

$A = begin{bmatrix} 1 & 2 \ 3 & 4 end{bmatrix}$

$|A| = (1*4)-(3*2)= -2$

In [28]:
matrix1 = tf.constant ([[1, 2],[3, 4]], tf.float32)

In [29]:
PKO = tf.matrix_determinant(matrix1)

In [30]:
sess = tf.Session()
print(sess.run(PKO))

-2.0


### 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.

In [31]:
PPS = tf.diag([1.2, 1.5, 1.0, 7.1, 2, 8.3])

In [32]:
sess = tf.Session()
print(sess.run(PPS))

[[1.2 0.  0.  0.  0.  0. ]
[0.  1.5 0.  0.  0.  0. ]
[0.  0.  1.  0.  0.  0. ]
[0.  0.  0.  7.1 0.  0. ]
[0.  0.  0.  0.  2.  0. ]
[0.  0.  0.  0.  0.  8.3]]


### 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.

In [33]:
ABC = tf.truncated_normal([2, 3])

In [34]:
sess = tf.Session()
print(sess.run(ABC))

[[ 0.05371307  1.4564506  -1.7267214 ]
[-1.7192566   1.5986782   0.91717476]]

In [35]:
print(ABC.shape)

(2, 3)


Random values for the mean: 7 and standard deviation: 2. Generate matrix: 3 by 5, the values should be in the format: float32.

In [39]:
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/

In [40]:
print(sess.run(tf.fill([4,2],4)))

[[4 4]
[4 4]
[4 4]
[4 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.

https://docs.w3cub.com/tensorflow~python/tf/random_uniform/

In [41]:
GAD = tf.random_uniform([3,7], minval=4, maxval=8, dtype=tf.float32)

In [42]:
sess = tf.Session()

[[5.8656416 4.652042  4.3458786 5.7581544 6.0479655 6.6700726 6.035503 ]
[5.729369  7.1762366 6.052859  6.8669724 6.278867  5.959975  7.3483486]
[7.441143  7.3443627 6.4590683 5.6526484 5.656465  4.5448027 5.9519987]]


### 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.

In [43]:
import numpy as np
KAT = np.array([[1., 2., 3.],[-3., -7., -1.],[0., 5., -2.]])
KAT

Out[43]:
array([[ 1.,  2.,  3.],
[-3., -7., -1.],
[ 0.,  5., -2.]])
In [44]:
DOK = tf.convert_to_tensor(KAT)

In [45]:
sess = tf.Session()
print(sess.run(DOK))

[[ 1.  2.  3.]
[-3. -7. -1.]
[ 0.  5. -2.]]