Python 笔记 PyTorch train_GPU1

import torch.optim
import torchvision
from torch.utils.data import DataLoader
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.tensorboard import SummaryWriter

"""
    GPU训练
    方法1:
    1.网络模型
    2.数据(imgs)
    3.损失函数
    +.cuda()即可
    
    方法2:
    .to(device)
    device = torch.device("cpu")
    torch.device("cuda")
    torch.device("cuda:0")
    torch.device("cuda:1")
"""

# 1.准备数据集
train_data = torchvision.datasets.CIFAR10("D:/dev/python/pyWork/Season2/Stage1/data/myimg", True,
                                          transform=torchvision.transforms.ToTensor(), download=True)
test_data = torchvision.datasets.CIFAR10("D:/dev/python/pyWork/Season2/Stage1/data/myimg", False,
                                         transform=torchvision.transforms.ToTensor(), download=True)

# 2.加载数据集
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)


# 3.搭建神经网络
class Test(nn.Module):

    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        # 利用Sequential使代码更简洁, 更易管理
        self.module1 = Sequential(
            Conv2d(3, 32, 5, 1, 2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, 1, 2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, 1, 2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self, x):
        x = self.module1(x)
        return x


# 4.创建网络模型
test = Test()
test = test.cuda()  #

# 5.创建损失函数
loss_func = nn.CrossEntropyLoss()
loss_func = loss_func.cuda()

# 6.创建优化器
optimizer = torch.optim.SGD(test.parameters(), lr=0.01)

# 7.设置网络训练的一些参数
total_train_step = 0  # 记录训练次数
total_test_step = 0  # 记录测试的次数
epoch = 31  # 训练的轮数
test_data_size = len(test_data)

# 8.训练模型及优化
# test.train()
writer = SummaryWriter("D:/dev/python/pyWork/Season2/Stage1/logs")
for i in range(epoch):
    print(f"-------------- 第{i + 1}轮训练开始 --------------")
    for data in train_dataloader:
        imgs, targets = data
        imgs = imgs.cuda()
        targets = targets.cuda()
        outputs = test(imgs)
        loss = loss_func(outputs, targets)

        # 优化
        optimizer.zero_grad()  # 梯度清零
        loss.backward()
        optimizer.step()
        total_train_step = total_train_step + 1
        if total_train_step % 100 == 0:  # 只有100的倍数才会输出, 避免无用信息
            print(f"训练次数{total_train_step}, Loss:{loss}")
            writer.add_scalar("train_loss", loss.item(), total_train_step)  # 将训练损失写入

    # 9.测试模型
    # test.eval()
    total_test_loss = 0
    total_accuracy = 0
    with torch.no_grad():
        for data in test_dataloader:
            imgs, targets = data
            imgs = imgs.cuda()
            targets = targets.cuda()
            outputs = test(imgs)
            loss = loss_func(outputs, targets)
            total_test_loss = total_test_loss + loss
            accuracy = (outputs.argmax(1) == targets).sum()
            total_accuracy = total_accuracy + accuracy

    print(f"整体测试集上的Loss:{total_test_loss}")
    print(f"整体测试集上的正确率:{total_accuracy / test_data_size}")
    writer.add_scalar("test_loss", total_test_loss, total_test_step)  # 将测试损失写入
    total_test_step = total_test_step + 1
    # 10.保存模型
    if i == 30:
        torch.save(test, f"test_{i}.pth")
        print("模型已保存")

writer.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值