[Numpy-discussion] optimize.fmin_cg bug

Nadav Horesh nadavh at visionsense.com
Fri Dec 29 00:02:07 CST 2006


optimize.fmin_cg fails when the function to be minimized returns a numpy scalar:

>>> from numpy import *
>>> from scipy import optimize
>>> f = lambda x: exp(x)-x
>>> df = lambda x: exp(x)-1.0
>>> scipy.optimize.fmin(f, [0.2])

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    scipy.optimize.fmin(f, [0.2])
NameError: name 'scipy' is not defined
>>> optimize.fmin(f, [0.2])
Optimization terminated successfully.
         Current function value: 1.000000
         Iterations: 15
         Function evaluations: 30
array([  3.88578059e-16])
>>> optimize.fmin_cg(f, [0.2], df)

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    optimize.fmin_cg(f, [0.2], df)
  File "C:\Python25\Lib\site-packages\scipy\optimize\optimize.py", line 855, in fmin_cg
    old_fval_backup,old_old_fval_backup)
  File "C:\Python25\Lib\site-packages\scipy\optimize\optimize.py", line 471, in line_search
    phi0, derphi0, c1, c2)
  File "C:\Python25\Lib\site-packages\scipy\optimize\optimize.py", line 359, in zoom
    a_j = _cubicmin(a_lo, phi_lo, derphi_lo, a_hi, phi_hi, a_rec, phi_rec)
  File "C:\Python25\Lib\site-packages\scipy\optimize\optimize.py", line 309, in _cubicmin
    [A,B] = numpy.dot([[dc**2, -db**2],[-dc**3, db**3]],[fb-fa-C*db,fc-fa-C*dc])
ValueError: objects are not aligned

# Here is the cure: Make f return a python float

>>> f = lambda x: float(exp(x)-x)
>>> optimize.fmin_cg(f, [0.2], df)
Optimization terminated successfully.
         Current function value: 1.000000
         Iterations: 2
         Function evaluations: 14
         Gradient evaluations: 8
array([ -8.15285339e-14])


I had this error with scipy 0.5.2 and scipy from svn.

  Nadav.


More information about the Numpy-discussion mailing list