mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

Is the LSTM example out ?

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

Hi all, I wonder if the lstm example is out? also is there C++ code examples? I've realized that most examples are in python : )

Kublai-Jing avatar Oct 20 '15 21:10 Kublai-Jing

Please check https://github.com/dmlc/mxnet/tree/master/example/rnn

antinucleon avatar Oct 21 '15 20:10 antinucleon

seems that it only supports fixed seq_len ? And the compute graph has to ben pre-compiled with fixed sequence length. Is there a suggested solution for variable length model ?

Kublai-Jing avatar Oct 22 '15 03:10 Kublai-Jing

One way to deal with that is unroll to maximum length you want to deal with, do padding on left over so batching is still enabled.

We can probably enable a condition on the internal nodes to stop execution until the maximum length of current batch

tqchen avatar Oct 22 '15 03:10 tqchen

Another way is simply compile a executor for one time-step, and use the imperative API to do the unrolling, this might actually be the easier way, and can be achieved in current API

tqchen avatar Oct 22 '15 03:10 tqchen

but in that case, does backprop through time still works ? When we proceed, the gradient will overwrite the gradient for previous step, right?

Kublai-Jing avatar Oct 22 '15 04:10 Kublai-Jing

More specific about previous comment. The problem is when to stop so not waste computation on things that are not needed. What can be done is compile a executor for every time step, say t0, t1, t2, t3.

And use imperative API to chain them up, so the control of stop goes into python side.

tqchen avatar Oct 22 '15 04:10 tqchen

We still need a different executor for each time stamp, but use python side to decide when to stop.

tqchen avatar Oct 22 '15 04:10 tqchen

does that mean we need to compile an executor every time? instead of current solution which compiles once

Kublai-Jing avatar Oct 22 '15 04:10 Kublai-Jing

No, compile executor for each time-stamp say t0, t1, t2, and you can reuse t0, t1, t2 next time, but if you need t3, then compile a new one.

tqchen avatar Oct 22 '15 04:10 tqchen

@Kublai-Jing Interested in contribute a improved version?

tqchen avatar Oct 22 '15 04:10 tqchen

yeah~ need some time to understand the whole mxnet though...

Kublai-Jing avatar Oct 22 '15 04:10 Kublai-Jing

Hope the explanation makes sense to you. There are documents explaining things, but be sure to ask if we miss something, or even better contribute to the things so others can benefit from the answer.

tqchen avatar Oct 22 '15 04:10 tqchen

This approach can also be generalized a bit, for example, unroll an executor every T step where T is a small number that we might not care about. And use imperative way to chain things up

tqchen avatar Oct 22 '15 04:10 tqchen

yes. I might wanna contribute some examples I thought useful for newbie like me. I think that will make people understand whole programming model more quickly

Kublai-Jing avatar Oct 22 '15 04:10 Kublai-Jing

That is awesome

tqchen avatar Oct 22 '15 04:10 tqchen

Will MXNet support something like the scan operator in Theano in the future? It would be really great to have one so that coding RNN will get much easier.

sxjscience avatar Oct 22 '15 05:10 sxjscience

@tqchen I assume what you mean is to compile a list of LSTM cell : LSTM[i] = LSTM for t_i ,

for t in xrange(T):
    LSTM[t] = lstm(...)
    executor[t] = LSTM[t].bind(...)
for t in xrange(len(x_i)):
    input = x_i[t]
    LSTM[t].forward()
for t in reverse(xrange(len(x_i)):
    LSTM[t].backward(...)
update

something like this. instead of having one unrolled LSTM with fixed sequence length. This should be more flexible and allow more control from the python side.

Aside from efficiency, if we use minibatch, what happens if some data already reaches its end of sequence ? If we pad zeros to those data, I believe they will still contribute to the gradient signals, which seems wrong.

Kublai-Jing avatar Oct 22 '15 19:10 Kublai-Jing

Yes,that was what I mean. The padding trick should be ok. As there is already a EOF tag introduced, and we can simply pad future labels to be EOF, and input to be EOF. The encoder/decoder will catch that EOF follows EOF, and thus no need to depend on previous result, and basically less gradient will be propagated back. This is not perfect solution, but should work

tqchen avatar Oct 22 '15 19:10 tqchen

yeah ! we still get wrong gradient, but let the network learns to fix that...lol

Kublai-Jing avatar Oct 22 '15 19:10 Kublai-Jing

I'm not sure what would be the standard way of dealing with LSTM. But it seems if along with the labels, we provide a stream of is_end_of_seq masks. And multiply 1-is_end_of_seq (maybe with an index shift) with the prev_hidden_states. Then it will allow variable-length sequences and automatically reset state propagation over time at the end of the sequence. A minor issue is if the underlying sequence is longer than the un-rolled seq length of the architecture, back-propagate-over-time will get truncated. But I think this won't be a too big issue if we un-roll for a large enough sequence size.

pluskid avatar Oct 22 '15 22:10 pluskid