2017-10-13 10 views
1

Tensor X whithの形状[B、L、E](長さEのL個のベクトルのB個のバッチ)を持っています。このTensor Xから、各バッチでN個のベクトルをランダムに選び、Yを形[B、N、E]で作成したいと思います。Tensorflowの別のものからランダムなテンソルを選ぶ

私はtf.random_uniformとtf.gatherを組み合わせることを試みたが、私は本当に次元に苦労し、Yを得ることができない

答えて

2

あなたはこのようなものを使用することができます

import tensorflow as tf 
import numpy as np 

B = 3 
L = 5 
E = 2 
N = 3 

input = np.array(range(B * L * E)).reshape([B, L, E]) 
print(input) 
print("#################################") 

X = tf.constant(input) 
batch_range = tf.tile(tf.reshape(tf.range(B, dtype=tf.int32), shape=[B, 1, 1]), [1, N, 1]) 
random = tf.random_uniform([B, N, 1], minval = 0, maxval = L - 1, dtype = tf.int32) 

indices = tf.concat([batch_range, random], axis = 2) 

output = tf.gather_nd(X, indices) 
with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    print(sess.run(indices)) 
    print("#################################") 
    print(sess.run(output)) 
関連する問題