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

[Numpy] Fix collect_params().zero_grad() in gluon numpy interface #16716

Merged
merged 4 commits into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions python/mxnet/gluon/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from collections import OrderedDict, defaultdict
import warnings
import numpy as np
import mxnet as mx

from ..base import mx_real_t, MXNetError
from .. import symbol, ndarray, initializer, context
Expand Down Expand Up @@ -896,15 +895,20 @@ def zero_grad(self):
continue
for g in p.list_grad():
if g.stype == 'row_sparse':
mx.ndarray.zeros_like(g, out=g)
ndarray.zeros_like(g, out=g)
else:
arrays[g.context].append(g)

if len(arrays) == 0:
return

for arr in arrays.values():
mx.nd.reset_arrays(*arr, num_arrays=len(arr))
if is_np_array():
for arr in arrays.values():
for ele in arr:
ele[()] = 0
else:
for arr in arrays.values():
ndarray.reset_arrays(*arr, num_arrays=len(arr))

def reset_ctx(self, ctx):
"""Re-assign all Parameters to other contexts.
Expand Down
17 changes: 17 additions & 0 deletions tests/python/unittest/test_numpy_gluon.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,23 @@ def hybrid_forward(self, F, x, weight):
assert_almost_equal(out.asnumpy(), (x.asnumpy() + const_arr), atol=1e-5, rtol=1e-4, use_broadcast=False)


@use_np
def test_parameters_zero_grad():
for hybridize in [False, True]:
net = gluon.nn.HybridSequential()
for _ in range(5):
net.add(gluon.nn.Dense(10))
if hybridize:
net.hybridize()
net.initialize()
out = net(mx.np.ones((32, 8)))
for v in net.collect_params().values():
v.grad()[()] = 1
net.collect_params().zero_grad()
for v in net.collect_params().values():
assert_almost_equal(v.grad().asnumpy(), mx.np.zeros_like(v.grad()).asnumpy())


if __name__ == '__main__':
import nose
nose.runmodule()