New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
optimize.approx_fprime & jacobians #2993
Comments
…enting vectorized version.
f is supposed to return a scalar, see docstring From what I remember when I looked at the function last time, everything worked as expected. |
Yes, it works correctly. It evaluates only gradients, however, not Jacobians. |
Can be seen as a documentation issue. From reading the docstring of The docstring of
On a related note, I wonder is there's a pressing reason for
|
Ok, yep, the documentation is a bit confusing here. @MartinSavko: in principle it would be OK to extend |
At the present time It would be better to expose |
Based on the comments, looks like the doc fix is a good first issue. |
So it looks like So the question is, I could (a) make |
It might be better to keep
It should be fairly straightforward to allow
|
One thing to keep in mind if someone's willing to work on finite differencing: it might be better to separate scalar derivatives, gradients of vector functions and jacobians early on because of broadcasting. |
Hello,
While trying to run scipy.optimize.check_grad function in the following code
!/usr/bin/python
'''Fitting the function y = c + r*sin(phi + alpha)'''
from math import sin, cos, pi
import scipy.optimize as op
import numpy as np
def f(als, c=1., r=1., phi=15*pi/180.):
return np.array([y(a, c, r, phi) for a in als])
def y(a, c=1., r=1., phi=15*pi/180.):
return c + r * sin(phi + a)
def y_grad(als, c=1., r=1., phi=15*pi/180):
return np.array([y_prime(a, c, r, phi) for a in als])
def y_prime(a, c=1., r=1., phi=15*pi/180):
return -r * cos(phi + a)
def diffsquares(y_exp, y_fit):
return (y_exp - y_fit)**2
step = 2_pi / 360.
a = np.arange(0., 2_pi + step, step)
result = op.check_grad(f, y_grad, a)
print 'result', result
on Ubuntu 12.04 (scipy version '0.9.0') I get the following error:
Traceback (most recent call last):
File "./fit.py", line 28, in
result = op.check_grad(f, y_grad, a)
File "/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 382, in check_grad
return sqrt(sum((grad(x0,_args)-approx_fprime(x0,func,_epsilon,_args))*2))
File "/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 377, in approx_fprime
grad[k] = (f(((xk+ei,)+args)) - f0)/epsilon
ValueError: setting an array element with a sequence.
looking in the source code I think there is an obvious typo there, the numerator is not indexed, I have checked the code on gitHub and the bug seems to be still present (line 610 https://github.com/scipy/scipy/blob/master/scipy/optimize/optimize.py) .
line 610
grad[k] = (f(*((xk + d,) + args)) - f0) / d[k]
should probably read
grad[k] = (f(*((xk + d,) + args))[k] - f0[k]) / d[k]
Cheers,
Martin
The text was updated successfully, but these errors were encountered: