Hands on learning and deep learning - Tensorflow data operation

Posted by ukalpa on Wed, 15 Dec 2021 00:00:42 +0100

import tensorflow as tf
tf.__version__
'2.3.0'

1. Create tensor

  • Tensor is the main tool for storing and transforming data. The multidimensional arrays of tensor and Numpy are similar, but tensor can provide functions such as GPU calculation and automatic gradient calculation
# Create row vector

x = tf.constant(range(12))
x
<tf.Tensor: shape=(12,), dtype=int32, numpy=array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])>
print(x.shape)
print(len(x)) # Gets the number of elements in the vector
(12,)
12
# Use the reshape function to change the shape of the row vector x to (3, 4)
x = tf.reshape(x,(3,4))
x
<tf.Tensor: shape=(3, 4), dtype=int32, numpy=
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])>
  • Notice that the shape in the X attribute has changed. The above x.reshape((3, 4)) can also be written as x.reshape((-1, 4)) or x.reshape((3, -1)). Since the number of elements of X is known, the - 1 here can be inferred from the number of elements and the size of other dimensions.

2. Create a tensor of the specified shape

tf.zeros((2,3,4))
<tf.Tensor: shape=(2, 3, 4), 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.]]], dtype=float32)>
tf.ones((3,4))
<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]], dtype=float32)>
  • Specify the value of each element in the tensor through the Python list
y = tf.constant([[1,2,3,4],[4,5,6,7],[7,8,9,10]])
y
<tf.Tensor: shape=(3, 4), dtype=int32, numpy=
array([[ 1,  2,  3,  4],
       [ 4,  5,  6,  7],
       [ 7,  8,  9, 10]])>
  • Randomly generate the value of each element in the tensor. Let's create a tensor with shape (3, 4). Each element is randomly sampled in a normal distribution with a mean of 0 and a standard deviation of 1.

  • tf.random.normal(shape=(3,4), mean=0, stddev=1)

    1. Mean mean
    2. Standard deviation of normal distribution
tf.random.normal(shape=(3,4), mean=0, stddev=1)
<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[ 0.2814369 ,  1.1456217 ,  1.1553345 ,  0.91973084],
       [-0.24770287, -0.8750045 , -0.04902882, -0.02854874],
       [ 1.4356786 ,  2.6822388 , -0.58117944,  1.7481779 ]],
      dtype=float32)>

3. Tensor operation

print("x => ", x.shape)
print("y => ", x.shape)

print(x)
print(y)
x =>  (3, 4)
y =>  (3, 4)
tf.Tensor(
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]], shape=(3, 4), dtype=int32)
tf.Tensor(
[[ 1  2  3  4]
 [ 4  5  6  7]
 [ 7  8  9 10]], shape=(3, 4), dtype=int32)

3.1 addition by element:

  • Add by corresponding element
  • x. Y is all (3,4). Add by element, and the shape of the result remains unchanged
res01 = x + y 
res01
<tf.Tensor: shape=(3, 4), dtype=int32, numpy=
array([[ 1,  3,  5,  7],
       [ 8, 10, 12, 14],
       [15, 17, 19, 21]])>

3.2 multiplication by element:

  • Multiply the corresponding elements. Note that it is not a dot multiplication of the matrix
res02 = x * y
res02
<tf.Tensor: shape=(3, 4), dtype=int32, numpy=
array([[  0,   2,   6,  12],
       [ 16,  25,  36,  49],
       [ 56,  72,  90, 110]])>

3.3 division by element:

res03 = x / y
res03
<tf.Tensor: shape=(3, 4), dtype=float64, numpy=
array([[0.        , 0.5       , 0.66666667, 0.75      ],
       [1.        , 1.        , 1.        , 1.        ],
       [1.14285714, 1.125     , 1.11111111, 1.1       ]])>

3.4 index calculation by element:

  • Type conversion TF cast(y, tf.float32)
  • Exponential operation TF exp(Y)
    1. Tensors of type bfloat16, half, float32, float64, complex64 or complex128.
    2. exp(y) = e^y
y = tf.cast(y, tf.float32) # Convert the type of y to float32
tf.exp(y) # Exponential operation
<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[2.7182817e+00, 7.3890562e+00, 2.0085537e+01, 5.4598148e+01],
       [5.4598148e+01, 1.4841316e+02, 4.0342880e+02, 1.0966332e+03],
       [1.0966332e+03, 2.9809580e+03, 8.1030840e+03, 2.2026467e+04]],
      dtype=float32)>

3.5 matrix multiplication

  • Matrix multiplication requires that the rows and columns on both sides correspond: (3,4) < = = > (4,3)
  • Data types on both sides correspond to int32 < = = > int32
  • tf.matmul(x,y)
    1. Matrix multiplication
  • tf.transpose(y)
    1. Function: transpose
    2. (3,4) => (4,3)
y = tf.cast(y,tf.int32)
tf.matmul(x, tf.transpose(y))
<tf.Tensor: shape=(3, 3), dtype=int32, numpy=
array([[ 20,  38,  56],
       [ 60, 126, 192],
       [100, 214, 328]])>

3.6 matrix splicing

  • tf.concat([x,y],axis = 0)
    1. axis represents dimension: 0 represents row and 1 represents column
  • Two matrices are connected on the row (dimension 0, the leftmost element in the shape) and the column (dimension 1, the second element from the left in the shape)
  • Splicing two matrices on a row is equivalent to stacking the matrix up and down = > shape = (6,4)
  • Splicing two matrices on a column is equivalent to stacking the matrix left and right = > shape = (3,8)
print(x)
print(y)
tf.Tensor(
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]], shape=(3, 4), dtype=int32)
tf.Tensor(
[[ 1  2  3  4]
 [ 4  5  6  7]
 [ 7  8  9 10]], shape=(3, 4), dtype=int32)
tf.concat([x,y],axis = 0)
<tf.Tensor: shape=(6, 4), dtype=int32, numpy=
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [ 1,  2,  3,  4],
       [ 4,  5,  6,  7],
       [ 7,  8,  9, 10]])>
tf.concat([x,y],axis = 1)
<tf.Tensor: shape=(3, 8), dtype=int32, numpy=
array([[ 0,  1,  2,  3,  1,  2,  3,  4],
       [ 4,  5,  6,  7,  4,  5,  6,  7],
       [ 8,  9, 10, 11,  7,  8,  9, 10]])>

3.7 matrix comparison

  • Compare the elements of the corresponding position
tf.equal(x,y)
<tf.Tensor: shape=(3, 4), dtype=bool, numpy=
array([[False, False, False, False],
       [ True,  True,  True,  True],
       [False, False, False, False]])>

3.8 element summation

  • tf.reduce_sum(x) sums all elements in X
  • tf.norm(x) calculates the norm of X vector
tf.reduce_sum(x) # numpy=66 result
<tf.Tensor: shape=(), dtype=int32, numpy=66>
x = tf.cast(x, tf.float32)
tf.norm(x)
<tf.Tensor: shape=(), dtype=float32, numpy=22.494444>

4. Broadcasting mechanism

  • When two tensors with different shapes are calculated by elements, the broadcasting mechanism may be triggered: copy the elements appropriately to make the two tensors have the same shape, and then calculate by elements
  • A: (3,1) B: (1,2)
  • Since A and B are matrices with three rows and one column and one row and two columns respectively, if A + B is to be calculated, the three elements of the first column in A are broadcast (copied) to the second column, while the two elements of the first row in B are broadcast (copied) to the second and third rows. In this way, the two matrices with three rows and two columns can be added by elements.
A = tf.reshape(tf.constant(range(3)), (3,1))
B = tf.reshape(tf.constant(range(2)), (1,2))

A,B
(<tf.Tensor: shape=(3, 1), dtype=int32, numpy=
 array([[0],
        [1],
        [2]])>,
 <tf.Tensor: shape=(1, 2), dtype=int32, numpy=array([[0, 1]])>)
# Trigger the broadcast mechanism, and the element in A copies the first column to the second column = > (3,2)
# The element in B copies the first line to the second and third lines = > (3,2)
A + B 
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[0, 1],
       [1, 2],
       [2, 3]])>

5. Index

  • In tensor, the index represents the position of elements. The indexes of tensor are incremented one by one from 0. For example, the row indexes of a matrix with 3 rows and 2 columns are 0, 1 and 2 respectively, and the column indexes are 0 and 1 respectively.

5.1 get the index of the specified range

  • x[0:3] # takes three rows with indexes of 0, 1 and 2
x[0:3] # Take three rows with indexes 0, 1 and 2
<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[ 0.,  1.,  2.,  3.],
       [ 4.,  5.,  6.,  7.],
       [ 8.,  9., 10., 11.]], dtype=float32)>

5.2 assigning values to specified elements

  • tf.Variable must be converted to variable when modifying the Tensor value
    1. Variable through TF The variable class is created and tracked. tf.Variable represents a tensor whose value can be changed by performing an operation on it
    2. X[1,2].assign(9): assign a value to an element with an index of 1,2
x = tf.Variable(x)
x
<tf.Variable 'Variable:0' shape=(3, 4) dtype=float32, numpy=
array([[ 0.,  1.,  2.,  3.],
       [ 4.,  5.,  6.,  7.],
       [ 8.,  9., 10., 11.]], dtype=float32)>
x[1,2].assign(9)
<tf.Variable 'UnreadVariable' shape=(3, 4) dtype=float32, numpy=
array([[ 0.,  1.,  2.,  3.],
       [ 4.,  5.,  9.,  7.],
       [ 8.,  9., 10., 11.]], dtype=float32)>

5.3 assigning values to specified columns

x = tf.Variable(x)
x[1:2,:].assign(tf.ones(x[1:2,:].shape, dtype=tf.float32) * 3)
<tf.Variable 'UnreadVariable' shape=(3, 4) dtype=float32, numpy=
array([[ 0.,  1.,  2.,  3.],
       [ 3.,  3.,  3.,  3.],
       [ 8.,  9., 10., 11.]], dtype=float32)>

6. Mutual conversion between tensor and numpy

  • Use the array function and asnumpy function to transform the data between NDArray and NumPy formats. Next, the NumPy instance is transformed into a tensor instance
  • numpy => tensor : tf.constant§
  • tensor => numpy : np.array(T)
import numpy as np
P = np.ones((3,4))
T = tf.constant(P)
T
<tf.Tensor: shape=(3, 4), dtype=float64, numpy=
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])>
np.array(T)
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])

Topics: Python TensorFlow Deep Learning