1.初始准备
Keras 可以用各种不同的深度学习套件当底层, 我们在此指定用 Tensorflow 以确保执行的一致性。
%env KERAS_BACKEND=tensorflow
env: KERAS_BACKEND=tensorflow 再来是我们标准数据分析动作!
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
/home/nbuser/anaconda3_420/lib/python3.5/site-packages/matplotlib/font_manager.py:281: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment. 'Matplotlib is building the font cache using fc-list.
2.读入 MNIST 数据库
MNIST 是有一堆 0-9 的手写数字图库。有 6 万笔训练资料, 1 万笔测试资料。它是 "Modified" 版的 NIST 数据库, 原来的版本有更多资料。这个 Modified 的版本是由 LeCun, Cortes, 及 Burges 等人做的。可以参考这个数据库的原始网页。
MNIST 可以说是 Deep Learning 最有名的范例, 它被 Deep Learning 大师 Hinton 称为「机器学习的果蝇」。
3.由 Keras 读入 MNIST
Keras 很貼心的幫我們準備好 MNIST 數據庫, 我們可以這樣讀進來 (第一次要花點時間)。
from keras.datasets import mnist
/home/nbuser/anaconda3_420/lib/python3.5/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`. from ._conv import register_converters as _register_converters Using TensorFlow backend.
(x_train, y_train), (x_test, y_test) = mnist.load_data()
Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz 11264000/11490434 [============================>.] - ETA: 0s 我们来看看训练资料是不是 6 万笔、测试资料是不是有 1 笔。
len(x_train)
60000
len(x_test)
10000
特别要注意的是, 万一在读的过程中失败, 你需要找到下载的部份数据集删去, 然后在一个网路通𣈱的地方再下载一次。
3.数据库的内容
每笔输入 (x) 就是一个手写的 0-9 中一个数字的图档, 大小为 28x28。而输出 (y) 当然就是「正确答案」。我们来看看编号 9487 的训练资料。
x_train[9487].shape
(28, 28)
因为是图档, 当然可以显示出来!
plt.imshow(x_train[9487], cmap='Greys')
<matplotlib.image.AxesImage at 0x7f9ce0d7c278>
我们人眼辨识就知道这是 1, 我们看答案是不是和我们想的一样。
y_train[9487]
1