TensorFlowMNIST如何實(shí)現(xiàn)手寫數(shù)據(jù)集-創(chuàng)新互聯(lián)

小編給大家分享一下TensorFlow MNIST如何實(shí)現(xiàn)手寫數(shù)據(jù)集,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括屏山網(wǎng)站建設(shè)、屏山網(wǎng)站制作、屏山網(wǎng)頁制作以及屏山網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,屏山網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到屏山省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

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è)維度是用來索引圖片,第二個(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()

看完了這篇文章,相信你對(duì)“TensorFlow MNIST如何實(shí)現(xiàn)手寫數(shù)據(jù)集”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

名稱欄目:TensorFlowMNIST如何實(shí)現(xiàn)手寫數(shù)據(jù)集-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://www.bm7419.com/article42/cesghc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、虛擬主機(jī)網(wǎng)站導(dǎo)航、網(wǎng)站排名、靜態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)