当前位置:网站首页>3. MNIST dataset classification
3. MNIST dataset classification
2022-07-08 01:02:00 【booze-J】
article
One 、MNIST Data sets and Softmax
1.MNIST Data sets
Most examples use handwritten digits MNIST Data sets . The dataset contains 60,000 Examples and for training 10,000 An example for testing .
Each picture contains 28*28 Pixel , stay MNIST The training data set is a shape of [60000,28,28] Tensor , We first need to convert the data set into [60000,784], Then it can be put into the network for training . The first dimension number is used to index images , The second dimension number is used to index the pixels in each image . Generally, we also need to normalize the data in the picture 0~1 Between .
MNIST The label of the dataset is between 0-9 The number of , We need to convert labels into "one-hotvectors". One one-hot A vector other than one digit is 1 Outside , The other dimension numbers are 0, Such as tag 0 Will be expressed as ([1,0,0,0,0,0,0,0,0,0]), label 3 Will be expressed as ([0,0,0,1,0,0,0,0,0,0]).
therefore ,MNIST The label of the dataset is a [60000,10] Digital matrix of .
28*28=784, Each picture has 784 Pixels , Corresponding 784 Neurons . The final output 10 Neurons correspond to 10 A digital .
2.Softmax
Softmax The function is to convert the output of neural network into probability value .
We know MNIST The result is 0-9, Our model may infer the number of a picture 9 Is the probability that 80%, It's the number. 8 Is the probability that 10%, Then the probability of other numbers is smaller , The total probability adds up to 1. This is a use of softmax Classic case of regression model .softmax The model can be used to assign probabilities to different objects .
Two 、MNIST Data set classification
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 .
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
from tensorflow.keras.optimizers import SGD
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,784) reshape() Middle parameter filling -1 Parameter results can be calculated automatically Divide 255.0 To normalize
x_train = x_train.reshape(x_train.shape[0],-1)/255.0
x_test = x_test.reshape(x_test.shape[0],-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
# Creating models Input 784 Neurons , Output 10 Neurons
model = Sequential([
# Define output yes 10 Input is 784, Set offset to 1, add to softmax Activation function
Dense(units=10,input_dim=784,bias_initializer='one',activation="softmax"),
])
# Define optimizer
sgd = SGD(lr=0.2)
# Define optimizer ,loss_function, The accuracy of calculation during training
model.compile(
optimizer=sgd,
loss="mse",
metrics=['accuracy']
)
# Training models
model.fit(x_train,y_train,batch_size=32,epochs=10)
# Evaluation model
loss,accuracy = model.evaluate(x_test,y_test)
print("\ntest loss",loss)
print("accuracy:",accuracy)
The final run result :
Be careful
Dense(units=10,input_dim=784,bias_initializer='one',activation="softmax")
It's used here softmax Activation function .- Here we use
fit
Method of model training , The previous linear regression and nonlinear regression model training methods are different from this .
Code :
model.compile(
optimizer=sgd,
loss="mse",
metrics=['accuracy']
)
Add metrics=['accuracy']
, You can calculate the accuracy rate during training .
边栏推荐
- SDNU_ACM_ICPC_2022_Summer_Practice(1~2)
- 【笔记】常见组合滤波电路
- 9.卷积神经网络介绍
- NVIDIA Jetson测试安装yolox过程记录
- The weight of the product page of the second level classification is low. What if it is not included?
- 5g NR system messages
- 13.模型的保存和載入
- 新库上线 | CnOpenData中华老字号企业名录
- Cross modal semantic association alignment retrieval - image text matching
- Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades(KDD20)
猜你喜欢
Service mesh introduction, istio overview
7.正则化应用
They gathered at the 2022 ecug con just for "China's technological power"
jemter分布式
基于微信小程序开发的我最在行的小游戏
语义分割模型库segmentation_models_pytorch的详细使用介绍
133. 克隆图
Codeforces Round #804 (Div. 2)(A~D)
FOFA-攻防挑战记录
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
随机推荐
第四期SFO销毁,Starfish OS如何对SFO价值赋能?
Cancel the down arrow of the default style of select and set the default word of select
串口接收一包数据
跨模态语义关联对齐检索-图像文本匹配(Image-Text Matching)
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades(KDD20)
完整的模型训练套路
51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up
German prime minister says Ukraine will not receive "NATO style" security guarantee
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
Su embedded training - C language programming practice (implementation of address book)
Swift get URL parameters
《因果性Causality》教程,哥本哈根大学Jonas Peters讲授
The method of server defense against DDoS, Hangzhou advanced anti DDoS IP section 103.219.39 x
攻防演练中沙盘推演的4个阶段
9. Introduction to convolutional neural network
Four stages of sand table deduction in attack and defense drill
3.MNIST数据集分类
10.CNN应用于手写数字识别
Course of causality, taught by Jonas Peters, University of Copenhagen
130. Zones environnantes