tensorflow之张量
基础概念
张量具有相同的数据类型(称为dtype)的多维数组。
存在一定的相似之处与numpy中的数组。
类似于Python中的数值和字符串数据结构特性。
所有张量均为不可变对象:无法修改现有数据内容,并不能直接更新现有数据;只能生成新的对象来存储新数据。
标量(0秩张量):仅包含一个数值信息。
向量(1秩张量):由多个数值元素构成的一维结构。
矩阵(2秩张量):由行和列组成的二维结构。
#标量
rank_0_tensor = tf.constant(4)
print(rank_0_tensor)
#标量
rank_1_tensor = tf.constant([2.1,3.2,4.3])
print(rank_1_tensor)
#矩阵
rank_2_tensor = tf.constant([[1,2],[3,4],[5,6]],dtype = tf.float16)
print(rank_2_tensor)
#包含3个轴的张量
rank_3_tensor = tf.constant([
[[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]],
[[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],
[[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29]],])
print(rank_3_tensor)```
```python
tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor([2.1 3.2 4.3], shape=(3,), dtype=float32)
tf.Tensor(
[[1. 2.]
[3. 4.]
[5. 6.]], shape=(3, 2), dtype=float16)
tf.Tensor(
[[[ 0 1 2 3 4]
[ 5 6 7 8 9]]
[[10 11 12 13 14]
[15 16 17 18 19]]
[[20 21 22 23 24]
[25 26 27 28 29]]], shape=(3, 2, 5), dtype=int32)

3轴张量的不同表现形式

张量通常包含不同类型的数值数据,不仅有浮点型和整型数据等基本类型,还有更为复杂的数据形式。这些复杂的数据类型能够有效表示更丰富的信息内容。
形状简介
张量具有形状特征。
形状表示张量各轴上的元素数量。
秩定义为张量所具有的轴的数量。标量不具备任何维度(秩为0),向量具有一维(秩1),矩阵具有二维结构(秩2)。
轴/维度是张 tensor 中特定的方向或方向概念。
大小指的是该 tensor 所包含的所有元素总数,并等于其形状向量各分项数值相乘的结果值之积值总和计算方式所得结果值总和计算方式所得结果值总和计算方式所得结果值总和计算方式所得结果值总和计算方式所得结果值总和计算方式所得结果值总和计算方式所得结果值总和计算方式所得结果值总和计算方式所得结果值总和计算方式所得结果值总和计算方式所得结果值总和计算方式所得结果值总和其他属性信息的操作方法简便高效?
rank_4_tensor = tf.zeros([3,2,4,5])
print("type of every element:",rank_4_tensor.dtype) #dtype
print("number of dimensions:",rank_4_tensor.ndim)#维度
print("shape of tensor:",rank_4_tensor.shape)
print("elements along axis 0 of tensor:",rank_4_tensor.shape[0])
print("elements along the last axis of tensor:",tf.size(rank_4_tensor).numpy())
Type of every element: <dtype: 'float32'>
Number of dimensions: 4
Shape of tensor: (3, 2, 4, 5)
Elements along axis 0 of tensor: 3
Elements along the last axis of tensor: 5
Total number of elements (3*2*4*5): 120
通常按照从全局到局部依次排列的方式排列各轴:首先是批处理轴(Batch Axis),接着是空间维度(Spatial Dimension),最后则是每个位置所具有的特征。

单轴索引采用从起始位置开始计算的方式进行编号。
负数值则表示从末尾往回数的序列位置。
在切片操作中,默认情况下冒号用于区分start位置、stop位置以及step值。
rank_1_tensor = tf.constant([0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
print("First:", rank_1_tensor[0].numpy())
print("Second:", rank_1_tensor[1].numpy())
print("Last:", rank_1_tensor[-1].numpy())
print("Everything:", rank_1_tensor[:].numpy())
print("Before 4:", rank_1_tensor[:4].numpy())
print("From 4 to the end:", rank_1_tensor[4:].numpy())
print("From 2, before 7:", rank_1_tensor[2:7].numpy())
print("Every other item:", rank_1_tensor[::2].numpy())
print("Reversed:", rank_1_tensor[::-1].numpy())
first:0
second:1
last:34
Everything: [ 0 1 1 2 3 5 8 13 21 34]
Before 4: [0 1 1 2]
From 4 to the end: [ 3 5 8 13 21 34]
From 2, before 7: [1 2 3 5 8]
Every other item: [ 0 1 3 8 21]
Reversed: [34 21 13 8 5 3 2 1 1 0]
多轴索引
对于高秩张量的每个单独的轴,遵循与单轴完全相同的规则。
rank_2_tensor = tf.constant([[1. 2.]
[3. 4.]
[5. 6.]])
Everything: [ 0 1 1 2 3 5 8 13 21 34]
Before 4: [0 1 1 2]
From 4 to the end: [ 3 5 8 13 21 34]
From 2, before 7: [1 2 3 5 8]
Every other item: [ 0 1 3 8 21]
Reversed: [34 21 13 8 5 3 2 1 1 0]
Second row: [3. 4.]
Second column: [2. 4. 6.]
Last row: [5. 6.]
First item in last column: 2.0
Skip the first row:
[[3. 4.]
[5. 6.]]
操作形状
dtypes详解 了解数据类型的详细说明 通过Tensor.dtype属性可以获取tf.Tensor的详细数据类型信息 在创建tf.tensor时可以从Python对象中选择指定的数据类型 不同数据类型的之间可以进行转换 tf.cast()函数的作用是执行tensorflow中的张量数据类型转换 例如读入的图片如果是int8类型的 需要在训练前将图像的数据格式转换为float32
cast函数的作用是将输入数据转换为指定的数据类型。输入x为待转换的多维数据结构。目标数据类型由dtype参数指定。可选的name参数用于标识此操作。
import tensorflow as tf
t1 = tf.Variable([1,2,3,4,5])
t2 = tf.cast(t1,dtype=tf.float32)
print 't1: {}'.format(t1)
print 't2: {}'.format(t2)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(t2)
print t2.eval()
# print(sess.run(t2))
t1: <tf.Variable 'Variable:0' shape=(5,) dtype=int32_ref>
t2: Tensor("Cast:0", shape=(5,), dtype=float32)
[ 1. 2. 3. 4. 5.]
the_f64_tensor = tf.constant([2.2, 3.3, 4.4], dtype=tf.float64)
the_f16_tensor = tf.cast(the_f64_tensor, dtype=tf.float16)
# Now, let's cast to an uint8 and lose the decimal precision
the_u8_tensor = tf.cast(the_f16_tensor, dtype=tf.uint8)
print(the_u8_tensor)
tf.Tensor([2 3 4], shape=(3,), dtype=uint8)
在特定条件下,在一组张量上实施组合运算时,在应对大尺寸张量的情况下会对小尺寸张量进行扩展处理。
最基础且常见的情况是尝试将一个张量与一个标量进行相乘或相加操作。
在这种情况下会对该标量施加广播操作以使其与其它参数具有相同的形状。
t1: <tf.Variable 'Variable:0' shape=(5,) dtype=int32_ref>
t2: Tensor("Cast:0", shape=(5,), dtype=float32)
[ 1. 2. 3. 4. 5.]
tf.Tensor([2 4 6], shape=(3,), dtype=int32)
tf.Tensor([2 4 6], shape=(3,), dtype=int32)
tf.Tensor([2 4 6], shape=(3,), dtype=int32)
通常情况下,在计算和存储资源方面使用广播运算更为高效;这是因为 broadcast operations do not explicitly expand tensors in memory.
