def test_zeros(): tensor = zeros((2, 3, 4)) assert len(list(tensor)) == 2 assert tensor.op.gpu is False tensor2 = zeros((2, 3, 4), chunk_size=1) # tensor's op key must be equal to tensor2 assert tensor.op.key == tensor2.op.key assert tensor.key != tensor2.key tensor3 = zeros((2, 3, 3)) assert tensor.op.key != tensor3.op.key assert tensor.key != tensor3.key # test create chunk op of zeros manually chunk_op1 = TensorZeros(dtype=tensor.dtype) chunk1 = chunk_op1.new_chunk(None, shape=(3, 3), index=(0, 0)) chunk_op2 = TensorZeros(dtype=tensor.dtype) chunk2 = chunk_op2.new_chunk(None, shape=(3, 4), index=(0, 1)) assert chunk1.op.key != chunk2.op.key assert chunk1.key != chunk2.key tensor = zeros((100, 100), chunk_size=50) tensor = tile(tensor) assert len({c.op.key for c in tensor.chunks}) == 1 assert len({c.key for c in tensor.chunks}) == 1
def testZeros(self): tensor = zeros((2, 3, 4)) self.assertEqual(len(list(tensor)), 2) self.assertFalse(tensor.op.gpu) tensor2 = zeros((2, 3, 4), chunk_size=1) # tensor's op key must be equal to tensor2 self.assertEqual(tensor.op.key, tensor2.op.key) self.assertNotEqual(tensor.key, tensor2.key) tensor3 = zeros((2, 3, 3)) self.assertNotEqual(tensor.op.key, tensor3.op.key) self.assertNotEqual(tensor.key, tensor3.key) # test create chunk op of zeros manually chunk_op1 = TensorZeros(dtype=tensor.dtype) chunk1 = chunk_op1.new_chunk(None, shape=(3, 3), index=(0, 0)) chunk_op2 = TensorZeros(dtype=tensor.dtype) chunk2 = chunk_op2.new_chunk(None, shape=(3, 4), index=(0, 1)) self.assertNotEqual(chunk1.op.key, chunk2.op.key) self.assertNotEqual(chunk1.key, chunk2.key) tensor = zeros((100, 100), chunk_size=50) tensor = tensor.tiles() self.assertEqual(len({c.op.key for c in tensor.chunks}), 1) self.assertEqual(len({c.key for c in tensor.chunks}), 1)
def testRuntimeError(self): with option_context({'eager_mode': True}): a = mt.zeros((10, 10)) with self.assertRaises(ValueError): op = TileFailOp() b = op.new_tileable(None) b.build_graph(tiled=True) r = a + 1 self.assertIn(repr(np.zeros((10, 10)) + 1), repr(r)) np.testing.assert_array_equal(r.fetch(), np.zeros((10, 10)) + 1) a = mt.zeros((10, 10)) # a's data is used by r, # if not deleted, a's data would not be gc collected del r with self.assertRaises(ValueError): a.fetch()
def testRuntimeError(self): from mars.utils import kernel_mode @kernel_mode def raise_error(*_): raise ValueError with option_context({'eager_mode': True}): a = mt.zeros((10, 10)) with self.assertRaises(ValueError): raise_error(a) r = a + 1 self.assertIn(repr(np.zeros((10, 10)) + 1), repr(r)) np.testing.assert_array_equal(r.fetch(), np.zeros((10, 10)) + 1) a = mt.zeros((10, 10)) with self.assertRaises(ValueError): a.fetch()
def testRuntimeError(self): with option_context({'eager_mode': True}): a = mt.zeros((10, 10)) with self.assertRaises(ValueError): op = TileFailOp() b = op.new_tileable(None) b.build_graph(tiled=True) r = a + 1 self.assertIn(repr(np.zeros((10, 10)) + 1), repr(r)) np.testing.assert_array_equal(r.fetch(), np.zeros((10, 10)) + 1)
def test_pca_deterministic_output(self): rng = np.random.RandomState(0) X = mt.tensor(rng.rand(10, 10)) for solver in self.solver_list: transformed_X = mt.zeros((20, 2)) for i in range(20): pca = PCA(n_components=2, svd_solver=solver, random_state=rng) transformed_X[i, :] = pca.fit_transform(X)[0] np.testing.assert_allclose( transformed_X.execute(), mt.tile(transformed_X[0, :], 20).reshape(20, 2).execute())
def test_min_max_scaler1d(setup): X_list_1row = X_1row.to_numpy().tolist() X_list_1col = X_1col.to_numpy().tolist() # Test scaling of dataset along single axis for X in [X_1row, X_1col, X_list_1row, X_list_1col]: scaler = MinMaxScaler(copy=True) X_scaled = scaler.fit(X).transform(X) if isinstance(X, list): X = mt.array(X) # cast only after scaling done if _check_dim_1axis(X) == 1: assert_array_almost_equal(X_scaled.min(axis=0), mt.zeros(n_features)) assert_array_almost_equal(X_scaled.max(axis=0), mt.zeros(n_features)) else: assert_array_almost_equal(X_scaled.min(axis=0), .0) assert_array_almost_equal(X_scaled.max(axis=0), 1.) assert scaler.n_samples_seen_ == X.shape[0] # check inverse transform X_scaled_back = scaler.inverse_transform(X_scaled) assert_array_almost_equal(X_scaled_back, X) # Constant feature X = mt.ones((5, 1)) scaler = MinMaxScaler() X_scaled = scaler.fit(X).transform(X) assert X_scaled.min().to_numpy() >= 0. assert X_scaled.max().to_numpy() <= 1. assert scaler.n_samples_seen_ == X.shape[0] # Function interface X_1d = X_1row.ravel() min_ = X_1d.min() max_ = X_1d.max() assert_array_almost_equal((X_1d - min_) / (max_ - min_), minmax_scale(X_1d, copy=True))
def test_pca_score3(setup): # Check that probabilistic PCA selects the right model n, p = 200, 3 rng = np.random.RandomState(0) Xl = mt.tensor(rng.randn(n, p) + rng.randn(n, 1) * np.array([3, 4, 5]) + np.array([1, 0, 7])) Xt = mt.tensor(rng.randn(n, p) + rng.randn(n, 1) * np.array([3, 4, 5]) + np.array([1, 0, 7])) ll = mt.zeros(p) for k in range(p): pca = PCA(n_components=k, svd_solver='full') pca.fit(Xl) ll[k] = pca.score(Xt) assert ll.argmax().to_numpy() == 1