Python implements the convolutional neural network classification model (CNN classification algorithm) project based on PyTorch

Explanation: This is a machine learning practical project (with data + code + documentation + video explanation ). If you need data + code + documentation + video explanation, you can go directly to the end of the article to get it.




1. Project background

The convolutional neural network, referred to as the convolutional network, differs from the ordinary neural network in that the neurons in its convolutional layer only cover the local range of the input feature, which has the characteristics of sparse connectivity and weight shared, and the filter can extract the key features of the image. Because of this feature, convolutional neural networks can give better results in image recognition. 

This project implements the Minst dataset convolutional neural network classification model based on PyTorch.

2. Data Acquisition

The modeling data of this time comes from the Internet (compiled by the author of this project), and the statistics are as follows:

 

3. Data preprocessing

3.1  View data

View data:

key code:

 

3.2 Dataset shape view

Dataset shape:

The key code is as follows:  

 

4. Exploratory Data Analysis

4.1 View pictures 

Use the imshow () method of the Matplotlib tool to draw pictures:

 

5. Feature engineering

5.1 Establish feature data and label data

The key code is as follows:

6. Build a convolutional neural network classification model

Mainly use the CNN layer network for object classification.

6.1  Model Construction

6.2  Iterative process

 

7. Model evaluation

7.1 Evaluation indicators and results

Evaluation indicators mainly include accuracy rate, precision rate, recall rate (recall rate), F1 score and so on.

 

As can be seen from the above table, the accuracy rate of the model is 99.14%, and the F1 score is 0.9914, which shows that the model has a good effect.

7.2 Classification report

 

As can be seen from the above figure, the F1 score of classification 0 is 0.99; the F1 score of classification 1 is 0.99; the F1 score of classification 2 is 0.99 and so on.

8. Conclusion and Outlook

In summary, this project implements the convolutional neural network classification model based on PyTorch and evaluates the model, which finally proves that the model we proposed works better.

# 定义模型训练函数
def train(model, optimizer, criterion, train_loader):
    model.train()  # 设置训练模式
    for batch_idx, (data, target) in enumerate(train_loader):  # 循环
        optimizer.zero_grad()  # 清空过往梯度
        output = model(data)  # 预测


本次机器学习项目实战所需的资料,项目资源如下:

项目说明:
链接:https://pan.baidu.com/s/1dW3S1a6KGdUHK90W-lmA4w 
提取码:bcbp



train_images, train_targets = (train_dataset._load_data())  # 加载训练集数据:特征、标签
test_images, test_targets = (test_dataset._load_data())  # 加载测试集数据:特征、标签

print('*********************训练集特征数据**************************')
print(train_images[0][0])
print('*********************训练集标签数据**************************')
print(train_targets[0])
print('*********************训练集特征形状**************************')
print(train_images.size())
print('*********************训练集标签形状**************************')
print(train_targets.size())

 For more project practice, see the list of machine learning project practice collections:

List of actual combat collections of machine learning projects


 

Guess you like

Origin blog.csdn.net/weixin_42163563/article/details/131896462