|
1 数据类型
1.1 数值类型
- 标量:单个实数,维度(秩)为0,shape为 [ ]
- 向量:n个实数的有序集合,秩为1,shape为[n]
- 矩阵:n行m列的有序集合,秩为2,shape为[n, m]
- 张量:所有维度数 dim > 2 的数组统称为张量,张量的每个维度也叫做轴(axis)
在tensorflow中,一般也把标量、向量、矩阵也统称为张量,不作区分。
1.1.1数据类型的定义
In [3]: tf.constant(1.2)
Out[3]: <tf.Tensor: id=1, shape=(), dtype=float32, numpy=1.2>
id是tensorflow中内部索引对象的编号,shape表示张量的形状,dtype表示张量的数据类型及精度
In [4]: tf.constant([1, 2, 3])
Out[4]: <tf.Tensor: id=2, shape=(3,), dtype=int32, numpy=array([1, 2, 3], dtype=int32)>
向量的定义必须通过List类型传给tf.constant()创建。
In [6]: tf.constant([[1, 2], [3, 4]])
Out[6]:
<tf.Tensor: id=4, shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
[3, 4]], dtype=int32)>
In [8]: tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
Out[8]:
<tf.Tensor: id=6, shape=(2, 2, 2), dtype=int32, numpy=
array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]], dtype=int32)>
1.1.2 数值精度
常用的数值精度有:tf.int16, tf.int32, tf.int64, tf.float16, tf.float32, tf.float64, 其中tf.float64即为tf.double
可通过dtype设置数值精度
In [10]: tf.constant(2, dtype = tf.double)
Out[10]: <tf.Tensor: id=8, shape=(), dtype=float64, numpy=2.0>
通过访问张量的dtype成员属性可以判断张量的保存精度
In [7]: a.dtype
Out[7]: tf.float32
1.2 字符串类型
In [9]: tf.constant('Hello Tensorflow.')
Out[9]: <tf.Tensor: id=7, shape=(), dtype=string, numpy=b'Hello Tensorflow.’>
在tf.strings模块中,提供了常见的字符串的工具函数,如拼接join(), 长度length(),切分split()等。
1.3 布尔类型
In [3]: tf.constant(True)
Out[3]: <tf.Tensor: id=1, shape=(), dtype=bool, numpy=True>
布尔类型的张量只需传入python的布尔数据类型,转换成tensorflow内部布尔类型即可。
1.4 类型转换 (tf.cast)
可通过tf.cast()函数进行转换
In [11]: a = tf.constant(np.pi, dtype = tf.float32)
In [12]: tf.cast(a, tf.double)
Out[12]: <tf.Tensor: id=4, shape=(), dtype=float64, numpy=3.1415927410125732>
1.5 待优化类型(tf.variable)
为了区分需要计算梯度信息的张量与不需要计算梯度信息的张量,Tensorflow增加了一种专门数据类型来支持梯度信息的记录:tf.variable,tf.variable类型在普通张量类型基础上增加了name,trainable等属性来计算图的构建。对于需要计算梯度并优化的张量,如神经网络的 和 ,需要通过tf.Vatiable包裹以便Tensorflow跟踪相关的梯度信息。
In [17]: a.name
Out[17]: 'Variable:0'
In [18]: a.trainable
Out[18]: True
|