pytorch 区间loss 损失函数

x33g5p2x  于2021-11-10 转载在 其他  
字(0.6k)|赞(0)|评价(0)|浏览(252)

pytorch 区间loss 损失函数

我们知道sigmoid可以把值转化为0-1之间。

tanh函数可以把值转化到[-1,1]之间,

但是在回归时,希望梯度是均匀的,有么有别的方法呢?

答案是肯定的,

解决方法1:

data=torch.sin(data),这个跟SIGMOD效果差不多,

周期性函数,把值变到了[-1,1]之间。

周期性函数,把值变到了[0,1]之间,加了abs这个不收敛:

loss0 = bce_loss(torch.abs(torch.cos(out)), labels_v)

解决方法2:

比如要回归A,希望A在1到-1之间,可以设计损失函数:

import torch

class My_Loss3(torch.nn.Module):
    def __init__(self):
        super(My_Loss3, self).__init__()

    def forward(self, pred_angle0, label):
        l1 = torch.clamp(torch.abs(pred_angle0)-1, min=0)
        c1 = torch.abs(pred_angle0 - label)
        c2 =torch.abs(2 - c1)
        l3 = torch.where(c1 > c2, c2, c1)
        return torch.mean(0.1*l3 + l1)

希望

相关文章

微信公众号

最新文章

更多