supergrad.utils.optimize.scipy_minimize#
- supergrad.utils.optimize.scipy_minimize(fun, x0, args=(), method=None, jac='jax', hess=None, hessp=None, bounds=None, constraints=(), tol=None, callback=None, options=None, *, jit: bool = True, fwd_ad: bool = False, logging: bool = False)[source]#
Minimization of scalar function of one or more variables.
SuperGrad-supported implementation of
scipy.optimize._minimize.minimize().Original docstring below.
- Parameters:
fun (callable) –
The objective function to be minimized:
fun(x, *args) -> float
where
xis a 1-D array with shape (n,) andargsis a tuple of the fixed parameters needed to completely specify the function.Suppose the callable has signature
f0(x, *my_args, **my_kwargs), wheremy_argsandmy_kwargsare required positional and keyword arguments. Rather than passingf0as the callable, wrap it to accept onlyx; e.g., passfun=lambda x: f0(x, *my_args, **my_kwargs)as the callable, wheremy_args(tuple) andmy_kwargs(dict) have been gathered before invoking this function.x0 (ndarray, shape (n,)) – Initial guess. Array of real elements of size (n,), where
nis the number of independent variables.args (tuple, optional) – Extra arguments passed to the objective function and its derivatives (fun, jac and hess functions).
method (str or callable, optional) –
Type of solver. Should be one of
’Nelder-Mead’ (see here)
’Powell’ (see here)
’CG’ (see here)
’BFGS’ (see here)
’Newton-CG’ (see here)
’L-BFGS-B’ (see here)
’TNC’ (see here)
’COBYLA’ (see here)
’COBYQA’ (see here)
’SLSQP’ (see here)
’trust-constr’(see here)
’dogleg’ (see here)
’trust-ncg’ (see here)
’trust-exact’ (see here)
’trust-krylov’ (see here)
custom - a callable object, see below for description.
If not given, chosen to be one of
BFGS,L-BFGS-B,SLSQP, depending on whether or not the problem has constraints or bounds.jac ({callable, '2-point', '3-point', 'cs', bool}, optional) –
Method for computing the gradient vector. Only for CG, BFGS, Newton-CG, L-BFGS-B, TNC, SLSQP, dogleg, trust-ncg, trust-krylov, trust-exact and trust-constr. If it is a callable, it should be a function that returns the gradient vector:
jac(x, *args) -> array_like, shape (n,)
where
xis an array with shape (n,) andargsis a tuple with the fixed parameters. If jac is a Boolean and is True, fun is assumed to return a tuple(f, g)containing the objective function and the gradient. Methods ‘Newton-CG’, ‘trust-ncg’, ‘dogleg’, ‘trust-exact’, and ‘trust-krylov’ require that either a callable be supplied, or that fun return the objective and gradient. If None or False, the gradient will be estimated using 2-point finite difference estimation with an absolute step size. Alternatively, the keywords {‘2-point’, ‘3-point’, ‘cs’} can be used to select a finite difference scheme for numerical estimation of the gradient with a relative step size. These finite difference schemes obey any specified bounds.hess ({callable, '2-point', '3-point', 'cs', HessianUpdateStrategy}, optional) –
Method for computing the Hessian matrix. Only for Newton-CG, dogleg, trust-ncg, trust-krylov, trust-exact and trust-constr. If it is callable, it should return the Hessian matrix:
hess(x, *args) -> {LinearOperator, spmatrix, array}, (n, n)
where
xis a (n,) ndarray andargsis a tuple with the fixed parameters. The keywords {‘2-point’, ‘3-point’, ‘cs’} can also be used to select a finite difference scheme for numerical estimation of the hessian. Alternatively, objects implementing the HessianUpdateStrategy interface can be used to approximate the Hessian. Available quasi-Newton methods implementing this interface are:BFGS
SR1
Not all of the options are available for each of the methods; for availability refer to the notes.
hessp (callable, optional) –
Hessian of objective function times an arbitrary vector p. Only for Newton-CG, trust-ncg, trust-krylov, trust-constr. Only one of hessp or hess needs to be given. If hess is provided, then hessp will be ignored. hessp must compute the Hessian times an arbitrary vector:
hessp(x, p, *args) -> ndarray shape (n,)
where
xis a (n,) ndarray,pis an arbitrary vector with dimension (n,) andargsis a tuple with the fixed parameters.bounds (sequence or Bounds, optional) –
Bounds on variables for Nelder-Mead, L-BFGS-B, TNC, SLSQP, Powell, trust-constr, COBYLA, and COBYQA methods. There are two ways to specify the bounds:
Instance of Bounds class.
Sequence of
(min, max)pairs for each element in x. None is used to specify no bound.
constraints ({Constraint, dict} or List of {Constraint, dict}, optional) –
Constraints definition. Only for COBYLA, COBYQA, SLSQP and trust-constr.
Constraints for ‘trust-constr’ and ‘cobyqa’ are defined as a single object or a list of objects specifying constraints to the optimization problem. Available constraints are:
LinearConstraint
NonlinearConstraint
Constraints for COBYLA, SLSQP are defined as a list of dictionaries. Each dictionary with fields:
- typestr
Constraint type: ‘eq’ for equality, ‘ineq’ for inequality.
- funcallable
The function defining the constraint.
- jaccallable, optional
The Jacobian of fun (only for SLSQP).
- argssequence, optional
Extra arguments to be passed to the function and Jacobian.
Equality constraint means that the constraint function result is to be zero whereas inequality means that it is to be non-negative. Note that COBYLA only supports inequality constraints.
tol (float, optional) – Tolerance for termination. When tol is specified, the selected minimization algorithm sets some relevant solver-specific tolerance(s) equal to tol. For detailed control, use solver-specific options.
options (dict, optional) –
A dictionary of solver options. All methods except TNC accept the following generic options:
- maxiterint
Maximum number of iterations to perform. Depending on the method each iteration may use several function evaluations.
For TNC use maxfun instead of maxiter.
- dispbool
Set to True to print convergence messages.
For method-specific options, see
show_options().callback (callable, optional) –
A callable called after each iteration.
All methods except TNC, SLSQP, and COBYLA support a callable with the signature:
callback(intermediate_result: OptimizeResult)
where
intermediate_resultis a keyword parameter containing an OptimizeResult with attributesxandfun, the present values of the parameter vector and objective function. Note that the name of the parameter must beintermediate_resultfor the callback to be passed an OptimizeResult. These methods will also terminate if the callback raisesStopIteration.All methods except trust-constr (also) support a signature like:
callback(xk)
where
xkis the current parameter vector.Introspection is used to determine which of the signatures above to invoke.
- Returns:
res – The optimization result represented as a
OptimizeResultobject. Important attributes are:xthe solution array,successa Boolean flag indicating if the optimizer exited successfully andmessagewhich describes the cause of the termination. See OptimizeResult for a description of other attributes.- Return type:
OptimizeResult
References