小土堆pytorch教程学习笔记P29

该博客介绍了使用PyTorch进行深度学习模型训练和测试的完整流程。首先,通过DataLoader加载CIFAR10数据集,然后创建网络模型,定义损失函数(CrossEntropyLoss)和优化器(SGD)。在训练阶段,利用tudui.train()进入训练模式,进行前向传播、计算损失、反向传播和优化。在测试阶段,通过tudui.eval()进入评估模式,计算测试集上的损失和精度。整个过程记录了训练和测试的指标,并在每个训练轮结束后保存模型。

P29.完整的模型训练套路(三)

  • training start
tudui.train()

train(mode=True)

Sets the module in training mode.

This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g. Dropout, BatchNorm, etc.

  • testing start
tudui.eval()

eval()

Sets the module in evaluation mode.

This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g. Dropout, BatchNorm, etc.

  • 步骤

准备数据集

利用DataLoader加载数据集

创建网络模型

损失函数

优化器

设置训练网络的一些参数(训练次数、测试次数、训练轮数、tensorboard)

训练步骤开始(tudui.train(),从dataloader中取数据,计算误差,优化器优化模型,展示输出)

测试步骤开始(tudui.eval(),with torch.no_grad()只需要测试,不需要使用梯度调整,从dataloader中取数据,计算误差,设置并展示指标,保存模型)

import torchvision
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from P27_model import *

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

# length
train_data_size = len(train_data)
test_data_size = len(test_data)
# if train_data_size=10,The length of train dataset is 10
print("The length of train dataset is:{}".format(train_data_size))
print("The length of test dataset is:{}".format(test_data_size))

# load dataset with DataLoder
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)

# build a network model
tudui = Tudui()

# loss function
loss_fn = nn.CrossEntropyLoss()

# optimizer
# learning_rate = 0.01
# 1e-2 = 1 x (10)^(-2) = 1/100 = 0.01
learning_rate = 1e-2
optimizer = torch.optim.SGD(tudui.parameters(), lr=learning_rate)

# set some parameters of training network
# record the number of training
total_train_step = 0
# record the number of testing
total_test_step = 0
# number of training rounds
epoch = 10

# add tensorboard
writer = SummaryWriter("logs_train")

for i in range(epoch):
    print("------The {} round training start------".format(i+1))

    # training start
    for data in train_dataloader:
        imgs, targets = data
        outputs = tudui(imgs)
        loss = loss_fn(outputs, targets)

        # use optimizer to optimize the model
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        total_train_step = total_train_step + 1
        if total_train_step % 100 == 0:
            print("training round:{}, Loss:{}".format(total_train_step, loss.item()))
            writer.add_scalar("train_loss", loss.item(), total_train_step)

    # testing start
    total_test_loss = 0
    total_accuracy = 0
    with torch.no_grad():
        for data in test_dataloader:
            imgs, targets = data
            outputs = tudui(imgs)
            loss = loss_fn(outputs, targets)
            total_test_loss = total_test_loss + loss.item()
            accuracy = (outputs.argmax(1) == targets).sum()
            total_accuracy = total_accuracy + accuracy
    print("The total loss on testing:{}".format(total_test_loss))
    print("The accuracy of testing:{}".format(total_accuracy/test_data_size))
    writer.add_scalar("test_loss", total_test_loss, total_test_step)
    writer.add_scalar("test_accuracy", total_accuracy/test_data_size, total_test_step)
    total_test_step = total_test_step + 1

    torch.save(tudui, "tudui_{}.pth".format(i))
    # torch.save(tudui.state_dict(), "tudui_{}.pth".format(i))
    print("model saved")

writer.close()

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值