欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python神經(jīng)網(wǎng)絡(luò)tensorflow利用訓(xùn)練好的模型進(jìn)行預(yù)測

 更新時(shí)間:2022年05月06日 09:27:59   作者:Bubbliiiing  
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)tensorflow利用訓(xùn)練好的模型進(jìn)行預(yù)測,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

學(xué)習(xí)前言

在神經(jīng)網(wǎng)絡(luò)學(xué)習(xí)中slim常用函數(shù)與如何訓(xùn)練、保存模型文章里已經(jīng)講述了如何使用slim訓(xùn)練出來一個(gè)模型,這篇文章將會(huì)講述如何預(yù)測。

載入模型思路

載入模型的過程主要分為以下四步:

1、建立會(huì)話Session;

2、將img_input的placeholder傳入網(wǎng)絡(luò),建立網(wǎng)絡(luò)結(jié)構(gòu);

3、初始化所有變量;

4、利用saver對象restore載入所有參數(shù)。

這里要注意的重點(diǎn)是,在利用saver對象restore載入所有參數(shù)之前,必須要建立網(wǎng)絡(luò)結(jié)構(gòu),因?yàn)榫W(wǎng)絡(luò)結(jié)構(gòu)對應(yīng)著cpkt文件中的參數(shù)。

(網(wǎng)絡(luò)層具有對應(yīng)的名稱scope。)

實(shí)現(xiàn)代碼

在運(yùn)行實(shí)驗(yàn)代碼前,可以直接下載代碼,因?yàn)榇嬖谠S多依賴的文件

import tensorflow as tf
import numpy as np
from nets import Net
from tensorflow.examples.tutorials.mnist import input_data
def compute_accuracy(x_data,y_data):
    global prediction
    y_pre = sess.run(prediction,feed_dict={img_input:x_data})
    correct_prediction = tf.equal(tf.arg_max(y_data,1),tf.arg_max(y_pre,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    result = sess.run(accuracy,feed_dict = {img_input:x_data})
    return result
mnist = input_data.read_data_sets("MNIST_data",one_hot = "true")
slim = tf.contrib.slim
# img_input的placeholder
img_input = tf.placeholder(tf.float32, shape = (None, 784))
img_reshape = tf.reshape(img_input,shape = (-1,28,28,1))
# 載入模型
sess = tf.Session()
Conv_Net = Net.Conv_Net()
# 將img_input的placeholder傳入網(wǎng)絡(luò)
prediction = Conv_Net.net(img_reshape)
# 載入模型
ckpt_filename = './logs/model.ckpt-20000'
# 初始化所有變量
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
# 恢復(fù)
saver.restore(sess, ckpt_filename)
print(compute_accuracy(mnist.test.images,mnist.test.labels))

運(yùn)行結(jié)果為:

0.9921

以上就是python神經(jīng)網(wǎng)絡(luò)tensorflow利用訓(xùn)練好的模型進(jìn)行預(yù)測的詳細(xì)內(nèi)容,更多關(guān)于tensorflow模型預(yù)測的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論