Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Tensorflow中的另一个张量中选择随机张量_Tensorflow - Fatal编程技术网

从Tensorflow中的另一个张量中选择随机张量

从Tensorflow中的另一个张量中选择随机张量,tensorflow,Tensorflow,我有一个形状为[B,L,E]的张量X(比如说,长度为E的L向量的B个批次)。从这个张量X中,我想在每个批次中随机选取N个向量,然后用形状[B,N,E]创建Y 我试着将tf.random_uniform和tf.gather结合起来,但我真的很难处理维度,无法得到Y。您可以使用类似这样的方法: import tensorflow as tf import numpy as np B = 3 L = 5 E = 2 N = 3 input = np.array(range(B * L * E)).

我有一个形状为[B,L,E]的张量X(比如说,长度为E的L向量的B个批次)。从这个张量X中,我想在每个批次中随机选取N个向量,然后用形状[B,N,E]创建Y


我试着将tf.random_uniform和tf.gather结合起来,但我真的很难处理维度,无法得到Y。

您可以使用类似这样的方法:

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))