sciai.common.LbfgsOptimizer

View Source On Gitee
class sciai.common.LbfgsOptimizer(closure, weights)[source]

L-BFGS second-order optimizer, which is currently only supported in PYNATIVE_MODE.

Parameters
  • closure (Callable) – The function which gives the loss.

  • weights (list[Parameter]) – The parameter to be optimized.

Inputs:
  • options (Mapping[str, Any]) - Ref to mindspore.scipy.minimize.

Outputs:

OptimizeResults, Object holding optimization results.

Supported Platforms:

GPU CPU Ascend

Examples

>>> import mindspore as ms
>>> from mindspore import nn, ops
>>> from sciai.architecture.basic_block import NoArgNet
>>> from sciai.common import LbfgsOptimizer
>>> ms.set_seed(1234)
>>> class Net1In1Out(nn.Cell):
>>>     def __init__(self):
>>>         super().__init__()
>>>         self.dense1 = nn.Dense(2, 1)
>>>     def construct(self, x):
>>>         return self.dense1(x).sum()
>>> net = Net1In1Out()
>>> x = ops.ones((3, 2), ms.float32)
>>> cell = NoArgNet(net, x)
>>> optim_lbfgs = LbfgsOptimizer(cell, list(cell.trainable_params()))
>>> res = optim_lbfgs.construct(options=dict(maxiter=None, gtol=1e-6))
>>> print(res.x)
[0.00279552 0.00540159  0.        ]