tensorflow学习|reduce_sum、reshape、expand_dim、batch_matmul(小记)
最近在看tensorflow Cookbook,记录一下偶尔会记不清的几个api,如果有那里写的不对请多指正!
首先是reduce_sum:
reduce_sum(
input_tensor,
axis=None,
keep_dims=False,
name=None,
reduction_indices=None
)
input_tensor:表示输入
axis:(直译为轴)表示在那个维度进行sum操作。
keep_dims:表示是否保留原始数据的维度,False相当于执行完后原始数据就会少一个维度。
# 'x' is [[1, 1, 1]
# [1, 1, 1]]
tf.reduce_sum(x)# 6
tf.reduce_sum(x, 0)# [2, 2, 2]
tf.reduce_sum(x, 1)# [3, 3]
tf.reduce_sum(x, 1, keep_dims=True)# [[3], [3]]
tf.reduce_sum(x, [0, 1])# 6
其次是reshape:
tf.reshape(tensor, shape, name=None)
tensor:表示输入张量
shape:想要转换为的shape形式
(可以存在一个-1表示缺省值,意思为自动计算,不需要手动输入,例如[-1,1]意思是为转换为n行1列)
# tensor 'tensor' is [1, 2, 3]
tf.reshape(tensor, [-1, 1])''' [[1],
[2],
[3]]'''
其次是expand_dim:
tf.expand_dims(input, dim, name=None)
imput:表示输入张量
dim:维数的索引(-1为最后一维)
# shape of tensor 'tensor' is [3, 2, 3] 注意是tensor的shape
tf.expand_dims(tensor, -1)# shape of tensor 'tensor' is [3, 2, 3, 1]
tf.expand_dims(tensor, 1)# shape of tensor 'tensor' is [3, 1, 2, 3]
tf.expand_dims(tensor, 0)# shape of tensor 'tensor' is [1, 3, 2, 3]
batch_matmul:
tf.batch_matmul(input_tensorA, input_tensorB)
新版本tf已经移出该函数,使用matmul替换即可达到一样的效果
# 对于三维张量的tA(shape = [a, b, c])和tB(shape = [a, c, d])
tf.batch_matmul(tA, tB)# shape = [a, b, d]
对于三维张量的tA(shape = [a, b, c])和tB(shape = [a, c, d])
a为batch_size做批乘法
比如 tA(shape = [10, 4, 6])和tB(shape = [10, 6, 2])
结果为 shape = [10, 4, 2]
batch_size保持不变
posted on 2018-08-14 21:47 shillyshally 阅读(398) 评论(0) 收藏 举报
浙公网安备 33010602011771号