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

Commit

Permalink
Set idx2name for Optimizer object (#14703) (#14772)
Browse files Browse the repository at this point in the history
* set idx2name for optimizer

* add unit test
  • Loading branch information
yuxihu authored and szha committed Apr 23, 2019
1 parent b7372d3 commit 0eab551
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
2 changes: 2 additions & 0 deletions python/mxnet/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,8 @@ def fit(self, X, y=None, eval_data=None, eval_metric='acc',
rescale_grad=(1.0/batch_size),
**(self.kwargs))
elif isinstance(self.optimizer, opt.Optimizer):
if not optimizer.idx2name:
optimizer.idx2name = param_idx2name.copy()
optimizer = self.optimizer

# do training
Expand Down
16 changes: 9 additions & 7 deletions python/mxnet/module/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,14 +505,14 @@ def init_optimizer(self, kvstore='local', optimizer='sgd',
batch_size *= kvstore.num_workers
rescale_grad = 1.0/batch_size

idx2name = {}
if update_on_kvstore:
idx2name.update(enumerate(self._exec_group.param_names))
else:
for k in range(len(self._context)):
idx2name.update({i*len(self._context)+k: n
for i, n in enumerate(self._exec_group.param_names)})
if isinstance(optimizer, str):
idx2name = {}
if update_on_kvstore:
idx2name.update(enumerate(self._exec_group.param_names))
else:
for k in range(len(self._context)):
idx2name.update({i*len(self._context)+k: n
for i, n in enumerate(self._exec_group.param_names)})
optimizer_params = dict(optimizer_params)
if 'rescale_grad' not in optimizer_params:
optimizer_params['rescale_grad'] = rescale_grad
Expand All @@ -528,6 +528,8 @@ def init_optimizer(self, kvstore='local', optimizer='sgd',
"is not normalized to 1.0/batch_size/num_workers (%s vs. %s). "%(
optimizer.rescale_grad, rescale_grad) +
"Is this intended?", stacklevel=2)
if not optimizer.idx2name:
optimizer.idx2name = idx2name.copy()

self._optimizer = optimizer
self._kvstore = kvstore
Expand Down
28 changes: 28 additions & 0 deletions tests/python/unittest/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,34 @@ def empty_fn(*args, **kwargs):
mod.fit(train_data, num_epoch=1)



def test_module_init_optimizer():
def get_module_idx2name(mod):
idx2name = {}
idx2name.update(enumerate(mod._exec_group.param_names))
return idx2name

data = mx.sym.Variable('data')
sym = mx.sym.FullyConnected(data, num_hidden=20, name='fc')
batch_size = 8
opt_params = {'learning_rate': 1, 'rescale_grad': 1.0 / batch_size}

# Pass an optimizer str
mod1 = mx.mod.Module(sym, ('data',), None, context=mx.cpu(0))
mod1.bind(data_shapes=[('data', (batch_size, 20))])
mod1.init_params()
mod1.init_optimizer(optimizer='sgd', optimizer_params=opt_params)
assert mod1._optimizer.idx2name == get_module_idx2name(mod1)

# Pass an Optimizer object
mod2 = mx.mod.Module(sym, ('data',), None, context=mx.cpu(0))
mod2.bind(data_shapes=[('data', (batch_size, 20))])
mod2.init_params()
opt = mx.optimizer.SGD(**opt_params)
mod2.init_optimizer(optimizer=opt)
assert mod2._optimizer.idx2name == get_module_idx2name(mod2)


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

1 comment on commit 0eab551

@KriteshGarg
Copy link

@KriteshGarg KriteshGarg commented on 0eab551 Jun 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getting error as optimizer not defined....

opt = mx.optimizer.Adam(learning_rate=1e-3, wd=0.0005,
	rescale_grad=1.0 / batchSize)

model = mx.model.FeedForward(
	ctx=[mx.gpu(0)], symbol=model,
	initializer=mx.initializer.Xavier(),
	arg_params=argParams,
	aux_params=auxParams,
	optimizer=opt,	num_epoch=110,
	begin_epoch=args["start_epoch"])

model.fit(
	X=trainIter,
	eval_data=valIter,
	eval_metric=metrics,
	batch_end_callback=batchEndCBs,
	epoch_end_callback=epochEndCBs)
```


Throws optimizer referenced before assignment in fit
Changed fit 
elif to this makes it work is this correct ? 


 ```
        # init optmizer
        if isinstance(self.optimizer, str):
            batch_size = data.batch_size
            if kvstore and 'dist' in kvstore.type and '_async' not in kvstore.type:
                batch_size *= kvstore.num_workers
            optimizer = opt.create(self.optimizer,
                                   rescale_grad=(1.0/batch_size),
                                   **(self.kwargs))
        elif isinstance(self.optimizer, opt.Optimizer):

            if not self.optimizer.idx2name:
                self.optimizer.idx2name = param_idx2name.copy()
            optimizer = self.optimizer


```
Please update if I am doing anything wrong ?

Please sign in to comment.