手写数字识别-小数据集

1.手写数字数据集

  • from sklearn.datasets import load_digits
  • digits = load_digits()

 手写数字识别-小数据集_第1张图片

 

 

 

2.图片数据预处理

  • x:归一化MinMaxScaler()
    手写数字识别-小数据集_第2张图片

     

     手写数字识别-小数据集_第3张图片

     

     

     

  • y:独热编码OneHotEncoder()或to_categorical

     

     手写数字识别-小数据集_第4张图片

     

     

     

  • 张量结构
    手写数字识别-小数据集_第5张图片

     

     

  • 训练集测试集划分

   

 

 

 

3.设计卷积神经网络结构

  • 绘制模型结构图,并说明设计依据。
    手写数字识别-小数据集_第6张图片

     

     

    # 导入相关包
    # 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)

     

     


    手写数字识别-小数据集_第7张图片

     

     

5.模型评价

  • model.evaluate()
    手写数字识别-小数据集_第8张图片
  • 交叉表与交叉矩阵
  • pandas.crosstab
    手写数字识别-小数据集_第9张图片

     

     

  • seaborn.heatmap
    手写数字识别-小数据集_第10张图片手写数字识别-小数据集_第11张图片

     

     

     
    补交以前的一份作业

    K均值算法--应用

    因为爬取天气数据用了很长时间。网址:https://www.cnblogs.com/ccla/p/12732967.html

     

你可能感兴趣的:(手写数字识别-小数据集)