手写数据集是深度学习中,最基础应用最广泛的数据集。
手写数据集内置在keras中
一)整体代码
import keras
from keras import layers
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import keras.datasets.mnist as mnist
(train_image, train_label), (test_image, test_label) = mnist.load_data()
train_image.shape, test_image.shape
a = np.array([1, 2, 3])
a.ndim
np.expand_dims(a, axis=-1).ndim
train_image = np.expand_dims(train_image, axis=-1)
train_image.shape
test_image = np.expand_dims(test_image, axis=-1)
test_image.shape
model = keras.Sequential()
model.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D())
model.summary()
model.add(layers.Flatten())
model.summary()
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(10, activation='softmax'))
model.summary()
train_label
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['acc']
)
model.fit(train_image, train_label, epochs=10, batch_size=512)
import numpy as np
np.argmax(model.predict(test_image[:10]),axis=1)
test_label[:10]
二)代码分析:
train_image.shape, test_image.shape
发现训练图像集有60000个28X28像素的图像。
但是60000,28,28分别对应图像的batch、height、width,只有两个维度,所以下面的操作就是需要去扩张图像的channel维度。(此操作并没有扩张数据)
注1:图像数据的shape
hight width channel
彩色图像: (h, w, 1)
conv2d: 图片输入的形状: batch(多少张图片), height(图片高度), width(图片宽度), channels
dense: 输入数据的形状: batch, data
a = np.array([1, 2, 3])
a.ndim
np.expand_dims(a, axis=-1).ndim
上面操作就是将 定义的一个一维数据 [1,2,3] 变为 二维数据 [ [1], [2], [3] ]
其中 np.expand_dims(a, axis=-1) 的参数axis= -1表示在最后一个维度上进行扩张
某某.ndim是去查看他的维度信息
train_image = np.expand_dims(train_image, axis=-1)
train_image.shape
test_image = np.expand_dims(test_image, axis=-1)
test_image.shape
上面的操作就是去预处睿图片的维度,下面就是预处理训练集和测试集图片后的结果