手写数字识别-小数据集
1.手写数字数据集
- from sklearn.datasets import load_digits
- digits = load_digits()
2.图片数据预处理
- x:归一化MinMaxScaler()
- y:独热编码OneHotEncoder()或to_categorical
- 张量结构
- 训练集测试集划分
3.设计卷积神经网络结构
- 绘制模型结构图,并说明设计依据。
# 导入相关包 # sequential设计层数 from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense,Dropout,Flatten,Conv2D,MaxPool2D #建立模型 model=Sequential() ks=(3,3) ips=X_train.shape[1:] #一层卷积 model.add(Conv2D(filters=16, kernel_size=ks, padding='same', input_shape=ips,activation='relu')) #池化层1 model.add(MaxPool2D(pool_size=(2,2))) model.add(Dropout(0.25)) #二层卷积 model.add(Conv2D(filters=32,kernel_size=ks,padding='same',activation='relu')) #池化层2 model.add(MaxPool2D(pool_size=(2,2))) model.add(Dropout(0.25)) #三层卷积 model.add(Conv2D(filters=64,kernel_size=ks,padding='same',activation='relu')) #四层卷积 model.add(Conv2D(filters=128,kernel_size=ks,padding='same',activation='relu')) #池化层3 model.add(MaxPool2D(pool_size=(2,2))) model.add(Dropout(0.25)) model.add(Flatten()) #平坦层 model.add(Dense(128,activation='relu')) #dense全连接层 model.add(Dropout(0.25)) model.add(Dense(10,activation='softmax')) #激活函数 softmax分类 model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 8, 8, 16) 160
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 4, 4, 16) 0
_________________________________________________________________
dropout (Dropout) (None, 4, 4, 16) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 4, 4, 32) 4640
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 2, 2, 32) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 2, 2, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 2, 2, 64) 18496
_________________________________________________________________
conv2d_3 (Conv2D) (None, 2, 2, 128) 73856
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 1, 1, 128) 0
_________________________________________________________________
dropout_2 (Dropout) (None, 1, 1, 128) 0
_________________________________________________________________
flatten (Flatten) (None, 128) 0
_________________________________________________________________
dense (Dense) (None, 128) 16512
_________________________________________________________________
dropout_3 (Dropout) (None, 128) 0
_________________________________________________________________
dense_1 (Dense) (None, 10) 1290
=================================================================
Total params: 114,954
Trainable params: 114,954
Non-trainable params: 0
_________________________________________________________________
4.模型训练
- model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
- train_history = model.fit(x=X_train,y=y_train,validation_split=0.2, batch_size=300,epochs=10,verbose=2)
5.模型评价
- model.evaluate()
- 交叉表与交叉矩阵
- pandas.crosstab
- seaborn.heatmap
补交以前的一份作业K均值算法--应用
因为爬取天气数据用了很长时间。网址:https://www.cnblogs.com/ccla/p/12732967.html