python神經(jīng)網(wǎng)絡tensorflow利用訓練好的模型進行預測
學習前言
在神經(jīng)網(wǎng)絡學習中slim常用函數(shù)與如何訓練、保存模型文章里已經(jīng)講述了如何使用slim訓練出來一個模型,這篇文章將會講述如何預測。
載入模型思路
載入模型的過程主要分為以下四步:
1、建立會話Session;
2、將img_input的placeholder傳入網(wǎng)絡,建立網(wǎng)絡結(jié)構(gòu);
3、初始化所有變量;
4、利用saver對象restore載入所有參數(shù)。
這里要注意的重點是,在利用saver對象restore載入所有參數(shù)之前,必須要建立網(wǎng)絡結(jié)構(gòu),因為網(wǎng)絡結(jié)構(gòu)對應著cpkt文件中的參數(shù)。
(網(wǎng)絡層具有對應的名稱scope。)
實現(xiàn)代碼
在運行實驗代碼前,可以直接下載代碼,因為存在許多依賴的文件
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)絡 prediction = Conv_Net.net(img_reshape) # 載入模型 ckpt_filename = './logs/model.ckpt-20000' # 初始化所有變量 sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() # 恢復 saver.restore(sess, ckpt_filename) print(compute_accuracy(mnist.test.images,mnist.test.labels))
運行結(jié)果為:
0.9921
以上就是python神經(jīng)網(wǎng)絡tensorflow利用訓練好的模型進行預測的詳細內(nèi)容,更多關(guān)于tensorflow模型預測的資料請關(guān)注腳本之家其它相關(guān)文章!
- PyCharm提示No Python Interpreter的正確解決辦法
- pycharm遠程連接服務器并配置python interpreter的方法
- python深度學習tensorflow訓練好的模型進行圖像分類
- Python實現(xiàn)自動駕駛訓練模型
- python神經(jīng)網(wǎng)絡AlexNet分類模型訓練貓狗數(shù)據(jù)集
- python神經(jīng)網(wǎng)絡slim常用函數(shù)訓練保存模型
- Python 實現(xiàn)LeNet網(wǎng)絡模型的訓練及預測
- Python人工智能深度學習模型訓練經(jīng)驗總結(jié)
- python interpret庫訓練模型助力機器學習
相關(guān)文章
Python 內(nèi)置變量和函數(shù)的查看及說明介紹
今天小編就為大家分享一篇Python 內(nèi)置變量和函數(shù)的查看及說明介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12pdf論文中python畫的圖Type 3 fonts字體不兼容的解決方案
這篇文章主要介紹了pdf論文中python畫的圖Type 3 fonts字體不兼容的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04關(guān)于tensorflow和keras版本的對應關(guān)系
這篇文章主要介紹了關(guān)于tensorflow和keras版本的對應關(guān)系,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06使用sublime text3搭建Python編輯環(huán)境的實現(xiàn)
這篇文章主要介紹了使用sublime text3搭建Python編輯環(huán)境的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01