def test_aca_func(): # check whether functional matrizitation is identical with original one N = 50 for obj in ["B1", "B2"]: C_list = [] ranks = np.array([N, N, N]) for mode in range(3): if mode == 0: if obj == "B1": functional_generator = aca_fun.mode_m_matricization_fun( aca_fun.b1, N, N, N) C, U, R = aca_fun.aca_partial_pivoting( functional_generator, N, N * N, N, 10e-4 / 3) tensor = np.asarray(utilis.get_B_one(N)) else: functional_generator = aca_fun.mode_m_matricization_fun( aca_fun.b2, N, N, N) C, U, R = aca_fun.aca_partial_pivoting( functional_generator, N, N * N, N, 10e-4 / 3) tensor = np.asarray(utilis.get_B_two(N)) else: Core_mat = tl.unfold(Core_ten, mode) C, U, R = aca_fun.aca_full_pivoting(Core_mat, 10e-4 / 3) ranks[mode] = U.shape[0] Core_ten = tl.fold(np.dot(U, R), mode, ranks) C_list.append(C) recon = utilis.reconstruct_tensor(C_list, Core_ten, tensor) error = utilis.frobenius_norm_tensor(recon - tensor) assert (error < 10e-4 * utilis.frobenius_norm_tensor(tensor))
def test_hosvd_B1(): # Full rank reconstruction should have low frob norm error N = 50 tensor = np.asarray(utilis.get_B_one(N)) Core, U = utilis.compute_core(tensor, ranks=[N, N, N]) recon_tensor = utilis.reconstruct_tensor(U, Core, tensor) error = utilis.frobenius_norm_tensor(recon_tensor - tensor) assert (error < 10e-10)
def test_generator_B1(): # check whether functional and loop B1 generator are equal N = 50 tensor = np.asarray(utilis.get_B_one(N)) gen_tensor = np.zeros((N, N, N)) for i in range(N): for j in range(N): for z in range(N): gen_tensor[i, j, z] = aca_fun.b1(i, j, z, N) assert (np.allclose(tensor, gen_tensor))
def test_fun_matriziation_b1(): # check whether functional matrizitation is identical with original one N = 50 for mode in range(3): tensor = np.asarray(utilis.get_B_one(N)) unfold_mat = tl.unfold(tensor, mode) functional_generator = aca_fun.mode_m_matricization_fun( aca_fun.b1, N, N, N) gen_mat = np.zeros((N, N * N)) for i in range(N): for j in range(N * N): gen_mat[i, j] = functional_generator(i, j, N) assert (np.allclose(unfold_mat, gen_mat))
def run_aca_full(): N = 200 C_list = [] ranks = np.array([N, N, N]) tensor = np.asarray(utilis.get_B_one(N)) for mode in range(3): if mode == 0: # Start with original matrix Core_mat = tl.unfold(tensor, mode) else: Core_mat = tl.unfold(Core_ten, mode) C, U, R = aca_fun.aca_full_pivoting(Core_mat, 10e-5) ranks[mode] = U.shape[0] print(f'Current ranks: {ranks}') Core_ten = tl.fold(np.dot(U, R), mode, ranks) C_list.append(C)
def test_speed_hosvd_B1_N200(benchmark): # Benchmark speed for N=200 for decomposition without reconstruction N = 200 tensor = np.asarray(utilis.get_B_one(N)) benchmark(utilis.compute_core, tensor, max_rel_error=10e-5)