tf.tile 对于张量块

tf.tile for blocks of tensor

我有以下张量 a,我想以两种不同的方式对其使用 tf.tile 以获得不同的结果。

a.eval() = array([[ 1],
       [ 2],
       [ 3],
       [10],
       [20],
       [30]], dtype=int32)

我知道我能做到:

a_rep = tf.tile(a, [1,2])

a_rep = tf.reshape(rep, (12, 1)) 

为了获得:

a_rep.eval() = array([[ 1],
       [ 1],
       [ 2],
       [ 2],
       [ 3],
       [ 3],
       [10],
       [10],
       [20],
       [20],
       [30],
       [30]], dtype=int32)

如何使用tf.tile得到下面的结果?我基本上想要 具有特定大小的张量块来重复 而不是只有一个值。

a_rep.eval() = array([[ 1],
       [ 2],
       [ 3],
       [ 1],
       [ 2],
       [3],
       [10],
       [20],
       [30],
       [10],
       [20],
       [30]], dtype=int32)

非常感谢您!

类似的技巧,您平铺第二个维度,但将 "groups" 堆叠到一个新的第三个维度:

import tensorflow as tf

with tf.Session() as sess:
    a = tf.constant([[ 1], [ 2], [ 3], [10], [20], [30]], dtype=tf.int32)
    group_size = 3
    repeats = 2
    result = tf.reshape(tf.tile(tf.reshape(a, (-1, 1, group_size)), (1, repeats, 1)),
                        (-1, 1))
    print(sess.run(result))

输出:

[[ 1]
 [ 2]
 [ 3]
 [ 1]
 [ 2]
 [ 3]
 [10]
 [20]
 [30]
 [10]
 [20]
 [30]]

这假定数组中的元素数可以被大小组整除。如果你想支持最后一个 "partial group" 也许你可以对完整的组执行上述操作,独立平铺最后一位并连接。