def testNormalizeExecution(self): raw_dense = np.random.rand(10, 10) raw_sparse = sps.random(10, 10, density=0.4, format='csr') for chunk_size in [10, 6, (10, 6), (6, 10)]: for raw, x in [ (raw_dense, mt.tensor(raw_dense, chunk_size=chunk_size)), (raw_sparse, mt.tensor(raw_sparse, chunk_size=chunk_size)) ]: for norm in ['l1', 'l2', 'max']: for axis in (0, 1): for use_sklearn in [True, False]: n = normalize(x, norm=norm, axis=axis, return_norm=False) n.op._use_sklearn = use_sklearn result = self.executor.execute_tensor( n, concat=True)[0] expected = sk_normalize(raw, norm=norm, axis=axis, return_norm=False) if sps.issparse(expected): expected = expected.A np.testing.assert_almost_equal( np.asarray(result), expected) raw_dense = np.random.rand(10, 10) raw_sparse = sps.random(10, 10, density=0.4, format='csr') # test copy and return_normalize for axis in (0, 1): for chunk_size in (10, 6, (6, 10)): for raw in (raw_dense, raw_sparse): x = mt.tensor(raw, chunk_size=chunk_size) n = normalize(x, axis=axis, copy=False, return_norm=True) results = self.executor.execute_tensors(n) raw_copy = raw.copy() try: expects = sk_normalize(raw_copy, axis=axis, copy=False, return_norm=True) except NotImplementedError: continue if sps.issparse(expects[0]): expected = expects[0].A else: expected = expects[0] np.testing.assert_almost_equal(np.asarray(results[0]), expected) np.testing.assert_almost_equal(results[1], expects[1])
def testNormalizeOp(self): with self.assertRaises(ValueError): normalize(mt.random.random(10, 3), norm='unknown') with self.assertRaises(ValueError): normalize(mt.random.random(10, 3), axis=-1) with self.assertRaises(ValueError): normalize(mt.random.rand(10, 3, 3))
def test_normalize_op(): with pytest.raises(ValueError): normalize(mt.random.random(10, 3), norm='unknown') with pytest.raises(ValueError): normalize(mt.random.random(10, 3), axis=-1) with pytest.raises(ValueError): normalize(mt.random.rand(10, 3, 3))