Tensorflow reuse neural network

时间:2017-11-08 22:09:36

标签: python tensorflow

I'm new in tensorflow and I've been training a simple neural network, but once is trained, I don't know how to reuse the NN to get the outputs of an input.

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)

So, once I've saved the model and try to restore it, I don't know how to continue to get the output of the NN from the "input_data".

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

1 个答案:

答案 0 :(得分:0)

假设您以某种方式创建图表/网络模型:

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 documentationhere,了解有关基础知识的更多信息。此外,还有一些其他线程可以处理类似的问题,例如thisthis one