当前位置:网站首页>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 .
边栏推荐
- [note] common combined filter circuit
- Malware detection method based on convolutional neural network
- 接口测试进阶接口脚本使用—apipost(预/后执行脚本)
- SDNU_ACM_ICPC_2022_Summer_Practice(1~2)
- 网络模型的保存与读取
- 9. Introduction to convolutional neural network
- What is load balancing? How does DNS achieve load balancing?
- Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
- AI zhetianchuan ml novice decision tree
- Four stages of sand table deduction in attack and defense drill
猜你喜欢
AI遮天传 ML-初识决策树
Class head up rate detection based on face recognition
DNS series (I): why does the updated DNS record not take effect?
[necessary for R & D personnel] how to make your own dataset and display it.
Image data preprocessing
130. 被圍繞的區域
国内首次,3位清华姚班本科生斩获STOC最佳学生论文奖
9. Introduction to convolutional neural network
Kubernetes Static Pod (静态Pod)
Redis, do you understand the list
随机推荐
1293_ Implementation analysis of xtask resumeall() interface in FreeRTOS
swift获取url参数
完整的模型训练套路
完整的模型验证(测试,demo)套路
5g NR system messages
New library online | cnopendata China Star Hotel data
The method of server defense against DDoS, Hangzhou advanced anti DDoS IP section 103.219.39 x
Service Mesh介绍,Istio概述
Password recovery vulnerability of foreign public testing
国内首次,3位清华姚班本科生斩获STOC最佳学生论文奖
9.卷积神经网络介绍
NVIDIA Jetson test installation yolox process record
8道经典C语言指针笔试题解析
基于卷积神经网络的恶意软件检测方法
【GO记录】从零开始GO语言——用GO语言做一个示波器(一)GO语言基础
Semantic segmentation model base segmentation_ models_ Detailed introduction to pytorch
NVIDIA Jetson测试安装yolox过程记录
AI遮天传 ML-初识决策树
7. Regularization application
Swift get URL parameters