def hess(self, x, z=None, **kwargs): """Return the dense Hessian of the objective at x.""" if z is None: z = np.zeros(self.ncon) xz = np.concatenate((x, z)) H = adolc.hessian(self._lag_trace_id, xz) return H[:self.nvar, :self.nvar]
def hessLag_adolc(x, lagrange, obj_factor, flag, user_data=None): if flag: result = (given['rind'], given['cind']) else: result = np.ravel(adolc.hessian(3, x)[given['rind'], given['cind']], order="C") return result
def __call__(self, x0, lagrange, obj_factor, flag, user_data = None): if flag: return (self.rind, self.cind) else: x = np.hstack([x0,lagrange,obj_factor]) result = adolc.hessian(lID,x) result1 = result[:nvar,:nvar] result = None result = sps.triu(result1,format='coo') return result.data
def __init__(self, x0, lagrange, obj_factor): x = np.hstack([x0,lagrange,obj_factor]) result = adolc.hessian(lID,x) print 'hess initialized' result1 = result[:nvar,:nvar] result = None result = sps.triu(result1,format='coo') result1 = None self.rind = result.row self.cind = result.col self.values = result.data # Only keep hess values w/ respect to x, not lagrange/obj_factor self.nnz = result.nnz
def ppRpWpW_adolc(self, sim, W, area): tag = self.ppRpWpW_tag if not self.ppRpWpW_traced: aW = adouble(W.flatten(order='F')) aarea = adouble(area) adolc.trace_on(tag) adolc.independent(aW) aW3 = np.reshape(aW, W.shape, order='F') pRpW = self.pRpW_adolc(sim, aW3, area) pRpW.flatten(order='F') adolc.dependent(pRpW) adolc.trace_off() return adolc.hessian(self.ppRpWpW_tag, W.flatten(order='F'))
def hessian(self,x): return adolc.hessian(self.tape_number, np.ravel(x))\ .reshape(x.shape\ + x.shape)
adolc.independent(x) y = f(x) adolc.dependent(y) adolc.trace_off() # point at which the derivatives should be evaluated x = random((N,D)) print '\n\n' print 'Sympy function = function check (should be almost zero)' print f(x) - sym_f(x) print '\n\n' print 'Sympy vs Hand Derived Gradient check (should be almost zero)' print df(x) - sym_df(x) print 'Sympy vs Ad Derived Gradient check (should be almost zero)' print adolc.gradient(0, numpy.ravel(x)).reshape(x.shape) - sym_df(x) print '\n\n' print 'Sympy vs Hand Derived Hessian check (should be almost zero)' print ddf(x) - sym_ddf(x) print 'Sympy vs Ad Derive Hessian check (should be almost zero)' print adolc.hessian(0, numpy.ravel(x)).reshape(x.shape + x.shape) - sym_ddf(x)
def hessianA_taped(self, XP): return adolc.hessian(self.adolcID, XP)
adolc.independent(x) y = f(x) adolc.dependent(y) adolc.trace_off() # point at which the derivatives should be evaluated x = random((N,D)) print('\n\n') print('Sympy function = function check (should be almost zero)') print(f(x) - sym_f(x)) print('\n\n') print('Sympy vs Hand Derived Gradient check (should be almost zero)') print(df(x) - sym_df(x)) print('Sympy vs Ad Derived Gradient check (should be almost zero)') print(adolc.gradient(0, numpy.ravel(x)).reshape(x.shape) - sym_df(x)) print('\n\n') print('Sympy vs Hand Derived Hessian check (should be almost zero)') print(ddf(x) - sym_ddf(x)) print('Sympy vs Ad Derive Hessian check (should be almost zero)') print(adolc.hessian(0, numpy.ravel(x)).reshape(x.shape + x.shape) - sym_ddf(x))
def hessian(self, x): return adolc.hessian(0,x)
trace_on(0) for n in range(N): for m in range(N): independent(aA[n,m]) aC = inv(aA)[0] ay = trace(aC) dependent(ay) trace_off() adolc_num_locations.append(tapestats(0)['NUM_LOCATIONS']) adolc_num_operations.append(tapestats(0)['NUM_OPERATIONS']) t_end = time() adolc_taping_times.append(t_end-t_start) t_start = time() H1 = adolc.hessian(0,A.ravel()) t_end = time() print('adolc needs %0.6f seconds'%(t_end - t_start)) adolc_times.append(t_end-t_start) # with ALGOPY t_start = time() cg = CGraph() FA = zeros((2,N**2,N,N)) for n in range(N): for m in range(N): FA[0,n*N+m,:,:] = A[:,:] FA[1,n*N+m,n,m] = 1.
def dense_hess(self, x, z, **kwargs): "Return the Hessian of the objective at x in dense format." return adolc.hessian(self._obj_trace_id, x)
def hessian(self, x): return adolc.hessian(0, x)
trace_on(0) for n in range(N): for m in range(N): independent(aA[n, m]) aC = inv(aA)[0] ay = trace(aC) dependent(ay) trace_off() adolc_num_locations.append(tapestats(0)['NUM_LOCATIONS']) adolc_num_operations.append(tapestats(0)['NUM_OPERATIONS']) t_end = time() adolc_taping_times.append(t_end - t_start) t_start = time() H1 = adolc.hessian(0, A.ravel()) t_end = time() print('adolc needs %0.6f seconds' % (t_end - t_start)) adolc_times.append(t_end - t_start) # with ALGOPY t_start = time() cg = CGraph() FA = zeros((2, N**2, N, N)) for n in range(N): for m in range(N): FA[0, n * N + m, :, :] = A[:, :] FA[1, n * N + m, n, m] = 1.