如何从张量中获得随机次张量?

时间:2019-05-31 14:03:21

标签: python tensorflow tensor

我想从张量中获得随机次张量,并且形状是固定的。例如, 我需要从左张量中获取右张量,并且每一行的索引都是随机的,就像这样:

[[1 4 3]         [[3]     [[4]
 [3 2 1]  ----->  [2]  or  [1] (generate randomly)
 [0 3 4]]         [3]]     [0]]

我尝试了tf.slice和tf.gather,它不起作用。我试图编写这样的代码测试用例:

import random
import tensorflow as tf

a = tf.convert_to_tensor([[[1, 4, 3]],
                          [[3, 2, 1]],
                          [[0, 3, 4]]])

T = a.get_shape().as_list()[0]

result_list = []

for i in range(T):
    idx = random.randint(0, 2)  # get a random idx
    result_list.append(a[i][0][idx])

y_hat = tf.reshape(tf.convert_to_tensor(result_list), shape=(T, 1))

with tf.Session() as sess:
    print(sess.run(y_hat))

    # y_hat: [[4]
    #         [1]
    #         [4]]

在此测试用例中,它起作用了。但是在真实环境中,'a'.shape =(None,3),所以
“ T = a.get_shape()。as_list()[0]”不是int值,我无法按range(T)迭代T。 例如:

import random
import tensorflow as tf

a = tf.placeholder(shape=(None, 3), dtype=tf.int32)

result_list = []
T = a.get_shape().as_list()[0]

for i in range(T):
    idx = random.randint(0, 2)  # get a random idx
    result_list.append(a[i][0][idx])

y_hat = tf.reshape(tf.convert_to_tensor(result_list), shape=(T, 1))

with tf.Session() as sess:

    a_instance = [[[1, 4, 3]],
                  [[3, 2, 1]],
                  [[0, 3, 4]]]

    print(sess.run(y_hat, feed_dict={a: a_instance}))

在这种情况下,它不起作用。谁能告诉我该怎么办?

2 个答案:

答案 0 :(得分:0)

这是使用tf.gather_nd的方法:

import tensorflow as tf

with tf.Graph().as_default(), tf.Session() as sess:
    tf.random.set_random_seed(0)
    a = tf.constant([[1, 4, 3],
                     [3, 2, 1],
                     [0, 3, 4]])
    s = tf.shape(a)
    rows, cols = s[0], s[1]
    # Row indices
    ii = tf.expand_dims(tf.range(rows), 1)
    # Column indices
    jj = tf.random.uniform((rows, 1), 0, cols, dtype=ii.dtype)
    # Gather result
    result = tf.gather_nd(a, tf.stack([ii, jj], axis=-1))
    # Print some results
    print(sess.run(result))
    # [[3]
    #  [2]
    #  [4]]
    print(sess.run(result))
    # [[4]
    #  [1]
    #  [0]]
    print(sess.run(result))
    # [[3]
    #  [2]
    #  [0]]

答案 1 :(得分:0)

我通常使用numpy库执行此操作。

import numpy as np

a_instance = np.array([[1,4,3],[3,2,1],[0,3,4]])
a_instance = a_instance.T # transpose the matrix
np.random.shuffle(a_instance) # it performs the shuffle of the rows
a_instance = a_instance.T

然后,您可以使用以下代码获得一列:

a_column = a_instance[:, 0]

通过这种方式,您可以将所需的随机列作为numpy数组,然后可以将其与tensorflow一起使用,如下所示:

...
print(sess.run(y_hat, feed_dict={a: [a_column.tolist()]}))

如果您不想永久修改“ a_instance”矩阵,请记住也要使用shuffle方法使用“ a_instance”的副本。