keras——经典模型之LeNet5 实现手写字识别
经典论文:Gradient-Based Learning Applied to Document Recognition
参考博文:https://blog.csdn.net/weixin_44344462/article/details/89212507
构建LeNet-5模型
#定义LeNet5网络 深度为1的灰度图像 def LeNet5(x_train, y_train, x_test, y_test): ########搭建网络########### #直接调用顺序模型 model = Sequential() #卷积和池化部分 model.add(Conv2D(input_shape=(28, 28, 1), filters=32, kernel_size=5, strides=1, activation='sigmoid', padding='same' )) model.add(MaxPool2D(pool_size=2, strides=2, padding='same')) model.add(Conv2D(filters=64, kernel_size=5, strides=2, activation='sigmoid', padding='same' )) model.add(MaxPool2D(pool_size=2, strides=2, padding='same')) model.add(Dropout(0.25)) #扁平化 model.add(Flatten()) #全连接层 model.add(Dense(128, activation='sigmoid')) model.add(Dropout(0.5)) model.add(Dense(128, activation='sigmoid')) model.add(Dropout(0.5)) model.add(Dense(10, activation='softmax')) ## 定义loss function #整合模型 model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] ) ##开始训练 print("\n开始训练:") model.fit(x_train, y_train, batch_size=100, epochs=20) #评价模型 print("\n评价模型:") final_loss, final_accuracy = model.evaluate(x_test, y_test) print("loss= ", final_loss) print("accuracy= ", final_accuracy) return model
结果