In the PyTorch documentation, the closure function passed to Optimizer.step()
is expected to return a float
. However, in the official example, the closure actually returns a torch.Tensor
.
In practice — especially when using optimizers like LBFGS
— the closure must return a torch.Tensor
so that backward()
can be called properly and the optimizer has access to the computation graph.
This inconsistency leads to issues with static type checkers like mypy
, which raise a type error if the closure returns a torch.Tensor
instead of a float
.
It would be helpful if the documentation and type annotations were updated to reflect the actual usage pattern. Or at least clarified that returning a Tensor
is not only acceptable, but often required.