def testTTTensorSimple(self): # Test that a tensor of ones and of zeros can be converted into TT with # TT-rank 1. shape = (2, 1, 4, 3) tens_arr = (np.zeros(shape).astype(np.float32), np.ones(shape).astype(np.float32)) for tens in tens_arr: tf_tens = tf.constant(tens) tt_tens = decompositions.to_tt_tensor(tf_tens, max_tt_rank=1) with self.test_session(): self.assertAllClose(tens, ops.full(tt_tens).eval()) dynamic_tt_ranks = shapes.tt_ranks(tt_tens).eval() static_tt_ranks = tt_tens.get_tt_ranks().as_list() self.assertAllEqual(dynamic_tt_ranks, static_tt_ranks) # Try to decompose the same tensor with unknown shape. tf_tens_pl = tf.placeholder(tf.float32, (None, None, None, None)) tt_tens = decompositions.to_tt_tensor(tf_tens_pl, max_tt_rank=1) tt_val = ops.full(tt_tens).eval({tf_tens_pl: tens}) self.assertAllClose(tens, tt_val) dynamic_tt_ranks = shapes.tt_ranks(tt_tens).eval( {tf_tens_pl: tens}) self.assertAllEqual(dynamic_tt_ranks, static_tt_ranks)
def testTTCompositeRankTensor(self): # Test if a composite rank (list of ranks) can be used for decomposition # for tensor. np.random.seed(1) np_tensor = np.random.rand(2, 3, 3, 1).astype(self.dtype.as_numpy_dtype) tf_tensor = tf.constant(np_tensor) tt_ranks = [1, 2, 3, 3, 1] tt_tensor = decompositions.to_tt_tensor(tf_tensor, max_tt_rank=tt_ranks) self.assertAllClose(np_tensor, self.evaluate(ops.full(tt_tensor)))
def testTTTensor(self): shape = (2, 1, 4, 3) np.random.seed(1) tens = np.random.rand(*shape).astype(self.dtype.as_numpy_dtype) tf_tens = tf.constant(tens) tt_tens = decompositions.to_tt_tensor(tf_tens, max_tt_rank=3) self.assertAllClose(tens, self.evaluate(ops.full(tt_tens))) dynamic_tt_ranks = self.evaluate(shapes.tt_ranks(tt_tens)) static_tt_ranks = tt_tens.get_tt_ranks().as_list() self.assertAllEqual(dynamic_tt_ranks, static_tt_ranks)
def testTTTensor(self): shape = (2, 1, 4, 3) np.random.seed(1) tens = np.random.rand(*shape).astype(np.float32) tf_tens = tf.constant(tens) tt_tens = decompositions.to_tt_tensor(tf_tens, max_tt_rank=3) with self.test_session(): self.assertAllClose(tens, ops.full(tt_tens).eval()) dynamic_tt_ranks = shapes.tt_ranks(tt_tens).eval() static_tt_ranks = tt_tens.get_tt_ranks().as_list() self.assertAllEqual(dynamic_tt_ranks, static_tt_ranks) # Try to decompose the same tensor with unknown shape. tf_tens_pl = tf.placeholder(tf.float32, (None, None, 4, None)) tt_tens = decompositions.to_tt_tensor(tf_tens_pl, max_tt_rank=3) tt_val = ops.full(tt_tens).eval({tf_tens_pl: tens}) self.assertAllClose(tens, tt_val) dynamic_tt_ranks = shapes.tt_ranks(tt_tens).eval( {tf_tens_pl: tens}) self.assertAllEqual(dynamic_tt_ranks, static_tt_ranks)
def testTTTensorSimple(self): # Test that a tensor of ones and of zeros can be converted into TT with # TT-rank 1. shape = (2, 1, 4, 3) tens_arr = (np.zeros(shape).astype(self.dtype.as_numpy_dtype), np.ones(shape).astype(self.dtype.as_numpy_dtype)) for tens in tens_arr: tf_tens = tf.constant(tens) tt_tens = decompositions.to_tt_tensor(tf_tens, max_tt_rank=1) self.assertAllClose(tens, self.evaluate(ops.full(tt_tens))) dynamic_tt_ranks = self.evaluate(shapes.tt_ranks(tt_tens)) static_tt_ranks = tt_tens.get_tt_ranks().as_list() self.assertAllEqual(dynamic_tt_ranks, static_tt_ranks)