Legacy autograd function with non-static forward method is deprecated. Please use new-style autograd

博客介绍了如何处理'Legacy autograd function with non-static forward method is deprecated'的警告,建议将旧版的autograd函数转换为使用静态forward方法的新样式。作者分享了自己遇到的问题,即类中只有forward函数而没有backward函数,通过移除对Function的继承并转换为普通类调用来解决问题。此外,还提到了网上的一些反向传播的解决方法,如添加staticmethod注释。
摘要由CSDN通过智能技术生成

 

Legacy autograd function with non-static forward method is deprecated. Please use new-style autograd function with static forward method jacke121

 

报错代码:

self.priors_pal2.type(type(x.data)).to(self.device)

我的解决方法:

我的这个类只有forward函数,么有backward,我就把继承Function去掉了。

改成普通的类调用,就可以了。

 

网上的有反向传播的解决方法:

加 staticmethod注释

class GradReverse(Function):

    # 重写父类方法的时候,最好添加默认参数,不然会有warning(为了好看。。)
    @ staticmethod
    def forward(ctx, x, lambd, **kwargs: None):
        # 其实就是传入dict{'lambd' = lambd} 
        ctx.lambd = lambd
        return x.view_as(x)

    @staticmethod
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值