pytorch config_error ai_generated true

RuntimeError: 参数组的学习率不是浮点数或标量张量

RuntimeError: parameter group's learning rate is not a float or a tensor of scalar type

ID: pytorch/tensor-requires-grad-and-optimizer-param-group

其他格式: JSON · Markdown 中文 · English
75%修复率
82%置信度
1证据数
2023-08-10首次发现

版本兼容性

版本状态引入弃用备注
1.10 active
1.11 active
1.12 active
1.13 active
2.0 active
2.1 active
2.2 active
2.3 active

根因分析

在优化器参数组中将非浮点数或非标量张量作为学习率传递,通常是因为使用了列表或数组而不是单个值。

English

Passing a non-float or non-scalar tensor as learning rate in optimizer parameter groups, often due to using a list or array instead of a single value.

generic

官方文档

https://pytorch.org/docs/stable/optim.html#per-parameter-options

解决方案

  1. Ensure each parameter group's 'lr' is a single float or a torch scalar tensor. For example, when creating optimizer with per-parameter options, use a float value directly.
  2. If using a learning rate scheduler that returns a tensor, convert it to a float before assigning to param_groups.

无效尝试

常见但无效的做法:

  1. 40% 失败

    This only changes the data type but doesn't fix the root cause if the value is still a collection.

  2. 60% 失败

    This may hide the error but leads to unexpected behavior if the list is not reduced.