def testTTVector(self): vec_shape = (2, 1, 4, 3) np.random.seed(1) rows = np.prod(vec_shape) vec = np.random.rand(rows, 1).astype(self.dtype.as_numpy_dtype) tf_vec = tf.constant(vec) tt_vec = decompositions.to_tt_matrix(tf_vec, (vec_shape, None)) self.assertAllClose(vec, self.evaluate(ops.full(tt_vec)))
def testTTVector(self): vec_shape = (2, 1, 4, 3) np.random.seed(1) rows = np.prod(vec_shape) vec = np.random.rand(rows, 1).astype(np.float32) tf_vec = tf.constant(vec) tt_vec = decompositions.to_tt_matrix(tf_vec, (vec_shape, None)) with self.test_session(): self.assertAllClose(vec, ops.full(tt_vec).eval())
def testTTMatrix(self): # Convert a np.prod(out_shape) x np.prod(in_shape) matrix into TT-matrix # and back. inp_shape = (2, 5, 2, 3) out_shape = (3, 3, 2, 3) np.random.seed(1) mat = np.random.rand(np.prod(out_shape), np.prod(inp_shape)) mat = mat.astype(self.dtype.as_numpy_dtype) tf_mat = tf.constant(mat) tt_mat = decompositions.to_tt_matrix(tf_mat, (out_shape, inp_shape), max_tt_rank=90) # TODO: why so bad accuracy? self.assertAllClose(mat, self.evaluate(ops.full(tt_mat)), atol=1e-5, rtol=1e-5)
def testTTCompositeRankMatrix(self): # Test if a composite rank (list of ranks) can be used for decomposition # for a matrix. inp_shape = (2, 3, 3, 2) out_shape = (1, 2, 2, 1) np.random.seed(1) mat = np.random.rand(np.prod(out_shape), np.prod(inp_shape)) mat = mat.astype(self.dtype.as_numpy_dtype) tf_mat = tf.constant(mat) tt_ranks = [10, 20, 30, 40, 30] tt_mat = decompositions.to_tt_matrix(tf_mat, (out_shape, inp_shape), max_tt_rank=tt_ranks) self.assertAllClose(mat, self.evaluate(ops.full(tt_mat)), atol=1e-5, rtol=1e-5)