Hidden state initialization for RNNs

shavitamit: <p>When using a standard <code>nn.GRU()</code> for performing RNNs, how should one initialize the hidden state?  Currently, I do the following:</p>
<pre><code>def init_hidden(self, batch_size):
    # variable of size [num_layers*num_directions, b_sz, hidden_sz]
    return Variable(torch.zeros(self.num_directions, batch_size, self.hidden_size)).cuda() 
</code></pre>
<p>This works - however, does this assume the network learns a different hidden state for each element in the batch?  Should I somehow be replicating the <strong>same</strong> initial hidden state for every element in the batch, so that the network doesn’t need to learn <code>batch_sz</code> hidden states, but rather one hidden state?</p>

shavitamit: <p>I don’t think you declare the hidden state assuming no batches, given that the documentation specifies that:</p>
<blockquote>
<p>h_0 (num_layers * num_directions, batch, hidden_size): tensor containing the initial hidden state for each element in the batch.</p>
</blockquote>
<p>This implies a hidden state of dimensions <code>[1, batch_sz, hidden_size]</code> for a single-layer uni-directional RNN.</p>

sg314: <p>Right, my mistake. Sorry, I missed that.</p>

SimonW: <p>I’m a bit confused. Why do you want the network to output same hidden state for each data in the batch? If so, there will be no correlation along the time dimension, and you would be essentially predicting current timestep output from current timestep input only. If that is something you want in your use case, maybe try non-recurrent models? <img src="//discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

shavitamit: <p>Thanks Simon.  The question is more around learning hidden states.  Currently I assign zero initial states, and I actually don’t make them volatile so they do have a <code>grad_fn</code>.  However, I don’t see the point of that, since I feed a new initial state to every sequence I get (before training the network with a new batch sequence, I call <code>init_hidden()</code> above.</p>
<p><a href="https://www.cs.toronto.edu/~hinton/csc2535/notes/lec10new.pdf" rel="nofollow noopener">This pdf</a> (by Hinton) mentions that it’s better to actually learn the hidden states of the network, which got me thinking to maybe assigning one hidden state to the network and essentially replicate it across each element in the batch, so it can learn the hidden state.</p>
<p>What do you think?</p>

SimonW: <p>I didn’t read the slides in details, but I still don’t understand why initializing your hidden state to something non-zero (independent of input) would be very different? Within the sequence, the hidden states are still passed through, which is a rather more important thing.</p>

shavitamit: <p>Fair enough - agreed.  My question wasn’t around what to initialize the hidden state to, whether zeros or 0.5, but rather whether it’s customary to initialize the hidden state <strong>before each sequence</strong> like I do above, or whether some people initialize the hidden state <strong>once</strong> during training and keep evolving it as the network sees more sequences (i.e., the <code>init_hidden()</code> function above would only be called <strong>once</strong> in the beginning, not before each sequence).</p>

SimonW: <p>You should to do it for each training sequence <img src="//discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:">. The logic is that you probably don’t want to introduce inter-data dependencies on training. If that happens, then what you get from a model depends on what you feed into it before. That is probably something you don’t want, especially in test time.</p>

shavitamit: <p>Got it - thanks Simon!</p>

Pratheesh_Kumar: <p>Yeah as you said Why do we need to specify batch_size . Here we are taking about initializing the the initial hidden states to the gru model so isn’t it supposed to be of shape<br>
[no_of_stacked_layer , hidden_size_of_gru]. Why do we need to include the batch_size in the shape . I couldn’t get my head around this. Can anyone clarify my doubts.</p>

ramisyuanbo: <p>Hi, since you are providing an all zero initial hidden, why not just pass a None as hidden? that works for me.</p>