python升级版本之后不再支持通过/符号进行张量和python原数据的运算处理,需要用函数实现。
报错信息:
RuntimeError: Integer division of tensors using div or / is no longer supported, and in a future release div will perform true division as in Python 3. Use true_divide or floor_divide (// in Python) instead.
出错代码:(100 * correct / total)
其中correct为张量,total为常量。
解决方案:(100 * torch.true_divide(correct,total))
使用pytorch模型提供的处理函数实现即可。
举一反三&总结:
/符号,精确除法,替代函数: torch.true_divide(a,b)
//符号,整除,替代函数:torch.floor_divide(a,b)
在Python升级后,PyTorch不再支持直接使用/进行张量与Python原生数据的精确除法。报错信息提示应使用torch.true_divide()代替。举例来说,当尝试计算(100*correct/total),其中correct为张量,total为常量,会触发RuntimeError。解决方法是将表达式改为(100*torch.true_divide(correct,total))。了解这个变化对于正确执行PyTorch模型中的数学运算至关重要。
658

被折叠的 条评论
为什么被折叠?



