欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

TensorFlow MNIST手寫數(shù)據(jù)集的實現(xiàn)方法

 更新時間:2020年02月05日 10:46:56   作者:Baby-Lily  
MNIST數(shù)據(jù)集中包含了各種各樣的手寫數(shù)字圖片,這篇文章主要介紹了TensorFlow MNIST手寫數(shù)據(jù)集的實現(xiàn)方法,需要的朋友可以參考下

MNIST數(shù)據(jù)集介紹

MNIST數(shù)據(jù)集中包含了各種各樣的手寫數(shù)字圖片,數(shù)據(jù)集的官網(wǎng)是:http://yann.lecun.com/exdb/mnist/index.html,我們可以從這里下載數(shù)據(jù)集。使用如下的代碼對數(shù)據(jù)集進行加載:

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

運行上述代碼會自動下載數(shù)據(jù)集并將文件解壓在MNIST_data文件夾下面。代碼中的one_hot=True,表示將樣本的標簽轉化為one_hot編碼。

MNIST數(shù)據(jù)集中的圖片是28*28的,每張圖被轉化為一個行向量,長度是28*28=784,每一個值代表一個像素點。數(shù)據(jù)集中共有60000張手寫數(shù)據(jù)圖片,其中55000張訓練數(shù)據(jù),5000張測試數(shù)據(jù)。

在MNIST中,mnist.train.images是一個形狀為[55000, 784]的張量,其中的第一個維度是用來索引圖片,第二個維度圖片中的像素。MNIST數(shù)據(jù)集包含有三部分,訓練數(shù)據(jù)集,驗證數(shù)據(jù)集,測試數(shù)據(jù)集(mnist.validation)。

標簽是介于0-9之間的數(shù)字,用于描述圖片中的數(shù)字,轉化為one-hot向量即表示的數(shù)字對應的下標為1,其余的值為0。標簽的訓練數(shù)據(jù)是[55000,10]的數(shù)字矩陣。

下面定義了一個簡單的網(wǎng)絡對數(shù)據(jù)集進行訓練,代碼如下:

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()

總結

以上所述是小編給大家介紹的TensorFlow MNIST手寫數(shù)據(jù)集的實現(xiàn)方法,希望對大家有所幫助!

相關文章

  • Python使用pickle模塊儲存對象操作示例

    Python使用pickle模塊儲存對象操作示例

    這篇文章主要介紹了Python使用pickle模塊儲存對象操作,結合實例形式分析了Python使用pickle模塊針對文件讀寫與轉換的相關操作技巧,需要的朋友可以參考下
    2018-08-08
  • Python Django框架防御CSRF攻擊的方法分析

    Python Django框架防御CSRF攻擊的方法分析

    這篇文章主要介紹了Python Django框架防御CSRF攻擊的方法,結合實例形式分析了Python Django框架防御CSRF攻擊的原理、配置方法與使用技巧,需要的朋友可以參考下
    2019-10-10
  • 圖文詳解感知機算法原理及Python實現(xiàn)

    圖文詳解感知機算法原理及Python實現(xiàn)

    感知機是二類分類的線性分類模型,其輸入為實例的特征向量,輸出為實例的類別(取+1和-1二值)。本文將為大家詳細講講感知機算法的原理及實現(xiàn),需要的可以參考一下
    2022-08-08
  • 使用python爬蟲獲取黃金價格的核心代碼

    使用python爬蟲獲取黃金價格的核心代碼

    這篇文章主要介紹了利用python爬蟲獲取黃金價格,需要的朋友可以參考下
    2018-06-06
  • Python ORM框架SQLAlchemy學習筆記之安裝和簡單查詢實例

    Python ORM框架SQLAlchemy學習筆記之安裝和簡單查詢實例

    這篇文章主要介紹了Python ORM框架SQLAlchemy學習筆記之安裝和簡單查詢實例,簡明入門教程,需要的朋友可以參考下
    2014-06-06
  • Python內置函數(shù)zip map filter的使用詳解

    Python內置函數(shù)zip map filter的使用詳解

    這篇文章主要介紹了Python內置函數(shù)zip map filter的使用,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • Django框架驗證碼用法實例分析

    Django框架驗證碼用法實例分析

    這篇文章主要介紹了Django框架驗證碼用法,結合實例形式分析了Python Django框架驗證碼的功能、實現(xiàn)方法及相關操作技巧,需要的朋友可以參考下
    2019-05-05
  • Python做文本按行去重的實現(xiàn)方法

    Python做文本按行去重的實現(xiàn)方法

    每行在promotion后面包含一些數(shù)字,如果這些數(shù)字是相同的,則認為是相同的行,對于相同的行,只保留一行。接下來通過本文給大家介紹Python做文本按行去重的實現(xiàn)方法,感興趣的朋友一起看看吧
    2016-10-10
  • python爬取NUS-WIDE數(shù)據(jù)庫圖片

    python爬取NUS-WIDE數(shù)據(jù)庫圖片

    本文給大家分享的是使用Python制作爬蟲爬取圖片的小程序,非常的簡單,但是很實用,有需要的小伙伴可以參考下
    2016-10-10
  • python讀取pdf格式文檔的實現(xiàn)代碼

    python讀取pdf格式文檔的實現(xiàn)代碼

    這篇文章主要給大家介紹了關于python讀取pdf格式文檔的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04

最新評論