如何创建两个图形进行训练和验证?

时间:2018-10-13 07:10:59

标签: tensorflow

当我阅读有关图和会话(Graphs and Sessions)的张量流指导时,我发现他们建议创建两个图进行训练和验证。

enter image description here

我认为这是合理的,我想使用它,因为我的训练和验证模型是不同的(对于编码器-解码器模式或辍学而言)。但是,我不知道如何在不使用tf.saver()的情况下使训练图中的变量可用于测试图。
当我创建两个图并在每个图内创建变量时,我发现这两个变量完全不同,因为它们属于不同的图。 我已经在Google上搜索了很多,而且我知道对此问题有疑问,例如question1。但是仍然没有有用的答案。如果有任何代码示例,或者任何人都知道如何分别创建两个图形进行训练和验证,例如:

def train_model():
    g_train = tf.graph()
    with g_train.as_default():
        train_models
def validation_model():
    g_test = tf.graph()
    with g_test.as_default():
         test_models

1 个答案:

答案 0 :(得分:1)

一种简单的方法是创建一个“转发函数”,以定义模型并根据其他参数更改行为。

这里是一个例子:

def forward_pass(x, is_training, reuse=tf.AUTO_REUSE, name='model_forward_pass'):

  # Note the reuse attribute as it tells the getter to either create the graph or get the weights

  with tf.variable_scope(name=name, reuse=reuse):
     x = tf.layers.conv(x, ...)
     ...
     x = tf.layers.dense(x, ...)
     x = tf.layers.dropout(x, rate, training=is_training) # Note the is_training attribute

     ...
     return x

现在,您可以在代码中的任何位置调用“ forward_pass”函数。您只需要提供is_training属性即可使用正确的辍学模式。只要“ variable_scope”的“名称”相同,“ reuse”参数将自动为您的体重获取正确的值。

例如:

train_logits_model1 = forward_pass(x_train, is_training=True, name='model1')
# Graph is defined and dropout is used in training mode

test_logits_model1 = forward_pass(x_test, is_training=False, name='model1')
# Graph is reused but the dropout behaviour change to inference mode

train_logits_model2 = forward_pass(x_train2, is_training=True, name='model2')
# Name changed, model2 is added to the graph and dropout is used in training mode

要在您希望拥有两个分离的图形时添加此答案,可以使用分配函数来添加图形:

train_graph = forward_pass(x, is_training=True, reuse=False, name='train_graph')

...
test_graph = forward_pass(x, is_training=False, reuse=False, name='test_graph')

...
train_vars = tf.get_collection('variables', 'train_graph/.*')
test_vars = tf.get_collection('variables','test_graph/.*')
test_assign_ops = []
for test, train in zip(test_vars, train_vars):
    test_assign_ops += [tf.assign(test, train)]
assign_op = tf.group(*test_assign_ops)

sess.run(assign_op) # Replace vars in the test_graph by the one in train_graph

我是方法1的拥护者,因为它更清洁并减少了内存使用。