如何在测试集上构建用于训练和评估的模型 [英] How to structure the model for training and evaluation on the test set

查看:45
本文介绍了如何在测试集上构建用于训练和评估的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想训练一个模型.每 1000 步,我想在测试集上对其进行评估并将其写入 tensorboard 日志.然而,有一个问题.我有这样的代码:

I want to train a model. Every 1000 steps, I want to evaluate it on the test set and write it to the tensorboard log. However, there's a problem. I have a code like this:

image_b_train, label_b_train = tf.train.shuffle_batch(...)
out_train = model.inference(image_b_train)
accuracy_train = tf.reduce_mean(...)

image_b_test, label_b_test = tf.train.shuffle_batch(...)
out_test = model.inference(image_b_test)
accuracy_test = tf.reduce_mean(...)

其中模型推理声明模型中的变量.然而,有一个问题.对于测试集,我有一个单独的队列,我不能用 tensorflow 将一个队列交换为另一个队列.

where model inference declares the variables in the model. However, there's a problem. For the test set I have a separate queue, and I can't swap one queue for another with tensorflow.

目前我通过创建 2 个图来解决这个问题,一个用于训练,另一个用于测试.我使用 tf.train.Saver 从一张图复制到另一张图.另一种解决方案可能是使用 tf.get_variable,但这是一个全局变量,我不喜欢它,因为我的代码可重用性降低.

Currently I solved the problem by creating 2 graphs, one for training and the other for testing. I copy from one graph to the other with tf.train.Saver. Another solution might be to use tf.get_variable, but this is a global variable, and I don't like it because my code becomes less reusable.

推荐答案

是的,您需要两张图.这些图可以共享变量.这可以通过以下方式完成:

Yes, you need two graphs. These graphs can share variables. This can be done by:

  1. 使用 Keras 层(来自 tf.contrib.keras),让您一次定义模型并使用它来计算两个推理图
  2. 使用带有 tf.get_variable 的细长样式层(来自 tf.layers)并重用
  3. 使用 tf.make_template 制作您自己的类模型对象,该对象可以调用一次以构建训练图,一次构建推理图
  4. 使用 tf.estimator.Estimator 可让您定义模型函数一次并自动运行它以供您训练和评估
  1. Using Keras layers (from tf.contrib.keras) which let you define the model once and use it to compute two inference graphs
  2. Using slim-style layers (from tf.layers) with tf.get_variable and reuse
  3. Using tf.make_template to make your own model-like object which can be called once to build the training graph and once to build the inference graph
  4. Using tf.estimator.Estimator which lets you define a model function once and runs it automatically for training and evaluation for you

还有其他选项,但其中任何一个都得到了很好的支持,应该可以解除对您的阻止.

There are other options, but any of these is well-supported and should unblock you.

这篇关于如何在测试集上构建用于训练和评估的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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