You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2019/12/30 20:33:26 UTC

[GitHub] [incubator-mxnet] reminisce commented on a change in pull request #17188: [Numpy] Add linalg.eig/eigh/eigvals/eigvalsh op

reminisce commented on a change in pull request #17188: [Numpy] Add linalg.eig/eigh/eigvals/eigvalsh op
URL: https://github.com/apache/incubator-mxnet/pull/17188#discussion_r362083569
 
 

 ##########
 File path: tests/python/unittest/test_numpy_op.py
 ##########
 @@ -4291,6 +4291,377 @@ def newInvertibleMatrix_2D(shape, max_cond=4):
                 check_tensorsolve(mx_out, a.asnumpy(), b.asnumpy(), axes)
 
 
+@with_seed()
+@use_np
+def test_np_linalg_eigvals():
+    class TestEigvals(HybridBlock):
+        def __init__(self):
+            super(TestEigvals, self).__init__()
+
+        def hybrid_forward(self, F, a):
+            return F.np.linalg.eigvals(a)
+
+    def check_eigvals(x, a_np):
+        try:
+            x_expected = _np.linalg.eigvals(a_np)
+        except Exception as e:
+            print("a:", a_np)
+            print("a shape:", a_np.shape)
+            print(e)
+        else:
+            assert x.shape == x_expected.shape
+            if 0 not in x.shape:
+                n = int(_np.prod(x.shape[:-1])) if len(shape) > 1 else 1
+                x = x.reshape(n, -1)
+                x_expected = x_expected.reshape(n, -1)
+                for i in range(n):
+                    x1 = _np.sort(x[i].asnumpy())
+                    x2 = _np.sort(x_expected[i])
+                    assert_almost_equal(x1, x2, rtol=rtol, atol=atol)
+
+    def newMatrixWithRealEigvals_2D(n):
+        shape = (n, n)
+        q = _np.ones(shape)
+        while 1:
+            D = _np.diag(_np.random.uniform(-1.0, 1.0, shape[-1]))
+            I = _np.eye(shape[-1]).reshape(shape)
+            v = _np.random.uniform(-1., 1., shape[-1]).reshape(shape[:-1] + (1,))
+            v = v / _np.linalg.norm(v, axis=-2, keepdims=True)
+            v_T = _np.swapaxes(v, -1, -2)
+            U = I - 2 * _np.matmul(v, v_T)
+            q = _np.matmul(U, D)
+            if (_np.linalg.cond(q, 2) < 3):
+                break
+        D = _np.diag(_np.random.uniform(-10.0, 10.0, n))
+        q_inv = _np.linalg.inv(q)
+        return _np.matmul(_np.matmul(q_inv, D), q)
+
+    def newMatrixWithRealEigvals_nD(shape):
+        n = int(_np.prod(shape[:-2])) if len(shape) > 2 else 1
+        return _np.array([newMatrixWithRealEigvals_2D(shape[-1]) for i in range(n)]).reshape(shape)
+
+    shapes = [
+        (0, 0),
+        (1, 1),
+        (3, 3),
+        (5, 5),
+        (1, 0, 0),
+        (0, 4, 4),
+        (1, 4, 4),
+        (2, 4, 4),
+        (5, 5, 5),
+        (1, 1, 4, 4),
+        (2, 3, 4, 4)
+    ]
+    dtypes = ['float32', 'float64']
+    for hybridize in [True, False]:
+        for shape, dtype in itertools.product(shapes, dtypes):
+            rtol = 1e-2 if dtype == 'float32' else 1e-3
+            atol = 1e-4 if dtype == 'float32' else 1e-5
+            test_eigvals = TestEigvals()
+            if hybridize:
+                test_eigvals.hybridize()
+            if 0 in shape:
+                a_np = _np.ones(shape)
+            else:
+                a_np = newMatrixWithRealEigvals_nD(shape)
+            a = np.array(a_np, dtype=dtype)
+            # check eig validity
+            w = test_eigvals(a)
+            check_eigvals(w, a.asnumpy())
+
+
+@with_seed()
+@use_np
+def test_np_linalg_eigvalsh():
+    class TestEigvalsh(HybridBlock):
+        def __init__(self, UPLO):
+            super(TestEigvalsh, self).__init__()
+            self._UPLO = UPLO
+
+        def hybrid_forward(self, F, a):
+            return F.np.linalg.eigvalsh(a, UPLO=self._UPLO)
+
+    def check_eigvalsh(w, a_np, UPLO):
+        try:
+            w_expected = _np.linalg.eigvalsh(a_np, UPLO)
+        except Exception as e:
+            print("a:", a_np)
+            print("a shape:", a_np.shape)
+            print(e)
+        else:
+            assert w.shape == w_expected.shape
+            assert_almost_equal(w, w_expected, rtol=rtol, atol=atol)
+
+    def newOrthonormalMatrix_2D(n):
 
 Review comment:
   Please add util functions to `mxnet.test_utils`. Also, please adopt python function's naming convention.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services