<jupyter_start><jupyter_text><jupyter_code>from google.colab import drive
drive.mount('/gdrive')<jupyter_output>Mounted at /gdrive<jupyter_text>**2-1 Data Structure**

Program = Data Structure + Algorithm

TensorFlow Program = Data Structure of Tensor + Algorithm in Graph

Tensor and graph are key concepts of TensorFlow.

The fundamental data structure in TensorFlow is Tensor, which is multi-dimentional array. Tensor is similar with the array in numpy.

There are two types of tensor accoring to the behavior: constant and variable.

The value of constant cannot be re-assigned in the graph, while variable can be re-assigned through operators such as assign. **1. Constant Tensor**

The data type of tensor is basically corresponding to `numpy.array`<jupyter_code>import numpy as np
import tensorflow as tf

i = tf.constant(1) #tf.int32 type constant
l = tf.constant(1, dtype=tf.int64) #tf.int64 type constant
f = tf.constant(1.23) #tf.float32 type constant
d = tf.constant(3.14, dtype=tf.double) #tf.double type constant
s = tf.constant("hello world") #tf.string type constant
b = tf.constant(True) #tf.bool type constant

print(tf.int64 == np.int64)
print(tf.bool == np.bool)
print(tf.double == np.float64)
print(tf.string == np.unicode) #tf.string type is not equal to np.unicode type<jupyter_output>True
True
True
False<jupyter_text>Each data type can be represented by tensor in different rank.

Scalars are tensors with rank = 0, arrays are with rank = 1, matrix are with rank = 2

Colorful image has three channels (RGB), which can be represented as a tensor with rank = 3.

There is a temporal dimension for video so it could be represented as a rank 4 tensor.

An intuitive way to understand: the number of the square brackets equals to the rank of the tensor.<jupyter_code>scalar = tf.constant(1)

print(tf.rank(scalar))
print(scalar.numpy().ndim) #tf.rank equals to the ndim function in numpy 
scalar
vector = tf.constant([1.0, 2.0, 3.0, 4.0]) # A vector is a rank 1 tensor

print(tf.rank(vector))
print(vector.numpy().ndim)
vector
matrix = tf.constant([[1.0,2.0],[3.0,4.0]]) #A matrix is a rank 2 tensor

print(tf.rank(matrix))
print(np.ndim(matrix.numpy()))
matrix
tensor3 = tf.constant([
                       [[1.0,2.0], [3.0,4.0]],
                       [[5.0,6.0], [7.0,8.0]]
                       ])  # A rank 3 tensor
print(tensor3)
print(tf.rank(tensor3))
tensor3
tensor4 = tf.constant([[[[1.0,1.0],[2.0,2.0]],[[3.0,3.0],[4.0,4.0]]],
                        [[[5.0,5.0],[6.0,6.0]],[[7.0,7.0],[8.0,8.0]]]])  # A rank 4 tensor
print(tensor4)
print(tf.rank(tensor4))<jupyter_output>tf.Tensor(
[[[[1. 1.]
   [2. 2.]]

  [[3. 3.]
   [4. 4.]]]


 [[[5. 5.]
   [6. 6.]]

  [[7. 7.]
   [8. 8.]]]], shape=(2, 2, 2, 2), dtype=float32)
tf.Tensor(4, shape=(), dtype=int32)<jupyter_text>We use tf.cast to change the data type of the tensors.

The method numpy() is for converting the data type from tensor to numpy array.

The method shape is for checking up the size of tensor.<jupyter_code>h = tf.constant([123, 456], dtype=tf.int32)
f = tf.cast(h, tf.float32)
print(h.dtype, f.dtype)
y = tf.constant([[1.0, 2.0], [3.0, 4.0]])
print(y.numpy())
print(y.shape)
u = tf.constant(u"Hello World")
print(u.numpy())
print(u.numpy().decode("utf-8"))<jupyter_output>b'Hello World'
Hello World<jupyter_text>**2. Variable Tensor**

the trainable parameters in the modls are usually defined as variables<jupyter_code># the value of a constant is NOT changeable. Re-assignment creates a new space in the memory.
c = tf.constant([1.0, 2.0])
print(c)
print(id(c))

c = c + tf.constant([1.0, 1.0])
print(c)
print(id(c))
# The value of a variable is changeable throught re-assigning methods such as assign, assign_add, etc.
v = tf.Variable([1.0, 2.0], name='v')
print(v)
print(id(v))

v.assign_add([1.0, 1.0])
print(v)
print(id(v))<jupyter_output><tf.Variable 'v:0' shape=(2,) dtype=float32, numpy=array([1., 2.], dtype=float32)>
140265894042928
<tf.Variable 'v:0' shape=(2,) dtype=float32, numpy=array([2., 3.], dtype=float32)>
140265894042928
{'fraction_non_alphanumeric': 0.148301978988517, 'fraction_numerical': 0.0520400684094796, 'mean_word_length': 3.891278375149343, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '613639', 'license': 'Apache-2.0', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1479, 'n_words': 456, 'path': 'notebooks/2_1_Data_Structure.ipynb', 'repo_name': 'LxYuan0420/eat_tensorflow2_in_30_days', 'size': '4093'}