在Tensorflow中对1D张量应用置换

时间:2017-11-23 15:47:33

标签: tensorflow permutation composition

我想创建以下节点:

a=tf.variable([2,0,1,4,3])      (a permutation)
b=tf.variable([11,12,13,14,15])

如何拥有

c="b[a]"=tf.variable([13,11,12,15,14])

3 个答案:

答案 0 :(得分:1)

我找到了另一种方法来解决我的问题,我认为这是一个很好的问题:( tf.gather

a = [2,0,1,4,3]
b = tf.constant([11,12,13,14,15])
c=tf.gather(b,a)
print(sess.run(c))

它给了[12 13 11 14 15]这就是我想要的

答案 1 :(得分:0)

很抱歉,但张量没有permute功能。而且你不能迭代张量或变量中的元素。这是我能提出的最接近的地方:

a = [2,0,1,4,3]
b = tf.constant([11,12,13,14,15])
b_vecs = tf.unstack(b)
b_new = [b_vecs[index] for index in a]
c = tf.stack(b_new)

答案 2 :(得分:0)

如果(伪)随机排列对于您的应用程序是可接受的,则可以使用颠簸来执行排列:

import python as np
arr=np.array([11,12,13,14,15])
np.random.shuffle(arr)
c=tf.Variable(arr)