def test_tree_arithmetic(): raws = [np.random.rand(10, 10) for _ in range(10)] tensors = [tensor(a, chunk_size=3) for a in raws] t = tree_add(*tensors, combine_size=4) assert isinstance(t.op, TensorTreeAdd) assert t.issparse() is False assert len(t.inputs) == 3 assert len(t.inputs[0].inputs) == 4 assert len(t.inputs[-1].inputs) == 2 t = tree_multiply(*tensors, combine_size=4) assert isinstance(t.op, TensorTreeMultiply) assert t.issparse() is False assert len(t.inputs) == 3 assert len(t.inputs[0].inputs) == 4 assert len(t.inputs[-1].inputs) == 2 raws = [sps.random(5, 9, density=.1) for _ in range(10)] tensors = [tensor(a, chunk_size=3) for a in raws] t = tree_add(*tensors, combine_size=4) assert isinstance(t.op, TensorTreeAdd) assert t.issparse() is True assert len(t.inputs) == 3 assert len(t.inputs[0].inputs) == 4 assert len(t.inputs[-1].inputs) == 2 t = tree_multiply(*tensors, combine_size=4) assert isinstance(t.op, TensorTreeMultiply) assert t.issparse() is True assert len(t.inputs) == 3 assert len(t.inputs[0].inputs) == 4 assert len(t.inputs[-1].inputs) == 2
def testTreeArithmetic(self): raws = [np.random.rand(10, 10) for _ in range(10)] tensors = [tensor(a, chunk_size=3) for a in raws] t = tree_add(*tensors, combine_size=4) self.assertIsInstance(t.op, TensorTreeAdd) self.assertFalse(t.issparse()) self.assertEqual(len(t.inputs), 3) self.assertEqual(len(t.inputs[0].inputs), 4) self.assertEqual(len(t.inputs[-1].inputs), 2) t = tree_multiply(*tensors, combine_size=4) self.assertIsInstance(t.op, TensorTreeMultiply) self.assertFalse(t.issparse()) self.assertEqual(len(t.inputs), 3) self.assertEqual(len(t.inputs[0].inputs), 4) self.assertEqual(len(t.inputs[-1].inputs), 2) raws = [sps.random(5, 9, density=.1) for _ in range(10)] tensors = [tensor(a, chunk_size=3) for a in raws] t = tree_add(*tensors, combine_size=4) self.assertIsInstance(t.op, TensorTreeAdd) self.assertTrue(t.issparse()) self.assertEqual(len(t.inputs), 3) self.assertEqual(len(t.inputs[0].inputs), 4) self.assertEqual(len(t.inputs[-1].inputs), 2) t = tree_multiply(*tensors, combine_size=4) self.assertIsInstance(t.op, TensorTreeMultiply) self.assertTrue(t.issparse()) self.assertEqual(len(t.inputs), 3) self.assertEqual(len(t.inputs[0].inputs), 4) self.assertEqual(len(t.inputs[-1].inputs), 2)
def test_tree_arithmetic_execution(setup): raws = [np.random.rand(10, 10) for _ in range(10)] tensors = [tensor(a, chunk_size=3) for a in raws] res = tree_add(*tensors, 1.0).execute().fetch() np.testing.assert_array_almost_equal( res, 1.0 + functools.reduce(operator.add, raws)) res = tree_multiply(*tensors, 2.0).execute().fetch() np.testing.assert_array_almost_equal( res, 2.0 * functools.reduce(operator.mul, raws)) raws = [sps.random(5, 9, density=.1) for _ in range(10)] tensors = [tensor(a, chunk_size=3) for a in raws] res = tree_add(*tensors).execute().fetch() np.testing.assert_array_almost_equal( res.toarray(), functools.reduce(operator.add, raws).toarray())
def testTreeArithmeticExecution(self): raws = [np.random.rand(10, 10) for _ in range(10)] tensors = [tensor(a, chunk_size=3) for a in raws] res = self.executor.execute_tensor(tree_add(*tensors, 1.0), concat=True)[0] np.testing.assert_array_almost_equal( res, 1.0 + functools.reduce(operator.add, raws)) res = self.executor.execute_tensor(tree_multiply(*tensors, 2.0), concat=True)[0] np.testing.assert_array_almost_equal( res, 2.0 * functools.reduce(operator.mul, raws)) raws = [sps.random(5, 9, density=.1) for _ in range(10)] tensors = [tensor(a, chunk_size=3) for a in raws] res = self.executor.execute_tensor(tree_add(*tensors), concat=True)[0] np.testing.assert_array_almost_equal( res.toarray(), functools.reduce(operator.add, raws).toarray())