私は訓練されたLSTMモデルを保存しており、テストで使用するために予測を復元したいと考えています。私はthis postに従おうとしていました。しかし、私は間違いを起こしています。 、Saverを使用してTensorflowでlstm訓練モデルを保存および復元する方法は?
その後x = tf.placeholder('tf.float32', [None, input_vec_size, 1])
y = tf.placeholder('tf.float32')
def recurrent_neural_network(x):
layer = {'weights': tf.Variable(tf.random_normal([n_hidden, n_classes])),
'biases': tf.Variable(tf.random_normal([n_classes]))}
x = tf.transpose(x, [1, 0, 2])
x = tf.reshape(x, [-1, 1])
x = tf.split(x, input_vec_size, 0)
lstm_cell = rnn.BasicLSTMCell(n_hidden, state_is_tuple=True)
outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)
output = tf.add(tf.matmul(outputs[-1], layer['weights']), layer['biases'])
return output
def train_neural_network(x):
prediction = recurrent_neural_network(x)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
Training ...
saver.save(sess, os.path.join(os.getcwd(), 'my_test_model'))
訓練段階で、私は
def test_neural_network(input_data):
with tf.Session() as sess:
#sess.run(tf.global_variables_initializer())
new_saver = tf.train.import_meta_graph('my_test_model.meta')
new_saver.restore(sess, tf.train.latest_checkpoint('./'))
prediction = tf.get_default_graph().get_tensor_by_name("prediction:0")
Calculate features from input_data ...
result = sess.run(tf.argmax(prediction.eval(feed_dict={x: features}), 1))
をしようとしています。しかし、これは次のようなエラーがスローされます:ここで私が試したものです を:
KeyError: "The name 'prediction:0' refers to a Tensor which does not exist. The operation, 'prediction', does not exist in the graph."
それから私は、追加しようとしましたtf.add_to_collection('prediction', prediction)
を保存してから、復元後にprediction = tf.get_collection('prediction')[0]
に置き換えてください。しかし、これは私に次のエラーを与える:私は最初のエラーのために知って
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_2' with dtype float and shape [?,34,1] [[Node: Placeholder_2 = Placeholderdtype=DT_FLOAT, shape=[?,34,1], _device="/job:localhost/replica:0/task:0/cpu:0"]]
、私は復元するために名前を割り当てることになったがprediction
はtensorflow変数ではないのです。私はいくつかの前の記事と記事を見てきましたが、実際の解決策を考え出すことはできません。ですから、私の質問は次の通りです:
- 概念的に間違っているのですか?もしそうなら、何?
- そうでない場合は、実装エラーがありますか?そして私はそれをどのように解決するのですか?
ありがとうございます。