I have a scalar valued function that I’d like to minimize, but, by default, it’s not readily vectorized. In particular, it depends parametrically on the solution of an ODE, and vaguely looks like:
def f(x, ode_soln): ... val = spicy.integrate.quad(lambda t: sin(x * t) * ode_soln.sol(t),0,1) return val By default, if I try to do spicy.optimize.minimize(lambda x: f(x,ode_soln)) I get an error that indicates that minimize thinks f accepts arrays of data. I can remedy this by writing a little looped version, def f_vec(xvec, ode_soln): fvals = np.zeros_like(xvec) for j in range(xvec.size): fvals[j] = f(xvec[j], ode_soln) return fvals but I’m wondering if: 1. Is there a smarter/more elegant solution to handling the vectorized input? 2. Is there a way to just tell minimize that it’s going to have to evaluate f one point at a time, rather than writing some other function? -gideon _______________________________________________ SciPy-User mailing list [hidden email] https://mail.python.org/mailman/listinfo/scipy-user |
On Tue, May 2, 2017 at 9:22 PM, Gideon Simpson <[hidden email]> wrote: I have a scalar valued function that I’d like to minimize, but, by default, it’s not readily vectorized. In particular, it depends parametrically on the solution of an ODE, and vaguely looks like: minimize() takes two arguments: a callable function that takes an array of values to be adjusted, and an array of starting values. Presumably, `x` is the array of values that you would like optimized, but you have to provide starting values. I don't think you need the lambda, but you might want something like: scipy.optimize.minimize(f, x0, args=(ode_soln,)) where x0 is an array of starting values. I can remedy this by writing a little looped version, It does handle vectorized input, or perhaps I'm not understanding your question.
No, or well it depends what you mean by "one point at a time". The objective function provided should take an array of candidate values for the parameters and return either an array to be minimized in the least-squares sense or the scalar cost value. Hope that helps, --Matt _______________________________________________ SciPy-User mailing list [hidden email] https://mail.python.org/mailman/listinfo/scipy-user |
Free forum by Nabble | Edit this page |