nn.MarginRankingLoss使用详解

import torch

criterion = torch.nn.MarginRankingLoss(margin = 0.3,reduction='mean')
x1 = torch.Tensor([3,2])
x2 = torch.Tensor([1,4])
y  = torch.Tensor([1,2])
loss = criterion(x1,x2,y)
print(loss)

结果:

tensor(2.1500)

 

详解:

验证:

对于第1个点,x1=3, x2 = 1, y=1。 max(-y*(x1-x2)+margin, 0) = max(-1*(3-1)+0.3, 0) = max(-1.7, 0) = 0

对于第2个点,x1=2, x2 = 4, y=2。 max(-y*(x1-x2)+margin, 0) = max(-2*(2-4)+0.3, 0) = max(4.3, 0) = 4.3

求平均:2.15

 

posted @ 2023-03-04 09:02  sqsq  阅读(449)  评论(0编辑  收藏  举报