Torch.nn.utils.rnn.pad_sequence error!

Suraj520: <p>I am working with machine translation task using LSTMs(English to Hinglish), I am facing an error related to the pad_sequence module while trying to use it in a collate_fn for leveraging multiple batches.</p>
<p>The snippet of the dataset, data loader and collater(where this pad_sequence is used) is quoted below along with Debug Log highlighting shapes of dataset thus formed.</p>
<h4><a name="h-1-dataset-class-1" class="anchor" href="#h-1-dataset-class-1"></a>1. Dataset Class</h4>
<pre><code class="lang-auto">class TranslationDataset(Dataset):

    def __init__(self,dataframe,english_vocab,hinglish_vocab,transform=None):
        self.dataframe = dataframe
        self.english = self.dataframe['English'].values.tolist()
        self.hinglish = self.dataframe['Hinglish'].values.tolist()
        self.input_lang = english_vocab
        self.output_lang = hinglish_vocab
        #building vocabulary
        for english_sent in self.english:
            self.input_lang.process_sentence(english_sent)
        for hinglish_sent in self.hinglish:
            self.output_lang.process_sentence(hinglish_sent)
        # creating tensors
        self.hinglish_tensors =[tensor_from_sentence(hinglish_vocab,sentence) for sentence in self.hinglish]
        self.english_tensors = [tensor_from_sentence(english_vocab,sentence) for sentence in self.english]
        

    def __len__(self):
        return len(self.dataframe)

    def __getitem__(self,index):
        hinglish_sample = self.hinglish_tensors[index]
        english_sample = self.english_tensors[index]
        sample = {'input':english_sample,'output':hinglish_sample}

        return sample
</code></pre>
<h4><a name="h-2-debug-log-of-__getitem__-with-shapes-2" class="anchor" href="#h-2-debug-log-of-__getitem__-with-shapes-2"></a>2. Debug Log of <strong>getitem</strong> with shapes</h4>
<pre><code class="lang-auto">train_dataset = TranslationDataset(train_df,english_vocab,hinglish_vocab)
item = train_dataset.__getitem__(5)
print(item['input'], item['input'].shape, item['output'],item['output'].shape)
</code></pre>
<blockquote>
<p>STDOUT ::<br>
tensor([[84, 27, 85, 86, 25, 87, 11, 17, 15, 88, 89, 90, 91, 92, 15, 93, 17, 29,<br>
94, 23,  2]]) torch.Size([1, 21]) tensor([[ 66,  24,  81,  34, 110, 111, 112,  44, 113, 114, 115,  38, 116,  82,<br>
117,   7,  44, 118, 119, 120,  85, 121,   2]]) torch.Size([1, 23])</p>
</blockquote>
<h4><a name="h-3-collater-class-3" class="anchor" href="#h-3-collater-class-3"></a>3. Collater Class</h4>
<pre><code class="lang-auto">#collate_function
class Collater(object):
    def __init__(self, pad_index):
        self.pad_index = pad_index

    def __call__(self, batch):

        input = [item['input'] for item in batch]
        output = [item['output'] for item in batch]
        input = pad_sequence(input, batch_first=False, padding_value=self.pad_index)
        output = pad_sequence(output, batch_first=False, padding_value=self.pad_index)

        item = {'input':input, 'output':output}
        return item
</code></pre>
<h4><a name="h-4-usage-of-collater-class-in-collate_fn-arg-while-creating-dataloader-4" class="anchor" href="#h-4-usage-of-collater-class-in-collate_fn-arg-while-creating-dataloader-4"></a>4. Usage of Collater class in collate_fn arg while creating dataloader</h4>
<pre><code class="lang-auto">pad_idx = english_vocab.stoi["&lt;PAD&gt;"]
train_loader = DataLoader(train_dataset,batch_size=4,shuffle=True, num_workers=4, pin_memory=True,drop_last=True, collate_fn=Collater(pad_idx))#, collate_fn=Collater(0))
</code></pre>
<h4><a name="h-5-debug-log-of-dataloader-not-being-able-to-ensure-multiple-batches-5" class="anchor" href="#h-5-debug-log-of-dataloader-not-being-able-to-ensure-multiple-batches-5"></a>5. Debug Log of DataLoader not being able to ensure multiple batches</h4>
<pre><code class="lang-auto">for batch in train_loader:
    print(batch['input'].shape, batch['output'].shape)
    break
</code></pre>
<blockquote>
<p>STDOUT<br>
input = pad_sequence(input, batch_first=False, padding_value=self.pad_index)<br>
File “/home/suraj/anaconda3/envs/torch_dl/lib/python3.8/site-packages/torch/nn/utils/rnn.py”, line 398, in pad_sequence<br>
return torch._C._nn.pad_sequence(sequences, batch_first, padding_value)<br>
RuntimeError: The size of tensor a (27) must match the size of tensor b (10) at non-singleton dimension 1</p>
</blockquote>
<p>Don’t want to try F.pad but use pad_sequences only since it kind of worked for image captioning task !</p>
<p>Thanks in advance for the assist <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=12" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"> !</p>

Suraj520: <p>Solved !<br>
Apparently, It’s a silly mistake in get item.</p>
<pre><code class="lang-auto">hinglish_sample = self.hinglish_tensors[index][0]
english_sample = self.english_tensors[index][0]
</code></pre>