码迷,mamicode.com
首页 > 其他好文 > 详细

手写数字识别-小数据集

时间:2020-06-14 16:33:10      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:sea   数据集   kernel   size   爬取   处理   style   rop   ==   

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.模型评价

手写数字识别-小数据集

标签:sea   数据集   kernel   size   爬取   处理   style   rop   ==   

原文地址:https://www.cnblogs.com/ccla/p/13125277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!