日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python保存模型_MNIST数据集训练完如何保存成模型文件?

發布時間:2023/12/10 python 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python保存模型_MNIST数据集训练完如何保存成模型文件? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data

# In[12]:

#載入數據集

mnist = input_data.read_data_sets("E://YangBen//MNIST_data",one_hot=True)

#每個批次的大小

batch_size = 100

#計算一共有多少個批次

n_batch = mnist.train.num_examples // batch_size

#定義兩個placeholder

x = tf.placeholder(tf.float32,[None,784])

y = tf.placeholder(tf.float32,[None,10])

#創建一個簡單的神經網絡

W = tf.Variable(tf.zeros([784,10]))

b = tf.Variable(tf.zeros([10]))

prediction = tf.nn.softmax(tf.matmul(x,W)+b)

# ///隱藏層//

w1 = tf.Variable(tf.truncated_normal([784, 500], stddev=0.1))

b1 = tf.Variable(tf.zeros([500]))

L1 = tf.nn.relu(tf.matmul(x, w1) + b1)

w2 = tf.Variable(tf.truncated_normal([500, 300], stddev=0.1))

b2 = tf.Variable(tf.zeros([300]))

L2 = tf.nn.relu(tf.matmul(L1, w2) + b2)

# ///隱藏層//

# 輸出層

w3 = tf.Variable(tf.truncated_normal([300, 10], stddev=0.1))

b3 = tf.Variable(tf.zeros([10]))

prediction = tf.nn.softmax(tf.matmul(L2, w3)+b3)

#二次代價函數

relu = tf.reduce_mean(tf.square(y-prediction))

#使用梯度下降法

train_step = tf.train.GradientDescentOptimizer(0.2).minimize(relu)

#初始化變量

init = tf.global_variables_initializer()

#結果存放在一個布爾型列表中

correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) #argmax返回一維張量中最大的值所在的位置

#求準確率

accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

with tf.Session() as sess:

sess.run(init)

for epoch in range(5):

for batch in range(n_batch):

batch_xs,batch_ys = mnist.train.next_batch(batch_size)

sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})

acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})

print("Iter " + str(epoch)+",Testing Accuracy"+str(acc))

這是我的代碼,中間加了Saver之后還是報錯

總結

以上是生活随笔為你收集整理的python保存模型_MNIST数据集训练完如何保存成模型文件?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。