Skip to content

Commit

Permalink
Reduce numerical error on numerical gradient calculations
Browse files Browse the repository at this point in the history
Fixes apache#11720
Overall will reduce flakiness of tests using numerical gradients
  • Loading branch information
larroy committed Aug 6, 2019
1 parent 51c07e5 commit 998b3f8
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions python/mxnet/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,18 +906,6 @@ def check_numeric_gradient(sym, location, aux_states=None, numeric_eps=1e-3, rto
if ctx is None:
ctx = default_context()

def random_projection(shape):
"""Get a random weight matrix with not too small elements
Parameters
----------
shape : list or tuple
"""
# random_projection should not have elements too small,
# otherwise too much precision is lost in numerical gradient
plain = np.random.rand(*shape) + 0.1
return plain

location = _parse_location(sym=sym, location=location, ctx=ctx, dtype=dtype)
location_npy = {k:v.asnumpy() for k, v in location.items()}
aux_states = _parse_aux_states(sym=sym, aux_states=aux_states, ctx=ctx,
Expand All @@ -941,12 +929,11 @@ def random_projection(shape):
input_shape = {k: v.shape for k, v in location.items()}
_, out_shape, _ = sym.infer_shape(**input_shape)
proj = mx.sym.Variable("__random_proj")
out = sym * proj
out = sym + proj
out = mx.sym.make_loss(out)

location = dict(list(location.items()) +
[("__random_proj", mx.nd.array(random_projection(out_shape[0]),
ctx=ctx, dtype=dtype))])
location = dict(list(location.items()) + [("__random_proj",
mx.nd.random.uniform(shape=out_shape[0], ctx=ctx, dtype=dtype))])
args_grad_npy = dict([(k, np.random.normal(0, 0.01, size=location[k].shape))
for k in grad_nodes]
+ [("__random_proj", np.random.normal(0, 0.01, size=out_shape[0]))])
Expand Down

0 comments on commit 998b3f8

Please sign in to comment.