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

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

对于这个问题直接看官方文档就能够了:python

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

或者,看个人解决方案:spa

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

result = A / n # not supported in torch 1.6.0


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

这个floor_divide至关于python中的'//',即获得的结果为整型(去掉了小数点后的数字)htm

若是你不想要这种除法,想获得带小数点的准确数值,您能够:文档

result = torch.true_divide(A, n)

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

相关文章
相关标签/搜索