TensorFlow MNIST手寫數(shù)據(jù)集的實(shí)現(xiàn)方法
MNIST數(shù)據(jù)集介紹
MNIST數(shù)據(jù)集中包含了各種各樣的手寫數(shù)字圖片,數(shù)據(jù)集的官網(wǎng)是:http://yann.lecun.com/exdb/mnist/index.html,我們可以從這里下載數(shù)據(jù)集。使用如下的代碼對(duì)數(shù)據(jù)集進(jìn)行加載:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
運(yùn)行上述代碼會(huì)自動(dòng)下載數(shù)據(jù)集并將文件解壓在MNIST_data文件夾下面。代碼中的one_hot=True,表示將樣本的標(biāo)簽轉(zhuǎn)化為one_hot編碼。
MNIST數(shù)據(jù)集中的圖片是28*28的,每張圖被轉(zhuǎn)化為一個(gè)行向量,長(zhǎng)度是28*28=784,每一個(gè)值代表一個(gè)像素點(diǎn)。數(shù)據(jù)集中共有60000張手寫數(shù)據(jù)圖片,其中55000張訓(xùn)練數(shù)據(jù),5000張測(cè)試數(shù)據(jù)。
在MNIST中,mnist.train.images是一個(gè)形狀為[55000, 784]的張量,其中的第一個(gè)維度是用來(lái)索引圖片,第二個(gè)維度圖片中的像素。MNIST數(shù)據(jù)集包含有三部分,訓(xùn)練數(shù)據(jù)集,驗(yàn)證數(shù)據(jù)集,測(cè)試數(shù)據(jù)集(mnist.validation)。
標(biāo)簽是介于0-9之間的數(shù)字,用于描述圖片中的數(shù)字,轉(zhuǎn)化為one-hot向量即表示的數(shù)字對(duì)應(yīng)的下標(biāo)為1,其余的值為0。標(biāo)簽的訓(xùn)練數(shù)據(jù)是[55000,10]的數(shù)字矩陣。
下面定義了一個(gè)簡(jiǎn)單的網(wǎng)絡(luò)對(duì)數(shù)據(jù)集進(jìn)行訓(xùn)練,代碼如下:
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
tf.reset_default_graph()
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
w = tf.Variable(tf.random_normal([784, 10]))
b = tf.Variable(tf.zeros([10]))
pred = tf.matmul(x, w) + b
pred = tf.nn.softmax(pred)
cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices=1))
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
training_epochs = 25
batch_size = 100
display_step = 1
save_path = 'model/'
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(training_epochs):
avg_cost = 0
total_batch = int(mnist.train.num_examples/batch_size)
for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
_, c = sess.run([optimizer, cost], feed_dict={x:batch_xs, y:batch_ys})
avg_cost += c / total_batch
if (epoch + 1) % display_step == 0:
print('epoch= ', epoch+1, ' cost= ', avg_cost)
print('finished')
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print('accuracy: ', accuracy.eval({x:mnist.test.images, y:mnist.test.labels}))
save = saver.save(sess, save_path=save_path+'mnist.cpkt')
print(" starting 2nd session ...... ")
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver.restore(sess, save_path=save_path+'mnist.cpkt')
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print('accuracy: ', accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))
output = tf.argmax(pred, 1)
batch_xs, batch_ys = mnist.test.next_batch(2)
outputval= sess.run([output], feed_dict={x:batch_xs, y:batch_ys})
print(outputval)
im = batch_xs[0]
im = im.reshape(-1, 28)
plt.imshow(im, cmap='gray')
plt.show()
im = batch_xs[1]
im = im.reshape(-1, 28)
plt.imshow(im, cmap='gray')
plt.show()
總結(jié)
以上所述是小編給大家介紹的TensorFlow MNIST手寫數(shù)據(jù)集的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助!
相關(guān)文章
基于Python開發(fā)一個(gè)選擇題訓(xùn)練工具
選擇題作為一種高效的方式被廣泛應(yīng)用于各類培訓(xùn)與考試中,為了幫助學(xué)生高效學(xué)習(xí)與自測(cè),本篇文章將采用Python編寫一款基于?Python?開發(fā)的選擇題訓(xùn)練工具,需要的可以參考下2024-12-12
安裝好Pycharm后如何配置Python解釋器簡(jiǎn)易教程
這篇文章主要介紹了安裝好Pycharm后如何配置Python解釋器簡(jiǎn)易教程,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
Python2實(shí)現(xiàn)的圖片文本識(shí)別功能詳解
這篇文章主要介紹了Python2實(shí)現(xiàn)的圖片文本識(shí)別功能,結(jié)合實(shí)例形式分析了Python pytesser庫(kù)的安裝及使用pytesser庫(kù)識(shí)別圖片文字相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
判斷Threading.start新線程是否執(zhí)行完畢的實(shí)例
這篇文章主要介紹了判斷Threading.start新線程是否執(zhí)行完畢的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
在Pycharm中自動(dòng)添加時(shí)間日期作者等信息的方法
今天小編就為大家分享一篇在Pycharm中自動(dòng)添加時(shí)間日期作者等信息的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
詳解Python中的from..import絕對(duì)導(dǎo)入語(yǔ)句
絕對(duì)導(dǎo)入其實(shí)非常簡(jiǎn)單,即是用from語(yǔ)句在import前指明頂層package名,下面我們通過(guò)兩個(gè)例子來(lái)詳解Python中的from..import絕對(duì)導(dǎo)入語(yǔ)句2016-06-06

