def testFrobeniusNormTens(self): # Frobenius norm of a batch of TT-tensors. with self.test_session() as sess: tt = initializers.tensor_batch_with_random_cores((2, 1, 3), batch_size=3, dtype=self.dtype) norm_sq_actual = ops.frobenius_norm_squared(tt) norm_actual = ops.frobenius_norm(tt) vars = [norm_sq_actual, norm_actual, ops.full(tt)] norm_sq_actual_val, norm_actual_val, tt_val = sess.run(vars) tt_val = tt_val.reshape((3, -1)) norm_sq_desired_val = np.sum(tt_val * tt_val, axis=1) norm_desired_val = np.sqrt(norm_sq_desired_val) self.assertAllClose(norm_sq_actual_val, norm_sq_desired_val) self.assertAllClose(norm_actual_val, norm_desired_val, atol=1e-5, rtol=1e-5)
def testFrobeniusNormMatrix(self): # Frobenius norm of a TT-matrix. shape_list = (((2, 2), (3, 4)), ((2, 3, 4), (2, 2, 2))) rank_list = (1, 2) with self.test_session() as sess: for tensor_shape in shape_list: for rank in rank_list: tt = initializers.random_matrix(tensor_shape, tt_rank=rank, dtype=self.dtype) norm_sq_actual = ops.frobenius_norm_squared(tt) norm_actual = ops.frobenius_norm(tt) vars = [norm_sq_actual, norm_actual, ops.full(tt)] norm_sq_actual_val, norm_actual_val, tt_val = sess.run(vars) tt_val = tt_val.flatten() norm_sq_desired_val = tt_val.dot(tt_val) norm_desired_val = np.linalg.norm(tt_val) self.assertAllClose(norm_sq_actual_val, norm_sq_desired_val) self.assertAllClose(norm_actual_val, norm_desired_val, atol=1e-5, rtol=1e-5)
def testFrobeniusNormTens(self): # Frobenius norm of a TT-tensor. shape_list = ((2, 2), (2, 3, 4), (4, 2, 5, 2)) rank_list = (1, 2) for shape in shape_list: for rank in rank_list: tt = initializers.random_tensor(shape, tt_rank=rank, dtype=self.dtype) norm_sq_actual = ops.frobenius_norm_squared(tt) norm_actual = ops.frobenius_norm(tt, epsilon=0.0) vars = [norm_sq_actual, norm_actual, ops.full(tt)] norm_sq_actual_val, norm_actual_val, tt_val = self.evaluate( vars) tt_val = tt_val.flatten() norm_sq_desired_val = tt_val.dot(tt_val) norm_desired_val = np.linalg.norm(tt_val) self.assertAllClose(norm_sq_actual_val, norm_sq_desired_val) self.assertAllClose(norm_actual_val, norm_desired_val, atol=1e-5, rtol=1e-5)