def calc_jacobian(*args, **kwargs): try: tag = kwargs["tag"] except: tag = 0 try: sparse = kwargs["sparse"] except: sparse = True if sparse: try: shape = kwargs["shape"] except: raise ValueError( "'shape' should be passed to calculate sparse jacobian!") options = np.array([0, 0, 0, 0], dtype=int) result = ad.colpack.sparse_jac_no_repeat(tag, *args, options=options) nnz = result[0] ridx = result[1] cidx = result[2] values = result[3] assert nnz > 0 jac = sp.csr_matrix((values, (ridx, cidx)), shape=shape) jac = jac.toarray() else: jac = ad.jacobian(tag, *args) return jac
def lm_jac(x,f,jac,p): tmpjac = adolc.jacobian(adolcID,x) m = tmpjac.shape[0] n = tmpjac.shape[1] for i in range(m): jac[i][:] = tmpjac[i,:] f[:] = adolc.function(adolcID,x) return
def adjoint_solve(self, data, weight_sens=False): # Evaluate the jacobian of residuals w.r.t. states and augmentation field ad.trace_on(1) ad_states = ad.adouble(self.states) ad_beta = ad.adouble(self.beta) ad.independent(ad_states) ad.independent(ad_beta) ad_res = self.evalResidual(ad_states, ad_beta) ad.dependent(ad_res) ad.trace_off() jacres = ad.jacobian(1, np.hstack((self.states, self.beta))) Rq = jacres[:,0:np.shape(self.states)[0]] Rb = jacres[:,np.shape(self.states)[0]:] # Obtain the jacobian of objective function w.r.t. states and augmentation field Jq, Jb = self.getObjJac(data) # Solve the discrete adjoint system to obtain sensitivity psi = np.linalg.solve(Rq.T,Jq) sens = Jb - np.matmul(Rb.T,psi) # Obtain the sensitivity of the objective function w.r.t. NN weights if weight_sens==True: d_weights = nn.nn.nn_get_weights_sens(np.asfortranarray(self.nn_params["network"]), self.nn_params["act_fn"], self.nn_params["loss_fn"], self.nn_params["opt"], np.asfortranarray(self.nn_params["weights"]), np.asfortranarray(self.features), 1, np.shape(self.beta)[0], np.asfortranarray(sens), np.asfortranarray(self.nn_params["opt_params_array"])) return d_weights else: return sens
def evalJacobian(self, states, beta_inv): # Evaluate jacobian of residuals w.r.t. states ad.trace_on(1) ad_states = ad.adouble(states) ad.independent(ad_states) ad_res = self.evalResidual(ad_states, beta_inv) ad.dependent(ad_res) ad.trace_off() return ad.jacobian(1, states)
def pRpW_adolc(self, sim, W, area): tag = self.pRpW_tag if not self.pRpW_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') aresidual = q1d.evaluate_residual(sim, aW3, area) aresidual.flatten(order='F') adolc.dependent(aresidual) adolc.trace_off() return adolc.jacobian(self.pRpW_tag, W.flatten(order='F'))
def eval_and_deriv(dev, vPort): """ Evaluates current and charge sources of a nonlinear device. vPort is a numpy vector with input voltages Returns a tuple with one vector for currents and charges and another for the jacobian. """ try: tag = dev._tag except AttributeError: create_tape(dev, vPort) tag = dev._tag fout = ad.function(tag, vPort) jac = ad.jacobian(tag, vPort) return (fout, jac)
def getObjJac(self, data): ad.trace_on(1) ad_states = ad.adouble(self.states) ad_beta = ad.adouble(self.beta) ad.independent(ad_states) ad.independent(ad_beta) ad_obj = self.getObjRaw(ad_states, data, ad_beta) ad.dependent(ad_obj) ad.trace_off() jacobj = ad.jacobian(1, np.hstack((self.states, self.beta))) Jq = jacobj[:,0:np.shape(self.states)[0]] Jb = jacobj[:,np.shape(self.states)[0]:] return Jq[0,:], Jb[0,:]
def calc_jacobian(*args, **kwargs): """Wrapper to adolc utilities for calculation of jacobian. Input: args: independent variable used to record the tape tag: tag of the record to differentiate sparse: calculate sparse jacobian. While the jacobian is calculated using sparse utilities the solution is returned as dense matrix. It is not efficient but does not matter for small jacobians. shape: shape of the jacobian matrix, required only when using sparse. """ try: tag = kwargs["tag"] except: tag = 0 try: sparse = kwargs["sparse"] except: sparse = True if sparse: try: shape = kwargs["shape"] except: raise ValueError( "'shape' should be passed to calculate sparse jacobian!") options = np.array([0, 0, 0, 0], dtype=int) result = ad.colpack.sparse_jac_no_repeat(tag, *args, options=options) nnz = result[0] ridx = result[1] cidx = result[2] values = result[3] assert nnz > 0 jac = sp.csr_matrix((values, (ridx, cidx)), shape=shape) jac = jac.toarray() else: jac = ad.jacobian(tag, *args) return jac
xlabel(r'time $t$ []') ylabel(r'measurement function $h(t,x,p,q)$') legend((meas_plot,starting_plot,correct_plot,est_plot,hplot),('measurements','initial guess','true','estimated','measurement model')) title('Parameter Estimation') savefig('parameter_estimation.png') savefig('parameter_estimation.eps') # PERFORM OED # =========== v0 = v.copy() # tape the objective function with Algopy J=adolc.jacobian(2,v)[:,:2] cg = CGraph() J0 = J.copy() J1 = zeros(shape(J)) FJ = Function(Mtc(J0,J1)) Ff = Phi(FJ) cg.independentFunctionList = [FJ] cg.dependentFunctionList = [Ff] cg.plot('testgraph.png') cg.plot('testgraph.svg') # perform steepest descent optimization vbar = inf count = 0 while numpy.linalg.norm(vbar)>10**-8:
def jac_pos(self, x, **kwargs): """Return dense Jacobian of reformulated constraints at x.""" return adolc.jacobian(self._cons_pos_trace_id, x)
def jac(self, x, **kwargs): """Return dense constraints Jacobian at x.""" return adolc.jacobian(self._con_trace_id, x)
Np = size(p) Nq = size(q) Nv = size(v) Nm = 100 ts = linspace(0, 10, Nm) # TAPING THE INTEGRATOR adolc.trace_on(1) av = adolc.adouble(v) adolc.independent(av) ax = explicit_euler(av[0], f, ts, av[:Np], av[Np:]) adolc.dependent(ax) adolc.trace_off() # COMPUTING FUNCTION AND JACOBIAN FROM THE TAPE y = adolc.zos_forward(1, v, 0) J = adolc.jacobian(1, v) x_plot = plot(ts, y, 'b') x_analytical_plot = plot(ts, phi(ts, p, q), 'b.') xp0_plot = plot(ts, J[:, 0], 'g') xp0_analytical_plot = plot(ts, phip0(ts, p, q), 'g.') xp1_plot = plot(ts, J[:, 1], 'r') xp1_analytical_plot = plot(ts, phip1(ts, p, q), 'r.') show() print J
for ns, s in enumerate(sp): if s == 1: y[ns] *= x[n] return y x = numpy.random.rand(N) adolc.trace_on(0) x = adolc.adouble(x) adolc.independent(x) y = F(x) adolc.dependent(y) adolc.trace_off() x = numpy.random.rand(N) y = F(x) y2 = adolc.function(0,x) assert numpy.allclose(y,y2) options = numpy.array([0,0,0,0],dtype=int) pat = adolc.sparse.jac_pat(0,x,options) result = adolc.colpack.sparse_jac_no_repeat(0,x,options) print adolc.jacobian(0,x) print pat print result
def jacA_taped(self, XP): return adolc.jacobian(self.adolcID, XP)
def _adolc_jac(self, x, **kwargs): "Evaluate the constraints Jacobian from the ADOL-C tape." return adolc.jacobian(self._con_trace_id, x)
def dFdp(p, q, ts, Sigma, etas): v[:Np] = p[:] return adolc.jacobian(1, v)[:, :Np]
import numpy import math import adolc # tape a function evaluation ax = numpy.array([adolc.adouble(0.) for n in range(2)]) # ay = adolc.adouble(0) adolc.trace_on(13) adolc.independent(ax) ay = numpy.sin(ax[0] + ax[1]*ax[0]) adolc.dependent(ay) adolc.trace_off() x = numpy.array([3., 7.]) y = numpy.zeros(1) adolc.tape_to_latex(13, x, y) y = adolc.function(13, x) g = adolc.gradient(13, x) J = adolc.jacobian(13, x) print('function y=', y) print('gradient g=', g) print('Jacobian J=', J)
xlabel(r'time $t$ []') ylabel(r'measurement function $h(t,x,p,q)$') legend((meas_plot, starting_plot, correct_plot, est_plot, hplot), ('measurements', 'initial guess', 'true', 'estimated', 'measurement model')) title('Parameter Estimation') savefig('parameter_estimation.png') savefig('parameter_estimation.eps') # PERFORM OED # =========== v0 = v.copy() # tape the objective function with Algopy Jtmp = adolc.jacobian(2, v)[:, :2] Jshp = shape(Jtmp) J = zeros((DM - 1, 1, Jshp[0], Jshp[1])) J[0, 0, :, :] = Jtmp cg = CGraph() FJ = Function(Mtc(J)) Ff = Phi(FJ) cg.independentFunctionList = [FJ] cg.dependentFunctionList = [Ff] #cg.plot('testgraph.png') #cg.plot('testgraph.svg') #def gradient_of_PHI(v): #""" computes grad(PHI) as needed in the steepest descent optimization"""
xlabel(r'time $t$ []') ylabel(r'measurement function $h(t,x,p,q)$') legend((meas_plot, starting_plot, correct_plot, est_plot, hplot), ('measurements', 'initial guess', 'true', 'estimated', 'measurement model')) title('Parameter Estimation') savefig('parameter_estimation.png') savefig('parameter_estimation.eps') # PERFORM OED # =========== v0 = v.copy() # tape the objective function with Algopy J = adolc.jacobian(2, v)[:, :2] cg = CGraph() J0 = J.copy() J1 = zeros(shape(J)) FJ = Function(Mtc(J0, J1)) Ff = Phi(FJ) cg.independentFunctionList = [FJ] cg.dependentFunctionList = [Ff] cg.plot('testgraph.png') cg.plot('testgraph.svg') # perform steepest descent optimization vbar = inf count = 0 while numpy.linalg.norm(vbar) > 10**-8:
def dFdp(p,q,ts,Sigma, etas): v = concatenate((p,q)) return adolc.jacobian(2,v)[:,:Np]
def dFdp(p, q, ts, Sigma, etas): v = concatenate((p, q)) return adolc.jacobian(2, v)[:, :Np]
for n, sp in enumerate(sparsity_pattern_list): for ns, s in enumerate(sp): if s == 1: y[ns] *= x[n] return y x = numpy.random.rand(N) adolc.trace_on(0) x = adolc.adouble(x) adolc.independent(x) y = F(x) adolc.dependent(y) adolc.trace_off() x = numpy.random.rand(N) y = F(x) y2 = adolc.function(0, x) assert numpy.allclose(y, y2) options = numpy.array([0, 0, 0, 0], dtype=int) pat = adolc.sparse.jac_pat(0, x, options) result = adolc.colpack.sparse_jac_no_repeat(0, x, options) print(adolc.jacobian(0, x)) print(pat) print(result)
Np = size(p) Nq = size(q) Nv = size(v) Nm = 100 ts = linspace(0,10,Nm) # TAPING THE INTEGRATOR adolc.trace_on(1) av = adolc.adouble(v) adolc.independent(av) ax = explicit_euler(av[0],f,ts,av[:Np],av[Np:]) adolc.dependent(ax) adolc.trace_off() # COMPUTING FUNCTION AND JACOBIAN FROM THE TAPE y = adolc.zos_forward(1,v,0) J = adolc.jacobian(1,v) x_plot = plot(ts, y,'b') x_analytical_plot = plot(ts,phi(ts,p,q),'b.') xp0_plot = plot(ts, J[:,0], 'g') xp0_analytical_plot = plot(ts, phip0(ts,p,q), 'g.') xp1_plot = plot(ts, J[:,1], 'r') xp1_analytical_plot = plot(ts, phip1(ts,p,q), 'r.') show() print J
xlabel(r'time $t$ []') ylabel(r'measurement function $h(t,x,p,q)$') legend((meas_plot,starting_plot,correct_plot,est_plot,hplot),('measurements','initial guess','true','estimated','measurement model')) title('Parameter Estimation') savefig('parameter_estimation.png') savefig('parameter_estimation.eps') # PERFORM OED # =========== v0 = v.copy() # tape the objective function with Algopy Jtmp = adolc.jacobian(2,v)[:,:2] Jshp = shape(Jtmp) J = zeros((DM-1,1,Jshp[0],Jshp[1])) J[0,0,:,:] = Jtmp cg = CGraph() FJ = Function(Mtc(J)) Ff = Phi(FJ) cg.independentFunctionList = [FJ] cg.dependentFunctionList = [Ff] #cg.plot('testgraph.png') #cg.plot('testgraph.svg') #def gradient_of_PHI(v): #""" computes grad(PHI) as needed in the steepest descent optimization""" #DM = 2
def A_jacaA_taped(self, XP): return adolc.function(self.adolcID, XP), adolc.jacobian(self.adolcID, XP)
def dFdp(p,q,ts,Sigma, etas): v[:Np] = p[:] return adolc.jacobian(1,v)[:,:Np]
def jac_adolc(x): return adolc.jacobian(2,x)
for ns, s in enumerate(sp): if s == 1: y[ns] *= x[n] return y x = numpy.random.rand(N) adolc.trace_on(0) x = adolc.adouble(x) adolc.independent(x) y = F(x) adolc.dependent(y) adolc.trace_off() x = numpy.random.rand(N) y = F(x) y2 = adolc.function(0,x) assert numpy.allclose(y,y2) options = numpy.array([0,0,0,0],dtype=int) pat = adolc.sparse.jac_pat(0,x,options) result = adolc.colpack.sparse_jac_no_repeat(0,x,options) print(adolc.jacobian(0,x)) print(pat) print(result)