我在张量流队列阅读器中生成了一个形状为 (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
Run Code Online (Sandbox Code Playgroud)
我想在这个输出张量的第 4 维上选择一个随机切片,这样每次调用新批次时,也会采用一个新的随机切片。我已经尝试了以下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])
Run Code Online (Sandbox Code Playgroud)
但是,这rand_slice_ind每次都会返回相同的值。我认为这与使用在图形外部生成的非 tensorflow 对象有关。
我也尝试了一些类似的东西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])
Run Code Online (Sandbox Code Playgroud)
但这会导致梯度计算出现问题。有小费吗?
您可以尝试使用Gather来实现此目的:
slice_length = 128
data_length = data.shape[your_axis]
max_offset = data_length - slice_length
random_offset = tf.random.uniform((), minval=0, maxval=max_offset, dtype=tf.dtypes.int64)
slice_indices = tf.range(0, slice_length, dtype=tf.dtypes.int64)
random_slice = tf.gather(data, slice_indices + random_offset, axis=your_axis)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1128 次 |
| 最近记录: |