我正在做一个通过Tensorflow提升(4层DNN到5层DNN)的例子。我在TF中使用save session和restore,因为TF tute中有一个简短的段落: “例如,你可能已经训练了一个4层的神经网络,现在你想训练一个5层的新模型,将先前训练的模型的4层参数恢复到新模型的前4层中。”tensorflow tute在https://www.tensorflow.org/how_tos/variables/中给出了灵感。
然而,我发现没有人问过当检查点保存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)
其中,“fcl1”中的变量(或从“fcl1”调用的变量),以便我有“fcl1/Variable”和“fcl1/Variable U1”用于权重和偏移-“fcl2”、“fclf”和“outputl”由saver.save()存储在脚本中,而不使用“boosting”层。但是,由于我们现在有了“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库来完成。获取模型中的变量列表:
现在变量是一个张量列表,但是对于每个元素,您可以访问其name属性。使用此选项,可以指定只想还原除增强以外的层,例如:
这样你就可以恢复你的4层了。理论上,您可以通过创建另一个服务器来尝试从检查点捕获某个变量的值,该服务器只会增加变量列表并重命名检查点中选择的变量,但我真的不认为这是您需要的。
由于这是模型的自定义层,并且您在任何地方都没有此变量,因此只需在工作流中初始化它,而不是尝试导入它。例如,可以在调用完全连接的函数时传递此参数:
不过,您需要自己检查详细信息,因为我不确定您的导入是什么,您在这里使用的是哪个函数。
一般来说,我建议您查看slim库,这将使您更容易为层定义模型和作用域(而不是通过使用定义它,您可以在调用函数时传递作用域参数)。如果是斯利姆的话,看起来会是这样的:
相关问题 更多 >
编程相关推荐