报错如下:
UserWarning: Legacy autograd function with non-static forward method is deprecated and will be removed in 1.3. Please use new-style autograd function with static forward method. (Example:
https://pytorch.org/docs/stable/autograd.html#torch.autograd.Function)
报错原因: pytorch版本问题
使用PyTorch编写代码的时候,我们通常将模型整合进一个类,在init方法里声明模型结构,在forward方法里约定模型里数据的正向流动,然后PyTorch自动生成数据的方向传播backward方法。
PyTorch在1.3版本及之后,规定forward方法必须是静态方法。
违反了该原则的代码将会在运行时报上述错误。
于是我花了一下的时间把pytorch从1.6.0降到1.2.0.。。。。结果还是报错????????????
正确的解决方法:
在forward方法声明上一行加入@staticmethod装饰器,即可完成修改。
后记心得:这个解决方法在报错后面的链接里已经给出了官方示例。。。嘤嘤嘤,以后一定要仔细看错误报告。完!