首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow重用神经网络

Tensorflow重用神经网络
EN

Stack Overflow用户
提问于 2017-11-09 06:09:36
回答 1查看 511关注 0票数 2

我是tensorflow的新手,我一直在训练一个简单的神经网络,但是一旦训练完成,我就不知道如何重用NN来获得输入的输出。

代码语言:javascript
复制
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的输出。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2017-11-09 06:33:03

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

代码语言:javascript
复制
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指定一个更容易查找的名称。然后,您可以使用以下命令重新加载模型并获得预测结果:

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47190556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档