摘录一下之前项目遇到这个问题时,在Stack Overflow上提问获得的几种解决方案。
问题
I want to get a random sub-tensor from a tensor, and the shape is fixed. For example, I need to get the right tensor from the left tensor, and index is random for every row, just like this:
[[1 4 3] [[3] [[4]
[3 2 1] -----> [2] or [1] (generate randomly)
[0 3 4]] [3]] [0]]
I tried tf.slice and tf.gather, It doesn't work. And I tried to write a code testcase like this:
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]]
In this testcase, It worked. But in a real environment, 'a'.shape=(None, 3), so
'T = a.get_shape().as_list()[0]' is not a int value, i can't iterate T by range(T). For example:
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}))
In this case, it doesn't work. Who can tell me what should i do?
回答1
This is how you can do that with 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]]
回答2
I normally use the numpy library to do this.
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
Then you can get one column as wanted with the following code:
a_column = a_instance[:, 0]
In this way you have the wanted random column as numpy array that you can then use with tensorflow as showed:
...
print(sess.run(y_hat, feed_dict={a: [a_column.tolist()]}))
Remember also to use a copy of "a_instance" with the shuffle method if you do not want to modify the "a_instance" matrix permanently.