Explicitly initialize RNN/LSTM/GRU states #2314
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of changes:
The current from-scratch implementations of the RNN, LSTM and GRU do some complicated juggling with the initial states of the recurrent module.
Example for the current LSTM:
d2l-en/chapter_recurrent-modern/lstm.md
Lines 286 to 304 in f4432f3
While this is clever and avoids a little bit of code, it has a few downsides:
if H is None
block that runs at every time step, but only does something in the first step of the RNN. From a good programming perspective, thisif
should be placed outside the loop.With this pull request, I'd like to suggest some changes to initialize the states explicitly before the for loop. This requires one or two extra lines of code, but I think it would look cleaner and might make the models easier to follow.
Note that I added a
self.num_hiddens
to the RNN/LSTM/GRU classes. This is isn't absolutely necessary, you could also get this from the shape of the weights, but I think giving it a name might help.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.