2

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

更加具体:

我有一个 tensorflow 模型,其权重如下所示:

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))

到上面定义的模型权重?

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

4

1 回答 1

0

我认为您的问题已包含在 TF 教程“选择要保存和恢复的变量”中

# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Add ops to save and restore only 'v2' using the name "my_v2"
saver = tf.train.Saver({"my_v2": v2})
# Use the saver object normally after that.
...
于 2017-04-29T20:18:23.327 回答