我是tensorflow的新手,我一直在训练一个简单的神经网络,但是一旦训练完成,我就不知道如何重用NN来获得输入的输出。
def train_neural_network(x,y,aDataTrain,aTargetTrain,aDataTest,aTargetTest):
batch_size = 500
prediction = neural_network_model(x,len(aDataTrain[0]))
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))
optimizer = tf.train.AdamOptimizer().minimize(cost)
hm_epochs = 1
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epochs):
epoch_loss = 0
i = 0
while i < len(aDataTrain):
start = i
end = i + batch_size
batch_x = np.array(aDataTrain[start:end])
batch_y = np.array(aTargetTrain[start:end])
_,c = sess.run([optimizer,cost],feed_dict={x:batch_x,y:batch_y})
epoch_loss += c
i += batch_size
print ("Epoch", epoch, "completed out of", hm_epochs, "loss", epoch_loss)
correct =tf.equal(tf.argmax(prediction,1), tf.argmax(y,1))
accurracy = tf.reduce_mean(tf.cast(correct,'float'))
finalAcc = accurracy.eval({x:aDataTest,y:aTargetTest})
saver.save(sess, 'model/model.ckpt')
print("Accuracy:",finalAcc)因此,一旦我保存了模型并尝试恢复它,我就不知道如何继续从"input_data“中获得NN的输出。
def execute_neural_network(x,y,aDataTrain,aTargetTrain,aDataTest,aTargetTest):
batch_size = 1
y_pred = []
prediction = neural_network_model(x,len(aDataTrain[0]))
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))
optimizer = tf.train.AdamOptimizer().minimize(cost)
input_data = [5.0, 3.0, 1.0, 5.0, 6.0, 5.0, 2.0, 4.0, 7.0, 6.0, 3.0, 3.0, 3.0, 3.0, 3.0, 4.0, 2.0, 3.0, 3.0, 3.0, 3.0, 3.0, 2.0, 3.0, 2.0, 3.0, 2.0, 3.0, 3.0, 4.0, 3.0, 3.0, 2.0, 4.0, 3.0, 3.0, 2.0, 4.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 61.0, 21.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 75.0, 3.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 6.0, 35.0, 11.0, 10.0, 33.0, 24.0, 6.0, 2.0, 2.0, 3.0, 4.0, 3.0, 3.0, 8.0, 6.0, 5.0, 6.0, 5.0, 8.0, 9.0, 13.0, 7.0, 25.0, 11.0, 2.0, 2.0, 2.0, 2.0, 2.0]
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, 'model/model.ckpt')
#Get neural network output from input_data发布于 2017-11-09 06:33:03
假设您以某种方式创建了图/网络的模型:
with tf.Session() as sess:
#do other stuff
predictionOp = tf.argmax(py_x, 1)
saver.save(sess, 'model') 其中predictionOp是变量,它是网络的输出。
您可以在之后添加类似这样的内容:tf.add_to_collection("predictionOp", predictionOp),以便为predictionOp指定一个更容易查找的名称。然后,您可以使用以下命令重新加载模型并获得预测结果:
with tf.Session() as sess:
new_saver = tf.train.import_meta_graph('model.meta')
new_saver.restore(sess, 'model')
predictionOp = tf.get_collection("predictionOp")[0]
#get the prediction
prediction = sess.run(predictionOp, feed_dict={"x:0": input_data})有关更多信息,请查看tensorflow documentation和here,了解有关基础知识的更多信息。此外,还有一些其他线程处理类似的问题,如this和this one。
https://stackoverflow.com/questions/47190556
复制相似问题