UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone…的解决方案

今天跑程序的过程中,遇到两个报错信息,由于不耽误程序的运行,之前一直没有留意,今天给修复了一下bug

报错信息:

UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).   y_support=torch.tensor(y_support,dtype=torch.int64)

解决方案:torch.tensor改为:torch.as

#改之前
y_support=torch.tensor(y_support,dtype=torch.int64)


#改之后
y_support=torch.as_tensor(y_support,dtype=torch.int64)

另一个报错信息:

 UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.

报错原因:

softmax()函数被弃用,虽然程序还是可以运行成功,但是这个做法不被pytorch所赞成。这个写法在早期的pytorch版本是没有警告的,现在因为其他考虑,要加上有指明dim参数。

解决方案: nn.Softmax()改为nn.Softmax(dim=1)

注意:dim=某个数字,这个数字可以根据自己的需要进行更改。

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2023年9月17日
下一篇 2023年9月17日

相关推荐