loss = torch.nn.L1Loss(reduction='sum')
result = loss(inputs, targets)
print(result)
平方差
loss_mse = torch.nn.MSELoss()
result = loss_mse(inputs, targets)
print(result)
交叉熵
x = torch.tensor([0.1, 0.2, 0.3])
y = torch.tensor([1])
x = torch.reshape(x, [1, 3])
loss_cross = torch.nn.CrossEntropyLoss()
result = loss_cross(x, y)
print(result)
1.2 反向传播
反向传播:根据损失函数,反向传播计算梯度,然后更新参数,使得损失函数最小化
import torch
import torchvision
from torchvision import transforms
from torch.utils.data import DataLoader
from torch import nn
from torch.utils.tensorboard import SummaryWriter
https://wsxk.github.io/pytorch2/
损失函数与反向传播
1.1 损失函数(loss function) 1.2 反向传播
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 = torch.nn.L1Loss(reduction='sum') result = loss(inputs, targets) print(result)
平方差
loss_mse = torch.nn.MSELoss() result = loss_mse(inputs, targets) print(result)
交叉熵
x = torch.tensor([0.1, 0.2, 0.3]) y = torch.tensor([1]) x = torch.reshape(x, [1, 3]) loss_cross = torch.nn.CrossEntropyLoss() result = loss_cross(x, y) print(result)
1.2 反向传播 反向传播:根据损失函数,反向传播计算梯度,然后更新参数,使得损失函数最小化 import torch import torchvision from torchvision import transforms from torch.utils.data import DataLoader from torch import nn from torch.utils.tensorboard import SummaryWriter
diy_transform = transforms.Compose([transforms.ToTensor()]) dataset = torchvision.datasets.CIFAR10(root=