def f_fcn(x): A = algopy.zeros((2,2), dtype=x) A[0,0] = x[0] A[1,0] = x[1] * x[0] A[0,1] = x[1] Q,R = algopy.qr(A) return R[0,0]
def f_fcn(x): A = algopy.zeros((2, 2), dtype=x) A[0, 0] = x[0] A[1, 0] = x[1] * x[0] A[0, 1] = x[1] Q, R = algopy.qr(A) return R[0, 0]
def eval_covariance_matrix_qr(J1, J2): M, N = J1.shape K, N = J2.shape Q, R = qr_full(J2.T) Q2 = Q[:, K:].T J1_tilde = dot(J1, Q2.T) Q, R = qr(J1_tilde) V = solve(R.T, Q2) return dot(V.T, V)
def eval_covariance_matrix_qr(J1, J2): M,N = J1.shape K,N = J2.shape Q,R = qr_full(J2.T) Q2 = Q[:,K:].T J1_tilde = dot(J1,Q2.T) Q,R = qr(J1_tilde) V = solve(R.T, Q2) return dot(V.T,V)
def test_pullback_gradient(self): (D,M,N) = 3,3,2 P = M*N A = UTPM(numpy.zeros((D,P,M,M))) A0 = numpy.random.rand(M,N) for m in range(M): for n in range(N): p = m*N + n A.data[0,p,:M,:N] = A0 A.data[1,p,m,n] = 1. cg = CGraph() A = Function(A) Q,R = qr(A) B = dot(Q,R) y = trace(B) cg.independentFunctionList = [A] cg.dependentFunctionList = [y] # print cg # print y.x.data g1 = y.x.data[1] g11 = y.x.data[2] # print g1 ybar = y.x.zeros_like() ybar.data[0,:] = 1. cg.pullback([ybar]) for m in range(M): for n in range(N): p = m*N + n #check gradient assert_array_almost_equal(y.x.data[1,p], A.xbar.data[0,p,m,n]) #check hessian assert_array_almost_equal(0, A.xbar.data[1,p,m,n])
A = dot(x,x.T) + alpha*dot(y,y.T) A = A[:,:2] # Method 1: Naive approach Apinv = dot(inv(dot(A.T,A)),A.T) print('naive approach: A Apinv A - A = 0 \n', dot(dot(A, Apinv),A) - A) print('naive approach: Apinv A Apinv - Apinv = 0 \n', dot(dot(Apinv, A),Apinv) - Apinv) print('naive approach: (Apinv A)^T - Apinv A = 0 \n', dot(Apinv, A).T - dot(Apinv, A)) print('naive approach: (A Apinv)^T - A Apinv = 0 \n', dot(A, Apinv).T - dot(A, Apinv)) # Method 2: Using the differentiated QR decomposition Q,R = qr(A) tmp1 = solve(R.T, A.T) tmp2 = solve(R, tmp1) Apinv = tmp2 print('QR approach: A Apinv A - A = 0 \n', dot(dot(A, Apinv),A) - A) print('QR approach: Apinv A Apinv - Apinv = 0 \n', dot(dot(Apinv, A),Apinv) - Apinv) print('QR approach: (Apinv A)^T - Apinv A = 0 \n', dot(Apinv, A).T - dot(Apinv, A)) print('QR approach: (A Apinv)^T - A Apinv = 0 \n', dot(A, Apinv).T - dot(A, Apinv)) # Method 3: Stable evaluation of the analytical derivative formula A0 = A.data[0,0] A1 = A.data[1,0] Q0, R0 = numpy.linalg.qr(A0)
A = A[:, :2] # Method 1: Naive approach Apinv = dot(inv(dot(A.T, A)), A.T) print('naive approach: A Apinv A - A = 0 \n', dot(dot(A, Apinv), A) - A) print('naive approach: Apinv A Apinv - Apinv = 0 \n', dot(dot(Apinv, A), Apinv) - Apinv) print('naive approach: (Apinv A)^T - Apinv A = 0 \n', dot(Apinv, A).T - dot(Apinv, A)) print('naive approach: (A Apinv)^T - A Apinv = 0 \n', dot(A, Apinv).T - dot(A, Apinv)) # Method 2: Using the differentiated QR decomposition Q, R = qr(A) tmp1 = solve(R.T, A.T) tmp2 = solve(R, tmp1) Apinv = tmp2 print('QR approach: A Apinv A - A = 0 \n', dot(dot(A, Apinv), A) - A) print('QR approach: Apinv A Apinv - Apinv = 0 \n', dot(dot(Apinv, A), Apinv) - Apinv) print('QR approach: (Apinv A)^T - Apinv A = 0 \n', dot(Apinv, A).T - dot(Apinv, A)) print('QR approach: (A Apinv)^T - A Apinv = 0 \n', dot(A, Apinv).T - dot(A, Apinv)) # Method 3: Stable evaluation of the analytical derivative formula A0 = A.data[0, 0]
# M number of rows of J1 # N number of cols of J1 # K number of rows of J2 (must be smaller than N) D, P, M, N, K, Nx = 2, 1, 100, 3, 1, 1 # 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)))) Q, R = Function.qr_full(J2.T) Q2 = Q[:, K:].T J1_tilde = dot(J1, Q2.T) Q, R = qr(J1_tilde) V = solve(R.T, Q2) C = dot(V.T, V) cg1.trace_off() cg1.independentFunctionList = [J1, J2] cg1.dependentFunctionList = [C] print('covariance matrix: C =\n', C) print('check that Q2.T spans the nullspace of J2:\n', dot(J2, Q2.T)) # METHOD 2: image space method (potentially numerically unstable) cg2 = CGraph() J1 = Function(J1.x) J2 = Function(J2.x)
# K number of rows of J2 (must be smaller than N) D,P,M,N,K,Nx = 2,1,100,3,1,1 # 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)))) Q,R = Function.qr_full(J2.T) Q2 = Q[:,K:].T J1_tilde = dot(J1,Q2.T) Q,R = qr(J1_tilde) V = solve(R.T, Q2) C = dot(V.T,V) cg1.trace_off() cg1.independentFunctionList = [J1, J2] cg1.dependentFunctionList = [C] print('covariance matrix: C =\n',C) print('check that Q2.T spans the nullspace of J2:\n', dot(J2,Q2.T)) # METHOD 2: image space method (potentially numerically unstable) cg2 = CGraph() J1 = Function(J1.x) J2 = Function(J2.x)