You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by zh...@apache.org on 2020/07/31 20:57:27 UTC

[incubator-mxnet] branch v1.x updated: Add unit tests for potri and potrf backward and check output shape in unit tests. (#18803)

This is an automated email from the ASF dual-hosted git repository.

zhasheng pushed a commit to branch v1.x
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/v1.x by this push:
     new 1a31cea  Add unit tests for potri and potrf backward and check output shape in unit tests. (#18803)
1a31cea is described below

commit 1a31cea22b785770958855721d1357d43be737a6
Author: Joe Evans <gi...@250hacks.net>
AuthorDate: Fri Jul 31 13:55:43 2020 -0700

    Add unit tests for potri and potrf backward and check output shape in unit tests. (#18803)
    
    Co-authored-by: Joe Evans <jo...@amazon.com>
---
 tests/nightly/test_large_array.py | 40 +++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 16 deletions(-)

diff --git a/tests/nightly/test_large_array.py b/tests/nightly/test_large_array.py
index 1f4443a..e26d73c 100644
--- a/tests/nightly/test_large_array.py
+++ b/tests/nightly/test_large_array.py
@@ -1171,26 +1171,34 @@ def test_tensor():
 
 def test_linalg():
     def check_potrf():
-        # creating an identity matrix input
-        A = nd.zeros((LARGE_SQ_X, LARGE_SQ_X))
-        for i in range(LARGE_SQ_X):
-            A[i,i] = 1
+        def run_potrf(inp):
+            inp.attach_grad()
+            with mx.autograd.record():
+                out = mx.nd.linalg.potrf(inp)
+            return inp.grad, out
 
-        out = nd.linalg.potrf(A)
-        # output should be an identity matrix
-        for i in range(LARGE_SQ_X):
-            assert out[i,i] == 1
+        A = get_identity_mat(LARGE_SQ_X)
+        grad, out = run_potrf(A)
+        assert(out.shape == (LARGE_SQ_X, LARGE_SQ_X))
+        assert(out[0, 0] == 1)
+        out.backward()
+        assert(grad.shape == (LARGE_SQ_X, LARGE_SQ_X))
+        assert(grad[0, 0] == 0.5)
 
     def check_potri():
-        # creating an identity matrix input
-        A = nd.zeros((LARGE_SQ_X, LARGE_SQ_X))
-        for i in range(LARGE_SQ_X):
-            A[i,i] = 1
+        def run_potri(inp):
+            inp.attach_grad()
+            with mx.autograd.record():
+                out = mx.nd.linalg.potri(inp)
+            return inp.grad, out
 
-        out = nd.linalg.potri(A)
-        # output should be an identity matrix
-        for i in range(LARGE_SQ_X):
-            assert out[i,i] == 1
+        A = get_identity_mat(LARGE_SQ_X)
+        grad, out = run_potri(A)
+        assert(out.shape == (LARGE_SQ_X, LARGE_SQ_X))
+        assert(out[0, 0] == 1)
+        out.backward()
+        assert(grad.shape == (LARGE_SQ_X, LARGE_SQ_X))
+        assert(grad[0, 0] == -2)
     
     def check_syrk_batch():
         # test both forward and backward