tf.repeat() tensorflow

tf.repeat(input, repeats, axis=None, name=None)

参数:

  • input: tensor
  • repeats: 重复次数, note: len(repeats) must equal input.shape[axis] if axis is not None
  • axis:维度,1则横向增加,0则列向增加。如果axis没有参数,则会先flatten数组,变成一维再重复

举例:

tf.repeat([[1,2],[1,2]], 2, axis=1)
[[1 1 2 2]
 [1 1 2 2]]
 
tf.repeat([[1,2],[1,2]], [1,2], axis=1)
 [[1 2 2]
 [1 2 2]]
  
可见对于repeats为一个整数时,所有元素均重复N次,对于一位数组[a,b],input的第一个元素重复a次,第二个元素重复b次。
当axis为0时
tf.repeat([[1,2],[3,4]], [1,2], axis=0)
 [[1 2]
 [3 4]
 [3 4]]
 
当不设置axis,则将被拉平
tf.repeat([[1,2],[3,4]], 2)
 [1 1 2 2 3 3 4 4]
 
对于一维tensor
tf.repeat([1,2], 2)
 [1 1 2 2]
 
tmp = tf.constant([1, 2])
tf.repeat(tf.reshape(tmp,shape=(2,1)), 2, axis=0)
[[1]
 [1]
 [2]
 [2]]
posted @ 2021-04-21 12:47  ho_ho  阅读(2309)  评论(0编辑  收藏  举报