Pytorch学习--神经网络--完整的模型训练套路

一、下载数据集

python 复制代码
train_data = torchvision.datasets.CIFAR10(root="datasets",train=True,transform=torchvision.transforms.ToTensor(),download=True)
train_data = torchvision.datasets.CIFAR10(root="datasets",train=False,transform=torchvision.transforms.ToTensor(),download=True)

二、数据大小

python 复制代码
train_data_len = len(train_data)
test_data_len = len(test_data)
print(train_data_len)
print(test_data_len)

三、数据加载

python 复制代码
train = DataLoader(train_data,batch_size=64)
test = DataLoader(test_data,batch_size=64)

四、模型定义

python 复制代码
class Mary(nn.Module):
    def __init__(self):
        super(Mary,self).__init__()
        self.model1 = nn.Sequential(
            Conv2d(3, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )
    def forward(self,x):
        x = self.model1(x)
        return x

五、定义对象

python 复制代码
Yorelee = Mary()

六、损失函数

python 复制代码
loss_fn = nn.CrossEntropyLoss()

七、优化器

python 复制代码
learning_rate = 0.01
optimizer = torch.optim.SGD(Yorelee.parameters(),lr=learning_rate)

八、训练过程

python 复制代码
epoch = 10
for i in range(epoch):
    print("***********第{}轮训练***********".format(i+1))
    index = 1
    for data in train:
        imgs,targets = data
        output = Yorelee(imgs)
        loss = loss_fn(output,targets)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        print("({}) ".format(index),loss.item())
        index += 1

九、训练过程+测试过程

python 复制代码
# 训练过程
epoch = 10
for i in range(epoch):
    print("***********第{}轮训练***********".format(i+1))
    index = 1
    for data in train:
        imgs,targets = data
        output = Yorelee(imgs)
        loss = loss_fn(output,targets)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        print("({}) ".format(index),loss.item())
        index += 1

    #测试过程
    total_loss = 0
    with torch.no_grad():
        for data in test:
            imgs,targets = data
            output = Yorelee(imgs)
            loss = loss_fn(output,targets)
            total_loss += loss
    print("测试: ",total_loss.item())

十、Tensorboard 可视化

python 复制代码
#  Tensorboard 可视化
writer = SummaryWriter("logs")

# 训练过程
epoch = 10
for i in range(epoch):
    print("***********第{}轮训练***********".format(i+1))
    index = 1
    for data in train:
        imgs,targets = data
        output = Yorelee(imgs)
        loss = loss_fn(output,targets)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        print("({}) ".format(index),loss.item())
        if index % 100 == 0:
            writer.add_scalar("train_loss",loss,index)
        index += 1

    #测试过程
    total_loss = 0
    with torch.no_grad():
        for data in test:
            imgs,targets = data
            output = Yorelee(imgs)
            loss = loss_fn(output,targets)
            total_loss += loss
    print("测试: ",total_loss.item())
    writer.add_scalar("test_loss", total_loss, i)
writer.close()

十一、对于分类任务

可以用准确率来衡量,而不是用 total_loss 来衡量效果

先来举个简单的例子:


代码如下:

python 复制代码
import torch

output = torch.tensor([[0.1,0.2],
                       [0.3,0.4]])
output = torch.argmax(output,dim=1)
preds = torch.tensor([0,1])
print((output==preds).sum())

输出:

python 复制代码
tensor(1)

十二、对测试过程的重新改进

python 复制代码
#  Tensorboard 可视化
writer = SummaryWriter("logs")

# 训练过程
epoch = 10
for i in range(epoch):
    print("***********第{}轮训练***********".format(i+1))
    index = 1
    for data in train:
        imgs,targets = data
        output = Yorelee(imgs)
        loss = loss_fn(output,targets)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        print("({}) ".format(index),loss.item())
        if index % 100 == 0:
            writer.add_scalar("train_loss",loss,index)
        index += 1

    #测试过程
    total_loss = 0
    with torch.no_grad():
        find_true = 0
        for data in test:
            imgs,targets = data
            output = Yorelee(imgs)
            # loss = loss_fn(output,targets)
            # total_loss += loss
            output = torch.argmax(output,dim=1)
            find_true += (output == targets).sum()
    print("测试: ",find_true/test_data_len.item())
    writer.add_scalar("test_loss", find_true/test_data_len, i)

十三、总结

model.py

python 复制代码
import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear


class Mary(nn.Module):
    def __init__(self):
        super(Mary,self).__init__()
        self.model1 = nn.Sequential(
            Conv2d(3, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )
    def forward(self,x):
        x = self.model1(x)
        return x

if __name__ == '__main__':
    input = torch.ones((64,3,32,32))
    Yorelee = Mary()
    output = Yorelee(input)
    print(output.shape)

train.py

python 复制代码
import torch
import torchvision
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

from model import *

# 数据集
train_data = torchvision.datasets.CIFAR10(root="datasets",train=True,transform=torchvision.transforms.ToTensor(),download=True)
test_data = torchvision.datasets.CIFAR10(root="datasets",train=False,transform=torchvision.transforms.ToTensor(),download=True)

# 数据大小
train_data_len = len(train_data)
test_data_len = len(test_data)
print(train_data_len)
print(test_data_len)

# 数据加载
train = DataLoader(train_data,batch_size=64)
test = DataLoader(test_data,batch_size=64)

# 定义对象
Yorelee = Mary()

# 损失函数
loss_fn = nn.CrossEntropyLoss()

# 优化器
learning_rate = 0.01
optimizer = torch.optim.SGD(Yorelee.parameters(),lr=learning_rate)

#  Tensorboard 可视化
writer = SummaryWriter("logs")

# 训练过程
epoch = 10
for i in range(epoch):
    print("***********第{}轮训练***********".format(i+1))
    index = 1
    for data in train:
        imgs,targets = data
        output = Yorelee(imgs)
        loss = loss_fn(output,targets)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        print("({}) ".format(index),loss.item())
        if index % 100 == 0:
            writer.add_scalar("train_loss",loss,index)
        index += 1

    #测试过程
    total_loss = 0
    with torch.no_grad():
        find_true = 0
        for data in test:
            imgs,targets = data
            output = Yorelee(imgs)
            # loss = loss_fn(output,targets)
            # total_loss += loss
            output = torch.argmax(output,dim=1)
            find_true += (output == targets).sum()
    print("测试: ",find_true/test_data_len.item())
    writer.add_scalar("test_loss", find_true/test_data_len, i)
writer.close()

十四、XX.train() 和 XX.eval() 的说明



相关推荐
张铁铁是个小胖子15 分钟前
微服务学习
java·学习·微服务
sp_fyf_20241 小时前
【大语言模型】ACL2024论文-35 WAV2GLOSS:从语音生成插值注解文本
人工智能·深度学习·神经网络·机器学习·语言模型·自然语言处理·数据挖掘
AITIME论道1 小时前
论文解读 | EMNLP2024 一种用于大语言模型版本更新的学习率路径切换训练范式
人工智能·深度学习·学习·机器学习·语言模型
Dovir多多2 小时前
Python数据处理——re库与pydantic的使用总结与实战,处理采集到的思科ASA防火墙设备信息
网络·python·计算机网络·安全·网络安全·数据分析
明明真系叻2 小时前
第二十六周机器学习笔记:PINN求正反解求PDE文献阅读——正问题
人工智能·笔记·深度学习·机器学习·1024程序员节
XianxinMao3 小时前
Transformer 架构对比:Dense、MoE 与 Hybrid-MoE 的优劣分析
深度学习·架构·transformer
88号技师4 小时前
2024年12月一区SCI-加权平均优化算法Weighted average algorithm-附Matlab免费代码
人工智能·算法·matlab·优化算法
IT猿手4 小时前
多目标应用(一):多目标麋鹿优化算法(MOEHO)求解10个工程应用,提供完整MATLAB代码
开发语言·人工智能·算法·机器学习·matlab
青春男大4 小时前
java栈--数据结构
java·开发语言·数据结构·学习·eclipse
88号技师4 小时前
几款性能优秀的差分进化算法DE(SaDE、JADE,SHADE,LSHADE、LSHADE_SPACMA、LSHADE_EpSin)-附Matlab免费代码
开发语言·人工智能·算法·matlab·优化算法