[SciPy-user] Cannot get optimize.fmin_bfgs to work

josef.pktd@gmai... josef.pktd@gmai...
Tue Mar 3 05:48:24 CST 2009


On Tue, Mar 3, 2009 at 4:15 AM, Williamson, Ross
<Ross.Williamson@usap.gov> wrote:
> Hi All
>
> I'm trying to get optimize.fmin_bfgs to work with no luck - I have an x,y array of numbers (about 100) to which I'm trying to fit a curve to. Both the auto fprime calculator and my own Jacbian do not work:
>
> ValueError: setting an array element with a sequence.
>
> I'm presuming I'm doing something really silly but I cannot find a simple example on the web (The one using the Rosenbrock function does not help).
>
> The reason for doing this is that I want to progress to the bounded version of this function as leastsq works (but does not allow for bounded data).
>
> Please find code below:
>
> from numpy import zeros
> from scipy import optimize
> def fitfunc(p, x):
>
>   a_white = p[0]
>   a_pink = p[1]
>   a_red = p[2]
>   a_phot = p[3]
>   fk = p[4]  # photon_noise roll-off ~ 11.4 Hz mean 2008 season
>   alpha2 = p[5]
>
>   #result = wlevel*(1.+(f_knee/x)^alpha)
>   result = a_white + a_pink * x ** (-1.0) + a_red * x ** (-1.0 * alpha2) + a_phot / (1. + x ** 2 / fk ** 2)
>
>   return result
>
> def fitgrad(p,x,y,err):
>   #Calculate Jacobian
>   J = zeros(len(p),float)
>   J[0] = 1.0
>   J[1] = 1.0/x
>   J[2] = x ** (-1.0 * alpha2)
>   J[3] = 1.0/(1. + x ** 2 / fk ** 2)
>   J[4] = (2 * a_phot * x**2)/(fk**3 * ((x**2/a_phot**2) + 1)**2)
>   J[5] = a_red * x **(-1.0* alpha2) * log(x)
>
>   return J
>
> def errorfunc(p,x,y,err):
>    err_r = (y-fitfunc(p,x))/err
>    print err_r
>    return err_r
>
> def test(x,y,noise):
>
>    pinit = [0.00125645, 0.00062822510, 0.00062822510,0.00062822510, 11.0, 2.0]
>
>    out = optimize.leastsq(errorfunc,pinit,args=(x,y,noise), full_output=1)
>    out2 = optimize.fmin_bfgs(errorfunc,pinit,fprime=fitgrad,args=(x,y,noise))


fmin_bfgs and other optimization functions  expect errorfunc to be the
objective function, which returns a single value

e.g. define func = np.sum(errorfunc**2)

and then try optimize.fmin_bfgs(func,pinit,fprime=fitgrad,args=(x,y,noise))

this is different from leastsq, where taking the square and summing is
done inside of leastsq

Josef

>    print out[0]
>    print out[1]
>
>    return out
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>


More information about the SciPy-user mailing list