Understanding pack_padded_sequence and pad_packed_sequence

vijendra_rana: <p>Hi,<br>
I have a problem understanding these 2 utilities. Not able to figure out what it does.</p>
<p>For eg. I was trying to replicate this with example from <a href="https://discuss.pytorch.org/t/simple-working-example-how-to-use-packing-for-variable-length-sequence-inputs-for-rnn/2120">Simple working example how to use packing for variable-length sequence inputs for rnn</a></p>
<p>I have followed the pytorch documentation and coded with batch First</p>
<pre>
import torch
import torch.nn as nn
from torch.autograd import Variable

batch_size = 3
max_length = 3
hidden_size = 2
n_layers =1
num_input_features = 1
input_tensor = torch.zeros(batch_size,max_length,num_input_features)
input_tensor[0] = torch.FloatTensor([1,2,3])
input_tensor[1] = torch.FloatTensor([4,5,0])
input_tensor[2] = torch.FloatTensor([6,0,0])
batch_in = Variable(input_tensor)
seq_lengths = [3,2,1]
pack = torch.nn.utils.rnn.pack_padded_sequence(batch_in, seq_lengths, batch_first=True)
print (pack)
</pre>
<p>Here I get output as</p>
<pre>
PackedSequence(data=Variable containing:
 1
 4
 6
 2
 5
 3
[torch.FloatTensor of size 6x1]
, batch_sizes=[3, 2, 1])
</pre>
<p>I could retrieve the original sequence back if I do</p>
<pre>
torch.nn.utils.rnn.pad_packed_sequence(pack,[3,2,1])
</pre>
<p>which is obvious.</p>
<p>But can somebody help me to understand how and why we got that output ‘pack’ with size (6,1). Also the whole functionality, in general, I mean why we need these 2 utilities and how it is useful.<br>
Thanks in advance for the help.</p>
<p>Cheers,<br>
Vijendra</p>

sinhasam: <aside class="quote no-group" data-username="vijendra_rana" data-post="1" data-topic="4099">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vijendra_rana/48/786_2.png" class="avatar"> vijendra_rana:</div>
<blockquote>
<p>Also the whole functionality, in general, I mean why we need these 2 utilities and how it is useful.</p>
</blockquote>
</aside>
<p>They are used for seq to seq models with variable lengths. Such as a sentence can be of variable length, and to feed it into any class of an RNN, you need to be able to get the output at the right time step.</p>
<p>Furthermore, check your dimensions on, and compare them to the original link:</p>
<aside class="quote no-group" data-username="vijendra_rana" data-post="1" data-topic="4099">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vijendra_rana/48/786_2.png" class="avatar"> vijendra_rana:</div>
<blockquote>
<p>input_tensor = torch.zeros(batch_size,max_length,num_input_features)</p>
</blockquote>
</aside>
<p>You should be getting a 3x3 PackedSequence, but you just have a small bug in the code, which is why you are getting a 6x1 PackedSequence.</p>

matthew_zeng: <p>Actually the original example set a wrong shape for input, that’s why you get 6x1 instead of 3x3.</p>
<p>The original example set shape as [batch_size, 1, max_length], which is obviously wrong</p>
<p>Without packing, you have to unroll your RNN to a fixed length. Then, you will get a fixed length of output. The desired output should have different length, so you have to mask them by yourself.</p>
<p>if you feed your minibatch with packing, the output of each example will have different length, so you don’t have to mask something by yourself.</p>

moonlightlane: <p>Hi there, thanks for the clarification! I also saw these two posts and compare then and can confirm the original post has the wrong dimension.</p>
<p>I am not sure if I understand how pytorch RNN operates, though: for example, I don’t necessary need to use pack_padded_sequence, correct? I can simply manually zero pad all sequences in a minibatch to the longest sequence, and then throw it into the RNN, which accepts input of dimension [seq_len, batch, input_size]? I think doing so (manually padding each sequence) is the same as use the pack_padded_sequence function, correct?</p>
<p>Thank you in advance for a further clarification!</p>

matthew_zeng: <p>Right, you don’t have to use pack_padded_sequence. Padding is fine, but it is different from using pack_padded_seq. For packed input, RNN will not perform calculation on pad elements.</p>
<p>For example, you have a padded mini batch (size 2), zero is padding.</p>
<p>1 1 1<br>
1 0 0</p>
<p>The output will be 3 (seq length) x 2 (batch size). However, packed input will result in a packed output contains (3 x 1 and 1 x 1). If you feed pack into RNN, it will not calculate output for your pad element. Moreover, <code>hidden</code> will be the hidden after the last valid input instead of hidden after the last zero padding (if you feed pad into rnn, hidden will be in the case).</p>
<p>RNN actually does not distinguish pad and valid elements, and it performs the same calculation on them. You may need to clean the output (e.g., mask output) to get the result you want. Dynamic RNN (feed with packed input) does not have this problem.</p>

sritvik: <p>Getting a similar issue here,</p>
<p>the inputs provided for pack_padded_sequence: <em>sent</em>, <em>sent_len</em>.<br>
Where <em>sent</em> is the input (batch_size, seq_length, features/embedding_dim), with dimension [torch.FloatTensor of size 100x16x200]<br>
and <em>sent_len</em> is a list containing the actual (unpadded) lengths of all sequences in the batch.</p>
<pre><code>print sent.size() # (100L, 16L, 200L), i.e., (batch size, padded sequence length,embedding dimension / feature)
print len(sent_len) # 100, i.e., the length of the list containing sequence lengths of each element in the batch

sent_packed = nn.utils.rnn.pack_padded_sequence(sent, sent_len,batch_first=True)
print sent_packed
</code></pre>
<p>Now the output of sent_packed is expected to have a dimension same as sent, right?<br>
I am getting the following:</p>
<pre><code>PackedSequence(data=Variable containing:
 0.1084  0.3546  0.4458  ...  -1.1613  1.1618  0.4275
 0.0564 -1.0614  0.1452  ...  -0.7359 -0.2980 -1.9538
 0.8342  0.2849  0.5471  ...  -0.9297 -0.3760  0.4382
          ...             â±             ...          
 0.5107  1.6905  0.3308  ...  -0.8220  0.7505 -0.9616
 1.5038  0.3528 -1.4010  ...  -0.9663 -0.7744 -0.5839
-0.7513  1.6879 -0.1883  ...   1.1898  0.5734  0.1458
[torch.FloatTensor of size 818x200]
, batch_sizes=[100, 100, 100, 100, 100, 99, 77, 58, 35, 25, 11, 5, 3, 2, 2, 1])
</code></pre>
<p>Any idea what’s the bug?</p>

Suthee: <p>PackedSequence(data=Variable containing:<br>
1<br>
4<br>
6<br>
2<br>
5<br>
3<br>
[torch.FloatTensor of size 6x1]<br>
, batch_sizes=[3, 2, 1])</p>
<p>I thought the output is expected and correct.<br>
The feature dimension is 1, so each row of 6x1 Tensor represents one token of the given sequence. There are 6 tokens total and 3 sequences. Then, batch_sizes = [3,2,1] also makes sense because the first iteration to RNN should contain the first tokens of all 3 sequences ( which is [1, 4, 6]). Then for the next iterations, batch size of 2 implies the second tokens out of 3 sequences which is [2, 5] because the last sequence has a length of 1.</p>
<p>Did I misunderstand something here? Thanks!</p>

Suthee: <p>I am new here so I might be wrong. I thought this is a correct behavior.  There are 818 tokens/elements and each has 200 dimensions.</p>
<p>np.sum([100, 100, 100, 100, 100, 99, 77, 58, 35, 25, 11, 5, 3, 2, 2, 1]) is 818.</p>

11177: <p>You are absolutely right!</p>

sitara_J: <p>I run your codes but I didn’t get the output ,instead I got the error<br>
“The expanded size of the tensor (1) must match the existing size (3) at non-singleton dimension 1”<br>
Can anybody tell me why and how to fix it? thank you!</p>

hughperkins: <p>I get that error too.  I rewrote it slightly, and got rid of the error, but I havent double checked whether it’s diong what it’s supposed to be doing. No error message though <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<pre><code>import torch
import torch.nn as nn


batch_size = 3
max_length = 3

batch_in = torch.LongTensor(batch_size, max_length).zero_()
batch_in[0] = torch.LongTensor([1, 2, 3])
batch_in[1] = torch.LongTensor([4, 5, 0])
batch_in[2] = torch.LongTensor([6, 0, 0])
seq_lengths = [3, 2, 1]
pack = torch.nn.utils.rnn.pack_padded_sequence(batch_in, seq_lengths, batch_first=True)
print(pack)
</code></pre>
<p>output:</p>
<pre><code>PackedSequence(data=tensor([ 1,  4,  6,  2,  5,  3]), batch_sizes=tensor([ 3,  2,  1]))</code></pre>

Revo_Let: <p>Is there an effect of choosing to use PackedSequence over manual padding in terms of loss/accuracy? Suppose I padded all my sequences with 0’s to a fixed length. During training I retrieve the output from the last hidden unit in an LSTM/RNN for prediction (in a many-to-one fashion.) In many of the samples, a bunch of meaningless 0’s have been incorporated in the prediction. Should this hurt accuracy? It sounds so, but I can’t really prove it…</p>

matthew_zeng: <p>Padding has an effect on hidden. However, in many cases (e.g., the length of input are quite close) the effect is quite small.</p>
<p>If a batch consists of a very long sequence and some very short sequences, then the short ones have to be padded with many zeros. Then, performing RNN on the short sequences which are padded with zeros will be equal to performing RNN on zero sequences, right?</p>

Suzyahyah: <p>Padding: Standardises variable length sequence</p>
<p>Packing: Format for RNN to ignore the “pads”. Note that we feed the original length (before padding) as input to the pack_pad_sequence function.</p>
<p>The whole sequence is</p>
<ol>
<li>pad</li>
<li>embed</li>
<li>pack_padded<br>
– [rnn] --&gt;</li>
<li>pad_packed</li>
<li>eval</li>
</ol>
<p>The second pad_packed is basically an “unpack”.</p>
<p>Here is a minimal working example with some explanation, hope it helps.<br>
<a href="https://suzyahyah.github.io/pytorch/2019/07/01/DataLoader-Pad-Pack-Sequence.html" class="onebox" target="_blank" rel="nofollow noopener">https://suzyahyah.github.io/pytorch/2019/07/01/DataLoader-Pad-Pack-Sequence.html</a></p>

tpatel0409: <aside class="quote no-group" data-username="hughperkins" data-post="12" data-topic="4099">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/hughperkins/48/915_2.png" class="avatar"> hughperkins:</div>
<blockquote>
<p>PackedSequence(data=tensor([ 1, 4, 6, 2, 5, 3]), batch_sizes=tensor([ 3, 2, 1]))</p>
</blockquote>
</aside>
<p>One Basic doubt here,<br>
Isn’t the input already Zero padded here ?<br>
I think, The only thing which pack padded sequence gonna do is pack the data so that the dynamic unrolling of the RNN happens accordingly and the extra zero padded sequences aren’t gonna be used in forward pass.</p>

ironv: <p>Thanks for posting the example on your blog (…DataLoader-Pad-Pack-Sequence.html), I found it extremely useful.  One of the things which is seemingly glossed over is the initialization of the hidden state</p>
<pre><code class="lang-auto">output_packed, hidden = rnn(x_packed, hidden)
</code></pre>
<p>can you please elaborate on how to initialize the hidden state.  Should it be done</p>
<ul>
<li>beginning of every minibatch?</li>
<li>beginning of every epoch?</li>
<li>values set to 0 or random values?</li>
<li>should the hidden state be explicitly saved and passed back in?</li>
</ul>
<p>Thx.</p>

Suzyahyah: <p>Cheers, I’m really glad it helped! (and apologies to the mods and other readers for the cross post)</p>
<p>If each data instance is not related e.g, random shuffled, it is more appropriate to initialize the hidden state each time at the start of the sequence.</p>
<ul>
<li>
<p><strong>Beginning of every minibatch:</strong> This implies that we are using the hidden state from the previous sentence/sequence to initialise the next sequence. We might want to do this if the instances in the minibatch are related. Maybe a minibatch is a paragraph of sentences?</p>
</li>
<li>
<p><strong>Beginning of every epoch:</strong> Same as above, except on a larger scale.</p>
</li>
<li>
<p><strong>Values set to 0 or random:</strong> Both are valid approaches. There is less risk of overfitting if the hidden state is randomly initialized, as compared to 0 all the time. Having said that there are “stronger” ways of preventing overfitting like dropout which is tunable. So I would think if your goal is to squeeze the last bit of performance, try both. If you are implementing for research, just go with 0.</p>
</li>
</ul>
<p>There are also more advanced methods like learning the initial hidden state, which is an ongoing area of research.</p>

Pratheesh_Kumar: <p>In case of a chatbot application where I  feed batch of inputs in that case how do I initialize the hidden state(inital)</p>