P = 2 * N x = UTPM(numpy.zeros((D, P, 2 * N, 1))) x.data[0, :] = numpy.random.rand(2 * N, 1) x.data[1, :, :, 0] = numpy.eye(P) y = x[N:] x = x[:N] # wrap the UTPM instance in a Function instance to trace all operations # that have x as an argument # create a CGraph instance that to store the computational trace cg = CGraph().trace_on() x = Function(x) y = Function(y) z = f(x, y) cg.trace_off() # define dependent and independent variables in the computational procedure cg.independentFunctionList = [x, y] cg.dependentFunctionList = [z] # Since the UTPM instrance is wrapped in a Function instance we have to access it # by y.x. That means the Jacobian is grad1 = z.x.data[1, :, 0] print('forward gradient g(x) = \n', grad1) # Now we want to compute the same Jacobian in the reverse mode of AD # before we do that we have a look what the computational graph looks like: # print 'Computational graph is', cg
J1_tilde = dot(J1, Q2.T) Q, R = qr(J1_tilde) V = solve(R.T, Q2) return dot(V.T, V) # dimensions of the involved matrices D, P, M, N, K, Nx = 2, 1, 5, 3, 1, 1 # trace the function evaluation of METHOD 1: nullspace method cg1 = CGraph() J1 = Function(UTPM(numpy.random.rand(*(D, P, M, N)))) J2 = Function(UTPM(numpy.random.rand(*(D, P, K, N)))) C = eval_covariance_matrix_qr(J1, J2) y = C[0, 0] cg1.trace_off() cg1.independentFunctionList = [J1, J2] cg1.dependentFunctionList = [y] print('covariance matrix: C =\n', C) # trace the function evaluation of METHOD 2: naive method (potentially numerically unstable) cg2 = CGraph() J1 = Function(J1.x) J2 = Function(J2.x) C2 = eval_covariance_matrix_naive(J1, J2) y = C2[0, 0] cg2.trace_off() cg2.independentFunctionList = [J1, J2] cg2.dependentFunctionList = [y] print('covariance matrix: C =\n', C2)
from algopy import CGraph, Function cg = CGraph() cg.trace_on() x = Function(1) y = Function(3) z = x * y + x cg.trace_off() cg.independentFunctionList = [x,y] cg.dependentFunctionList = [z] print cg cg.plot('example_tracer_cgraph.png')
J1_tilde = dot(J1,Q2.T) Q,R = qr(J1_tilde) V = solve(R.T, Q2) return dot(V.T,V) # dimensions of the involved matrices D,P,M,N,K,Nx = 2,1,5,3,1,1 # trace the function evaluation of METHOD 1: nullspace method cg1 = CGraph() J1 = Function(UTPM(numpy.random.rand(*(D,P,M,N)))) J2 = Function(UTPM(numpy.random.rand(*(D,P,K,N)))) C = eval_covariance_matrix_qr(J1, J2) y = C[0,0] cg1.trace_off() cg1.independentFunctionList = [J1, J2] cg1.dependentFunctionList = [y] print('covariance matrix: C =\n',C) # trace the function evaluation of METHOD 2: naive method (potentially numerically unstable) cg2 = CGraph() J1 = Function(J1.x) J2 = Function(J2.x) C2 = eval_covariance_matrix_naive(J1, J2) y = C2[0,0] cg2.trace_off() cg2.independentFunctionList = [J1, J2] cg2.dependentFunctionList = [y] print('covariance matrix: C =\n',C2)
n_p = 512 # Datenpunkte pro Periode t = np.arange(n * n_p) / (n_p * f) # Zeitvektor current = i_hat * (np.sin(2 * np.pi * f * t) + 0.7 * np.sin(6 * np.pi * f * t + 1)) # Stromvorgabe H = current / (2 * np.pi * r) # Resultierende Feldvorgabe graph = CGraph() graph.trace_on() x = Function([alpha, a, k, c, Msat]) # Parametervektor p = {'alpha': x[0], 'a': x[1], 'k': x[2], 'c': x[3], 'm_sat': x[4]} model = JilesAthertonModel.from_dict(p) M = model.integrate_rk4(t, H) H = H[::2] t = t[::2] B = mu_0 * (H + M) dB_dt = np.zeros(np.size(B)) new = np.append([0.0], (B[1:] - B[0:-1]) / (t[1:] - t[0:-1])) P = np.sum(0.5 * H * new) graph.trace_off() graph.independentFunctionList = [x] graph.dependentFunctionList = [P] a = graph.gradient([alpha, a, k, c, Msat]) print(a)