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

- **ID:** `pytorch/tensor-requires-grad-and-optimizer-param-group`
- **领域:** pytorch
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.10 | active | — | — |
| 1.11 | active | — | — |
| 1.12 | active | — | — |
| 1.13 | active | — | — |
| 2.0 | active | — | — |
| 2.1 | active | — | — |
| 2.2 | active | — | — |
| 2.3 | active | — | — |

## 解决方案

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.
   ```

## 无效尝试

- **** — This only changes the data type but doesn't fix the root cause if the value is still a collection. (40% 失败率)
- **** — This may hide the error but leads to unexpected behavior if the list is not reduced. (60% 失败率)
