一、Loss函数
1.1 使用L1Loss函数
运行如下代码,使用L1Loss函数:
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))
targets = torch.reshape(targets, (1, 1, 1, 3))
loss = L1Loss()
result = loss(inputs, targets)
print(result)
运行结果:
tensor(0.6667)
给L1Loss函数的reduction赋值“sum’
loss = L1Loss(reduction='sum')
运行结果:
tensor(2.)
1.2 使用MSELoss函数
代码如下:
loss_mse = nn.MSELoss()
result_mse = loss_mse(inputs, targets)
运行结果如下:
tensor(1.3333)
1.3 使用交叉熵损失函数
代码如下:
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)
再构造网络识别CIFAR10数据集:
class Cow(nn.Module):
def __init__(self):
super(Cow, 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
cow = Cow()
for data in dataloader:
imgs, targets = data
outputs = cow(imgs)
print(outputs)
print(targets)
部分结果如下:
tensor([5])
tensor([[-0.1190, 0.0869, -0.1648, -0.1100, 0.0507, -0.0409, -0.0936, 0.0471,
-0.0934, 0.0593]], grad_fn=<AddmmBackward0>)
tensor([1])
tensor([[-0.0982, 0.0904, -0.1566, -0.1100, 0.0389, -0.0490, -0.0699, 0.0153,
-0.0730, 0.0411]], grad_fn=<AddmmBackward0>)
tensor([4])
tensor([[-0.1061, 0.0895, -0.1504, -0.1072, 0.0405, -0.0439, -0.0601, 0.0277,
-0.0786, 0.0287]], grad_fn=<AddmmBackward0>)
tensor([2])
tensor([[-0.1079, 0.0770, -0.1545, -0.1160, 0.0461, -0.0392, -0.0656, 0.0452,
-0.0762, 0.0448]], grad_fn=<AddmmBackward0>)
再使用交叉熵损失函数:
loss = nn.CrossEntropyLoss()
cow = Cow()
for data in dataloader:
imgs, targets = data
outputs = cow(imgs)
result_loss = loss(outputs, targets)
print(result_loss)
部分结果如下:
tensor(2.2760, grad_fn=<NllLossBackward0>)
tensor(2.3173, grad_fn=<NllLossBackward0>)
tensor(2.3213, grad_fn=<NllLossBackward0>)
tensor(2.3041, grad_fn=<NllLossBackward0>)
tensor(2.2267, grad_fn=<NllLossBackward0>)
tensor(2.2145, grad_fn=<NllLossBackward0>)
tensor(2.3352, grad_fn=<NllLossBackward0>)
tensor(2.2295, grad_fn=<NllLossBackward0>)
tensor(2.2732, grad_fn=<NllLossBackward0>)
这就是神经网络输出和真实输出之间的误差。
二、使用反向传播
代码如下:
result_loss.backward()
对代码进行调试,查看使用反向传播前后的梯度值:
单步调试后grad的值发生了变化
说明可以对某些参数进行反向传播进行更新。