我正在做一个通过 Tensorflow 增强(4 层 DNN 到 5 层 DNN)的示例。我在 TF 中使用保存会话和恢复来制作它,因为 TF 教程中有一个简短的段落:
'例如,你可能已经训练了一个 4 层的神经网络,现在想要训练一个 5 层的新模型,将之前训练模型的 4 层的参数恢复到新模型的前 4 层。 ',tensorflow tute 的启发之处https://www.tensorflow.org/how_tos/variables/ https://www.tensorflow.org/how_tos/variables/.
然而,我发现没有人询问当检查点保存 4 层参数时如何使用“恢复”,但我们需要将其放入 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)
其中变量在“fcl1”内部(或从“fcl1”调用) - 这样我就可以使用“fcl1/Variable”和“fcl1/Variable_1”来表示权重和偏差 -“fcl2”、“fclf”和“outputl”由 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”层。