def test_permutation_execute(setup): rs = tensor.random.RandomState(0) x = rs.permutation(10) res = x.execute().fetch() assert not np.all(res[:-1] < res[1:]) np.testing.assert_array_equal(np.sort(res), np.arange(10)) arr = from_ndarray([1, 4, 9, 12, 15], chunk_size=2) x = rs.permutation(arr) res = x.execute().fetch() assert not np.all(res[:-1] < res[1:]) np.testing.assert_array_equal(np.sort(res), np.asarray([1, 4, 9, 12, 15])) arr = from_ndarray(np.arange(48).reshape(12, 4), chunk_size=2) # axis = 0 x = rs.permutation(arr) res = x.execute().fetch() assert not np.all(res[:-1] < res[1:]) np.testing.assert_array_equal(np.sort(res, axis=0), np.arange(48).reshape(12, 4)) # axis != 0 x2 = rs.permutation(arr, axis=1) res = x2.execute().fetch() assert not np.all(res[:, :-1] < res[:, 1:]) np.testing.assert_array_equal(np.sort(res, axis=1), np.arange(48).reshape(12, 4))
def testPermutationExecute(self): x = tensor.random.permutation(10) res = self.executor.execute_tensor(x, concat=True)[0] self.assertFalse(np.all(res[:-1] < res[1:])) np.testing.assert_array_equal(np.sort(res), np.arange(10)) arr = from_ndarray([1, 4, 9, 12, 15], chunk_size=2) x = tensor.random.permutation(arr) res = self.executor.execute_tensor(x, concat=True)[0] self.assertFalse(np.all(res[:-1] < res[1:])) np.testing.assert_array_equal(np.sort(res), np.asarray([1, 4, 9, 12, 15])) arr = from_ndarray(np.arange(48).reshape(12, 4), chunk_size=2) # axis = 0 x = tensor.random.permutation(arr) res = self.executor.execute_tensor(x, concat=True)[0] self.assertFalse(np.all(res[:-1] < res[1:])) np.testing.assert_array_equal(np.sort(res, axis=0), np.arange(48).reshape(12, 4)) # axis != 0 x2 = tensor.random.permutation(arr, axis=1) res = self.executor.execute_tensor(x2, concat=True)[0] self.assertFalse(np.all(res[:, :-1] < res[:, 1:])) np.testing.assert_array_equal(np.sort(res, axis=1), np.arange(48).reshape(12, 4))
def testRandom(self): arr = rand(2, 3) self.assertIsNotNone(arr.dtype) arr = beta(1, 2, chunk_size=2).tiles() self.assertEqual(arr.shape, ()) self.assertEqual(len(arr.chunks), 1) self.assertEqual(arr.chunks[0].shape, ()) self.assertEqual(arr.chunks[0].op.dtype, np.dtype('f8')) arr = beta([1, 2], [3, 4], chunk_size=2).tiles() self.assertEqual(arr.shape, (2, )) self.assertEqual(len(arr.chunks), 1) self.assertEqual(arr.chunks[0].shape, (2, )) self.assertEqual(arr.chunks[0].op.dtype, np.dtype('f8')) arr = beta([[2, 3]], from_ndarray([[4, 6], [5, 2]], chunk_size=2), chunk_size=1, size=(3, 2, 2)).tiles() self.assertEqual(arr.shape, (3, 2, 2)) self.assertEqual(len(arr.chunks), 12) self.assertEqual(arr.chunks[0].op.dtype, np.dtype('f8'))
def testBetaExecute(self): arr = tensor.random.beta(1, 2, chunk_size=2).tiles() arr.chunks[0].op._seed = 0 self.assertEqual(self.executor.execute_tensor(arr)[0], np.random.RandomState(0).beta(1, 2)) arr = tensor.random.beta([1, 2], [3, 4], chunk_size=2).tiles() arr.chunks[0].op._seed = 0 self.assertTrue(np.array_equal(self.executor.execute_tensor(arr)[0], np.random.RandomState(0).beta([1, 2], [3, 4]))) arr = tensor.random.beta([[2, 3]], from_ndarray([[4, 6], [5, 2]], chunk_size=2), chunk_size=1, size=(3, 2, 2)).tiles() for c in arr.chunks: c.op._seed = 0 res = self.executor.execute_tensor(arr, concat=True)[0] self.assertEqual(res[0, 0, 0], np.random.RandomState(0).beta(2, 4)) self.assertEqual(res[0, 0, 1], np.random.RandomState(0).beta(3, 6)) self.assertEqual(res[0, 1, 0], np.random.RandomState(0).beta(2, 5)) self.assertEqual(res[0, 1, 1], np.random.RandomState(0).beta(3, 2)) arr = tensor.random.RandomState(0).beta([[3, 4]], [[1], [2]], chunk_size=1) tensor.random.seed(0) arr2 = tensor.random.beta([[3, 4]], [[1], [2]], chunk_size=1) self.assertTrue(np.array_equal(self.executor.execute_tensor(arr, concat=True)[0], self.executor.execute_tensor(arr2, concat=True)[0]))
def test_random(): arr = rand(2, 3) assert arr.dtype is not None arr = tile(beta(1, 2, chunk_size=2)) assert arr.shape == () assert len(arr.chunks) == 1 assert arr.chunks[0].shape == () assert arr.chunks[0].op.dtype == np.dtype('f8') arr = tile(beta([1, 2], [3, 4], chunk_size=2)) assert arr.shape == (2, ) assert len(arr.chunks) == 1 assert arr.chunks[0].shape == (2, ) assert arr.chunks[0].op.dtype == np.dtype('f8') arr = tile( beta([[2, 3]], from_ndarray([[4, 6], [5, 2]], chunk_size=2), chunk_size=1, size=(3, 2, 2))) assert arr.shape == (3, 2, 2) assert len(arr.chunks) == 12 assert arr.chunks[0].op.dtype == np.dtype('f8')
def testRandomSerialize(self): arr = RandomState(0).beta([[2, 3]], from_ndarray([[4, 6], [5, 2]], chunk_size=2), chunk_size=1, size=(3, 2, 2)).tiles() chunk = arr.chunks[0] self.assertEqual(chunk.op.dtype, np.dtype('f8')) serials = self._pb_serial(chunk) chunk2 = self._pb_deserial(serials)[chunk.data] self.assertEqual(chunk.index, chunk2.index) self.assertEqual(chunk.op.state, chunk2.op.state) self.assertEqual(chunk.op.seed, chunk2.op.seed)
def testPermutation(self): x = permutation(10) self.assertEqual(x.shape, (10, )) self.assertIsInstance(x.op, TensorPermutation) x = x.tiles() self.assertEqual(len(x.chunks), 1) self.assertIsInstance(x.chunks[0].op, TensorPermutation) arr = from_ndarray([1, 4, 9, 12, 15], chunk_size=2) x = permutation(arr) self.assertEqual(x.shape, (5, )) self.assertIsInstance(x.op, TensorPermutation) x = x.tiles() arr = get_tiled(arr) self.assertEqual(len(x.chunks), 3) self.assertTrue(np.isnan(x.chunks[0].shape[0])) self.assertIs(x.chunks[0].inputs[0].inputs[0].inputs[0], arr.chunks[0].data) arr = rand(3, 3, chunk_size=2) x = permutation(arr) self.assertEqual(x.shape, (3, 3)) self.assertIsInstance(x.op, TensorPermutation) x = x.tiles() arr = get_tiled(arr) self.assertEqual(len(x.chunks), 4) self.assertTrue(np.isnan(x.chunks[0].shape[0])) self.assertEqual(x.chunks[0].shape[1], 2) self.assertIs(x.cix[0, 0].inputs[0].inputs[0].inputs[0], arr.cix[0, 0].data) self.assertIs(x.cix[0, 0].inputs[0].inputs[1].inputs[0], arr.cix[1, 0].data) self.assertEqual(x.cix[0, 0].op.seed, x.cix[0, 1].op.seed) self.assertEqual(x.cix[0, 0].inputs[0].inputs[0].inputs[0].op.seed, x.cix[1, 0].inputs[0].inputs[0].inputs[0].op.seed) with self.assertRaises(np.AxisError): self.assertRaises(permutation('abc'))
def test_permutation(): x = permutation(10) assert x.shape == (10, ) assert isinstance(x.op, TensorPermutation) x = tile(x) assert len(x.chunks) == 1 assert isinstance(x.chunks[0].op, TensorPermutation) arr = from_ndarray([1, 4, 9, 12, 15], chunk_size=2) x = permutation(arr) assert x.shape == (5, ) assert isinstance(x.op, TensorPermutation) x = tile(x) arr = tile(arr) assert len(x.chunks) == 3 assert np.isnan(x.chunks[0].shape[0]) assert x.chunks[0].inputs[0].inputs[0].inputs[0].key == arr.chunks[ 0].data.key arr = rand(3, 3, chunk_size=2) x = permutation(arr) assert x.shape == (3, 3) assert isinstance(x.op, TensorPermutation) x = tile(x) arr = tile(arr) assert len(x.chunks) == 4 assert np.isnan(x.chunks[0].shape[0]) assert x.chunks[0].shape[1] == 2 assert x.cix[0, 0].op.seed == x.cix[0, 1].op.seed assert x.cix[0, 0].inputs[0].inputs[0].inputs[0].op.seed == \ x.cix[1, 0].inputs[0].inputs[0].inputs[0].op.seed with pytest.raises(np.AxisError): pytest.raises(permutation('abc'))