在数学里,张量是一种几何实体,广义上表示任意形式的数据,张量可以这么理解
0阶张量(rank) == 标量(scalar)
1阶张量(rank) == 向量(vector)
2阶张量(rank) == 矩阵(matrix)
3阶张量(rank) == 立方(tensor)
n阶张量。。。只能凭空想了
TensorFlow文档中使用了三种记号来方便地描述张量的维度:阶,形状以及维数.下表展示了他们之间的关系:
阶 | 形状 | 维数 | 实例 |
---|---|---|---|
0 | [ ] | 0-D | 一个 0维张量. 一个纯量. |
1 | [D0] | 1-D | 一个1维张量的形式[5]. |
2 | [D0, D1] | 2-D | 一个2维张量的形式[3, 4]. |
3 | [D0, D1, D2] | 3-D | 一个3维张量的形式 [1, 4, 3]. |
n | [D0, D1, ... Dn] | n-D | 一个n维张量的形式 [D0, D1, ... Dn]. |
张量,在tensorflow中表示某种相同数据类型和多维数组
重要的属性
1 数据类型(浮点型,整形,字符串)
2 数组形状(各个维度的大小)
张量的形状很重要,计算的时候必须确定形状
张量的形状不一定要在编译时确定(形状不一定确定,跟我们的输入数据有关),可以在运行的时候通过形状推断计算的出
那张量到底是什么?
1张量用来表示多维数据的。
2张量是执行操作时的输入或者输出的数据
Tensorflow 特别的张量
tf.constant 常量(不可改变)
tf.placeholder 占位符
tf.Variable 变量
#0阶张量 分别创建 字符串 整数 浮点数 复数
mammal = tf.Variable("Elephant",tf.string)
ignition = tf.Variable(451,tf.int16)
floating = tf.Variable(3.1415926,tf.float64)
its_complicated = tf.Variable(12.3 - 4.85j,tf.complex64)
(<tf.Variable 'Variable_4:0' shape=() dtype=string_ref>,
<tf.Variable 'Variable_5:0' shape=() dtype=int32_ref>,
<tf.Variable 'Variable_6:0' shape=() dtype=float32_ref>,
<tf.Variable 'Variable_7:0' shape=() dtype=complex128_ref>)
#1阶张量
mystr = tf.Variable(["Hello","World"],tf.string)
numbers = tf.Variable([1,2,3],tf.int16)
float_number = tf.Variable([1.1,2.2,3.3],tf.float64)
complicated = tf.Variable([12.3 - 4.85j,1 - 23.45j],tf.complex64)
(<tf.Variable 'Variable_8:0' shape=(2,) dtype=string_ref>,
<tf.Variable 'Variable_9:0' shape=(3,) dtype=int32_ref>,
<tf.Variable 'Variable_10:0' shape=(3,) dtype=float32_ref>,
<tf.Variable 'Variable_11:0' shape=(2,) dtype=complex128_ref>)
#2阶张量
hello = tf.Variable([["Hello"],["World"]],tf.string)
numbers1 = tf.Variable([[1],[2]],tf.int16)
float_number1 = tf.Variable([[1.1],[2.2]],tf.float64)
(<tf.Variable 'Variable_12:0' shape=(2, 1) dtype=string_ref>,
<tf.Variable 'Variable_13:0' shape=(2, 1) dtype=int32_ref>,
<tf.Variable 'Variable_14:0' shape=(2, 1) dtype=float32_ref>)
#4阶
my_image = tf.zeros([10,200,200,3])
<tf.Tensor 'zeros:0' shape=(10, 200, 200, 3) dtype=float32>