5

I am using the Scipy optimization module, specifically fmin_tnc and fmin_l_bfgs_b. However, I am receiving the message "IndexError: invalid index to scalar variable" when using either one.

What is the cause of this error?

And what is the meaning of this error message?

My practice code:

def f01(para):
    para1, para2 = para
    return 1+ (para1 -1)**2 + (para2 -2)**2

para0 = np.array([10, 10]) 
mybounds = [(-40,30),(-20,15)]

opt.fmin_l_bfgs_b(f01, para0,  bounds = mybounds )

Which returns:

Traceback (most recent call last):
  File "C:\Python27\mystuff\practice_optimize01.py", line 78, in <module>
    opt.fmin_l_bfgs_b(f01, para0,  bounds = mybounds )

  File "C:\Python27\lib\site-packages\scipy\optimize\lbfgsb.py", line 174, in fm
in_l_bfgs_b
**opts)

  File "C:\Python27\lib\site-packages\scipy\optimize\lbfgsb.py", line 294, in _m
 inimize_lbfgsb
    f, g = func_and_grad(x)

  File "C:\Python27\lib\site-packages\scipy\optimize\lbfgsb.py", line 249, in fu
nc_and_grad
    f = fun(x, *args)

  File "C:\Python27\lib\site-packages\scipy\optimize\optimize.py", line 55, in _
_call__
    self.jac = fg[1]
IndexError: invalid index to scalar variable.

Python 2.7.3, 32-bit. Numpy 1.6.2. Scipy 0.11.0b1. Windows XP and Vista.

1 Answer 1

13

fmin_l_bfgs_b expects that your function returns the function value and the gradient. You return only the function value.

If you only return the function value and don't provide a gradient, then you need to set approx_grad=True so that fmin_l_bfgs_b uses a numerical approximation to it.

See the description of the options in the docstring.

From my reading of the documentation, fmin_tnc has the same pattern, and same problem in your case.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this. Although in the documentation, I did not think of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.