【TensorFlow】带你搞懂 TensorFlow 中的张量数据结构

系列文章目录

第二章 TensorFlow 深度学习入门之 TensorFlow的核心概念


目录

系列文章目录

前言

一、常量的张量

1 张量的数据类型与numpy.array的关系

2 如何表示张量 Tensor

3  如何改变张量的数据类型

二、变量的张量



前言

程序 = 数据结构+算法

TensorFlow程序 = 张量数据结构 + 计算图算法语言

Tensorflow的基本数据结构是张量Tensor。张量即多维数组。Tensorflow的张量和numpy中的array很类似。

从行为特性来看,有两种类型的张量,常量Constant和变量Variable.

常量的值在计算图中不可以被重新赋值,变量可以在计算图中用assign或assign_add 重新赋值。

话不多说,直接上代码,带你搞懂 TensorFlow 中的张量数据结构。


一、常量的张量

1 张量的数据类型与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)

输出结果:
tf.int32 类型的常量: tf.Tensor(1, shape=(), dtype=int32)

tf.int64 类型的常量: tf.Tensor(1, shape=(), dtype=int64)

tf.float32 类型常量: tf.Tensor(3.14, shape=(), dtype=float32)

tf.double 类型的常量: tf.Tensor(3.14, shape=(), dtype=float64)

tf.string 类型的常量: tf.Tensor(b'hello world!', shape=(), dtype=string)

tf.bool 类型的常量: 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)
输出结果:
True
True
True
True
False

可以看见:

module 'numpy' has no attribute 'string'
tf.string 类型与 np.unicode 类型不一致,其他的类型还是很相似的

2 如何表示张量 Tensor

不同类型的数据可以用不同维度(rank)的张量来表示。

标量为0维张量,向量为1维张量,矩阵为2维张量。

彩色图像有rgb三个通道,可以表示为3维张量。

视频还有时间维,可以表示为4维张量。

可以简单地总结为:有几层中括号,就是多少维的张量。

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

输出结果:

# 0 维张量

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

# 1 维张量

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

# 2 维张量

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

# 3维张量

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 维张量

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  如何改变张量的数据类型

  • 可以用tf.cast() 改变张量的数据类型
  • 可以用numpy方法将tensorflow中的张量转化成numpy中的张量
  • 可以用shape方法查看张量的尺寸
  • 可以用 decode 将编码转为中文
# 用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)

输出结果:

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

输出结果:

b'\xe6\x88\x91\xe5\x96\x9c\xe6\xac\xa2\xe4\xb8\xad\xe6\x96\x87\xef\xbc\x81'
我喜欢中文!

二、变量的张量

模型中需要被训练的参数一般被设置成变量。

# 要注意 :常量值不可以改变,常量的重新赋值相当于创造新的内存空间
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))
输出结果:
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))

输出结果:

<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

猜你喜欢

转载自blog.csdn.net/m0_51816252/article/details/126789324