TensorFlow 1.1 Python API (一)
Tensor操作
- 切片与融合
操作 | 描述 |
---|---|
tf.slice(input_,begin,size,name=None) | 从tensor中提取一部分切片
# ‘input’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6]]] tf.slice(input, [1, 0, 0], [1, 1, 3]) ==> [[[3, 3, 3]]]; tf.slice(input, [1, 0, 0], [1, 2, 3]) ==> [[[3, 3, 3], [4, 4, 4]]]; tf.slice(input, [1, 0, 0], [2, 1, 3]) ==> [[[3, 3, 3]], [[5, 5, 5]]] |
tf.split(value, num_or_size_splits, axis=0, num=None, name=’split’) | 将tensor切割成subtensor,其中当num_or_size_splits为标量的时候,表示沿着axis将tensor均匀分成若干等分,当num_or_size_splits为tensor的时候,表示将tensor沿着axisa按照num_or_size_splits进行分割
# ‘value’ is a tensor with shape [5, 30] # Split ‘value’ into 3 tensors with sizes [4, 15, 11] along dimension 1; split0, split1, split2 = tf.split(value, [4, 15, 11], 1) tf.shape(split0) ==> [5, 4] tf.shape(split1) ==> [5, 15] tf.shape(split2) ==> [5, 11] # Split ‘value’ into 3 tensors along dimension 1 split0, split1, split2 = tf.split(value, num_or_size_splits=3, axis=1) tf.shape(split0) ==> [5, 10] |
tf.pad(tensor,paddings,mode=’CONSTANT’,name=None) | 在原有的矩阵上进行填充
# ‘t’ is [[1, 2, 3], [4, 5, 6]]. # ‘paddings’ is [[1, 1,], [2, 2]]. # rank of ‘t’ is 2. pad(t, paddings, “CONSTANT”) ==> [[0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 2, 3, 0, 0], [0, 0, 4, 5, 6, 0, 0], [0, 0, 0, 0, 0, 0, 0]] pad(t, paddings, “REFLECT”) ==> [[6, 5, 4, 5, 6, 5, 4], [3, 2, 1, 2, 3, 2, 1], [6, 5, 4, 5, 6, 5, 4], [3, 2, 1, 2, 3, 2, 1]] pad(t, paddings, “SYMMETRIC”) ==> [[2, 1, 1, 2, 3, 3, 2], [2, 1, 1, 2, 3, 3, 2], [5, 4, 4, 5, 6, 6, 5], [5, 4, 4, 5, 6, 6, 5]] |
tf.concat(values,axis,name=’concat’) | 沿着axis将tensor进行拼接
t1 = [[1, 2, 3], [4, 5, 6]] t2 = [[7, 8, 9], [10, 11, 12]] tf.concat([t1, t2], 0) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] tf.concat([t1, t2], 1) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]] # tensor t3 with shape [2, 3] # tensor t4 with shape [2, 3] tf.shape(tf.concat([t3, t4], 0)) ==> [4, 3] tf.shape(tf.concat([t3, t4], 1)) ==> [2, 6] |
tf.stack(values,axis=0,name=’stack’) | 将列表中所有tensor沿着axis进行拼接
# ‘x’ is [1, 4] # ‘y’ is [2, 5] # ‘z’ is [3, 6] stack([x, y, z]) => [[1, 4], [2, 5], [3, 6]] # Pack along first dim. stack([x, y, z], axis=1) => [[1, 2, 3], [4, 5, 6]] |
tf.reverse(tensor,axis,name=None) | 沿着某个axis进行反转
# tensor ‘t’ is [[[[ 0, 1, 2, 3], # [ 4, 5, 6, 7], # [ 8, 9, 10, 11]], # [[12, 13, 14, 15], # [16, 17, 18, 19], # [20, 21, 22, 23]]]] # tensor ‘t’ shape is [1, 2, 3, 4] # ‘dims’ is [3] or ‘dims’ is -1 reverse(t, dims) ==> [[[[ 3, 2, 1, 0], [ 7, 6, 5, 4], [ 11, 10, 9, 8]], [[15, 14, 13, 12], [19, 18, 17, 16], [23, 22, 21, 20]]]] # ‘dims’ is ‘[1]’ (or ‘dims’ is ‘[-3]’) reverse(t, dims) ==> [[[[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23] [[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]]] # ‘dims’ is ‘[2]’ (or ‘dims’ is ‘[-2]’) reverse(t, dims) ==> [[[[8, 9, 10, 11], [4, 5, 6, 7], [0, 1, 2, 3]] [[20, 21, 22, 23], [16, 17, 18, 19], [12, 13, 14, 15]]]] |
tf.transpose(a,perm=None,name=’transpose’) | 将tensor进行转置
# ‘x’ is [[1 2 3] # [4 5 6]] tf.transpose(x) ==> [[1 4] [2 5] [3 6] # ‘x’ is [[[1 2 3] # [4 5 6]] # [[7 8 9] # [10 11 12]]] tf.transpose(x, perm=[0, 2, 1]) ==> [[[1 4] [2 5] [3 6]] [[7 10] [8 11] [9 12]]] |
tf.one_hot(indices,depth,on_value=None,off_value=None,axis=None, dtype=None,name=None) | 返回一个one-hot tensor,在参数indices中的值表示tenosor中该位置的值取on_value,其余的位置取值为off_value。
如果on_value和off_value没有指定,那么on_value默认为1,off_value默认为0。 如果indices的rank为N,则输出的rank为N+1 indices = [0, 2, -1, 1] depth = 3 on_value = 5.0 off_value = 0.0 axis = -1 Then output is [4 x 3]: output = [5.0 0.0 0.0] // one_hot(0) [0.0 0.0 5.0] // one_hot(2) [0.0 0.0 0.0] // one_hot(-1) [0.0 5.0 0.0] // one_hot(1) indices = [[0, 2], [1, -1]] depth = 3 on_value = 1.0 off_value = 0.0 axis = -1 Then output is [2 x 2 x 3]: output = [ [1.0, 0.0, 0.0] // one_hot(0) [0.0, 0.0, 1.0] // one_hot(2) ][ [0.0, 1.0, 0.0] // one_hot(1) [0.0, 0.0, 0.0] // one_hot(-1) ] indices = [0, 1, 2] depth = 3 The output will be output = [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]] |
tf.gather(params, indices, validate_indices=None, name=None) | 根据indices从params中选出slices聚合成新的tensor |
- 数据类型转换Casting
操作 | 描述 |
---|---|
tf.string_to_number(string_tensor,out_type=None,name=None) | 字符串转化为数值 |
tf.to_double(x,name=’ToDouble’) | 转化为64位浮点类型-float64 |
tf.to_float(x,name=’ToFloat’) | 转化为32位浮点类型-float32 |
tf.to_int32(x,name=’ToInt32’) | 转化为32位整型-int32 |
tf.to_int64(x,name=’ToInt64’) | 转化为64位整型-int64 |
tf.cast(x,dtype,name=None) | 将张量转换为指定的数据类型
# tensor a is [1.8, 2.2], dtype=tf.float tf.cast(a, tf.int32) ==> [1, 2], dtype=tf.int32 |
- shape操作
操作 | 描述 |
---|---|
tf.shape(input,name=None,out_type=tf.int32) | 返回tensor的shape
# ‘t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]] shape(t) ==> [2, 2, 3] |
tf.size(input,name=None,out_type=tf.int32) | 返回tensor中元素的个数
# ‘t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]] size(t) ==> 12 |
tf.rank(input,name=None) | 返回tensor的rank,tensor的rank和矩阵的rank不同,它是指唯一表示tensor中所有元素所需的索引个个数,即常见的“ndims”,“degree”,“order”
# ‘t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]] # shape of tensor ‘t’ is [2, 2, 3] rank(t) ==> 3 |
tf.reshape(tensor,shape,name=None) | 将tensor转换成指定的shape
# tensor ‘t’ is [1, 2, 3, 4, 5, 6, 7, 8, 9] tensor ‘t’ has shape [9] reshape(t, [3, 3]) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # tensor ‘t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6]]] # tensor ‘t’ has shape [3, 2, 3] pass ‘[-1]’ to flatten ‘t’, reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6] |
tf.stack(values,axis=0,name=’stack’) | 将values中的tensor沿着axis堆叠起来生成一个高纬度的tensor,
# ‘x’ is [1, 4] # ‘y’ is [2, 5] # ‘z’ is [3, 6] stack([x, y, z]) => [[1, 4], [2, 5], [3, 6]] # Pack along first dim. stack([x, y, z], axis=1) => [[1, 2, 3], [4, 5, 6]] |
tf.squeeze(input,axis=None,name=None,squeeze_dims=None) | 将tensor中size为1的维度移除
# ‘t’ is a tensor of shape [1, 2, 1, 3, 1, 1] shape(squeeze(t)) ==> [2, 3]; # ‘t’ is a tensor of shape [1, 2, 1, 3, 1, 1] shape(squeeze(t, [2, 4])) ==> [1, 2, 3, 1] |
tf.expand_dims(input,axis=None,name=None,dim=None) | 在tensor中axis中插入size为1的一个维度
# ‘t’ is a tensor of shape [2] shape(expand_dims(t, 0)) ==> [1, 2] shape(expand_dims(t, 1)) ==> [2, 1] shape(expand_dims(t, -1)) ==> [2, 1] |
tf.meshgrid(*args, **kwargs) | 根据向量创建矩阵
#Calling X, Y = meshgrid(x, y) with the tensors x = [1, 2, 3] y = [4, 5, 6] results in X = [[1, 1, 1], [2, 2, 2], [3, 3, 3]] Y = [[4, 5, 6], [4, 5, 6], [4, 5, 6]], 其中生成矩阵X的列向量为x的复制,生成矩阵Y的行向量为y的复制 |