pytorch 实现smooth L1 loss

import numpy as np
import torch

import torch.nn.functional as F

a=torch.Tensor([1,5,3,0.5,0.9])
b=torch.Tensor([4,1,0,0.4,0.2])

loss1=F.smooth_l1_loss(a,b)

loss_part1=torch.abs(a-b)
loss_part2=loss_part1**2
loss_part2=loss_part2*0.50

print()
loss2=torch.where(loss_part1>=1,loss_part1-0.5,loss_part2)
loss2=torch.mean(loss2)
print(loss1)
print(loss2)

 

你可能感兴趣的:(deep,learning)