IndexError: index out of range in self, Positional Embedding

username: Pragna_Reddy_Mandadi
cooked: <p>I’m doing Convolution Seq-Seq Learning, following this notebook: <a href="https://github.com/bentrevett/pytorch-seq2seq/blob/master/5%20-%20Convolutional%20Sequence%20to%20Sequence%20Learning.ipynb" rel="noopener nofollow ugc">https://github.com/bentrevett/pytorch-seq2seq/blob/master/5%20-%20Convolutional%20Sequence%20to%20Sequence%20Learning.ipynb</a><br>
While training I get this error:<br>
IndexError                                Traceback (most recent call last)<br>
 in ()<br>
8     start_time = time.time()<br>
9<br>
—&gt; 10     train_loss = train(model, train_iterator, optimizer, criterion, CLIP)<br>
11     valid_loss = evaluate(model, valid_iterator, criterion)<br>
12</p>
<p>7 frames<br>
 in train(model, iterator, optimizer, criterion, clip)<br>
12         optimizer.zero_grad()<br>
13<br>
—&gt; 14         output, _ = model(src, trg[:,:-1])<br>
15<br>
16         <span class="hashtag">#output</span> = [batch size, trg len - 1, output dim]</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1101                 or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1102             return forward_call(*input, **kwargs)<br>
1103         # Do not call functions when jit is used<br>
1104         full_backward_hooks, non_full_backward_hooks = [], []</p>
<p> in forward(self, src, trg)<br>
15         <span class="hashtag">#encoder_combined</span> is encoder_conved plus (elementwise) src embedding plus<br>
16         #  positional embeddings<br>
—&gt; 17         encoder_conved, encoder_combined = self.encoder(src)<br>
18<br>
19         <span class="hashtag">#encoder_conved</span> = [batch size, src len, emb dim]</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1101                 or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1102             return forward_call(*input, **kwargs)<br>
1103         # Do not call functions when jit is used<br>
1104         full_backward_hooks, non_full_backward_hooks = [], []</p>
<p> in forward(self, src)<br>
48         tok_embedded = self.tok_embedding(src)<br>
49         print(pos.shape)<br>
—&gt; 50         pos_embedded = self.pos_embedding(pos)<br>
51<br>
52         <span class="hashtag">#tok_embedded</span> = pos_embedded = [batch size, src len, emb dim]</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1101                 or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1102             return forward_call(*input, **kwargs)<br>
1103         # Do not call functions when jit is used<br>
1104         full_backward_hooks, non_full_backward_hooks = [], []</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/nn/modules/sparse.py in forward(self, input)<br>
158         return F.embedding(<br>
159             input, self.weight, self.padding_idx, self.max_norm,<br>
 → 160             self.norm_type, self.scale_grad_by_freq, self.sparse)<br>
161<br>
162     def extra_repr(self) → str:</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in embedding(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)<br>
2042         # remove once script supports set_grad_enabled<br>
2043         <em>no_grad_embedding_renorm</em>(weight, input, max_norm, norm_type)<br>
 → 2044     return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)<br>
2045<br>
2046</p>
<p>IndexError: index out of range in self</p>
<p>Can anyone help me resolve this please</p>

username: ptrblck
cooked: <p>An <code>nn.Embedding</code> layer expects inputs to contain indices in the range <code>[0, num_embeddings]</code> while your input seems to contain indices which are out of bounds. Check the min and max values of your input and make sure they are in the aforementioned range.</p>