【pytorch】RuntimeError: Integer division of tensors using div or / is no longer supported【解决】

ok, 能搜到这篇文章大概遇到了我已经遇到过的问题。今天把pytorch升级到1.6.0,发现tensor和int之间的除法不能直接用'/'。明明1.5.0都是可以用的-_-。火炬这种邻代兼容性有点值得吐槽。

对于这个问题直接看官方文档就可以了:

https://pytorch.org/docs/stable/generated/torch.div.html

或者,看我的解决方案:

对于tensor A和整数n之间的除法:

result = A / n # not supported in torch 1.6.0


# solution
result = torch.floor_divide(A, n)

这个floor_divide相当于python中的'//',即得到的结果为整型(去掉了小数点后的数字)

如果你不想要这种除法,想得到带小数点的准确数值,您可以:

result = torch.true_divide(A, n)

根据具体情况选取以上两种除法,即可解决这个issue。

猜你喜欢

转载自blog.csdn.net/leviopku/article/details/108245066