Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
fix inv test flakiness using random matrices generated by SVD (#16782)
Browse files Browse the repository at this point in the history
  • Loading branch information
vexilligera authored and reminisce committed Nov 12, 2019
1 parent 27672b9 commit b9b56e6
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions tests/python/unittest/test_numpy_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -3155,14 +3155,22 @@ def check_inv(A_inv, data_np):
test_inv = TestInverse()
if hybridize:
test_inv.hybridize()
# use LU decomposition to generate invertible matrices
# generate well-conditioned matrices with small eigenvalues
if 0 in shape:
data_np = _np.ones(shape)
else:
n = shape[-1]
L = _np.tril(_np.random.uniform(-10., 10., shape))
U = _np.triu(_np.random.uniform(-10., 10., shape))
data_np = _np.matmul(L, U)
n = int(np.prod(np.array(shape[:-2]))) if len(shape) > 2 else 1
# eigenvalues
D = _np.array([_np.diag(_np.random.uniform(-10., 10., shape[-1])) \
for i in range(n)]).reshape(shape)
# orthogonal matrix through householder transformation
I = _np.array([_np.eye(shape[-1]) for i in range(n)]).reshape(shape)
v = _np.random.uniform(-10, 10,
int(np.prod(np.array(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)
data_np = _np.matmul(_np.matmul(U, D), _np.swapaxes(U, -1, -2))
data = np.array(data_np, dtype=dtype)
data.attach_grad()
with mx.autograd.record():
Expand Down

0 comments on commit b9b56e6

Please sign in to comment.