通俗理解,如下图所示,为理想与现实的差距
import torch
from torch.nn import L1Loss
inputs=torch.tensor([1,2,3],dtype=torch.float32)
targets=torch.tensor([1,2,5],dtype=torch.float32)
inputs=torch.reshape(inputs,(1,1,1,3))
tragets=torch.reshape(targets,(1,1,1,3))
loss=L1Loss()
result=loss(inputs,tragets)
print(result)
loss_mse=MSELoss()
result_mse=loss_mse(inputs,targets)
x=torch.tensor([0.1,0.2,0.3])
y=torch.tensor([1])
x=torch.reshape(x,(1,3))
loss_cross=nn.CrossEntropyLoss()
result_cross=loss_cross(x,y)
print(result_cross)#tensor(1.1019)
该网络的输出概率如图所示,batch_size设为1是为了方便查看每一个的输出概率,实际不会设置为1。
dataset=torchvision.datasets.CIFAR10('dataset',train=False,transform=torchvision.transforms.ToTensor(),download=False)
dataloader=DataLoader(dataset,batch_size=1)
class Felix(nn.Module):
def __init__(self):
super(Felix,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
felix=Felix()
for data in dataloader:
imgs,targets=data
outputs=felix(imgs)
print(outputs)
print(targets)
loss=nn.CrossEntropyLoss()
felix=Felix()
for data in dataloader:
imgs,targets=data
outputs=felix(imgs)
result_loss=loss(outputs,targets)
print(result_loss)
结果是实际输出和目标之间的差距
然后可以实现第二个目的,反向传播,进行参数的更新。
先断点运行。
网络名称(felix)->model1->Protected Attributes->_models->卷积层(‘0’)->weight->grad
此时梯度为none,还没有开始求值
loss=nn.CrossEntropyLoss()
felix=Felix()
optim=torch.optim.SGD(felix.parameters(),lr=0.01)#随机梯度下降
for data in dataloader:
imgs,targets=data
outputs=felix(imgs)
result_loss=loss(outputs,targets)
optim.zero_grad()#梯度设置为0
result_loss.backward()
optim.step()
loss=nn.CrossEntropyLoss()
felix=Felix()
optim=torch.optim.SGD(felix.parameters(),lr=0.01)#随机梯度下降
for epoch in range(20):
running_loss=0.0
for data in dataloader:
imgs,targets=data
outputs=felix(imgs)
result_loss=loss(outputs,targets)
optim.zero_grad()#梯度设置为0
result_loss.backward()
optim.step()
running_loss=running_loss+result_loss#每轮开始前将loss设置为0,在学习过程中所有的loss总和
print(running_loss)
root:下载路径
split:
transform:是否在数据集上进行变换
target_transform:在target上进行变换
vgg16=torchvision.models.vgg16(pretrained=False)#使用网络模型的参数没有经过训练
#保存方式1
torch.save(vgg16,'vgg16_method1.pth')
import torch
# 方式1-》保存方式1,加载模型
model=torch.load('vgg16_method1.pth')
print(model)
#保存方式2
torch.save(vgg16.state_dict(),'vgg16_method2.pth')
可以看出来是字典的形式
#方式2
model=torch.load('vgg16_method2.pth')
print(model)
vgg16=torchvision.models.vgg16(pretrained=False)
vgg16.load_state_dict(torch.load('vgg16_method2.pth'))
print(vgg16)
from model_save import *#其中model_save为要保存的模型