当前位置:网站首页>Does neural network sound tall? Take you to train a network from scratch (based on MNIST)
Does neural network sound tall? Take you to train a network from scratch (based on MNIST)
2022-07-29 10:00:00 【Mr.Winter`】
Catalog
1 What is neural network ?
We know that there are three main schools in the development of artificial intelligence :
- Semiotic school
- The connectionist school
- Behaviorism school
The connectionist school believes that : Trillions of neurons in the human brain are intricately interconnected , It is the source of intelligence . The core of connectionism is bionics and neuroscience , It focuses on the connection mechanism and learning algorithm between neural networks , Dedicated to representing a large number of neurons through computers , To simulate the intelligence of the brain . This is the origin of neural networks , Its formal definition is as follows :
Artificial neural network (Artificial Neural Networks,ANNs) Also referred to as neural network or connection model , It's a way of mimicking the behavioral characteristics of animal neural networks , Mathematical model of distributed parallel information processing algorithm . This network depends on the complexity of the system , By adjusting the interconnecting relationships between a large number of internal nodes , In order to achieve the purpose of processing information .
As shown in the figure below, it is a convolutional neural network framework for graphics and image processing , Today, start training from scratch on the following network .
2 Convolutional neural networks
Convolutional neural networks (Convolutional Neural Network, CNN) Feature extraction layer is added on the basis of fully connected neural network , It is mainly used in the field of computer vision , Processing pattern recognition 、 Image classification 、 Target detection and so on .
CNN comparison FCNN The reason why it is more suitable for visual tasks is , Its implementation Aggregation and compression of high-dimensional information .
For example , Take the pixels of a two-dimensional picture as input , be FCNN The number of neurons in the input layer will be very large , Then consider that each neuron is connected to all neurons in the adjacent layer , Therefore, as the optimization goal, the connection weight matrix increases exponentially , Bring unacceptable learning time complexity . and CNN Through the feature screening of image information , Filter out a large amount of redundant information mixed with pictures , Then map to the output space through a simple fully connected network , It will greatly reduce the complexity .

3 Experimental process
This question is aimed at classic MNIST Handwritten numeral classification experiment , be based on Pytorch Framework independent design neural network , Test network performance , And carry out some visual analysis . The experimental process is as follows :
- Build convolutional neural network ;
- Load data set . download MNIST Handwritten digital datasets , Divide the training set 、 Validation set and test set , And encapsulated as an iteratable data loader object ;
- Training models . Define loss function and optimization method , Calculate the loss through forward propagation , Then based on the back propagation optimization model parameters , After iterating until the training error converges, save the model locally ;
3.1 Building neural networks
As shown below , Build convolutional neural network . among
Conv2d: Convolution layerMaxPool2d: Pooling layerReLu: Activation function
The specific principles and functions of these neural network components will be analyzed in another article , This chapter focuses on application practice .
class CNN(nn.Module):
''' * @breif: Convolutional neural networks '''
def __init__(self):
super().__init__()
self.convPoolLayer_1 = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=10, kernel_size=5),
nn.MaxPool2d(kernel_size=2),
nn.ReLU()
)
self.convPoolLayer_2 = nn.Sequential(
nn.Conv2d(in_channels=10, out_channels=20, kernel_size=5),
nn.MaxPool2d(kernel_size=2),
nn.ReLU()
)
self.fcLayer = nn.Linear(320, 10)
def forward(self, x):
batchSize = x.size(0)
x = self.convPoolLayer_1(x)
x = self.convPoolLayer_2(x)
x = x.reshape(batchSize, -1)
x = self.fcLayer(x)
return x
3.2 Load data set
Use pytorch Provided Dataset Class to load and preview datasets
from abc import abstractmethod
import numpy as np
from torchvision.datasets import mnist
from torch.utils.data import Dataset
from PIL import Image
class mnistData(Dataset):
''' * @breif: MNIST Dataset abstract interface * @param[in]: dataPath -> Data set storage path * @param[in]: transforms -> Data set transformation '''
def __init__(self, dataPath: str, transforms=None) -> None:
super().__init__()
self.dataPath = dataPath
self.transforms = transforms
self.data, self.label = [], []
def __len__(self) -> int:
return len(self.label)
def __getitem__(self, idx: int):
img = self.data[idx]
if self.transforms:
img = self.transforms(img)
return img, self.label[idx]
def loadData(self, train: bool) -> list:
''' * @breif: Download and load datasets * @param[in]: train -> Whether it is a training set * @retval: List of data and labels '''
# If there is no dataset under the specified directory, download
dataSet = mnist.MNIST(self.dataPath, train=train, download=True)
# Initialize data and labels
data = [ i[0] for i in dataSet ]
label = [ i[1] for i in dataSet ]
return data, label

3.3 Training models
Considering that this practice is a multi classification problem , Therefore, the output of the final network is a ten dimensional vector and passes through softmax Into a probability distribution , The loss function is designed as cross entropy , The optimization method is random gradient descent algorithm .
for images, labels in trainBar:
images, labels = images.to(config.device), labels.to(config.device)
# Gradient clear
opt.zero_grad()
# Positive communication
outputs = model(images)
# Calculate the loss
loss = F.cross_entropy(outputs, labels)
# Back propagation
loss.backward()
# Model update
opt.step()
The training process is as follows :

4 Algorithm analysis
After testing , At the same learning rate ,CNN stay 20 Generation has converged , but FCNN stay 20 It's only around the generation that it begins to converge . The generalization error of the test set shows CNN The accuracy of prediction reaches 95%, but FCNN Only 70%, So in the problem of image classification ,CNN Its efficiency and accuracy are far higher than FCNN. therefore CNN Strong learning and generalization ability , Just use the known pattern to CNN Train , The network has the ability to map and express the input and output .

More wonderful Columns :
- 《 Robot principle and technology 》
- 《ROS From entry to mastery 》
- 《 Computer vision course 》
- 《 machine learning 》
- 《 Numerical optimization method 》
- …
Private messages enter AI Technology exchange group , Whoring for nothing 50G E-books and teaching resources , Regularly release AI Knowledge dry goods 、 Free technology books and other benefits !
边栏推荐
- Modulenotfounderror: no module named 'pywt' solution
- 详解:到底什么是GPS北斗授时服务器?
- 不堆概念、换个角度聊多线程并发编程
- Summary of JD internship written examination questions
- MySQL infrastructure: SQL query statement execution process
- Is it safe to open an account online now? Do you want to know that you must go to the business hall to open an account now?
- Some suggestions for programmers to leave single
- Enterprise architecture | togaf architecture capability framework
- Is the marginal old technology in its 40s weak in the future or rising from the ground?
- Method of cocos2d-x sprite moving
猜你喜欢
![[AAAI] attention based spatiotemporal graph convolution network for traffic flow prediction](/img/3d/717bc3a47a58470edd7a815a976320.png)
[AAAI] attention based spatiotemporal graph convolution network for traffic flow prediction

MySQL infrastructure: SQL query statement execution process

高效能7个习惯学习笔记

这是一份不完整的数据竞赛年鉴!

mysql 数据库 期末复习题库

Pyqt5 rapid development and practice 6.1 three dimensions of good software & 6.2 layout management in pyqt5 & 6.3 absolute location layout of pyqt5

程序员脱离单身的一些建议

一文读懂Plato Farm的ePLATO,以及其高溢价缘由

Network security (5)

This is an incomplete data competition Yearbook!
随机推荐
机器学习入门的百科全书-2018年“机器学习初学者”公众号文章汇总
Mysql database final review question bank
SiC Power Semiconductor Industry Summit Forum successfully held
Leetcode question brushing - sorting
The function of that sentence
Pytest+allure generate test report
A sharp tool for data visualization Seaborn easy to get started
The maximum length of VARCHAR2 type in Oracle is_ Oracle modify field length SQL
Encyclopedia of introduction to machine learning - 2018 "machine learning beginners" official account article summary
还是有机会的
阿左的境界
Function - (C travel notes)
Examples of specific usage of diagnostic instructions in s7-1200 and s7-1500 (led+devicestates+modulestates)
Is the marginal old technology in its 40s weak in the future or rising from the ground?
那句话的作用
Meituan senior technical expert: DDD's practice in the evolution of tourism e-commerce architecture
2021年CS保研经历(六):系统填报 + 一些感想
Be tolerant and generous
最新翻译的官方PyTorch简易入门教程(PyTorch1.0版本)
【C语言】三子棋(智能下棋 + 阻拦玩家)