当前位置:网站首页>3.MNIST数据集分类
3.MNIST数据集分类
2022-07-07 23:11:00 【booze-J】
一、MNIST数据集及Softmax
1.MNIST数据集
大多数示例使用手写数字的MNIST数据集。该数据集包含60,000个用于训练的示例和10,000个用于测试的示例。
每一张图片包含28*28个像素,在MNIST训练数据集中是一个形状为[60000,28,28]的张量,我们首先需要把数据集转成[60000,784],然后才能放到网络中训练。第一个维度数字用来索引图片,第二个维度数字用来索引每张图片中的像素点。一般我们还需要把图片中的数据归一化0~1之间。
MNIST数据集的标签是介于0-9的数字,我们要把标签转化为"one-hotvectors"。一个one-hot向量除了一位数字是1外,其余维度数字都是0,比如标签0将表示为([1,0,0,0,0,0,0,0,0,0]),标签3将表示为([0,0,0,1,0,0,0,0,0,0])。
因此,MNIST数据集的标签是一个[60000,10]的数字矩阵。
28*28=784,每张图片有784个像素点,对应着784个神经元。最后输出10个神经元对应着10个数字。
2.Softmax
Softmax作用就是把神经网络的输出转化为概率值。
我们知道MNIST的结果是0-9,我们模型可能推测出一张图片的数字9的概率是80%,是数字8的概率是10%,然后其他数字的概率更小,总体概率加起来等于1。这是一个使用softmax回归模型的经典案例。softmax模型可以用来给不同的对象分配概率。
二、MNIST数据集分类
代码运行平台为jupyter-notebook,文章中的代码块,也是按照jupyter-notebook中的划分顺序进行书写的,运行文章代码,直接分单元粘入到jupyter-notebook即可。
1.导入第三方库
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.加载数据及数据预处理
# 载入数据
(x_train,y_train),(x_test,y_test) = mnist.load_data()
# (60000, 28, 28)
print("x_shape:\n",x_train.shape)
# (60000,) 还未进行one-hot编码 需要后面自己操作
print("y_shape:\n",y_train.shape)
# (60000, 28, 28) -> (60000,784) reshape()中参数填入-1的话可以自动计算出参数结果 除以255.0是为了归一化
x_train = x_train.reshape(x_train.shape[0],-1)/255.0
x_test = x_test.reshape(x_test.shape[0],-1)/255.0
# 换one hot格式
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes=10)
3.训练模型
# 创建模型 输入784个神经元,输出10个神经元
model = Sequential([
# 定义输出是10 输入是784,设置偏置为1,添加softmax激活函数
Dense(units=10,input_dim=784,bias_initializer='one',activation="softmax"),
])
# 定义优化器
sgd = SGD(lr=0.2)
# 定义优化器,loss_function,训练过程中计算准确率
model.compile(
optimizer=sgd,
loss="mse",
metrics=['accuracy']
)
# 训练模型
model.fit(x_train,y_train,batch_size=32,epochs=10)
# 评估模型
loss,accuracy = model.evaluate(x_test,y_test)
print("\ntest loss",loss)
print("accuracy:",accuracy)
最终运行结果:
注意
Dense(units=10,input_dim=784,bias_initializer='one',activation="softmax")
这里用到了softmax激活函数。- 这里我们使用的
fit
方法进行的模型训练,之前的线性回归和非线性回归的模型训练方式和这不同。
代码:
model.compile(
optimizer=sgd,
loss="mse",
metrics=['accuracy']
)
中添加metrics=['accuracy']
,可以在训练过程中计算准确率。
边栏推荐
- ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
- FOFA-攻防挑战记录
- 语义分割模型库segmentation_models_pytorch的详细使用介绍
- SDNU_ ACM_ ICPC_ 2022_ Summer_ Practice(1~2)
- Handwriting a simulated reentrantlock
- 《因果性Causality》教程,哥本哈根大学Jonas Peters讲授
- Experience of autumn recruitment in 22 years
- 9.卷积神经网络介绍
- v-for遍历元素样式失效
- CVE-2022-28346:Django SQL注入漏洞
猜你喜欢
Langchao Yunxi distributed database tracing (II) -- source code analysis
语义分割模型库segmentation_models_pytorch的详细使用介绍
第四期SFO销毁,Starfish OS如何对SFO价值赋能?
SDNU_ACM_ICPC_2022_Summer_Practice(1~2)
New library launched | cnopendata China Time-honored enterprise directory
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
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
QT establish signal slots between different classes and transfer parameters
接口测试进阶接口脚本使用—apipost(预/后执行脚本)
[necessary for R & D personnel] how to make your own dataset and display it.
随机推荐
股票开户免费办理佣金最低的券商,手机上开户安全吗
Summary of the third course of weidongshan
The weight of the product page of the second level classification is low. What if it is not included?
Analysis of 8 classic C language pointer written test questions
What if the testing process is not perfect and the development is not active?
Reentrantlock fair lock source code Chapter 0
Is it safe to open an account on the official website of Huatai Securities?
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades(KDD20)
NVIDIA Jetson test installation yolox process record
9.卷积神经网络介绍
NTT template for Tourism
letcode43:字符串相乘
大二级分类产品页权重低,不收录怎么办?
[go record] start go language from scratch -- make an oscilloscope with go language (I) go language foundation
基于微信小程序开发的我最在行的小游戏
华为交换机S5735S-L24T4S-QA2无法telnet远程访问
Interface test advanced interface script use - apipost (pre / post execution script)
【笔记】常见组合滤波电路
How is it most convenient to open an account for stock speculation? Is it safe to open an account on your mobile phone
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