Tensorflow:保存模型并恢复单个重量

时间:2017-04-29 19:10:06

标签: python tensorflow save deep-learning pre-trained-model

我的问题是:我保存了一个模型。现在我想更改模型(添加或删除一些图层),但仍然导入我之前训练过的权重。如何在初始化新零件的同时恢复模型的各个部分?

更具体一点:

我有一个张量流模型,其权重看起来像这样:

global_step = tf.get_variable("global_step", shape=[1],
                  trainable=False, initializer=tf.constant_initializer(1))

#TODO FUTURE: Time-video as input, for possible Seq2Seq model
#TODO: Add "regularizer=None"
Weights = {
    "W_Conv1": tf.get_variable("W_Conv1", shape=[3, 3, 1, 64],
            initializer=tf.random_normal_initializer(mean=0.00, stddev=0.01),
    ),
    "W_Conv2": tf.get_variable("W_Conv2", shape=[3, 3, 64, 64],
            initializer=tf.random_normal_initializer(mean=0.00, stddev=0.01),
    ),
    #not sure if we sum over all 64 channels?!
    "W_Local1": tf.get_variable("W_Local1", shape=[1, 16 * 8 * 64],
            initializer=tf.random_normal_initializer(mean=0.00, stddev=0.01),
    ),
    "W_Local2": tf.get_variable("W_Local2", shape=[1, 16 * 8 * 64],
            initializer=tf.random_normal_initializer(mean=0.00, stddev=0.01),
    ),
    "W_Affine1": tf.get_variable("W_Affine1", shape=[16*8*64, 512],
            initializer=tf.random_normal_initializer(mean=0.00, stddev=0.01),
    ),
    "W_Affine2": tf.get_variable("W_Affine2", shape=[512, 128],
            initializer=tf.random_normal_initializer(mean=0.00, stddev=0.01),
    ),
    "W_Affine3": tf.get_variable("W_Affine3", shape=[128, 10],
            initializer=tf.random_normal_initializer(mean=0.00, stddev=0.01),
    )
}

#TODO: Do local layers have a bias term?
Bias = {
    "b_Conv1": tf.get_variable("b_Conv1", shape=[1, 16, 8, 64],
            initializer=tf.random_normal_initializer(mean=0.00, stddev=0.01),
    ),
    "b_Conv2": tf.get_variable("b_Conv2", shape=[1, 16, 8, 64],
            initializer=tf.random_normal_initializer(mean=0.00, stddev=0.01),
    ),
    "b_Local1": tf.get_variable("b_Local1", shape=[1, 8192],
            initializer=tf.random_normal_initializer(mean=0.00, stddev=0.01),
    ),
    "b_Local2": tf.get_variable("b_Local2", shape=[1, 8192],
            initializer=tf.random_normal_initializer(mean=0.00, stddev=0.01),
    ),
    "b_Affine1": tf.get_variable("b_Affine1", shape=[1, 512],
            initializer=tf.random_normal_initializer(mean=0.00, stddev=0.01),
    ),
    "b_Affine2": tf.get_variable("b_Affine2", shape=[1, 128],
            initializer=tf.random_normal_initializer(mean=0.00, stddev=0.01),
    ),
    "b_Affine3": tf.get_variable("b_Affine3", shape=[1, 10],
            initializer=tf.random_normal_initializer(mean=0.00, stddev=0.01),
    )
}

假设我使用tf.train.Saver()保存此模型。 现在,我想运行这个模型。但是,在将来的某个时刻,我可能想要更改模型,但保持我迄今为止训练的权重。如何单独恢复某些重量? 更具体地说,如果我添加行

如何再次恢复模型
tmpval = tf.get_variable("new_var", shape=[1],
             initializer=tf.constant_initializer(0.1))

到上面定义的模型权重?

我是张量流的初学者,所以欢迎任何建议和想法。提前致谢! :)

1 个答案:

答案 0 :(得分:0)

我认为您的问题已在TF教程'Choosing which variables to save and restore'

中介绍
Foreach (x as y) && Foreach (a as b) {
  /* Magic happens here */