如何在TensorFlow中混合基于队列和基于Feed的输入

use*_*229 2 python-2.7 tensorflow

我最近迁移到一个full_connected样式模型,该模型从TFRecords文件生成的队列中读取输入.事实证明这更有效,但我仍然想用placeholder/feed_dict以交互方式传递参数.

有没有办法在feed_dict和full_connected功能中使用相同的计算图(比如你有一个在init方法中构建图的模型类)?你能获得占位符来接收出列值吗?

mrr*_*rry 8

一种可能性是使用最近添加的(在TensorFlow 0.8中)tf.placeholder_with_default()op,它允许您指定默认值(通常是队列/读取器的输出),并且还允许您提供可能具有不同形状的值.

例如,假设您的队列生成32个元素的批次,其中每个元素具有784个特征,以提供32 x 784矩阵.

input_from_queue = ...  # e.g. `queue.dequeue_many(32)` or `tf.train.batch(..., 32)`
# input_from_queue.get_shape() ==> (32, 784)

input = tf.placeholder_with_default(input_from_queue, shape=(None, 784))
# input.get_shape() ==> (?, 784)

# ...
train_op = ...

sess.run(train_op)  # Takes examples from `queue`.
sess.run(train_op, feed_dict={input: ...})  # Takes examples from `feed_dict`.
Run Code Online (Sandbox Code Playgroud)

这允许您根据需要提供可变大小的批次或使用输入阅读器.