Press "Enter" to skip to content

TensorFlow 笔记(2): MNIST dataset

TensorFlow 笔记(2): MNIST dataset

 

为什幺要了解MNIST dataset

 

关于Deep Learning的教学文章,很多都是从MNIST dataset做为第一个範例,原因是它资料格式与目的很简单,却也有一定的难度。再加上学习Deep Learning之前一定得先了解手上的资料,可以用来熟练Deep Learning常用的基本操作。

 

MNIST dataset

 

MNIST是手写数字的dataset, 里面的每张图片都是一个手写数字图片, 并且标示对应的数字。图片的大小是28×28, 颜色格式是灰阶

 

首先,先import会用到的function

 

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

 

接着下载MNIST data

 

(xtrain, ytrain), (xtest, ytest) = tf.keras.datasets.mnist.load_data()

 

这边可以看到,下载的资料分成training data以及test data。

 

training data又分成xtrain, ytrain。xtrain是影像资料,ytrain是该图片标示出对应的数字。xtest及ytest也是同样地代表图片以及对应的数字。

 

print('xtrain.shape={}'.format(xtrain.shape))
print('ytrain.shape={}'.format(ytrain.shape))
print('xtest.shape={}'.format(xtest.shape))
print('ytest.shape={}'.format(ytest.shape))
'''
xtrain.shape=(60000, 28, 28)
ytrain.shape=(60000,)
xtest.shape=(10000, 28, 28)
ytest.shape=(10000,)
'''

 

可以看到training data有60000笔,图片的大小是28×28,标示的数字有60000笔。test data有10000笔, 图片大小一样是28×28,标示的数字有10000笔。

 

接着我们来看其中一笔资料的样子,底下的function可用来绘出一张图片

 

def plot_img(ndarr):
 img = ndarr.copy()
 img.shape = (28,28)
 plt.imshow(img, cmap='gray')
 plt.show()

 

我们来看看index 5的图片

 

plot_img(xtrain[5,:])

 

同时我们将他的 label 印出来, 可以看到就是2

 

print('label of img 5= {}'.format(ytrain[5]))
# label of img 5= 2

 

Preprocessing

 

通常原始的dataset都需要一些preprocessing, reshape是常见的一个操作。

 

底下的操作可以将每一张图片从2D转成1D, 在reshape的参数里, 第一个维度大小就是资料数量, 第二个参数是1D的大小, 这里填-1代表让numpy自行计算剩下的大小

 

xtrain = xtrain.reshape(xtrain.shape[0], -1)
xtest = xtest.reshape(xtest.shape[0], -1)
print('xtrain.shape={}'.format(xtrain.shape))
print('xtest.shape={}'.format(xtest.shape))
'''
xtrain.shape=(60000, 784)
xtest.shape=(10000, 784)
'''

 

Normalize

 

一般来说,原始资料经过normalize会让训练过程更有效率。我们的图片格式是INT8的灰阶格式, 所以值的範围是0~255, 所以我们将它除以255做normalize

 

xtrain = xtrain.astype('float32')
xtest = xtest.astype('float32')
xtrain /= 255 # normalize
xtest /= 255 # normalize

 

One hot encoding

 

由于我们的图片被标示的是个数字,在训练模型时,难以计算预测的準确度如何,所以一般会将资料转成one hot encoding。

 

举例来说,原本标示的数字是 2
, 转成one hot encoding之后会变成 [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
, 可以看到只有 index 2的地方被设成1, 其他都是0

 

这样的好处是, 我们在预测时, 可以给出预测为每个数字的机率, 像是 [0, 0, 0.8, 0, 0, 0.2, 0, 0, 0, 0]
,代表预测2的机率是0.8, 预测5的机率是0.2, 然后我们可以一一比对猜测的结果如何

 

用底下的方式可以转成one hot encoding

 

ytrain = np.eye(10)[ytrain] # one hot encoding for 10 classes
ytest = np.eye(10)[ytest] # one hot encoding for 10 classes

 

Batch

 

在训练模型的时候,通常我们会把training data 分成一小块一小块做训练,而不是一次把所有training data都丢进去训练。底下的function可以将资料乱数排列之后取其中的batch size出来做训练

 

def next_batch(batch_size, data, labels):
 idx = np.arange(0 , len(data))
 np.random.shuffle(idx)
 idx = idx[:batch_size]
 data_shuffle = [data[i] for i in idx]
 labels_shuffle = [labels[i] for i in idx]
 return np.asarray(data_shuffle), np.asarray(labels_shuffle)xbatch, ybatch = next_batch(128, xtrain, ytrain) # batch size=128

 

结语

 

网路上找到使用 MNIST 的 tensorflow 文章, 大多都是早期的 tensorflow 版本, 下载资料库的方式不太一样, 并且不被较新的版本支援, 换成新的方式之后, 网路上的範例程式码都要再做修改, 像是 one hot encoding的方式, 或是 batch的方式, 都是得重新写过。

 

了解MNIST之后, 接着可以开始练习比较简单的 logistic regression model

Be First to Comment

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注