J'ai sauvegardé un modèle LSTM entraîné et je veux restaurer la prédiction pour l'utiliser dans les tests. J'essayais de suivre this post. Mais j'ai des erreurs. Voici ce que j'ai essayé:Comment enregistrer et restaurer un modèle entraîné par Lstm dans Tensorflow à l'aide de Saver?
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'))
Après cela, dans la phase de formation, je suis en train
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))
Mais cela jette l'erreur suivante:
KeyError: "The name 'prediction:0' refers to a Tensor which does not exist. The operation, 'prediction', does not exist in the graph."
J'ai essayé d'ajouter: tf.add_to_collection('prediction', prediction)
avant d'enregistrer et de remplacer par prediction = tf.get_collection('prediction')[0]
après la restauration. Mais cela me donne l'erreur suivante:
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"]]
Je sais que pour la première erreur, je suis censé attribuer un nom afin de restaurer, mais prediction
est pas une variable tensorflow. Je suis passé par quelques articles et articles précédents, mais incapable de trouver une solution de travail. Donc, mes questions sont:
- Est-ce que je fais quelque chose de mal conceptuellement? Si oui, quoi?
- Sinon, y a-t-il une erreur de mise en œuvre? Et comment puis-je le résoudre?
Merci.