我有张量流队列读取器生成的输入张量形状(1,512,512,32)

batch_input, batch_output = tf.train.shuffle_batch([image, label], batch_size=BATCH_SIZE, capacity=3 * BATCH_SIZE + min_queue_examples,
                                                    enqueue_many=True, min_after_dequeue=min_queue_examples, num_threads=16)
#BATCH_SIZE = 1

我想在此输出张量的第四维上选择一个随机切片,这样每次调用一个新批次时,也会采用一个新的随机切片 . 我用 numpy 试过以下

rand_slice_ind = np.random.randint(0, 32)
slice_begin = tf.constant([0, 0, 0, rand_slice_ind])
slice_input = tf.slice(batch_input, begin = slice_begin, size = [BATCH_SIZE, height, width, 1])

但是,每次都返回 rand_slice_ind 的相同值 . 我假设这与使用在图形外部生成的非张量流对象有关 .

我也尝试了 tf.random_uniform 的内容:

rand_slice_ind = tf.random_uniform([], minval=0, maxval=depth, dtype=tf.int32)    
slice_begin = tf.Variable([tf.constant(0, dtype=tf.int32), tf.constant(0, dtype=tf.int32), tf.constant(0, dtype=tf.int32), rand_slice_ind])

但这会导致梯度计算出现问题 . 有小费吗?