mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

bug in 'lr_mult' attribute and optimizer

Open sodabeta7 opened this issue 10 years ago • 11 comments

When user explicitly specify weight and bias variables in FullyConnected and Convolution, the `lr_mult`` attribute can not be set correctly, and the optimizer can not get correct learning rate in this case.

example:

W=mx.sym.Variable(name='custom-weight')
b=mx.sym.Variable(name='custom-bias')
fc=mx.sym.FullyConnected(data=data, weight=W, bias=b, num_hidden=10, name='fc')

sodabeta7 avatar Jul 29 '16 09:07 sodabeta7

@pluskid @piiswrong

sodabeta7 avatar Jul 29 '16 09:07 sodabeta7

W and b is not variables. they are ndarray type.

starimpact avatar Jul 29 '16 14:07 starimpact

@starimpact Thanks for your reply. But W & b are indeed mx.Variables. Current implementation only works when the weight and bias variables are generated by default, whose names would be fc_weight and fc_bias for the example case. But if we explicitly specify them and the name changes, it would be incorrect. You can simply verify that by printing lr in any optimizer.

sodabeta7 avatar Jul 29 '16 14:07 sodabeta7

what I mean is that you should create W and b as ndarray type. if you create them as variable type, you will fix the sizes in the bind function.It is complicated. And ,I think your name should be "_weight" and "_bias". I do not use optimizer, I update the parameters by myself code.

starimpact avatar Jul 29 '16 14:07 starimpact

If you create variables explicitly, you should follow the same naming convention, i.e. xxx_weight for weights and xxx_bias for bias.

piiswrong avatar Jul 30 '16 19:07 piiswrong

@piiswrong I explicitly create variables for sharing weight in RNN, just like the RNN example in MxNet repo.

sodabeta7 avatar Jul 30 '16 23:07 sodabeta7

@piiswrong I don't know if I made it clear. For implementing RNN and many other cases, users need to explicitly create shared weight and bias symbols between different operators, we cannot follow the naming convention you mentioned.

sodabeta7 avatar Aug 02 '16 18:08 sodabeta7

what you need to do is only to let your postfix be fixed.It is not contradictory with your implementation.

starimpact avatar Aug 03 '16 02:08 starimpact

@sodabeta7 I met the same problem. It seems that the problem is due to the _get_lr(self, index) function in optimizer.py. In lr *= self.lr_mult.get(self.idx2name[index], 1.0) (https://github.com/dmlc/mxnet/blob/master/python/mxnet/optimizer.py#L205) the names in idx2name does not exist in the keys of self.lr_mult. Thus it will return 1.0.

wshenx avatar Sep 19 '16 00:09 wshenx

if you create variables by hand, set attributes in the variable, e.g.:

W=mx.sym.Variable(name='custom-weight', attr={'lr_mult':'0.01'})
b=mx.sym.Variable(name='custom-bias')
fc=mx.sym.FullyConnected(data=data, weight=W, bias=b, num_hidden=10, name='fc')

piiswrong avatar Sep 19 '16 00:09 piiswrong

@piiswrong Thanks. That seems a good solution.

wshenx avatar Sep 19 '16 00:09 wshenx