【问题标题】:Custom Loss in Pytorch where object does not have attribute backward()Pytorch 中的自定义损失,其中对象没有向后属性()
【发布时间】:2018-07-26 13:57:11
【问题描述】:

我是 pytorch 的新手,我尝试创建自己的自定义损失。这真的很有挑战性。以下是我的损失。

class CustomLoss(nn.Module):

    def __init__(self,  size_average=True, reduce=True):
        """

        Args:
            size_average (bool, optional): By default, the losses are averaged
               over observations for each minibatch. However, if the field
               size_average is set to ``False``, the losses are instead summed for
               each minibatch. Only applies when reduce is ``True``. Default: ``True``
            reduce (bool, optional): By default, the losses are averaged
               over observations for each minibatch, or summed, depending on
               size_average. When reduce is ``False``, returns a loss per input/target
               element instead and ignores size_average. Default: ``True``
        """
        super(CustomLoss, self).__init__()      


    def forward(self, S, N, M, type='softmax',):

        return self.loss_cal(S, N, M, type)



    ### new loss cal
    def loss_cal(self, S, N, M, type="softmax",):
        """ calculate loss with similarity matrix(S) eq.(6) (7)
        :type: "softmax" or "contrast"
        :return: loss
        """

        self.A = torch.cat([S[i * M:(i + 1) * M, i:(i + 1)]
                               for i in range(N)], dim=0)        
        self.A = torch.autograd.Variable(self.A)        


        if type == "softmax":
            self.B = torch.log(torch.sum(torch.exp(S.float()), dim=1, keepdim=True) + 1e-8)
            self.B = torch.autograd.Variable(self.B)       
            total = torch.abs(torch.sum(self.A - self.B))        
        else:
            raise AssertionError("loss type should be softmax or contrast !")
        return total

当我运行以下命令时:

loss = CustomLoss()          
(loss.loss_cal(S=S,N=N,M=M))
loss.backward()

我收到以下错误:

C:\Program Files\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2113             magic_arg_s = self.var_expand(line, stack_depth)
   2114             with self.builtin_trap:
-> 2115                 result = fn(magic_arg_s, cell)
   2116             return result
   2117 

<decorator-gen-60> in time(self, line, cell, local_ns)

C:\Program Files\Anaconda3\lib\site-packages\IPython\core\magic.py in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189 
    190         if callable(arg):

C:\Program Files\Anaconda3\lib\site-packages\IPython\core\magics\execution.py in time(self, line, cell, local_ns)
   1178         else:
   1179             st = clock2()
-> 1180             exec(code, glob, local_ns)
   1181             end = clock2()
   1182             out = None

<timed exec> in <module>()

C:\Program Files\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __getattr__(self, name)
    530                 return modules[name]
    531         raise AttributeError("'{}' object has no attribute '{}'".format(
--> 532             type(self).__name__, name))
    533 
    534     def __setattr__(self, name, value):

AttributeError: 'CustomLoss' object has no attribute 'backward'

为什么会出现此错误?我没有遇到 TF 的这个错误。我的理解是,它与autograd有关吗?如果有人能解释为什么我会遇到这个错误,我可以弄清楚其余的。

【问题讨论】:

    标签: pytorch


    【解决方案1】:

    你好!

    问题是您尝试在模块上调用后向函数,而不是在变量上调用(您可能想要)。由于您没有在模块上实现后向功能,解释器找不到。所以你想要做的是:

    loss_func = CustomLoss()          
    loss = loss_func.loss_cal(S=S,N=N,M=M)
    loss.backward()
    

    一般来说: 您使用的是nn.Module,但实际上没有参数。虽然这可行,但这不是nn.Modules 的用途 - 因此应该避免。相反,只需制作一个纯函数 - 毕竟,你拥有的函数无论如何都是静态的。如果您真的想参加课程,请考虑您要创建的课程类型 - loss。然而,损失可能具有特殊的 pytorch 属性。所以你应该阅读here的讨论。

    【讨论】:

    猜你喜欢
    • 2021-03-08
    • 2020-07-18
    • 2020-11-30
    • 2021-05-02
    • 2020-04-09
    • 2019-11-12
    • 2019-11-02
    • 1970-01-01
    • 2019-05-27
    相关资源
    最近更新 更多