私はTensorflowを介してブースティング(4層DNN〜5層DNN)の例を行っています。 TF Tuteの簡潔な段落があるのでセッションを保存してTFで復元しています: 'たとえば、4つのレイヤーを持つニューラルネットを訓練したことがあります。新しいレイヤーを5つのレイヤーで訓練したいと思っています。以前に訓練されたモデルの4つのレイヤーから新しいモデルの最初の4つのレイヤーにパラメータを復元します。テンソルフローはhttps://www.tensorflow.org/how_tos/variables/にインスパイアされます。Tensorflowの新しいモデルのサブセットである変数を復元しますか?
しかし、チェックポイントが4つのレイヤのパラメータを保存するときに誰も "復元"を使用する方法を尋ねなかったことがわかりましたが、5つのレイヤにパラメータを保存して赤いフラグを立てる必要があります。重量およびバイアスをI 'はfcl1 /変数' を有するように 'fcl1/Variable_1' - 実際のコードでこれを作る
、Iは、変数内部(又はから呼び出さ) 'fcl1'
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)
を作っ - 'fcl2'、 'fclf'、および 'outputl'は、 'boosting'レイヤーなしでスクリプト内のsaver.save()によって保存されます。しかし、我々は今、層、saver.restore(SESの、「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)
、チェックポイントファイルは、元の4層モデルとして
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
を持っている層を '後押し' はありません。
あなたは 'tf.Saver' [コンストラクタ](https://www.tensorflow.org/api_docs/pythonの' var_list'パラメータを使用してモデルを復元することができます/ state_ops/saving_and_restoring_variables)。 その後、レイヤ5の初期化が適切に行われます。 – drpng