日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

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

作者:Bubbliiiing ? 更新時(shí)間: 2022-06-30 編程語言

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

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

載入模型思路

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

1、建立會話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

原文鏈接:https://blog.csdn.net/weixin_44791964/article/details/102584474

欄目分類
最近更新