Dom*_*ack 13 python tensorflow
我正在尝试运行张量流图来训练模型,并使用单独的评估数据集定期评估.训练和评估数据都是使用队列运行器实现的.
我目前的解决方案是在同一图表中创建两个输入并使用tf.cond依赖于is_training占位符.我的问题由以下代码突出显示:
import tensorflow as tf
from tensorflow.models.image.cifar10 import cifar10
from time import time
def get_train_inputs(is_training):
return cifar10.inputs(False)
def get_eval_inputs(is_training):
return cifar10.inputs(True)
def get_mixed_inputs(is_training):
train_inputs = get_train_inputs(None)
eval_inputs = get_eval_inputs(None)
return tf.cond(is_training, lambda: train_inputs, lambda: eval_inputs)
def time_inputs(inputs_fn, n_runs=10):
graph = tf.Graph()
with graph.as_default():
is_training = tf.placeholder(dtype=tf.bool, shape=(),
name='is_training')
images, labels = inputs_fn(is_training)
with tf.Session(graph=graph) as sess:
coordinator = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coordinator)
t = time()
for i in range(n_runs):
im, l = sess.run([images, labels], feed_dict={is_training: True})
dt = time() - t
coordinator.request_stop()
coordinator.join(threads)
return dt / n_runs
print('Train inputs: %.3f' % time_inputs(get_train_inputs))
print('Eval inputs: %.3f' % time_inputs(get_eval_inputs))
print('Mixed inputs: %.3f' % time_inputs(get_mixed_inputs))
Run Code Online (Sandbox Code Playgroud)
我也不得不注释掉image_summary行133的tensorflow/models/image/cifar10/cifar10_inputs.py.
这产生了以下结果:
Train inputs: 0.055
Eval inputs: 0.050
Mixed inputs: 0.105
Run Code Online (Sandbox Code Playgroud)
在混合的情况下,即使只使用了1,也会读取/解析两个输入.有没有办法避免这种冗余计算?或者是否有更好的方法在仍然利用队列运行器设置的培训/评估数据之间切换?
您是否已阅读此链接有关多输入的最后部分?我认为您可以is_training向输入函数添加一个参数,以区分训练数据和评估数据。然后,您可以重用共享变量来获取 eval 数据的 logits 并为 eval 构建一个操作。然后在你的图表中,运行valudation_accuracy=sess.run(eval_op)以获得评估准确性。
更新:
你好,根据我的理解,如果你想训练 n 批次,评估,训练,评估,你可以在同一个图中保留两个操作,无需构建新的操作。假设您已经构建了所有需要的功能,那么代码应如下所示:
#the following two steps will add train and eval input queue to the graph
train_inputs,train_labels = inputs(is_train=True)
eval_inputs,eval_labels = inputs(is_train=False)
with tf.variable_scope("inference") as scope:
train_logits = inference(train_inputs)
scope.reuse_variables()
eval_logits = inference(eval_inputs)
loss = loss(train_logits,train_labels)
eval_accuracy = accuracy(eval_logits,eval_labels)
#...add train op here,start queue runner and train it ...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3600 次 |
| 最近记录: |