Detectron2——SOLOv2代码报错——UserWarning: __floordiv__ is deprecated

报错问题:

.py:226: UserWarning: floordiv is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the ‘trunc’ function NOT ‘floor’). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode=‘trunc’), or for actual floor division, use torch.div(a, b, rounding_mode=‘floor’).coord_h = int((center_h / upsampled_size[0]) // (1. / num_grid))

错误分析

向下取整运算符 “//” 已经被废弃,使用torch.div()来替代

torch.div(a,b,rounding_mode='floor')  ## 向下取整
torch.div(a,b,rounding_mode='trunc')  ## 四舍五入

解决问题

 原文:
 coord_w = int((center_w / upsampled_size[1]) // (1. / num_grid))
 coord_h = int((center_h / upsampled_size[0]) // (1. / num_grid))
 改成:
 coord_w = int(torch.div((center_w / upsampled_size[1]), (1. / num_grid), rounding_mode='floor'))
 coord_h = int(torch.div((center_h / upsampled_size[0]), (1. / num_grid), rounding_mode='floor'))
  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这是一个警告提示,意味着在未来版本的PyTorch中,__floordiv__将不再使用,并且其行为将发生更改。当前,它向0舍入(类似于“trunc”函数而不是“floor”),这会导致负值的舍入不正确。为了保持当前行为,需要注意这个警告提示。 ### 回答2: 近期在使用 PyTorch 进行深度学习训练过程中,可能会遇到一个 warning: ```python UserWarning: __floordiv__ is deprecated, and its behavior will change in a future version of PyTorch. It currently rounds toward 0 (like the 'trunc' function not 'floor'). This results in incorrect rounding for negative values. To keep the current behavior, you can use //, floor_divide() or true_divide() (with round_mode='floor') instead. ``` 这个 warning 是表示 PyTorch 中的除法符号“//”和相应的函数 floor_divide() 和 true_divide() 将代替原来的 __floordiv__() 函数,同时 __floordiv__() 函数的行为将在未来的 PyTorch 版本中发生变化,如此时声明的般会向零舍入(类似于 'trunc' 函数而不是 'floor' 函数),这将导致负数的四舍五入不正确。 为了避免这种问题,可以在代码中将 __floordiv__() 函数改用 “//” 符号等代替方法,并将 round_mode 参数设置为“floor”。这样做可以保留当前行为,同时可以避免面临未来版本中的 warning 和问题。 总的来说,这个 warning 是 PyTorch 开发团队为了提高代码准确性和代码向后兼容性而发布的警告,开发者们应该积极跟进监测新版本的变化,并进行适当的代码调整。 ### 回答3: 在PyTorch中,出现了"__floordiv__ is deprecated"的警告。该警告是在某些版本中出现的,预示着未来 PyTorch 版本中的行为可能会发生变化。在当前版本中,__floordiv__ 会向0取整(像 "trunc" 函数而不是 "floor" 函数)。这意味着在处理负数时会出现不正确的取整。 为了保持当前行为,可以使用 "floor_divide" 函数而不是 "__floordiv__" 函数。这个函数在PyTorch中有专门的实现,会对给定数组的每个元素执行向下调整。如果输入值为正,则表现与 "__floordiv__" 函数相同,但如果输入值为负,则会向负无穷方向舍入,而不是像之前那样向零方向舍入。 此外,建议使用"//"(双斜杠)操作符取代 "__floordiv__" 函数,这样既可以更好地表达代码意图,也可以避免由于函数行为更改而产生的问题。但是请注意,如果代码仍然使用 "__floordiv__" 函数,这并不会导致任何问题,只是会产生一个警告。 总之,尽可能使用 "floor_divide" 函数或 "//" 操作符,以避免由于 PyTorch 版本更改而引起的问题。同时注意警告信息,及时更新代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值