01.17 pytorch学习(完整的模型训练套路(一))

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


class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.model1 = 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


train_data = torchvision.datasets.CIFAR10(root="./dataset2", train=True, transform=torchvision.transforms.ToTensor(),download=True)
test_data = torchvision.datasets.CIFAR10(root="./dataset2", train=False, transform=torchvision.transforms.ToTensor(),download=True)

#length 长度
train_data_size = len(train_data)
test_data_size = len(test_data)
print("训练数据集的长度为:{}".format(train_data_size))
print("测试数据集的长度为:{}".format(test_data_size))

#利用DataLoader加载数据集
train_data = DataLoader(train_data,batch_size=64)
test_data = DataLoader(test_data,batch_size=64)

#创建网络模型
tudui = Tudui()

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

#优化器
learning_rate = 1e-2
optimizer = torch.optim.SGD(tudui.parameters(),lr=learning_rate)

#设置训练网络的一些参数
#记录训练的次数
total_train_step = 0
#记录测试的次数
total_test_step = 0
#训练轮数
epoch = 10

writer = SummaryWriter("./logs")

for i in range(epoch):
    print("第{}轮训练开始".format(i+1))
    for data in train_data:
        img,targets = data
        output = tudui(img)
        loss = loss_fn(output,targets)
        writer.add_scalar("train", loss.item(),total_train_step)
        #优化器优化模型
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        total_train_step = total_train_step + 1
        if total_train_step % 100 == 0:
            print("训练次数:{},Loss:{}".format(total_train_step,loss))
    total_test_loss = 0
    with torch.no_grad():
        for data in test_data:
            img,targets = data
            output = tudui(img)
            loss = loss_fn(output, targets)
            writer.add_scalar("test", loss.item(), total_test_step)
            total_test_loss = total_test_loss + loss.item()
            total_test_step = total_test_step + 1
        print("整体测试集上的loss{}".format(total_test_loss))

writer.close()

输出:

Files already downloaded and verified
Files already downloaded and verified
训练数据集的长度为:50000
测试数据集的长度为:100001轮训练开始
训练次数:100,Loss:2.290745258331299
训练次数:200,Loss:2.276966094970703
训练次数:300,Loss:2.235224723815918
训练次数:400,Loss:2.134385108947754
训练次数:500,Loss:2.0246334075927734
训练次数:600,Loss:2.0128252506256104
训练次数:700,Loss:2.0046582221984863
整体测试集上的loss316.11608636379242轮训练开始
训练次数:800,Loss:1.8838609457015991
训练次数:900,Loss:1.8686732053756714
训练次数:1000,Loss:1.9141151905059814
训练次数:1100,Loss:1.9722687005996704
训练次数:1200,Loss:1.710410714149475
训练次数:1300,Loss:1.648863673210144
训练次数:1400,Loss:1.7540481090545654
训练次数:1500,Loss:1.8434439897537231
整体测试集上的loss305.0225634574893轮训练开始
训练次数:1600,Loss:1.7706611156463623
训练次数:1700,Loss:1.630061149597168
训练次数:1800,Loss:1.9372758865356445
训练次数:1900,Loss:1.6966317892074585
训练次数:2000,Loss:1.9074283838272095
训练次数:2100,Loss:1.5317140817642212
训练次数:2200,Loss:1.4539611339569092
训练次数:2300,Loss:1.8044062852859497
整体测试集上的loss264.791660785675054轮训练开始
训练次数:2400,Loss:1.7389321327209473
训练次数:2500,Loss:1.379348874092102
训练次数:2600,Loss:1.5718865394592285
训练次数:2700,Loss:1.665346384048462
训练次数:2800,Loss:1.5018153190612793
训练次数:2900,Loss:1.6004217863082886
训练次数:3000,Loss:1.3566491603851318
训练次数:3100,Loss:1.4977909326553345
整体测试集上的loss255.192974448204045轮训练开始
训练次数:3200,Loss:1.341180682182312
训练次数:3300,Loss:1.512349247932434
训练次数:3400,Loss:1.4751192331314087
训练次数:3500,Loss:1.5546340942382812
训练次数:3600,Loss:1.5790996551513672
训练次数:3700,Loss:1.3384708166122437
训练次数:3800,Loss:1.2884880304336548
训练次数:3900,Loss:1.4463378190994263
整体测试集上的loss246.731834173202516轮训练开始
训练次数:4000,Loss:1.4490960836410522
训练次数:4100,Loss:1.4267728328704834
训练次数:4200,Loss:1.5739071369171143
训练次数:4300,Loss:1.2011997699737549
训练次数:4400,Loss:1.1541255712509155
训练次数:4500,Loss:1.3856275081634521
训练次数:4600,Loss:1.4166735410690308
整体测试集上的loss236.639800429344187轮训练开始
训练次数:4700,Loss:1.3386189937591553
训练次数:4800,Loss:1.5188709497451782
训练次数:4900,Loss:1.3532589673995972
训练次数:5000,Loss:1.455378770828247
训练次数:5100,Loss:1.020859718322754
训练次数:5200,Loss:1.3468397855758667
训练次数:5300,Loss:1.2501678466796875
训练次数:5400,Loss:1.4011257886886597
整体测试集上的loss226.587743759155278轮训练开始
训练次数:5500,Loss:1.2370308637619019
训练次数:5600,Loss:1.2253111600875854
训练次数:5700,Loss:1.2848955392837524
训练次数:5800,Loss:1.2292295694351196
训练次数:5900,Loss:1.3054215908050537
训练次数:6000,Loss:1.5778721570968628
训练次数:6100,Loss:1.0871853828430176
训练次数:6200,Loss:1.1093010902404785
整体测试集上的loss217.878479242324839轮训练开始
训练次数:6300,Loss:1.396878957748413
训练次数:6400,Loss:1.1288025379180908
训练次数:6500,Loss:1.5935940742492676
训练次数:6600,Loss:1.116767406463623
训练次数:6700,Loss:1.103469967842102
训练次数:6800,Loss:1.1624258756637573
训练次数:6900,Loss:1.1174882650375366
训练次数:7000,Loss:0.9438201785087585
整体测试集上的loss209.335918188095110轮训练开始
训练次数:7100,Loss:1.2791147232055664
训练次数:7200,Loss:0.9959218502044678
训练次数:7300,Loss:1.1102135181427002
训练次数:7400,Loss:0.8791030645370483
训练次数:7500,Loss:1.2451682090759277
训练次数:7600,Loss:1.2461706399917603
训练次数:7700,Loss:0.8882855176925659
训练次数:7800,Loss:1.2989035844802856
整体测试集上的loss201.96278846263885

tensorboard上查看:
纵坐标为损失函数的值
横坐标为训练或测试的次数

train:
在这里插入图片描述
test:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

I Am Rex

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值