为训练和验证数据提供张量
Feeding tensors for training vs validation data
在张量流示例中,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
的好处是我只需要定义一次模型,而不必将模型的参数与其结构分开。
TL;DR
您可以将输入管道与训练、测试和验证步骤一起使用,而不是使用占位符。当你已经有了张量时,我没有理由不使用它。
相关文档
可以在 TF 的网站上找到更多信息。
例子
在此示例中,我们将发送模型和预期输出以训练和测试函数。唯一的区别是,有些我们使用占位符,而另一些我们使用张量。
import tensorflow as tf
def train(model, T):
"""Example train operation which returns sum of parameters."""
return model + T
def test(model, T):
"""Example test operation which returns parameters multiplied."""
return model * T
# Placeholders which will be required in the feed_dict once we execute a TF_Run.
x_placeholder_model = tf.placeholder(tf.int32)
t_placeholder = tf.placeholder(tf.int32)
# Tensors, using constants for brevity but these could be from an input pipeline
# or some other set of operations.
x_tensor_model = tf.constant([1, 2, 3])
t_tensor = tf.constant([1, 2, 3])
using_placeholder_train = train(x_placeholder_model, t_placeholder)
using_tensor_train = train(x_tensor_model, t_tensor)
using_placeholder_test = test(x_placeholder_model, t_placeholder)
using_tensor_test = test(x_tensor_model, t_tensor)
with tf.Session() as sess:
print(sess.run(
using_placeholder_train, feed_dict={
x_placeholder_model: [1, 2, 3],
t_placeholder: [1, 2, 3]}))
print(sess.run(
using_placeholder_test, feed_dict={
x_placeholder_model: [1, 2, 3],
t_placeholder: [1, 2, 3]}))
print(sess.run(using_tensor_train))
print(sess.run(using_tensor_test))
执行此代码将产生以下输出:
[2 4 6]
[1 4 9]
[2 4 6]
[1 4 9]
说明调用:
print(sess.run(
using_placeholder_train, feed_dict={
x_placeholder_model: [1, 2, 3],
t_placeholder: [1, 2, 3]}))
Return 与以下内容相同的输出:
print(sess.run(using_tensor_train))
无需对 feed_dict
进行任何添加,因为我们已经在图表上提供了张量。
警告:
当涉及输入队列时,此解决方案可能会导致严重问题。参见:https://groups.google.com/a/tensorflow.org/forum/#!msg/discuss/mLrt5qc9_uU/sGNbC7GpAwAJ
感谢@fwalch 在评论中指出这一点
无法完全按照您的要求进行操作,请参阅我的问题的答案 。
但是 0.7 版的新 public“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
您可以对 使用相同的技巧。
在张量流示例中,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
的好处是我只需要定义一次模型,而不必将模型的参数与其结构分开。
TL;DR
您可以将输入管道与训练、测试和验证步骤一起使用,而不是使用占位符。当你已经有了张量时,我没有理由不使用它。
相关文档
可以在 TF 的网站上找到更多信息。
例子
在此示例中,我们将发送模型和预期输出以训练和测试函数。唯一的区别是,有些我们使用占位符,而另一些我们使用张量。
import tensorflow as tf
def train(model, T):
"""Example train operation which returns sum of parameters."""
return model + T
def test(model, T):
"""Example test operation which returns parameters multiplied."""
return model * T
# Placeholders which will be required in the feed_dict once we execute a TF_Run.
x_placeholder_model = tf.placeholder(tf.int32)
t_placeholder = tf.placeholder(tf.int32)
# Tensors, using constants for brevity but these could be from an input pipeline
# or some other set of operations.
x_tensor_model = tf.constant([1, 2, 3])
t_tensor = tf.constant([1, 2, 3])
using_placeholder_train = train(x_placeholder_model, t_placeholder)
using_tensor_train = train(x_tensor_model, t_tensor)
using_placeholder_test = test(x_placeholder_model, t_placeholder)
using_tensor_test = test(x_tensor_model, t_tensor)
with tf.Session() as sess:
print(sess.run(
using_placeholder_train, feed_dict={
x_placeholder_model: [1, 2, 3],
t_placeholder: [1, 2, 3]}))
print(sess.run(
using_placeholder_test, feed_dict={
x_placeholder_model: [1, 2, 3],
t_placeholder: [1, 2, 3]}))
print(sess.run(using_tensor_train))
print(sess.run(using_tensor_test))
执行此代码将产生以下输出:
[2 4 6]
[1 4 9]
[2 4 6]
[1 4 9]
说明调用:
print(sess.run(
using_placeholder_train, feed_dict={
x_placeholder_model: [1, 2, 3],
t_placeholder: [1, 2, 3]}))
Return 与以下内容相同的输出:
print(sess.run(using_tensor_train))
无需对 feed_dict
进行任何添加,因为我们已经在图表上提供了张量。
警告:
当涉及输入队列时,此解决方案可能会导致严重问题。参见:https://groups.google.com/a/tensorflow.org/forum/#!msg/discuss/mLrt5qc9_uU/sGNbC7GpAwAJ
感谢@fwalch 在评论中指出这一点
无法完全按照您的要求进行操作,请参阅我的问题的答案
但是 0.7 版的新 public“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
您可以对