from keras.datasets import mnist
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras import backend
backend.set_image_data_format('channels_first')
# 设定随机种子
seed = 7
np.random.seed(seed)
# 从Keras导入Mnist数据集
(X_train, y_train), (X_validation, y_validation) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
X_validation = X_validation.reshape(X_validation.shape[0], 1, 28, 28).astype('float32')
# 格式化数据到0-1之前
X_train = X_train / 255
X_validation = X_validation / 255
# one-hot编码
y_train = np_utils.to_categorical(y_train)
y_validation = np_utils.to_categorical(y_validation)
# 创建模型
def create_model():
model = Sequential()
model.add(Conv2D(30, (5, 5), input_shape=(1, 28, 28), activation='relu'))#输入层(28*28),卷积层(5*5)
model.add(MaxPooling2D(pool_size=(2, 2)))#池化层(2*2)
model.add(Conv2D(15, (3, 3), activation='relu'))#卷积层(3*3)
model.add(MaxPooling2D(pool_size=(2, 2)))#池化层(2*2)
model.add(Dropout(0.2))#放弃层(20%)
model.add(Flatten())#扁平化层,多维的输入变成二维化
model.add(Dense(units=128, activation='relu'))#全连接层(128)
model.add(Dense(units=50, activation='relu'))#全连接层(50)
model.add(Dense(units=10, activation='softmax'))#输出层(10)
# 编译模型
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
model = create_model()
model.fit(X_train, y_train, epochs=10, batch_size=200, verbose=2)
score = model.evaluate(X_validation, y_validation, verbose=0)
print('CNN_Large: %.2f%%' % (score[1] * 100))
print('Success~')
gamblerofdestinyR14 发布了52 篇原创文章 · 获赞 1 · 访问量 918 私信 关注