optimization - Calculate Hessian & Gradient of a function using Python -
i'm implementing unconstrained minimization of 2d functions:
- for search direction, i'm using both steepest descent , newton descent.
- for step size, i'm using backtracking line search algorithm
the code simple (mainly future reference might find helpful): function optimize(f, df, hess_f, method)
looks this:
# prepare contour plot fig = pt.figure() xmesh, ymesh = np.mgrid[-5:5:100j,-5:5:100j] fmesh = f(np.array([xmesh, ymesh])) pt.axis("equal") pt.contour(xmesh, ymesh, fmesh) pt.contour(xmesh, ymesh, fmesh, 250) # initial guess + first update of search direction guesses = [np.array([5, 0.1])] x = guesses[-1] s = search_direction(df, hess_f, x, method) not_done = true while not_done: # calculate step size using backtracking line search: alpha_opt = backtracking_alpha(f, df, 0.5, 0.5, x, s) # update step next_guess = x + alpha_opt * s guesses.append(next_guess) # plot current step our updating contour graph: it_array = np.array(guesses) pt.plot(it_array.t[-2], it_array.t[-1], "-") # check stopping condition if (np.linalg.norm(guesses[-2] - guesses[-1]) < 0.0001): not_done = false # prepare next guess according search direction method x = guesses[-1] s = search_direction(df, hess_f, x, method) pt.show() print("method {2} converged to: {0}, in {1} iterations".format(x, len(guesses), method))
it works ok: example, function
we method 0 converged to: [ 1.37484167e-04 -8.24905001e-06], in 22 iterations method 1 converged to: [ 0. 0.], in 3 iterations
the thing bothers me if fact optimization functions explicitly requires not function i'm minimizing (which of course makes sense), gradient , hessian. right i'm "hard coding" these functions, so:
def f2(x): return 100*(x[0]-3)**2 + (x[1]-1)**2
def df2(x): return np.array([200*(x[0]-3), 2*(x[1]-1)])
def hess_f2(x): return np.array([200, 0, 0, 2]).reshape((2,2))
so question is, "pythonic" way generate functions calculate gradient , hessian of input function function, in way fit above implementation? i'm guessing it's simple main experience python scripting, haven't yet done this. thanks!
Comments
Post a Comment