scipy 1.8.0 Pypi GitHub Homepage
Other Docs
NotesParametersReturnsBackRef
fmin_cg(f, x0, fprime=None, args=(), gtol=1e-05, norm=inf, epsilon=1.4901161193847656e-08, maxiter=None, full_output=0, disp=1, retall=0, callback=None)

Notes

This conjugate gradient algorithm is based on that of Polak and Ribiere .

Conjugate gradient methods tend to work better when:

  1. f has a unique global minimizing point, and no local minima or other stationary points,

  2. f is, at least locally, reasonably well approximated by a quadratic function of the variables,

  3. f is continuous and has a continuous gradient,

  4. fprime is not too large, e.g., has a norm less than 1000,

  5. The initial guess, :None:None:`x0`, is reasonably close to f 's global minimizing point, :None:None:`xopt`.

Parameters

f : callable, ``f(x, *args)``

Objective function to be minimized. Here x must be a 1-D array of the variables that are to be changed in the search for a minimum, and :None:None:`args` are the other (fixed) parameters of f.

x0 : ndarray

A user-supplied initial estimate of :None:None:`xopt`, the optimal value of x. It must be a 1-D array of values.

fprime : callable, ``fprime(x, *args)``, optional

A function that returns the gradient of f at x. Here x and :None:None:`args` are as described above for f. The returned value must be a 1-D array. Defaults to None, in which case the gradient is approximated numerically (see :None:None:`epsilon`, below).

args : tuple, optional

Parameter values passed to f and fprime . Must be supplied whenever additional fixed parameters are needed to completely specify the functions f and fprime .

gtol : float, optional

Stop when the norm of the gradient is less than :None:None:`gtol`.

norm : float, optional

Order to use for the norm of the gradient ( -np.Inf is min, np.Inf is max).

epsilon : float or ndarray, optional

Step size(s) to use when fprime is approximated numerically. Can be a scalar or a 1-D array. Defaults to sqrt(eps) , with eps the floating point machine precision. Usually sqrt(eps) is about 1.5e-8.

maxiter : int, optional

Maximum number of iterations to perform. Default is 200 * len(x0) .

full_output : bool, optional

If True, return :None:None:`fopt`, :None:None:`func_calls`, :None:None:`grad_calls`, and :None:None:`warnflag` in addition to :None:None:`xopt`. See the Returns section below for additional information on optional return values.

disp : bool, optional

If True, return a convergence message, followed by :None:None:`xopt`.

retall : bool, optional

If True, add to the returned values the results of each iteration.

callback : callable, optional

An optional user-supplied function, called after each iteration. Called as callback(xk) , where xk is the current value of :None:None:`x0`.

Returns

xopt : ndarray

Parameters which minimize f, i.e., f(xopt) == fopt .

fopt : float, optional

Minimum value found, f(xopt). Only returned if :None:None:`full_output` is True.

func_calls : int, optional

The number of function_calls made. Only returned if :None:None:`full_output` is True.

grad_calls : int, optional

The number of gradient calls made. Only returned if :None:None:`full_output` is True.

warnflag : int, optional

Integer value with warning status, only returned if :None:None:`full_output` is True.

allvecs : list of ndarray, optional

List of arrays, containing the results at each iteration. Only returned if :None:None:`retall` is True.

Minimize a function using a nonlinear conjugate gradient algorithm.

See Also

minimize

common interface to all :None:None:`scipy.optimize` algorithms for unconstrained and constrained minimization of multivariate functions. It provides an alternative way to call fmin_cg , by specifying method='CG' .

Examples

Example 1: seek the minimum value of the expression a*u**2 + b*u*v + c*v**2 + d*u + e*v + f for given values of the parameters and an initial guess (u, v) = (0, 0) .

>>> args = (2, 3, 7, 8, 9, 10)  # parameter values
... def f(x, *args):
...  u, v = x
...  a, b, c, d, e, f = args
...  return a*u**2 + b*u*v + c*v**2 + d*u + e*v + f
... def gradf(x, *args):
...  u, v = x
...  a, b, c, d, e, f = args
...  gu = 2*a*u + b*v + d # u-component of the gradient
...  gv = b*u + 2*c*v + e # v-component of the gradient
...  return np.asarray((gu, gv))
... x0 = np.asarray((0, 0)) # Initial guess.
... from scipy import optimize
... res1 = optimize.fmin_cg(f, x0, fprime=gradf, args=args) Optimization terminated successfully. Current function value: 1.617021 Iterations: 4 Function evaluations: 8 Gradient evaluations: 8
>>> res1
array([-1.80851064, -0.25531915])

Example 2: solve the same problem using the :None:None:`minimize` function. (This :None:None:`myopts` dictionary shows all of the available options, although in practice only non-default values would be needed. The returned value will be a dictionary.)

>>> opts = {'maxiter' : None,    # default value.
...  'disp' : True, # non-default value.
...  'gtol' : 1e-5, # default value.
...  'norm' : np.inf, # default value.
...  'eps' : 1.4901161193847656e-08} # default value.
... res2 = optimize.minimize(f, x0, jac=gradf, args=args,
...  method='CG', options=opts) Optimization terminated successfully. Current function value: 1.617021 Iterations: 4 Function evaluations: 8 Gradient evaluations: 8
>>> res2.x  # minimum found
array([-1.80851064, -0.25531915])
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

scipy.optimize._optimize.fmin_cg

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /scipy/optimize/_optimize.py#1395
type: <class 'function'>
Commit: