Fa·Bryant 2023-08-18 10:48 采纳率: 100%
浏览 537
已结题

(已解决)UserWarning: __floordiv__ is deprecated

终端输出信息:

```bash
 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').
  time_vector = time_vector // time_win_aggr

代码位置:

```python
def aggregate_by_time(time_vector,time_win_aggr):
        time_vector = time_vector - time_vector.min()
        time_vector = time_vector // time_win_aggr
        return time_vector

解决办法:
把上述代码修改为下述代码即可:

def aggregate_by_time(time_vector,time_win_aggr):
        time_vector = time_vector - time_vector.min()
        time_vector = torch.div(time_vector, time_win_aggr, rounding_mode='trunc')
        return time_vector

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-08-18 12:39
    关注

    【以下回答由 GPT 生成】

    问题解决方案:

    根据给出的信息,问题是出现在PyTorch中的警告信息,即"UserWarning: floordiv is deprecated"。

    警告信息指出,在未来版本的PyTorch中,__floordiv__方法的行为将发生变化。目前的行为是向0('trunc'函数)舍入,而不是向下('floor')舍入。对于负值,会导致舍入错误。

    为了解决这个问题,可以按照警告信息所提供的建议修改代码。具体来说是将 // 运算符替换为 torch.div 方法,并指定 rounding_mode='trunc' 参数。

    以下是修改后的代码:

    def aggregate_by_time(time_vector, time_win_aggr):
        time_vector = time_vector - time_vector.min()
        time_vector = torch.div(time_vector, time_win_aggr, rounding_mode='trunc')
        return time_vector
    

    通过使用 torch.div 方法并设置 rounding_mode='trunc' 参数,可以避免警告信息并解决问题。

    希望对您有所帮助,如果还有其他问题,请随时提问!


    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月1日
  • 已采纳回答 8月24日
  • 创建了问题 8月18日