b站小土堆pytorch教程学习笔记
根据loss更新模型参数
1.计算实际输出与目标之间的差距
2.为我们更新输出提供一定的依据(反向传播)
1 MSEloss
import torch
from torch.nn import L1Loss
from torch import nn
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))
targets=torch.reshape(targets,(-1,1,1,3))
loss=L1Loss()
result=loss(inputs,targets)
loss_mse=nn.MSELoss()
result_mse=loss_mse(inputs,targets)
print(result)
print(result_mse)
tensor(0.6667)
tensor(1.3333)
2 Cross EntropyLoss
x=torch.tensor([0.1,0.2