# 1)计算绝对差总和:|0-1|+|1-1|+|2-1|+|3-1|=4;
# 2)再平均:4/4=1。
import torch
import torch.nn as nn
sample = torch.tensor([[1.,1.],[1.,1.]])
target = torch.tensor([[0.,1.],[2.,3.]])
criterion = nn.L1Loss()
loss = criterion(sample, target)
CLASS torch.nn.L1Loss(size_average=None, reduce=None, reduction: str = 'mean')
mean absolute error (MAE)
参数 | 描述 |
---|---|
size_average (bool, optional) | |
reduce (bool, optional) | |
reduction (string, optional) | ‘none’ , ‘mean’ , ‘sum’,‘none’ |
from torch import nn
import torch
loss = nn.L1Loss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.randn(3, 5)
output = loss(input, target)
output.backward()