我的意思是说,如果我最初的图中包含以下用于训练的操作:
with tf.Graph.as_default() as g: images, labels = load_batch(...) with slim.argscope(...): logits, end_points = inceptionResnetV2(images, num_classes..., is_training = True) loss = slim.losses.softmax_cross_entropy(logits, labels) optimizer = tf.train.AdamOptimizer(learning_rate = 0.002) train_op = slim.learning.create_train_op(loss, optimizer) sv = tf.train.Supervisor(...) with sv.managed_session() as sess: #perform your regular training loop here with sess.run(train_op)
这样可以很好地训练我的模型,但我想在我的sess
中定期运行一个小型验证数据集来评估我的模型。如果在同一个图中创建一个几乎完全相同的副本,会不会消耗太多内存,像这样:
images_val, labels_val = load_batch(...)with slim.argscope(...): logits_val, end_points_val = inceptionResnetV2(images, num_classes..., is_training = False) predictions = end_points_val['Predictions'] acc, acc_updates = tf.contrib.metrics.streaming_accuracy(predictions, labels_val) #and then following this, we can run acc_updates in a session to update the accuracy, which we can then print to monitor
我的担忧是,为了评估我的验证数据集,我需要将is_training
参数设置为False
以禁用dropout。但是,在同一个图中仅为验证目的从头开始创建一个完整的inception-resnet-v2模型会不会消耗太多内存?还是我应该创建一个全新的文件来独立运行验证?
理想情况下,我希望有三种数据集 – 一个用于训练,一个小型验证数据集用于在训练过程中测试,以及一个最终评估数据集。这个小型验证数据集将帮助我查看我的模型是否对训练数据过拟合。然而,如果我的提议会消耗太多内存,那么偶尔监控训练数据的得分是否等效?在训练时测试验证集有没有更好的方法?
回答:
TensorFlow的开发者已经考虑到了这一点,并使变量准备好共享。你可以查看这里的文档。
正确使用作用域可以使变量重用成为可能。一个非常好的例子(虽然上下文是语言模型,但无关紧要)是TensorFlow PTB Word LM。
这种方法的全局伪代码大致如下:
class Model: def __init__(self, train=True, params): """ Build the model """ tf.placeholder( ... ) tf.get_variable( ...) def main(_): with tf.Graph.as_default() as g: with tf.name_scope("Train"): with tf.variable_scope("Model", reuse=None): train = Model(train=True, params ) with tf.name_scope("Valid"): # Now reuse variables = no memory cost with tf.variable_scope("Model", reuse=True): # But you can set different parameters valid = Model(train=False, params) session = tf.Session ...
因此,你可以共享一些变量,而无需拥有完全相同的模型,因为参数可能会改变模型本身。
希望这对你有帮助pltrdy