This code was taken from Here. There it was mentioned that
Since the state of the network is held in the graph and not in the
layers, you can simply create an nn.Linear and reuse it over and over
again for the recurrence.
What i don’t understand is, how can one just increase input feature size in nn.Linear and say it is a RNN. Am i missing something here.
I don’t use much RNNs.
But that depends a lot on what you want to do.
If you’re looking for standard stuff, there are modules that exist that do many to many mapping or many to one mapping and stuff. You can find them here in the doc.
This implementation is useful if you want to do something less common and you need to control how each step of the RNN is done. Possibly changing the hidden between iterations and filtering which outputs you want to return.
i just looked up and saw that torch.cat combines multiple tensor’s. so, in the code, combined = torch.cat((input, hidden), 1)
this means that the combined is input and hidden layer’s combined right. so, self.i2h is from input to hidden layer and self.i2o is the output of combined to the next layer? so, in a way, he made a single tensor for the all the time steps. Is my understanding correct?
Well this is how an rnn works: you combine the current input with the previous step’s hidden state to get on one hand the output of this step and the new hidden state.
Here to do so, the input and the previous layer hidden state are combined together into a single tensor. And then a linear layer is used to get the output and another to get the new state.