[TensorFlow] Let you understand the tensor data structure in TensorFlow

Table of Contents of Series Articles

Chapter 2 TensorFlow: Introduction to Deep Learning: The Core Concepts of TensorFlow


Table of contents

Table of Contents of Series Articles

Preface

1. Constant tensor

1 The relationship between the data type of tensor and numpy.array

2 How to represent tensor Tensor

3 How to change the data type of a tensor

2. Tensor of variables



Preface

Program = data structure + algorithm

TensorFlow program = tensor data structure + computational graph algorithm language

The basic data structure of Tensorflow is the tensor Tensor. Tensors are multidimensional arrays. Tensorflow tensors are very similar to arrays in numpy.

From the perspective of behavioral characteristics, there are two types of tensors, constant and variable.

The value of a constant cannot be reassigned in the calculation graph, and the variable can be reassigned in the calculation graph using assign or assign_add.

Without further ado, let’s go directly to the code to help you understand the tensor data structure in TensorFlow.


1. Constant tensor

1 The relationship between the data type of tensor and numpy.array

import numpy as np
import tensorflow as tf

# tf.int32 类型的常量
a = tf.constant(1)
print('tf.int32 类型的常量:',a)

# tf.int64 类型的常量
b = tf.constant(1, dtype=tf.int64)
print('tf.int64 类型的常量:', b)

# tf.float32 类型常量
c = tf.constant(3.14)
print('tf.float32 类型常量:', c)

# tf.double 类型的常量
d = tf.constant(3.14, dtype=tf.double)
print('tf.double 类型的常量:', d)

# tf.string 类型的常量
e = tf.constant('hello world!')
print('tf.string 类型的常量:', e)

# tf.bool 类型的常量
f = tf.constant(True)
print('tf.bool 类型的常量:', f)

Output results: 
Constants of type tf.int32: tf.Tensor(1, shape=(), dtype=int32) 

Constants of type tf.int64: tf.Tensor(1, shape=(), dtype=int64) 

tf.float32 Type constant: tf.Tensor(3.14, shape=(), dtype=float32) 

tf.double type constant: tf.Tensor(3.14, shape=(), dtype=float64) 

tf.string type constant: tf.Tensor (b'hello world!', shape=(), dtype=string) 

tf.bool type constant: tf.Tensor(True, shape=(), dtype=bool)
# 查看张量的数据类型和numpy.array 是否一样
print(tf.int64 == np.int64)
print(tf.double == np.double)
print(tf.double == np.float64)
print(tf.bool == np.bool)
print(tf.string == np.unicode)
Output result: 
True 
True 
True 
True 
False

You can see:

module 'numpy' has no attribute 'string'
tf.string type is inconsistent with np.unicode type, other types are still very similar

2 How to represent tensor Tensor

Different types of data can be represented by tensors of different dimensions (rank).

A scalar is a 0-dimensional tensor, a vector is a 1-dimensional tensor, and a matrix is ​​a 2-dimensional tensor.

The color image has three RGB channels and can be expressed as a 3-dimensional tensor.

Video also has a time dimension, which can be represented as a 4-dimensional tensor.

It can be simply summarized as: how many layers of square brackets there are, that is, how many dimensions of the tensor there are.

# 0 维张量
scalar = tf.constant(2.1)
print(tf.rank(scalar))
# tf.rank() 的作用和 numpy的 ndim方法相同
# print(scalar.numpy().ndim)
print(scalar)
print(np.ndim(scalar))

# 1 维张量
vector = tf.constant([1, 2, 3])
print(tf.rank(vector))
print(vector)
print(np.ndim(vector))

# 2 维张量
matrix = tf.constant([[1, 2, 3], [4, 5, 6]])
print(tf.rank(matrix).numpy())
print(matrix)
print(np.ndim(matrix))

# 3维张量
tensor3 = tf.constant([[[1, 2, 3], [2, 3, 4]],[[3, 4, 5], [4, 5, 6]]])
print(tf.rank(tensor3))
print(tensor3)
print(np.ndim(tensor3))

# 4 维张量
tensor4 = tf.constant([[[[1, 2, 3], [2, 3, 4]],[[3, 4, 5], [4, 5, 6]]],
                      [[[1, 2, 3], [2, 3, 4]],[[3, 4, 5], [4, 5, 6]]]])
print(tensor4)
print(tf.rank(tensor4))
print(np.ndim(tensor4))

Output result:

# 0-dimensional tensor

tf.Tensor(0, shape=(), dtype=int32)
tf.Tensor(2.1, shape=(), dtype=float32)
0

# 1D tensor

tf.Tensor(1, shape=(), dtype=int32)
tf.Tensor([1 2 3], shape=(3,), dtype=int32)
1

# 2D tensor

2
tf.Tensor(
[[1 2 3]
 [4 5 6]], shape=(2, 3), dtype=int32)
2

# 3D tensor

tf.Tensor(3, shape=(), dtype=int32)
tf.Tensor(
[[[1 2 3]
  [2 3 4]]

 [[3 4 5]
  [4 5 6]]], shape=(2, 2, 3), dtype=int32)
3

#4 Dimensional Tensor

tf.Tensor(
[[[[1 2 3]
   [2 3 4]]
  [[3 4 5]
   [4 5 6]]]

 [[[1 2 3]
   [2 3 4]]
  [[3 4 5]
   [4 5 6]]]], shape=(2, 2, 2, 3), dtype=int32)
tf.Tensor(4, shape=(), dtype=int32)
4

3 How to change the data type of a tensor

  • You can use tf.cast() to change the data type of a tensor
  • You can use the numpy method to convert tensors in tensorflow into tensors in numpy
  • You can use the shape method to check the dimensions of the tensor
  • You can use decode to convert the encoding to Chinese
# 用tf.cast改变张量的数据类型
tensor = tf.constant([1.1, 2.2], dtype=tf.float64)
tensor_cast = tf.cast(tensor, tf.float32)
print(tensor.dtype)
print(tensor_cast.dtype)
print(tensor.shape)

Output result:

<dtype: 'float64'>
<dtype: 'float32'>
(2,)
# 用numpy方法将tensorflow中的张量转化成numpy中的张量
a = tf.constant([[1, 2], [3, 4]])
print('tensorflow中的张量:', a.shape)
print('numpy中的张量:',a.numpy())
Output results: 
Tensor in tensorflow: (2, 2) 
Tensor in numpy: [[1 2] 
 [3 4]]
# decode 可以将编码转为中文
b = tf.constant(u"我喜欢中文!")
print(b.numpy())
print(b.numpy().decode('utf-8'))

Output result:

b'\xe6\x88\x91\xe5\x96\x9c\xe6\xac\xa2\xe4\xb8\xad\xe6\x96\x87\xef\xbc\x81'I like Chinese 
!

2. Tensor of variables

The parameters that need to be trained in the model are generally set as variables.

# 要注意 :常量值不可以改变,常量的重新赋值相当于创造新的内存空间
c = tf.constant([1.1, 2.2])
print(c)
print(id(c))
# 改变常量值
c = c + tf.constant([0.9, 0.8])
print(c)
print(id(c))
Output result: 
tf.Tensor([1.1 2.2], shape=(2,), dtype=float32) 
2373561408680 
tf.Tensor([2. 3.], shape=(2,), dtype=float32) 
2373562947016
# 变量的值可以改变,可以通过assign,assign_add 等方法给变量重新赋值
d = tf.Variable([1.1, 2.2])
print(d)
print(id(d))

# 改变变量值,添加
d.assign_add([0.9, 0.8])
print(d)
print(id(d))

# 改变变量值,直接改变
d.assign([1.14, 2.25])
print(d)
print(id(d))

Output result:

<tf.Variable 'Variable:0' shape=(2,) dtype=float32, numpy=array([1.1, 2.2], dtype=float32)>
2373563227776
<tf.Variable 'Variable:0' shape=(2,) dtype=float32, numpy=array([2., 3.], dtype=float32)>
2373563227776
<tf.Variable 'Variable:0' shape=(2,) dtype=float32, numpy=array([1.14, 2.25], dtype=float32)>
2373563227776

Guess you like

Origin blog.csdn.net/m0_51816252/article/details/126789324