class torch.optim.LBFGS(params, lr=1, max_iter=20, max_eval=None, tolerance_grad=1e-07, tolerance_change=1e-09, history_size=100, line_search_fn=None)[source]

Implements L-BFGS algorithm, heavily inspired by minFunc.


This optimizer doesn’t support per-parameter options and parameter groups (there can be only one).


Right now all parameters have to be on a single device. This will be improved in the future.


This is a very memory intensive optimizer (it requires additional param_bytes * (history_size + 1) bytes). If it doesn’t fit in memory try reducing the history size, or use a different algorithm.

  • lr (float) – learning rate (default: 1)

  • max_iter (int) – maximal number of iterations per optimization step (default: 20)

  • max_eval (int) – maximal number of function evaluations per optimization step (default: max_iter * 1.25).

  • tolerance_grad (float) – termination tolerance on first order optimality (default: 1e-5).

  • tolerance_change (float) – termination tolerance on function value/parameter changes (default: 1e-9).

  • history_size (int) – update history size (default: 100).

  • line_search_fn (str) – either ‘strong_wolfe’ or None (default: None).


Add a param group to the Optimizer s param_groups.

This can be useful when fine tuning a pre-trained network as frozen layers can be made trainable and added to the Optimizer as training progresses.


param_group (dict) – Specifies what Tensors should be optimized along with group specific optimization options.


Loads the optimizer state.


state_dict (dict) – optimizer state. Should be an object returned from a call to state_dict().


Returns the state of the optimizer as a dict.

It contains two entries:

  • state - a dict holding current optimization state. Its content

    differs between optimizer classes.

  • param_groups - a list containing all parameter groups where each

    parameter group is a dict


Performs a single optimization step.


closure (Callable) – A closure that reevaluates the model and returns the loss.


Sets the gradients of all optimized torch.Tensor s to zero.


set_to_none (bool) – instead of setting to zero, set the grads to None. This will in general have lower memory footprint, and can modestly improve performance. However, it changes certain behaviors. For example: 1. When the user tries to access a gradient and perform manual ops on it, a None attribute or a Tensor full of 0s will behave differently. 2. If the user requests zero_grad(set_to_none=True) followed by a backward pass, .grads are guaranteed to be None for params that did not receive a gradient. 3. torch.optim optimizers have a different behavior if the gradient is 0 or None (in one case it does the step with a gradient of 0 and in the other it skips the step altogether).


Access comprehensive developer documentation for PyTorch

View Docs


Get in-depth tutorials for beginners and advanced developers

View Tutorials


Find development resources and get your questions answered

View Resources