如何使用 tf.train.shuffle_batch 为训练和推理构建 TF 图? [英] How can I build a TF Graph for both training and inference with tf.train.shuffle_batch?

查看:22
本文介绍了如何使用 tf.train.shuffle_batch 为训练和推理构建 TF 图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:卷积自编码器

我使用 tf.add_to_collections() 将 input_tensor 和 output_tensor 添加到图中

I add the input_tensor and output_tensor to the graph with tf.add_to_collections()

Input to neural network from shuffle batch: [batch_size, patch_size, patch_size, depth]
During training, this is [512, 32, 32, 3]

Input to neural network for inference: [batch_size, height, width, depth]
During inference, this can be [1, 100, 100, 3], or [3, 150, 150, 3], etc.

在推理过程中,我调用 tf.get_collections() 来提取这些节点,然后调用 sess.run().它说形状固定在 [512, 32, 32, 3],我该如何解决?

During inference, I call tf.get_collections() to extract these nodes and then call a sess.run(). It says that the shape is fixed at [512, 32, 32, 3], how can I solve this?

推荐答案

一种可能的解决方案是使用 tf.placeholder_with_default() op 放宽输入 op 的形状要求.例如:

One potential solution is to use a tf.placeholder_with_default() op to relax the shape requirement on the input op. For example:

input_batch = tf.train.shuffle_batch(..., batch_size=512)

input_placeholder = tf.placeholder_with_default(input_batch, [None, None, None, 3])

如果您运行的代码依赖于 input_placeholder 但不提供它,它将使用 tf.train.shuffle_batch() 的结果.或者,如果您为 input_placeholder 提供值,您可以提供任何 4-D 张量(深度为 3),因此您可以使用任何批量大小或图像大小.

If you run code that depends on input_placeholder but don't feed it, it will use the result of tf.train.shuffle_batch(). Alternatively, if you feed a value for input_placeholder you can feed any 4-D tensor (with depth 3), so you can use any batch size or image size.

但是请注意,这样做会禁用训练中的一些优化,因为每个批次的形状现在可以变化,至少在原则上是这样.这会阻止 TensorFlow 考虑一些内部的 tf.shape() 作为常量值调用,这可能意味着它需要在每个训练步骤上做更多的工作.最后,最好为训练和推理构建两个单独的图,因为它们可以单独优化.

Note, however, that doing this will disable some optimizations in training, because the shape of each batch can now vary, at least in principle. This prevents TensorFlow from considering some internal tf.shape() calls as constant values, which might mean that it needs to do more work on each training step. In the end, it may be better to build two separate graphs for training and inference, as these can be optimized separately.

这篇关于如何使用 tf.train.shuffle_batch 为训练和推理构建 TF 图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆