如TensorFlow名称所示,TensorFlow是定义和运行涉及张量的计算的框架。 张量是向量和矩阵向可能更高维度的推广。 在内部,TensorFlow将张量表示为基本数据类型的n维数组。
A tf.Tensor has the following properties:
- a data type (float32, int32, or string, for example)
- a shape
张量中的每个元素具有相同的数据类型,并且数据类型总是已知的。
形状(即它具有的维数和每个维度的大小)可能只是部分已知的。 如果输入的形状也是完全已知的,大多数操作会产生完全已知形状的张量,但在某些情况下,只能在图形执行时刻找到张量的形状。
有一些已经只定的张量类型
- tf.Variable
- tf.Constant
- tf.Placeholder
- tf.SparseTensor
除 tf.Variable之外,tensor的值在程序上下文中一般不会变化。但两次evaluation同一个tensor,可以返回不同的结果。例如:可以从磁盘读取数据,或者随机产生数据。
一,rank(等级)
一个tf.Tensor对象的等级是它的维数。 rank的同义词包括order , degree , n-dimension。 请注意,TensorFlow中的rank与数学中的矩阵rank并不相同。 如下表所示,TensorFlow中的每个等级对应于不同的数学实体:
rank 0
mammal = tf.Variable("Elephant", tf.string)
ignition = tf.Variable(451, tf.int16)
floating = tf.Variable(3.14159265359, tf.float64)
its_complicated = tf.Variable((12.3, -4.85), tf.complex64)
注意:一个字符串在TensorFlow中被视为单个项目,而不是字符序列。 可能是标量字符串,字符串向量等等。
rank 1
To create a rank 1 tf.Tensor object, you can pass a list of items as the initial value. For example:
mystr = tf.Variable(["Hello"], tf.string)
cool_numbers = tf.Variable([3.14159, 2.71828], tf.float32)
first_primes = tf.Variable([2, 3, 5, 7, 11], tf.int32)
its_very_complicated = tf.Variable([(12.3, -4.85), (7.5, -6.23)], tf.complex64)
Higher ranks
mymat = tf.Variable([[7],[11]], tf.int16)
myxor = tf.Variable([[False, True],[True, False]], tf.bool)
linear_squares = tf.Variable([[4], [9], [16], [25]], tf.int32)
squarish_squares = tf.Variable([ [4, 9], [16, 25] ], tf.int32)
rank_of_squares = tf.rank(squarish_squares)
mymatC = tf.Variable([[7],[11]], tf.int32)
高维的张量同样由一个n维数组组成。 例如,在图像处理过程中,使用等级4的许多张量,尺寸对应于批量中的示例,图像宽度,图像高度和色彩通道。
my_image = tf.zeros([10, 299, 299, 3]) # batch x height x width x color
使用tf.rank获得张量的rank
r = tf.rank(my3d)
# After the graph runs, r will hold the value 3.
tensor分割
由于tf.Tensor是一个n维的单元格数组,要访问tf.Tensor中的单个单元格,您需要指定n个索引。
二,shape (形状)
改变tensor的形状。
rank_three_tensor = tf.ones([3, 4, 5])
matrix = tf.reshape(rank_three_tensor, [6, 10]) # Reshape existing content into
# a 6x10 matrix
matrixB = tf.reshape(matrix, [3, -1]) # Reshape existing content into a 3x20
# matrix. -1 tells reshape to calculate
# the size of this dimension.
matrixAlt = tf.reshape(matrixB, [4, 3, -1]) # Reshape existing content into a
#4x3x5 tensor
# Note that the number of elements of the reshaped Tensors has to match the
# original number of elements. Therefore, the following example generates an
# error because no possible value for the last dimension will match the number
# of elements.
yet_another = tf.reshape(matrixAlt, [13, 2, -1]) # ERROR!
三,Data types(数据类型)
通过tf.cast强制装换张量类型。
# Cast a constant integer tensor into floating point.
float_tensor = tf.cast(tf.constant([1, 2, 3]), dtype=tf.float32)
当从python对象创建一个tf.Tensor时,你可以选择指定数据类型。 如果你不这样做,TensorFlow会选择一个可以表示数据的数据类型。 TensorFlow将Python整数转换为tf.int32,将python浮点数转换为tf.float32。 否则,TensorFlow使用numpy在转换为数组时使用的相同规则。
四,Evaluating Tensors(评估tensors)
计算图形一旦建立,您就可以运行生成特定tf.Tensor的计算并获取分配给它的值。方便调试tensorflow程序。
使用.eval()来评估张量结果,默认有一个 tf.Session 被运行
constant = tf.constant([1, 2, 3])
tensor = constant * constant
print tensor.eval()
有时,无法评估一个没有上下文的tf.Tensor,因为它的值可能取决于不可用的动态信息。 例如,如果不为占位符提供价值,则无法评估依赖于占位符的张量。
p = tf.placeholder(tf.float32)
t = p + 1.0
t.eval() # 失败
t.eval(feed_dict={p:2.0}) # feed 数据到placeholder,成功
五,Printing Tensors(输出Tensors)
出于调试的目的,您可能需要打印tf.Tensor的值。 虽然tfdbg提供了高级的调试支持,但TensorFlow也可以直接打印tf.Tensor的值。
t = <<some tensorflow operation>>
tf.Print(t, [t]) # 什么都不做
t = tf.Print(t, [t]) # 我们将输出值tf.Print
result = t + 1 # 结果已经被评估,t值将会被打印出来
完。