我想在训练集(is_training=True)和验证集(is_training=False)上运行给定的模型,特别是如何应用dropout。现在,prebuilt models公开了一个参数is_training,该参数在构建网络时传递给dropout层。问题是,如果我使用不同的is_training值调用该方法两次,我将得到两个不同的网络,它们不共享权重(我认为?)。我如何让这两个网络共享相同的权重,以便我可以运行我在验证集上训练的网络?
发布于 2016-09-08 17:58:08
我用你的评论写了一个解决方案,在训练和测试模式下使用Overfeat。(我不能测试它,这样你就可以检查它是否工作了?)
首先是一些导入和参数:
import tensorflow as tf
slim = tf.contrib.slim
overfeat = tf.contrib.slim.nets.overfeat
batch_size = 32
inputs = tf.placeholder(tf.float32, [batch_size, 231, 231, 3])
dropout_keep_prob = 0.5
num_classes = 1000在训练模式下,我们将一个正常范围传递给函数overfeat
scope = 'overfeat'
is_training = True
output = overfeat.overfeat(inputs, num_classes, is_training,
dropout_keep_prob, scope=scope)然后在测试模式下,我们使用reuse=True创建相同的作用域。
scope = tf.VariableScope(reuse=True, name='overfeat')
is_training = False
output = overfeat.overfeat(inputs, num_classes, is_training,
dropout_keep_prob, scope=scope)发布于 2017-03-01 22:29:09
您可以只使用is_training的占位符:
isTraining = tf.placeholder(tf.bool)
# create nn
net = ...
net = slim.dropout(net,
keep_prob=0.5,
is_training=isTraining)
net = ...
# training
sess.run([net], feed_dict={isTraining: True})
# testing
sess.run([net], feed_dict={isTraining: False})发布于 2017-09-27 14:42:59
这取决于情况,解决方案是不同的。
我的第一个选择是使用不同的过程来进行评估。您只需检查是否存在新的检查点,并将权重加载到评估网络中(使用is_training=False):
checkpoint = tf.train.latest_checkpoint(self.checkpoints_path)
# wait until a new check point is available
while self.lastest_checkpoint == checkpoint:
time.sleep(30) # sleep 30 seconds waiting for a new checkpoint
checkpoint = tf.train.latest_checkpoint(self.checkpoints_path)
logging.info('Restoring model from {}'.format(checkpoint))
self.saver.restore(session, checkpoint)
self.lastest_checkpoint = checkpoint第二种选择是在每个时期之后卸载图并创建新的评估图。这种解决方案浪费了大量的加载和卸载图形的时间。
第三个选项是共享权重。但是,向这些网络提供队列或数据集可能会导致问题,因此您必须非常小心。我只在暹罗网络中使用它。
with tf.variable_scope('the_scope') as scope:
your_model(is_training=True)
scope.reuse_variables()
your_model(is_training=False)https://stackoverflow.com/questions/39353503
复制相似问题