当前位置:网站首页>Explanation of CNN circular training | pytorch series (XXII)
Explanation of CNN circular training | pytorch series (XXII)
2022-07-28 02:52:00 【51CTO】
writing |AI_study

The original title :CNN Training Loop Explained - Neural Network Code Project
- Prepare the data
- Build a model
- Training models
- Build training loop
- Analysis of the results of the model
Single batch Training
We can put individual batch The training code is summarized as follows :
Output
One thing you'll notice is that , Every time you run this code, you get different results . This is because the model is created at the top every time , We know from previous articles that the weight of the model is randomly initialized .
Now let's see how to modify this code to use all of the batch, So we can use the whole training set to train .
all batch Training for (epoch)
Now? , To train all the batches available in our data loader , We need to make some changes and add an extra line of code :
We're going to create one for Loop to iterate all of batch Handle , Instead of getting a single batch Handle .
Because our training focus is on 60,000 Samples , So we're going to have 60,000 / 100 = 600 Sub iteration . For this reason , We're going to remove... From the loop print sentence , And track the total losses and the total number of correct predictions that were finally printed .
About this 600 One thing to note about the next iteration is , At the end of the loop , Our weights will be updated 600 Time . If we improve batch_size This number will go down if we lower batch_size This number will go up .
Last , Before we get to loss Tensor calls backward() After method , We know that gradients will be calculated and added to the network parameters grad Properties of the . on this account , We need to zero these gradients . We can use the zero_grad() Method to achieve this .
We're ready to run this code . This time the code will take longer , It's going to be because of the loop 600 Batches .
We got the result , We can see 60000 The correct total is 42104.
There is only one epoch( A complete data transfer ) after , It's already very good . Even if we make one epoch, We still need to remember , Weights have been updated 600 Time , It depends on our batch size . If you allow batch_batch It's bigger , such as 10,000, Then the weight will only be updated 6 Time , It's not going to be very good either .
Multiple epoch Of Training
To execute multiple epoch, All we have to do is put this code into for In circulation . We will also put epoch Add a number to print In the sentence .
After running this code , We get every one of them epoch Result :
We can see that the number of correct values has increased , and loss Less .
Complete training loop
Put all these together , We can put the Internet 、 Optimizer and train_loader Extracted from the training cycle unit .
optimizer = optim.Adam(network.parameters(), lr=0.01)
And then there's the visualization
We should now have a good understanding of the training cycle and how to use it PyTorch To build them .PyTorch The cool thing is , We can be like debugging forward() Function to debug the training loop code like that .
In the next article , We'll see how to get predictions for each sample in the training set , And use these predictions to create a confusion matrix . See you next class !
The content of the article is carefully studied , My level is limited , Translation cannot be perfect , But it really took a lot of effort , I hope you can move your sexy hands , Share a circle of friends , Support me ^_^
The original English link is :
https://deeplizard.com/learn/video/XfYmia3q2Ow




边栏推荐
- 初识C语言 -- 结构体,分支和循环语句
- IO flow: node flow and processing flow are summarized in detail.
- A 64 bit 8-stage pipelined adder based on FPGA
- How to simply realize the function of menu dragging and sorting
- Opengauss Developer Day 2022 sincerely invites you to visit the "database kernel SQL Engine sub forum" of Yunhe enmo
- Representation of children and brothers of trees
- JVM tuning -xms -xmx -xmn -xss
- 使用PyTorch的TensorBoard-可视化深度学习指标 | PyTorch系列(二十五)
- Is it safe to buy funds on Alipay? I want to make a fixed investment in the fund
- Lombok prompts variable log error when using JUnit test in idea
猜你喜欢

First knowledge of C language -- structure, branch and loop statements

Selenium+pytest+allure comprehensive exercise

unordered_map的hash function及hash bucket存储方式探索

新基建助力智能化道路交通领域的转型发展

CNN训练循环重构——超参数测试 | PyTorch系列(二十八)

超参数调整和实验-训练深度神经网络 | PyTorch系列(二十六)

Special network technology virtual host PHP version setting

【LeetCode】13. Linked List Cycle·环形链表

Interpretation of cesium3dtilesets using customshader and examples of Omni effects

Chapter III queue
随机推荐
【自我成长网站收集】
树的孩子兄弟表示法
Typescript (zero) -- introduction, environment construction, first instance
JS 事件对象 offsetX/Y clientX Y PageX Y
Why is there no unified quotation for third-party testing fees of software products?
Usage of delegate
[self growth website collection]
ERD online 4.0.0 free private deployment scheme
Flutter God operation learning (full level introduction)
AWS elastic three swordsman
JS event object offsetx/y clientx y pagex y
The virtual host website cannot access the self-test method
Email security report in the second quarter: email attacks have soared fourfold, and well-known brands have been used to gain trust
LETV responded that employees live an immortal life without internal problems and bosses; Apple refuses to store user icloud data in Russia; Dapr 1.8.0 release | geek headlines
D multi production single consumption
使用PyTorch的TensorBoard-可视化深度学习指标 | PyTorch系列(二十五)
[tutorial of using idea] shortcut key of idea
How to simply realize the function of menu dragging and sorting
[signal denoising] signal denoising based on Kalman filter with matlab code
Job 7.27 IO process