from keras import layers
from keras import models
model = models.Sequential()
# input_shape指定输入的tensor为28*28*1,这里分别对应的是图片的三个维度的属性,即image_height,image_weight,image_channel
# 指定filter的尺寸为3*3,filter的深度(或者个数)为32
# 该层的输出为(28-3+1)*(28-3+1)*32=26*26*32,这也是下一层输入的tensor维度
# 该层的参数数量为3*3*32+32=320
model.add(layers.Conv2D(32,(3,3),activation='relu',input_shape=(28,28,1)))
# 指定MaxPooling的filter为2*2,该层的输出为13*13*32
model.add(layers.MaxPooling2D((2,2)))
# 该层的输出为(13-3+1)*(13-3+1)*64=11*11*64
# 该层的参数数量为3*3*32*64+64=18496
model.add(layers.Conv2D(64,(3,3),activation='relu'))
# filter的尺寸为2*2,该层的输出为5*5*64
model.add(layers.MaxPooling2D((2,2)))
# 该层的输出为(5-3+1)*(5-3+1)*64=3*3*64
# 该层的参数数量为3*3*64*64+64 = 36928
model.add(layers.Conv2D(64,(3,3),activation='relu'))
# 拉平,该层的神经元数目为3*3*64=576
model.add(layers.Flatten())
# 全连接层,该层参数的数量为576*64+64=36928
model.add(layers.Dense(64,activation='relu'))
# softmax层,该层参数的数量为64*10+10=650
model.add(layers.Dense(10,activation='softmax'))
# 输出当前模型的相关信息
model.summary()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_4 (Conv2D) (None, 26, 26, 32) 320
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 13, 13, 32) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 11, 11, 64) 18496
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 5, 5, 64) 0
_________________________________________________________________
conv2d_6 (Conv2D) (None, 3, 3, 64) 36928
_________________________________________________________________
flatten_2 (Flatten) (None, 576) 0
_________________________________________________________________
dense_3 (Dense) (None, 64) 36928
_________________________________________________________________
dense_4 (Dense) (None, 10) 650
=================================================================
Total params: 93,322
Trainable params: 93,322
Non-trainable params: 0
_________________________________________________________________
from keras.datasets import mnist
from keras.utils import to_categorical
(train_images,train_labels),(test_images,test_labels) = mnist.load_data()
train_images = train_images.reshape((60000,28,28,1))
train_images = train_images.astype('float32')/255
test_images = test_images.reshape((10000,28,28,1))
test_images = test_images.astype('float32')/255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(train_images,train_labels,epochs=5,batch_size=64)
Epoch 1/5
60000/60000 [==============================] - 48s 798us/step - loss: 0.1648 - acc: 0.9492
Epoch 2/5
60000/60000 [==============================] - 49s 813us/step - loss: 0.0477 - acc: 0.9851
Epoch 3/5
60000/60000 [==============================] - 47s 791us/step - loss: 0.0325 - acc: 0.9897
Epoch 4/5
60000/60000 [==============================] - 46s 775us/step - loss: 0.0249 - acc: 0.9924
Epoch 5/5
60000/60000 [==============================] - 47s 784us/step - loss: 0.0200 - acc: 0.9936
test_loss,test_acc = model.evaluate(test_images,test_labels)
test_acc
10000/10000 [==============================] - 3s 265us/step
0.9916