本次是机器学习作业课的作业。
实验基于keras,构建了一个两个卷积层,两个全连接层的网络,第二个卷积层后加入了dropout率为0.25的dropout处理,在第一个全连接层后加入drop率为0.5的dropout处理。
十二个epoch后停止训练,能够在测试集上获得99.08%的accuracy。
> 这是训练过程
> Epoch 1/12
> 60000/60000 [==============================] - 117s 2ms/step - loss: 0.2489 - acc: 0.9225
> Epoch 2/12
> 60000/60000 [==============================] - 52s 872us/step - loss: 0.0894 - acc: 0.9736
> Epoch 3/12
> 60000/60000 [==============================] - 51s 853us/step - loss: 0.0624 - acc: 0.9812
> Epoch 4/12
> 60000/60000 [==============================] - 51s 849us/step - loss: 0.0506 - acc: 0.9848
> Epoch 5/12
> 60000/60000 [==============================] - 51s 844us/step - loss: 0.0422 - acc: 0.9871
> Epoch 6/12
> 60000/60000 [==============================] - 50s 840us/step - loss: 0.0351 - acc: 0.9885
> Epoch 7/12
> 60000/60000 [==============================] - 50s 840us/step - loss: 0.0305 - acc: 0.9908
> Epoch 8/12
> 60000/60000 [==============================] - 50s 840us/step - loss: 0.0280 - acc: 0.9910
> Epoch 9/12
> 60000/60000 [==============================] - 50s 840us/step - loss: 0.0238 - acc: 0.9924
> Epoch 10/12
> 60000/60000 [==============================] - 50s 840us/step - loss: 0.0222 - acc: 0.9930
> Epoch 11/12
> 60000/60000 [==============================] - 50s 841us/step - loss: 0.0202 - acc: 0.9934
> Epoch 12/12
> 60000/60000 [==============================] - 50s 840us/step - loss: 0.0188 - acc: 0.9942
> the loss 0.0300778467153
> the accuracy 0.9908
源码:
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Dropout,Flatten,Dense
from keras import backend as K
batch_size = 128
epoch = 12
img_rows, img_cols = 28, 28
(x_trains, y_trains), (x_tests, y_tests) = mnist.load_data()
# print(x_trains.shape) # (60000,28,28)
# print(y_tests.shape) #(10000,)
x_trains = x_trains.reshape(x_trains.shape[0], img_rows, img_cols, 1)
x_tests = x_tests.reshape(x_tests.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
# print(x_trains.dtype)
x_trains = x_trains.astype('float32')
x_tests = x_tests.astype('float32')
x_trains /= 255
x_tests /= 255
print(x_trains.shape[0], 'trainsamples')
print(x_tests.shape[0], 'testsamples')
y_trains = keras.utils.to_categorical(y_trains, num_classes=10)
y_tests = keras.utils.to_categorical(y_tests, num_classes=10)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
print("firstlayer:",model.output_shape)
model.add(Conv2D(64,(3,3),activation='relu'))
print("secondelayer:",model.output_shape)
model.add(Dropout(0.25))
model.add(Flatten())
print("thirdlayer:",model.output_shape)
model.add(Dense(128,activation='relu'))
print("fourthlayer:",model.output_shape)
model.add(Dropout(0.5))
model.add(Dense(10,activation='softmax'))
model.compile(optimizer=keras.optimizers.Adadelta(),loss=keras.losses.categorical_crossentropy,metrics=['accuracy'])
model.fit(x_trains,y_trains,batch_size=batch_size,epochs=epoch,verbose=1)
score = model.evaluate(x_tests,y_tests,verbose=0)
print("the loss",score[0])
print("the accuracy",score[1])