import tensorflow as tf
# 将input的某一维度复制多少次, len(input.shape()) 等于 len(multiples)
# tf.tile(input, multiples, name=None)
t = tf.constant([[1, 1, 1, 9], [2, 2, 2, 9], [7, 7, 7, 9]])
# 第一维度和第二维度都保持不变
z0 = tf.tile(t, multiples=[1, 1])
# 第1维度不变, 第二维度复制为2份
z1 = tf.tile(t, multiples=[1, 2])
# 第1维度复制为两份, 第二维度不变
z2 = tf.tile(t, multiples=[2, 1])
# tf.contrib.seq2seq.tile_batch(encoder_outputs, multiplier=self.beam_size)
encoder_outputs = tf.constant([[[1, 3, 1], [2, 3, 2]], [[2, 3, 4], [2, 3, 2]]])
print(encoder_outputs.get_shape()) # (2, 2, 3)
# 将batch内的每个样本复制3次, tile_batch() 的第2个参数是一个 int 类型数据
z4 = tf.contrib.seq2seq.tile_batch(encoder_outputs, multiplier=3)
with tf.Session() as sess:
print('z0:\n', sess.run(z0))
print('z1:\n', sess.run(z1))
print('z2:\n', sess.run(z2))
print('z4:\n', sess.run(z4))
运行结果:
(2, 2, 3)
z0:
[[1 1 1 9]
[2 2 2 9]
[7 7 7 9]]
z1:
[[1 1 1 9 1 1 1 9]
[2 2 2 9 2 2 2 9]
[7 7 7 9 7 7 7 9]]
z2:
[[1 1 1 9]
[2 2 2 9]
[7 7 7 9]
[1 1 1 9]
[2 2 2 9]
[7 7 7 9]]
z4:
[[[1 3 1]
[2 3 2]]
[[1 3 1]
[2 3 2]]
[[1 3 1]
[2 3 2]]
[[2 3 4]
[2 3 2]]
[[2 3 4]
[2 3 2]]
[[2 3 4]
[2 3 2]]]
import tensorflow as tf
x = tf.constant([[1, 1, 1], [2, 2, 2]])
with tf.Session() as sess:
print(sess.run(tf.reduce_sum(x))) # 所有求和
print(sess.run(tf.reduce_sum(x, 0))) # 按 列 求和
print(sess.run(tf.reduce_sum(x, 1))) # 按 行 求和
print(sess.run(tf.reduce_sum(x, [0, 1]))) # 行列求和
print(sess.run(tf.reduce_sum(x, reduction_indices=[1])))
x = tf.constant([[1, 2], [3, 4]])
with tf.Session() as sess:
print(sess.run(tf.reduce_mean(x))) # 所有求平均
print(sess.run(tf.reduce_mean(x, 0))) # 按 列 求和
print(sess.run(tf.reduce_mean(x, 1))) # 按行求平均
print(sess.run(tf.reduce_max(x)))
print(sess.run(tf.reduce_max(x, 0)))
print(sess.run(tf.reduce_max(x, 1)))
运行结果:
9
[3 6]
2
[2 3]
[1 3]
4
[3 4]
[2 4]