網站首頁 編程語言 正文
學習前言
在神經網絡學習中slim常用函數與如何訓練、保存模型文章里已經講述了如何使用slim訓練出來一個模型,這篇文章將會講述如何預測。
載入模型思路
載入模型的過程主要分為以下四步:
1、建立會話Session;
2、將img_input的placeholder傳入網絡,建立網絡結構;
3、初始化所有變量;
4、利用saver對象restore載入所有參數。
這里要注意的重點是,在利用saver對象restore載入所有參數之前,必須要建立網絡結構,因為網絡結構對應著cpkt文件中的參數。
(網絡層具有對應的名稱scope。)
實現代碼
在運行實驗代碼前,可以直接下載代碼,因為存在許多依賴的文件
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傳入網絡
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))
運行結果為:
0.9921
原文鏈接:https://blog.csdn.net/weixin_44791964/article/details/102584474
相關推薦
- 2022-10-26 Go?REFLECT?Library反射類型詳解_Golang
- 2022-05-24 .NET?Core剪裁器背后的技術及工作原理介紹_實用技巧
- 2022-12-22 Qt實現繪制一個簡單多邊形的示例代碼_C 語言
- 2022-08-25 C語言超詳細講解線性表_C 語言
- 2022-10-05 C#?獲取文件夾里所有文件名的詳細代碼_C#教程
- 2022-03-31 C語言運算符的重載詳解_C 語言
- 2022-04-16 Python數據結構與算法之跳表詳解_python
- 2024-03-03 layuiadmin新建tabs標簽頁,點擊保存,打開新的標簽頁并刷新
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支