TensorFlow MNIST手寫(xiě)數(shù)據(jù)集的實(shí)現(xiàn)方法
MNIST數(shù)據(jù)集介紹
MNIST數(shù)據(jù)集中包含了各種各樣的手寫(xiě)數(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張手寫(xiě)數(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手寫(xiě)數(shù)據(jù)集的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助!
相關(guān)文章
Python使用pickle模塊儲(chǔ)存對(duì)象操作示例
這篇文章主要介紹了Python使用pickle模塊儲(chǔ)存對(duì)象操作,結(jié)合實(shí)例形式分析了Python使用pickle模塊針對(duì)文件讀寫(xiě)與轉(zhuǎn)換的相關(guān)操作技巧,需要的朋友可以參考下2018-08-08圖文詳解感知機(jī)算法原理及Python實(shí)現(xiàn)
感知機(jī)是二類(lèi)分類(lèi)的線(xiàn)性分類(lèi)模型,其輸入為實(shí)例的特征向量,輸出為實(shí)例的類(lèi)別(取+1和-1二值)。本文將為大家詳細(xì)講講感知機(jī)算法的原理及實(shí)現(xiàn),需要的可以參考一下2022-08-08使用python爬蟲(chóng)獲取黃金價(jià)格的核心代碼
這篇文章主要介紹了利用python爬蟲(chóng)獲取黃金價(jià)格,需要的朋友可以參考下2018-06-06Python ORM框架SQLAlchemy學(xué)習(xí)筆記之安裝和簡(jiǎn)單查詢(xún)實(shí)例
這篇文章主要介紹了Python ORM框架SQLAlchemy學(xué)習(xí)筆記之安裝和簡(jiǎn)單查詢(xún)實(shí)例,簡(jiǎn)明入門(mén)教程,需要的朋友可以參考下2014-06-06Python內(nèi)置函數(shù)zip map filter的使用詳解
這篇文章主要介紹了Python內(nèi)置函數(shù)zip map filter的使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04python爬取NUS-WIDE數(shù)據(jù)庫(kù)圖片
本文給大家分享的是使用Python制作爬蟲(chóng)爬取圖片的小程序,非常的簡(jiǎn)單,但是很實(shí)用,有需要的小伙伴可以參考下2016-10-10python讀取pdf格式文檔的實(shí)現(xiàn)代碼
這篇文章主要給大家介紹了關(guān)于python讀取pdf格式文檔的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04