1,基本概念
mnist是乙個非常有名的手寫體數字識別資料集,在很多資料中,這個資料集都會被用作深度學習的入門樣例。而tensorflow的封裝讓使用mnist資料集變得更加方便。mnist資料集是nist資料集的乙個子集,mnist 資料集可在 獲取, 它包含了四個部分:
(1)training set images: train-images-idx3-ubyte.gz (9.9 mb, 解壓後 47 mb, 包含 60,000 個樣本)
(2)training set labels: train-labels-idx1-ubyte.gz (29 kb, 解壓後 60 kb, 包含 60,000 個標籤)
(3)test set images: t10k-images-idx3-ubyte.gz (1.6 mb, 解壓後 7.8 mb, 包含 10,000 個樣本)
(4)test set labels: t10k-labels-idx1-ubyte.gz (5kb, 解壓後 10 kb, 包含 10,000 個標籤)
2,**解讀
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('data',one_hot = true)
print("type of mnist is %s" %(type(mnist)))
print("number of trian data is %d" %(mnist.train.num_examples))#訓練集樣本個數
print("number of test data is %d" %(mnist.test.num_examples))#測試集樣本個數
#mnist資料集具體資訊(樣本,標籤)
trainimg = mnist.train.images
trainlabel = mnist.train.labels
testimg = mnist.test.images
testlabel = mnist.test.labels
print("type of trainimg is %s" %(type(trainimg))) #訓練集樣本的型別
print("type of trainlabel is %s" %(type(trainlabel)))#訓練標籤的型別
print("type of testimg is %s" %(type(testimg)))
print("type of testlabel is %s" %(type(testlabel)))
print("shape of trainimg is %s" %(trainimg.shape,))#訓練樣本的個數,單個樣本畫素點的個數(28*28)
print("shape of trainlabel is %s" %(trainlabel.shape,))#訓練標籤的個數,單個樣本可能類別的個數(10)
print("shape of testimg is %s" %(testimg.shape,))
print("shape of testlabel is %s" %(testlabel.shape,))
執行結果:
#展示訓練集樣本的例項
nsample=3
randidx = np.random.randint(trainimg.shape[0],size=nsample)
for i in randidx:
cur_img = np.reshape(trainimg[i,:],(28,28))
cur_label = np.argmax(trainlabel[i,:])
plt.matshow(cur_img,cmap = plt.get_cmap('gray'))
plt.title(""+str(i)+"th training data"+"label is"+str(cur_label))
plt.show()
#batch learning
batch_size = 128 #batch大小
batch_xs, batch_ys = mnist.train.next_batch(batch_size) #訓練集batch中樣本的個數(batch_xs),相應標籤的個數(batch_ys)
print("type of batch_xs is %s" %(type(batch_xs)))
print("type of batch_ys is %s" %(type(batch_ys)))
print("shape of batch_xs is %s" %(batch_xs.shape,))
print("shape of batch_ys is %s" %(batch_ys.shape,))
執行結果:
MNIST資料集介紹
mnist資料集包含了6w張作為訓練資料,1w作為測試資料。在mnist資料集中,每一張都代表了0 9中的乙個數字,的大小都是28 28,且數字都會出現在的正中間。資料集包含了四個檔案 t10k images idx3 ubyte.gz 測試資料 t10k labels idx1 ubyte.gz ...
MNIST資料集介紹
大多數示例使用手寫數字的mnist資料集 1 該資料集包含60,000個用於訓練的示例和10,000個用於測試的示例。這些數字已經過尺寸標準化並位於影象中心,影象是固定大小 28x28畫素 其值為0到1。為簡單起見,每個影象都被平展並轉換為784 28 28 個特徵的一維numpy陣列。在我們的示例...
載入 MNIST 資料集
使用 tensorflow 來讀取資料及標籤 from tensorflow.examples.tutorials.mnist import input data import tensorflow as tf 載入資料集 mnist input data.read data sets e soft...