小编bno*_*orm的帖子

从 Tensorflow 中的张量中提取随机切片

我在张量流队列阅读器中生成了一个形状为 (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)

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

python tensorflow

5
推荐指数
1
解决办法
1128
查看次数

Tensorflow 中的 Dice/Jaccard 系数优化

我正在尝试使用 Dice 系数或 Jaccard 系数来优化我的网络。我的问题是图像分割问题,因此我的输出是形状为 (1, 256, 256, 11) 的张量。为了计算我的输出和真实图像的交集,我采取

tf.argmax(output, axis = 3)
Run Code Online (Sandbox Code Playgroud)

它返回一个“int”数据类型,张量流优化器(特别是AdamOptimizer)似乎不接受,所以我然后将其转换为浮点数

tf.cast(tf.argmax(output, axis = 3), tf.float32)
Run Code Online (Sandbox Code Playgroud)

然而,似乎没有为 tf.cast (或 tf.argmax )定义梯度。有没有人能够成功实施

python optimization machine-learning neural-network tensorflow

3
推荐指数
1
解决办法
2656
查看次数