optimize.minimize - help me understand arrays as variables
I'm trying to use scipy.optimize.minimize. I've tried multiple "multivariate" methods that don't seem to actually take multivariate data and derivatives. Can someone tell me how I can make the multivariate part of the solver actually work? Here's an example: My main function the following (typical length for N is 3): input guess is a x0=np.array([1,2,3]) the optimization function returns: def calc_f3d(...): f3d = np.ones((np.max([3,N]),1) .... do some assignments to f3d[row,0] .... return np.linalg.norm(f3d) # numpy.array that's 3x1 The jacobian returns a Nx3 matrix: def jacob3d(...): df = np.ones((np.max([3,N]),3)) ... do some assignments to df[row,col] return df # note numpy.array that's 3x3 The optimize call is: OptimizeResult = optimize.minimize( fun=tdcalc.calc_f3d, x0=ract, jac=tdcalc.jacob3d, method='BFGS', args=(operdata,), tol=1.0e-8, options={'maxiter': 40000, 'xtol':1e-8}) <--- ops change based on whether using Newton-CG or BFGS When I use BFGS, I get: Traceback (most recent call last): File "./tdoa_calc.py", line 664, in <module> options={'maxiter': 40000, 'gtol':1e-8}) File "/usr/lib64/python2.7/site-packages/scipy/optimize/_minimize.py", line 348, in minimize return _minimize_bfgs(fun, x0, args, jac, callback, **options) File "/usr/lib64/python2.7/site-packages/scipy/optimize/optimize.py", line 779, in _minimize_bfgs old_fval, old_old_fval) File "/usr/lib64/python2.7/site-packages/scipy/optimize/linesearch.py", line 95, in line_search_wolfe1 c1=c1, c2=c2, amax=amax, amin=amin, xtol=xtol) File "/usr/lib64/python2.7/site-packages/scipy/optimize/linesearch.py", line 147, in scalar_search_wolfe1 alpha1 = min(1.0, 1.01*2*(phi0 - old_phi0)/derphi0) ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() When I use Newton-CG, I get: Traceback (most recent call last): File "./tdoa_calc.py", line 655, in <module> options={'maxiter': 40000, 'xtol':1e-8}) File "/usr/lib64/python2.7/site-packages/scipy/optimize/_minimize.py", line 351, in minimize **options) File "/usr/lib64/python2.7/site-packages/scipy/optimize/optimize.py", line 1320, in _minimize_newtoncg eta = numpy.min([0.5, numpy.sqrt(maggrad)]) File "/usr/lib64/python2.7/site-packages/numpy/core/fromnumeric.py", line 1982, in amin out=out, keepdims=keepdims) File "/usr/lib64/python2.7/site-packages/numpy/core/_methods.py", line 14, in _amin out=out, keepdims=keepdims) ValueError: setting an array element with a sequence.
`calc_f3d` needs to return a single number, the overall 'cost'. On 12 January 2015 at 11:55, KURT PETERS <peterskurt@msn.com> wrote:
I'm trying to use scipy.optimize.minimize. I've tried multiple "multivariate" methods that don't seem to actually take multivariate data and derivatives. Can someone tell me how I can make the multivariate part of the solver actually work?
Here's an example: My main function the following (typical length for N is 3):
input guess is a x0=np.array([1,2,3]) the optimization function returns: def calc_f3d(...): f3d = np.ones((np.max([3,N]),1) .... do some assignments to f3d[row,0] .... return np.linalg.norm(f3d) # numpy.array that's 3x1
The jacobian returns a Nx3 matrix: def jacob3d(...): df = np.ones((np.max([3,N]),3)) ... do some assignments to df[row,col] return df # note numpy.array that's 3x3
The optimize call is: OptimizeResult = optimize.minimize( fun=tdcalc.calc_f3d, x0=ract, jac=tdcalc.jacob3d, method='BFGS', args=(operdata,), tol=1.0e-8, options={'maxiter': 40000, 'xtol':1e-8}) <--- ops change based on whether using Newton-CG or BFGS
When I use BFGS, I get: Traceback (most recent call last): File "./tdoa_calc.py", line 664, in <module> options={'maxiter': 40000, 'gtol':1e-8}) File "/usr/lib64/python2.7/site-packages/scipy/optimize/_minimize.py", line 348, in minimize return _minimize_bfgs(fun, x0, args, jac, callback, **options) File "/usr/lib64/python2.7/site-packages/scipy/optimize/optimize.py", line 779, in _minimize_bfgs old_fval, old_old_fval) File "/usr/lib64/python2.7/site-packages/scipy/optimize/linesearch.py", line 95, in line_search_wolfe1 c1=c1, c2=c2, amax=amax, amin=amin, xtol=xtol) File "/usr/lib64/python2.7/site-packages/scipy/optimize/linesearch.py", line 147, in scalar_search_wolfe1 alpha1 = min(1.0, 1.01*2*(phi0 - old_phi0)/derphi0) ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
When I use Newton-CG, I get: Traceback (most recent call last): File "./tdoa_calc.py", line 655, in <module> options={'maxiter': 40000, 'xtol':1e-8}) File "/usr/lib64/python2.7/site-packages/scipy/optimize/_minimize.py", line 351, in minimize **options) File "/usr/lib64/python2.7/site-packages/scipy/optimize/optimize.py", line 1320, in _minimize_newtoncg eta = numpy.min([0.5, numpy.sqrt(maggrad)]) File "/usr/lib64/python2.7/site-packages/numpy/core/fromnumeric.py", line 1982, in amin out=out, keepdims=keepdims) File "/usr/lib64/python2.7/site-packages/numpy/core/_methods.py", line 14, in _amin out=out, keepdims=keepdims) ValueError: setting an array element with a sequence.
_______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
-- _____________________________________ Dr. Andrew Nelson _____________________________________
participants (2)
-
Andrew Nelson
-
KURT PETERS