python - 为训练和验证数据提供张量

标签 python tensorflow

在 tensorflow 示例中,feed_dict 用于将训练或验证输入发送到同一模型图中。不幸的是,您不能提供张量:

    Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.

我一直在使用输入管道和 TFRecordReader,所以我的数据从未真正进入 python。必须调用 run 将数据输入 python 并将其反馈给 tensorflow 似乎很愚蠢,而且速度肯定很慢。

有人对此有好的解决方案吗?

目前,我只是创建两个使用相同参数的模型子图的相同副本。这行得通,但迫使我以一种奇怪的方式组织我的代码。

编辑

例如,我目前正在做类似的事情:

model_params = BuildModelParams()
train_model = BuildModel(model_params, train_input)
test_model = BuildModel(model_params, test_input)

以便测试模型使用通过训练学习到的参数。 feed_dict 的好处是我只需要定义一次模型,而不必将模型的参数与其结构分开。

最佳答案

警告:

当涉及输入队列时,此解决方案可能会导致严重问题。请参阅:https://groups.google.com/a/tensorflow.org/forum/#!msg/discuss/mLrt5qc9_uU/sGNbC7GpAwAJ

感谢@fwalch 在评论中指出这一点


无法完全按照您的要求进行操作,请参阅我的问题的答案 here .

但是 0.7 版新公开的“cond”可以满足您的用例:

# Here are the two data streams.
train_data = tf.Variable(999)
test_data = tf.Variable(1000)

# This selects which stream to use.
select_test = tf.placeholder(dtype=bool,shape=[],name='select_test')
data = tf.cond(
    select_test,
    lambda:test_data,
    lambda:train_data
)

# Here is the model.
model = data-500;

init = tf.initialize_all_variables()
with tf.Session():
    init.run()

    # You just have to feed `select_test` when you evaluate the model.
    print(model.eval({select_test:False})) #499
    print(model.eval({select_test:True})) #500

您可以对 switching Batch Normalization to use a moving average during test 使用相同的技巧.

关于python - 为训练和验证数据提供张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35518072/

相关文章:

python - pycrypto-2.6.1(python 2.7.6) ubuntu14.04 LTS 中无法识别“exportKey”函数

tensorflow - TensorFlow 默认使用什么过滤器进行图像过滤?

tensorflow - 模块未找到错误: No module named 'tflite_support.metadata_writers'

python - tensorflow rnn : name 'seq2seq' is not defined

python - 更改 python 脚本的进程名称

python - 从文件python中写入特定行

tensorflow 启动时间?

python - 是否可以在 tensorboard 中可视化 keras 嵌入?

python - 抽象和具体文本的自然语言处理?

python - 如何在不使用 .get_attribute() 的情况下使用 Selenium python 获取属性