def test_ones_like_utpm(self): D, P, N, M = 3, 4, 5, 6 x = UTPM(numpy.random.random((D, P, N, M))) y = ones_like(x) data = numpy.zeros((D, P, N, M)) data[0, ...] = 1. assert_array_almost_equal(data, y.data)
def test_utpm2dirs(self): D, P, N, M, K = 2, 3, 4, 5, 6 u = UTPM(numpy.arange(D * P * N * M * K).reshape((D, P, N, M, K))) Vbar = utpm2dirs(u) assert_array_almost_equal( Vbar, numpy.arange(D * P * N * M * K).reshape((D, P, N, M, K)).transpose( (2, 3, 4, 1, 0)))
def test_utpm2base_and_dirs(self): D, P, N, M, K = 2, 3, 4, 5, 6 u = UTPM(numpy.arange(D * P * N * M * K).reshape((D, P, N, M, K))) x, V = utpm2base_and_dirs(u) assert_array_almost_equal(x, numpy.arange(N * M * K).reshape((N, M, K))) assert_array_almost_equal( V, numpy.arange(P * N * M * K, D * P * N * M * K).reshape( (D - 1, P, N, M, K)).transpose((2, 3, 4, 1, 0)))
def test_zeros_utpm_with_mpmath_instances_as_dtype(self): skiptest = False try: import mpmath except: skiptest = True if skiptest == False: x = UTPM(numpy.array([[mpmath.mpf(3)]])) A = zeros((2, 2), dtype=x) assert_equal(True, isinstance(A.data[0, 0, 0, 0], mpmath.mpf))
def test_tracer_on_mixed_utpm_ndarray_mul(self): D, P = 1, 1 A = numpy.arange(2 * 2, dtype=float).reshape(2, 2) x = UTPM(numpy.zeros((D, P, 2, 2))) def f(x): return sum(A * x) cg = CGraph() ax = Function(x) ay = f(ax) cg.independentFunctionList = [ax] cg.dependentFunctionList = [ay] assert_array_almost_equal(A, cg.gradient(x))
def test_dot_utpm(self): D, P, N = 3, 4, 5 x = UTPM(numpy.random.rand(D, P, N, N)) y = UTPM(numpy.random.rand(D, P, N, N)) assert_array_almost_equal(dot(x, y).data, UTPM.dot(x, y).data)
def test_zeros_like_utpm(self): D, P, N, M = 3, 4, 5, 6 x = UTPM(numpy.random.rand(*(D, P, N, M))) y = zeros_like(x) assert_array_almost_equal(numpy.zeros((D, P, N, M)), y.data)
def test_binary_function_utpm(self): D, P, N, M = 3, 4, 5, 6 x = UTPM(numpy.random.rand(*(D, P, N, M))) y = UTPM(numpy.random.rand(*(D, P, M, N))) assert_array_almost_equal(dot(x, y).data, UTPM.dot(x, y).data)
def test_unary_function_utpm(self): D, P, N = 3, 4, 5 x = UTPM(numpy.ones((D, P, N, N))) assert_array_almost_equal(trace(x).data, N * numpy.ones((D, P)))
def test_trace_utpm(self): D, P, N = 3, 4, 5 x = UTPM(numpy.random.rand(D, P, N, N)) assert_array_almost_equal(trace(x).data, UTPM.trace(x).data)
def test_inv_utpm(self): D, P, N = 3, 4, 5 x = UTPM(numpy.random.rand(D, P, N, N)) assert_array_almost_equal(inv(x).data, UTPM.inv(x).data)
# compute pullback WQ = Qbar_data WR = Rbar_data tic = time() out = AP.reverse([WQ, WR]) toc = time() runtime_pyadolc_pullback = toc - tic #---------------------------------------------- # STEP 2: # QR decomposition using LAPACK # using algopy for the differentiation #---------------------------------------------- # comute push forward A = UTPM( numpy.ascontiguousarray(A_data.transpose((3, 2, 0, 1)))) Q = UTPM(numpy.zeros((D, P, N, N))) R = UTPM(numpy.zeros((D, P, N, N))) tic = time() Q, R = UTPM.qr(A, out=(Q, R)) toc = time() runtime_algopy_push_forward = toc - tic # compute pullback Qbar = UTPM( numpy.ascontiguousarray(Qbar_data[0, ...].transpose( (3, 2, 0, 1)))) Rbar = UTPM( numpy.ascontiguousarray(Rbar_data[0, ...].transpose( (3, 2, 0, 1)))) tic = time()