Python_pytorch(五)模型训练

反向传播

Loss Function

Python_pytorch(五)模型训练_第1张图片

import torchvision
from torch import nn
from torch.nn import Flatten
from torch.utils.data import DataLoader

dataset=torchvision.datasets.CIFAR10("./data",train=False,transform=torchvision.transforms.ToTensor(),download=True)
data_loader=DataLoader(dataset,batch_size=1)

class My_mod(nn.Module):
    def __init__(self):
        super(My_mod,self).__init__()
        self.conv1=nn.Conv2d(3,32,5,1,2)
        self.maxpool=nn.MaxPool2d(2)
        self.conv2=nn.Conv2d(32,32,5,1,2)
        self.conv3=nn.Conv2d(32,64,5,1,2)
        self.relu=nn.ReLU()
        self.flatten=Flatten()
        self.linear=nn.Linear(1024,10)
        self.model=nn.Sequential(
            nn.Conv2d(3, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, 5, 1, 2),
            nn.MaxPool2d(2),
            Flatten(),
            nn.Linear(1024, 10)

        )
    def forward(self,x):
        x=self.model(x)
        return x
my_mod=My_mod()

loss=nn.CrossEntropyLoss()


for data in data_loader:
    img,target=data
    output=my_mod(img)
    print(output,target)


    ls=loss(output,target)
    ls.backward()

    print((ls))

加上优化器这模型就基本训练起来了?

import torch
import torchvision
from torch import nn
from torch.nn import Flatten
from torch.utils.data import DataLoader

dataset=torchvision.datasets.CIFAR10("./data",train=False,transform=torchvision.transforms.ToTensor(),download=True)
data_loader=DataLoader(dataset,batch_size=64)

class My_mod(nn.Module):
    def __init__(self):
        super(My_mod,self).__init__()
        self.conv1=nn.Conv2d(3,32,5,1,2)
        self.maxpool=nn.MaxPool2d(2)
        self.conv2=nn.Conv2d(32,32,5,1,2)
        self.conv3=nn.Conv2d(32,64,5,1,2)
        self.relu=nn.ReLU()
        self.flatten=Flatten()
        self.linear=nn.Linear(1024,10)
        self.model=nn.Sequential(
            nn.Conv2d(3, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, 5, 1, 2),
            nn.MaxPool2d(2),
            Flatten(),
            nn.Linear(1024, 10)

        )
    def forward(self,x):
        x=self.model(x)
        return x
my_mod=My_mod()

loss=nn.CrossEntropyLoss()
optim=torch.optim.SGD(my_mod.parameters(),lr=0.01)

for i in range(100):
    Lo=0.0
    for data in data_loader:
       
        img,target=data
        output=my_mod(img)
        #print(output,target)

        ls=loss(output,target)
        Lo=Lo+ls
        optim.zero_grad()
        ls.backward()
        optim.step()

    print(Lo)

利用和更改已经存在的模型

import torchvision
from torch import nn

vgg16_T=torchvision.models.vgg16(True)
vgg16_F=torchvision.models.vgg16(False)
print(vgg16_T)
#更改模型

#添加在features里
vgg16_T.add_module("add_linear",nn.Linear(1000,10))
#添加在classifier里面
vgg16_F.classifier.add_module("add_linear",nn.Linear(1000,10))

print(vgg16_T,vgg16_F)

模型的保存和加载

模型的保存
#模型的保存
import torchvision
import torch

vgg16=torchvision.models.vgg16(False)

#模型的保存1 不仅保存了参数,还保存了模型结构
torch.save(vgg16,"./method1.pth")

#模型的保存2 只是保存了参数(官方推荐,占用内存小)
torch.save(vgg16.state_dict(),"./method2.pth")

模型的加载
#模型的加载
import torch
import torchvision

#对于方法1的模型加载(恢复了参数和架构) 注意需要能访问到所需要的架构
model=torch.load("./method1.pth")
print(model)

#对于方法2的模型加载(只有参数)
vgg16=torchvision.models.vgg16(False)
vgg16.load_state_dict(torch.load("./method2.pth"))
print(vgg16)

你可能感兴趣的:(python,pytorch,深度学习)