恢复作为 Tensorflow 中新模型子集的变量?

Restore variables that are a subset of new model in Tensorflow?

我正在做一个通过 Tensorflow 提升(4 层 DNN 到 5 层 DNN)的例子。我在 TF 中使用保存会话和恢复来实现它,因为 TF tute 中有一个简短的段落: 'For example, you may have trained a neural net with 4 layers, and you now want to train a new model with 5 layers, restoring the parameters from the 4 layers of the previously trained model into the first 4 layers of the new model.',其中 tensorflow tute 在 https://www.tensorflow.org/how_tos/variables/ 中得到启发。

但是我发现checkpoint保存4层参数的时候没有人问过怎么用'restore'但是我们需要把它放到5层,这就亮红灯了。

用真实代码制作,我制作

with tf.name_scope('fcl1'):
    hidden_1 = fully_connected_layer(inputs, train_data.inputs.shape[1], num_hidden)            
with tf.name_scope('fcl2'):
    hidden_2 = fully_connected_layer(hidden_1, num_hidden, num_hidden)                
with tf.name_scope('fclf'):
    hidden_final = fully_connected_layer(hidden_2, num_hidden, num_hidden)    
with tf.name_scope('outputl'):
    outputs = fully_connected_layer(hidden_final, num_hidden, train_data.num_classes, tf.identity)
    outputs = tf.nn.softmax(outputs)
with tf.name_scope('boosting'):
    boosts = fully_connected_layer(outputs, train_data.num_classes, train_data.num_classes, tf.identity)

where variables inside (or called from) 'fcl1' - 所以我有 'fcl1/Variable' 和 'fcl1/Variable_1' 用于权重和偏差 - 'fcl2', 'fclf' , 和 'outputl' 在没有 'boosting' 层的脚本中由 saver.save() 存储。但是,由于我们现在有 'boosting' 层,因此 saver.restore(sess, "saved_models/model_list.ckpt") 不能像

那样工作
NotFoundError: Key boosting/Variable_1 not found in checkpoint

我真的很希望听到这个问题。谢谢你。 下面的代码是我遇到麻烦的代码的主要部分。

def fully_connected_layer(inputs, input_dim, output_dim, nonlinearity=tf.nn.relu):
    weights = tf.Variable(
        tf.truncated_normal(
            [input_dim, output_dim], stddev=2. / (input_dim + output_dim)**0.5), 
        'weights')
    biases = tf.Variable(tf.zeros([output_dim]), 'biases')
    outputs = nonlinearity(tf.matmul(inputs, weights) + biases)    

    return outputs

inputs = tf.placeholder(tf.float32, [None, train_data.inputs.shape[1]], 'inputs')
targets = tf.placeholder(tf.float32, [None, train_data.num_classes], 'targets')

with tf.name_scope('fcl1'):
    hidden_1 = fully_connected_layer(inputs, train_data.inputs.shape[1], num_hidden)            
with tf.name_scope('fcl2'):
    hidden_2 = fully_connected_layer(hidden_1, num_hidden, num_hidden)                
with tf.name_scope('fclf'):
    hidden_final = fully_connected_layer(hidden_2, num_hidden, num_hidden)    
with tf.name_scope('outputl'):
    outputs = fully_connected_layer(hidden_final, num_hidden, train_data.num_classes, tf.identity)

with tf.name_scope('error'):    
    error = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(outputs, targets))
with tf.name_scope('accuracy'):
    accuracy = tf.reduce_mean(tf.cast(
        tf.equal(tf.argmax(outputs, 1), tf.argmax(targets, 1)), 
        tf.float32))
with tf.name_scope('train'):
    train_step = tf.train.AdamOptimizer().minimize(error)

init = tf.global_variables_initializer()  
saver = tf.train.Saver()

with tf.Session() as sess:
    sess.run(init)
    saver.restore(sess, "saved_models/model.ckpt")
    print("Model restored")

    print("Optimization Starts!")
    for e in range(training_epochs):
        ...

   #Save model - save session        
    save_path = saver.save(sess, "saved_models/model.ckpt")
    ### I once saved the variables using var_list, but didn't work as well...
    print("Model saved in file: %s" % save_path)

为清楚起见,检查点文件有

fcl1/Variable:0

fcl1/Variable_1:0

fcl2/Variable:0

fcl2/Variable_1:0

fclf/Variable:0

fclf/Variable_1:0

outputl/Variable:0

outputl/Variable_1:0

因为原来的4层模型没有'boosting'层

在这种情况下,从检查点读取提升值看起来不对,我认为这不是你想要做的。显然您遇到了错误,因为在恢复变量时您首先捕获了模型中所有变量的列表,然后您在检查点中查找相应的变量,而检查点中没有这些变量。

您可以通过定义模型变量的子集来仅恢复模型的一部分。例如,您可以使用 tf.slim 库来完成。获取模型中的变量列表:

variables = slim.get_variables_to_restore()

现在变量是一个张量列表,但对于每个元素,您都可以访问其名称属性。使用它你可以指定你只想恢复层而不是提升,例如:

variables_to_restore = [v for v in variables if v.name.split('/')[0]!='boosting'] 
model_path = 'your/model/path'

saver = tf.train.Saver(variables_to_restore)

with tf.Session() as sess:
    saver.restore(sess, model_path)

这样你就可以恢复你的 4 层了。从理论上讲,您可以尝试通过创建另一个服务器来从检查点捕获其中一个变量的值,该服务器只会在变量列表中进行提升并从检查点重命名所选变量,但我真的认为这不是您在这里需要的。

由于这是您的模型的自定义层,并且您在任何地方都没有此变量,因此只需在您的工作流程中对其进行初始化,而不是尝试导入它。例如,您可以在调用函数时传递此参数 fully_connected:

weights_initializer = slim.variance_scaling_initializer()

不过你需要自己检查详细信息,因为我不确定你的导入是什么以及你在这里使用的是哪个函数。

一般来说,我建议您看一下 slim 库,这将使您更容易为层定义模型和范围(而不是通过 with 定义它,您可以在调用时传递一个范围参数一个函数)。它看起来像 slim:

boost = slim.fully_connected(input, number_of_outputs, activation_fn=None, scope='boosting', weights_initializer=slim.variance_scaling_initializer())