当前位置:网站首页>10. CNN applied to handwritten digit recognition
10. CNN applied to handwritten digit recognition
2022-07-08 01:02:00 【booze-J】
The code running platform is jupyter-notebook, Code blocks in the article , According to jupyter-notebook Written in the order of division in , Run article code , Glue directly into jupyter-notebook that will do . The comments given by the overall code are quite simple .
1. Import third-party library
import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense,Dropout,Convolution2D,MaxPooling2D,Flatten
from tensorflow.keras.optimizers import Adam
2. Loading data and data preprocessing
# Load data
(x_train,y_train),(x_test,y_test) = mnist.load_data()
# (60000, 28, 28)
print("x_shape:\n",x_train.shape)
# (60000,) Not yet one-hot code You need to operate by yourself later
print("y_shape:\n",y_train.shape)
# (60000, 28, 28) -> (60000,28,28,1)=( Number of pictures , Picture height , Image width , The number of channels in the picture ) reshape() Middle parameter filling -1 Parameter results can be calculated automatically Divide 255.0 To normalize
# Normalization is critical , It can greatly reduce the amount of calculation
x_train = x_train.reshape(-1,28,28,1)/255.0
x_test = x_test.reshape(-1,28,28,1)/255.0
# in one hot Format
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes=10)
3. Training models
# Define sequential model
model = Sequential()
# The first convolution layer Note that the first layer should write the size of the input image Later layers can be ignored
# input_shape Input plane
# filters Convolution kernel / Number of filters
# kernel_size Convolution window size
# strides step
# padding padding The way same/valid
# activation Activation function
model.add(Convolution2D(
input_shape=(28,28,1),
filters=32,
kernel_size=5,
strides=1,
padding="same",
activation="relu"
))
# The first pool
model.add(MaxPooling2D(
pool_size=2,
strides=2,
padding="same"
))
# The second pooling layer
model.add(Convolution2D(filters=64,kernel_size=5,strides=1,padding="same",activation="relu"))
# The second pooling layer
model.add(MaxPooling2D(pool_size=2,strides=2,padding="same"))
# Flatten the output of the second pool layer into 1 dimension
model.add(Flatten())
# The first full connection layer
model.add(Dense(units=1024,activation="relu"))
# Dropout Random selection 50% Neurons are trained
model.add(Dropout(0.5))
# The second full connection layer
model.add(Dense(units=10,activation="softmax"))
# Define optimizer Set the learning rate to 1e-4
adam = Adam(lr=1e-4)
# Define optimizer ,loss function, The accuracy of calculation during training
model.compile(optimizer=adam,loss="categorical_crossentropy",metrics=["accuracy"])
# Training models
model.fit(x_train,y_train,batch_size=64,epochs=10)
# Evaluation model
loss,accuracy=model.evaluate(x_test,y_test)
print("test loss:",loss)
print("test accuracy:",accuracy)
Code run results :
Some points needing attention in the code , Explanations and reminders are also given in the code comments .
Be careful
- The first layer of neural network is to write the size of the input image Later layers can be ignored
边栏推荐
- ReentrantLock 公平锁源码 第0篇
- Reentrantlock fair lock source code Chapter 0
- 51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up
- Langchao Yunxi distributed database tracing (II) -- source code analysis
- How does starfish OS enable the value of SFO in the fourth phase of SFO destruction?
- 2022-07-07: the original array is a monotonic array with numbers greater than 0 and less than or equal to K. there may be equal numbers in it, and the overall trend is increasing. However, the number
- Class head up rate detection based on face recognition
- 图像数据预处理
- tourist的NTT模板
- Introduction to paddle - using lenet to realize image classification method II in MNIST
猜你喜欢
网络模型的保存与读取
y59.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三二)
Lecture 1: the entry node of the link in the linked list
130. Surrounding area
13. Enregistrement et chargement des modèles
Huawei switch s5735s-l24t4s-qa2 cannot be remotely accessed by telnet
Image data preprocessing
基于卷积神经网络的恶意软件检测方法
AI遮天传 ML-初识决策树
Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
随机推荐
丸子官网小程序配置教程来了(附详细步骤)
12.RNN应用于手写数字识别
130. 被圍繞的區域
13. Model saving and loading
Password recovery vulnerability of foreign public testing
Marubeni official website applet configuration tutorial is coming (with detailed steps)
Interface test advanced interface script use - apipost (pre / post execution script)
German prime minister says Ukraine will not receive "NATO style" security guarantee
2022-07-07: the original array is a monotonic array with numbers greater than 0 and less than or equal to K. there may be equal numbers in it, and the overall trend is increasing. However, the number
Mathematical modeling -- knowledge map
What does interface testing test?
Y59. Chapter III kubernetes from entry to proficiency - continuous integration and deployment (III, II)
网络模型的保存与读取
Image data preprocessing
13.模型的保存和載入
Service mesh introduction, istio overview
New library online | information data of Chinese journalists
FOFA-攻防挑战记录
Redis, do you understand the list
Is it safe to open an account on the official website of Huatai Securities?