Trying to understand PackedSequence and pack_padded_sequence

Jesper_Rix: <p>Hi guys,</p>
<p>I’m new to PyTorch and i’m trying to grasp PackedSequence.</p>
<p>I have a batch of some sentences with variable length, which I wanna translate into PackedSequence, so i can feed them into an RNN.</p>
<p>The longest sentence in the batch is 11 words. Dimension of my word vectors are [1, 240] each.</p>
<p>I have padded each sentence to match the length of the longest sequence therefore my input Tensor is:</p>
<pre><code># batch_size, max_len, num_features
batch_in.size()
torch.Size([100, 11, 240])
</code></pre>
<p><code>packed_seq = nn.utils.rnn.pack_padded_sequence(batch_in, batch_in_lengths,batch_first=True)</code></p>
<p>And the output of packed_seq:</p>
<pre><code>PackedSequence(data=Variable containing:
 3.1625e-01 -4.8614e-01  4.9205e-01  ...  -3.2214e-01 -1.0503e+00 -7.7392e-01
-2.3958e-01 -5.7964e-01 -1.1852e+00  ...  -4.7232e-02 -8.6795e-01  9.3440e-03
-2.3958e-01 -5.7964e-01 -1.1852e+00  ...  -4.7232e-02 -8.6795e-01  9.3440e-03
                ...                   ⋱                   ...                
-5.1679e-01  7.7076e-01  4.5970e-01  ...  -3.2855e-01  8.4874e-01  2.6562e-01
 7.2816e-01 -5.6678e-01 -1.5125e+00  ...   3.1464e-01 -6.4921e-01 -1.1999e+00
 1.2031e+00 -1.0457e-01  5.2758e-01  ...  -5.4365e-01  7.4296e-01  1.9522e-01
[torch.DoubleTensor of size 379x240]
, batch_sizes=[100, 100, 100, 42, 13, 10, 7, 3, 2, 1, 1])
</code></pre>
<p>Can somebody explain me why the batch_sizes looks the way it does?</p>

chenyuntc: <p>well, the <code>batch_sizes</code> here  is not actually the <code>batch_size</code> of <code>batch_in</code></p>
<pre><code class="lang-auto">packed_seq = nn.utils.rnn.pack_padded_sequence(batch_in.transpose(0,1), batch_in_lengths,batch_first=False)
</code></pre>
<p>you’ll get familiar batch size.</p>

josmi9966: <p>I also do not understand where the batch_sizes shown for the packed sequence come from.</p>
<p>Also, I would like to know how to best “unsort” the result of running the packed sequence through an LSTM and then unpacking: the unsorting would need to be done in a way to preserve the gradients and I am not sure how to do that best.</p>

olegboiko: <p>RNN will produce number of hidden states equal to the size of the longest input. Let’s assume we train RNN on sentences in some large corpus of text. If longest sentence in training data contains <code>N</code> words, then RNN will produce <code>N</code> hidden states.</p>
<p>Sentences fed into RNN in batches. But different sentences may have different lengths. Consider the following batch of 2 sentences:<br>
Sentence 1: <code>"I love cats"</code><br>
Sentence 2: <code>"I hope this answer helps"</code><br>
<em>(for brevity, I omit tensor-like representation of these sentences, with word embeddings etc).</em></p>
<p>This batch will produce 5 hidden states because 5 is the size of the longest sentence in the batch:<br>
<code>len(["i", "hope", "this", "answer", "helps"]) == 5</code></p>
<p>Now, RNN has to feed information into its first hidden state. It takes first elements from each sequence in batch and feeds them into the first hidden state: <code>["i", "i"]</code>.<br>
Second hidden state will receive <code>["love", "hope"]</code>.<br>
Third hidden state will receive <code>["cats", "this"]</code>.<br>
But forth hidden state will receive just <code>["answer"]</code> because first sequence <code>"i love cats"</code> ended at step <code>#3</code>.</p>
<p><code>batch_sizes</code> is a list of sizes of inputs each hidden state receives. For this example, <code>batch_sizes</code> would be <code>[2, 2, 2, 1, 1]</code>. Each element in this list tells how many input elements go into corresponding hidden state of RNN.</p>
<p>I guess confusion stems from the fact that term <code>batch</code> is overloaded here. There is <code>batch</code> of sequences which goes into RNN, and then there is <code>batch</code> of inputs which goes into each step of the RNN.</p>

yagneshaunuicp: <p>@ <a href="https://discuss.pytorch.org/u/olegboiko">olegboiko</a>, Thank you for the reply, this was much needed. I think the PyTorch documentation needs improvement a lot.</p>