溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用tensorflow怎么加載mnist數據集

發(fā)布時間:2021-06-01 17:43:01 來源:億速云 閱讀:407 作者:Leah 欄目:開發(fā)技術

本篇文章給大家分享的是有關使用tensorflow怎么加載mnist數據集,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

mnist作為最基礎的圖片數據集,在以后的cnn,rnn任務中都會用到

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data

#數據集存放地址,采用0-1編碼
mnist = input_data.read_data_sets('F:/mnist/data/',one_hot = True)
print(mnist.train.num_examples)
print(mnist.test.num_examples)

trainimg = mnist.train.images
trainlabel = mnist.train.labels
testimg = mnist.test.images
testlabel = mnist.test.labels

#打印相關信息
print(type(trainimg))
print(trainimg.shape,)
print(trainlabel.shape,)
print(testimg.shape,)
print(testlabel.shape,)

nsample = 5
randidx = np.random.randint(trainimg.shape[0],size = nsample)

#輸出幾張數字的圖
for i in randidx:
  curr_img = np.reshape(trainimg[i,:],(28,28))
  curr_label = np.argmax(trainlabel[i,:])
  plt.matshow(curr_img,cmap=plt.get_cmap('gray'))
  plt.title(""+str(i)+"th Training Data"+"label is"+str(curr_label))
  print(""+str(i)+"th Training Data"+"label is"+str(curr_label))
  plt.show()

程序運行結果如下:

Extracting F:/mnist/data/train-images-idx3-ubyte.gz
Extracting F:/mnist/data/train-labels-idx1-ubyte.gz
Extracting F:/mnist/data/t10k-images-idx3-ubyte.gz
Extracting F:/mnist/data/t10k-labels-idx1-ubyte.gz
55000
10000
<class 'numpy.ndarray'>
(55000, 784)
(55000, 10)
(10000, 784)
(10000, 10)
52636th

輸出的圖片如下:

Training Datalabel is9

使用tensorflow怎么加載mnist數據集

以上就是使用tensorflow怎么加載mnist數據集,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI