mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

LSTM, 'add' and 'write' produce the same results?

Open Kublai-Jing opened this issue 11 years ago • 5 comments

Have been working on lstm recently. Found that 'add' and 'write' grad_reqs produce the same results

rnn_exec = rnn_sym.bind(ctx=ctx, args=arg_arrays,
                        args_grad=args_grad,
                        grad_req="write")    

which for the first two epochs, I have, screen shot 2015-11-25 at 7 55 23 pm

And for the 'add' version

rnn_exec = rnn_sym.bind(ctx=ctx, args=arg_arrays,
                        args_grad=args_grad,
                        grad_req="add")    

screen shot 2015-11-25 at 7 56 23 pm

These two should not be the same ?

Kublai-Jing avatar Nov 26 '15 03:11 Kublai-Jing

Yes!If you are using write, you can safely remove reset gradient code at the end of epoch

antinucleon avatar Nov 26 '15 04:11 antinucleon

I mean, if I am using 'write' for the lstm, should it produce the same (and correct) result as using 'add' ? If that's the case, where is 'add' useful? Thanks !

Kublai-Jing avatar Nov 26 '15 04:11 Kublai-Jing

hmm. I think I see what you mean, the 'add' only make difference when we call updater(idx, grad, weight).
Using write will overwrite the gradient inside that call, whereas using 'add' will add to that. So I guess the engine will itself add up gradients to the same parameter when we call backward, which means that using 'write' is also correct for lstm in this case.

Kublai-Jing avatar Nov 26 '15 04:11 Kublai-Jing

I think 'add' means the calculated gradient should be added to rather than written to the gradient variable. This is useful for RNN since RNN will calculate 't' ('t' is the number of unrolling length) gradients of the gate parameters, and all these gradients should be accumulated to update the gate parameters. In fact, I am quite surprised that the 'write' strategy gives the same result as 'add' strategy. Let me look into it.

jermainewang avatar Nov 26 '15 04:11 jermainewang

Is it related to this? #568

pluskid avatar Nov 26 '15 05:11 pluskid