def test_sqrt_in_norm_computation(self): def eval_f1(x): return algopy.sqrt(algopy.sum(x*x)) def eval_f2(x): return (algopy.sum(x*x))**0.5 cg1 = CGraph() x1 = Function(1.) y1 = eval_f1(x1) cg1.trace_off() cg1.independentFunctionList = [x1] cg1.dependentFunctionList = [y1] cg2 = CGraph() x2 = Function(1.) y2 = eval_f2(x2) cg2.trace_off() cg2.independentFunctionList = [x2] cg2.dependentFunctionList = [y2] x = numpy.random.rand(3) g1 = cg1.gradient([x])[0] g2 = cg2.gradient([x])[0] J1 = UTPM.extract_jacobian(eval_f1(UTPM.init_jacobian(x))) J2 = UTPM.extract_jacobian(eval_f2(UTPM.init_jacobian(x))) assert_array_almost_equal(g1,g2) assert_array_almost_equal(g2,J1) assert_array_almost_equal(J1,J2) assert_array_almost_equal(J2,g1)
def test_erf(self): """ compute y = erf(x**2 + 3.) """ def f(x): v1 = x**2 + 3. y = algopy.special.erf(v1) return y #FIXME: uncomment the rest when the pullback is implemented # use CGraph #cg = CGraph() #x = Function(numpy.array([1.])) #y = f(x) #cg.independentFunctionList = [x] #cg.dependentFunctionList = [y] #result1 = cg.jac_vec(numpy.array([2.]), numpy.array([1.])) #result2 = cg.jacobian(numpy.array([2.]))[0] # use UTPM x = UTPM.init_jacobian(numpy.array([2.])) y = f(x) result3 = UTPM.extract_jacobian(y)[0]
def test_hyp1f1(self): """ compute y = hyp1f1(1., 2., x**2 + 3.) """ def f(x): v1 = x**2 + 3. y = algopy.special.hyp1f1(1., 2., v1) return y # use CGraph cg = CGraph() x = Function(numpy.array([1.])) y = f(x) cg.independentFunctionList = [x] cg.dependentFunctionList = [y] result1 = cg.jac_vec(numpy.array([2.]), numpy.array([1.])) result2 = cg.jacobian(numpy.array([2.]))[0] # use UTPM x = UTPM.init_jacobian(numpy.array([2.])) y = f(x) result3 = UTPM.extract_jacobian(y)[0] assert_array_almost_equal(result1, result2) assert_array_almost_equal(result2, result3) assert_array_almost_equal(result3, result1)
def test_tangent_gradient(self): cg = CGraph() x = Function(1.) y1 = algopy.tan(x) cg.trace_off() cg.independentFunctionList = [x] cg.dependentFunctionList = [y1] g1 = cg.gradient([1.])[0] x = UTPM.init_jacobian(1.) assert_array_almost_equal(g1,UTPM.extract_jacobian(algopy.sin(x)/algopy.cos(x))) assert_array_almost_equal(g1,UTPM.extract_jacobian(algopy.tan(x)))
def test_most_drivers(self): def f(x): return x[0]*x[1]*x[2] + 7*x[1] def g(x): out = algopy.zeros(3, dtype=x) out[0] = 2*x[0]**2 out[1] = 7*x[0]*x[1] out[2] = 23*x[0] + x[2] return out x = numpy.array([1,2,3],dtype=float) v = numpy.array([1,1,1],dtype=float) w = numpy.array([4,5,6],dtype=float) # forward mode gradient res1 = UTPM.extract_jacobian(f(UTPM.init_jacobian(x))) # forward mode Jacobian res2 = UTPM.extract_jacobian(g(UTPM.init_jacobian(x))) # forward mode Jacobian-vector res3 = UTPM.extract_jac_vec(g(UTPM.init_jac_vec(x, v))) # trace f cg = algopy.CGraph() fx = algopy.Function(x) fy = f(fx) cg.trace_off() cg.independentFunctionList = [fx] cg.dependentFunctionList = [fy] # trace g cg2 = algopy.CGraph() fx = algopy.Function(x) fy = g(fx) cg2.trace_off() cg2.independentFunctionList = [fx] cg2.dependentFunctionList = [fy] # reverse mode gradient res4 = cg.gradient(x) assert_array_almost_equal(numpy.array( [x[1]*x[2], x[0]*x[2]+7, x[0]*x[1]]), res4) # forward/reverse mode Hessian res5 = cg.hessian(x) assert_array_almost_equal(numpy.array( [[0, x[2], x[1]], [x[2], 0., x[0]], [x[1], x[0], 0]]), res5) # forward/reverse mode Hessian-vector res6 = cg.hess_vec(x,v) assert_array_almost_equal(numpy.dot(res5, v), res6) # reverese mode Jacobian res7 = cg2.jacobian(x) assert_array_almost_equal(numpy.array( [[4*x[0], 0, 0], [7*x[1], 7*x[0], 0], [23., 0, 1]]), res7) # reverse mode vector-Jacobian res8 = cg2.vec_jac(w,x) assert_array_almost_equal(numpy.dot(w,res7), res8) # forward mode Jacobian-vector res9 = cg2.jac_vec(x,v) assert_array_almost_equal(numpy.dot(res7,v), res9) # forward/reverse mode vector-Hessian-vector res10 = cg2.vec_hess_vec(w,x,v) assert_array_almost_equal(numpy.array([4*v[0]*w[0]+ 7*v[1]*w[1], 7*w[1], 0]), res10)