title: pytorch embedding index out of range
body: <p>I'm following this tutorial here <a href="https://cs230-stanford.github.io/pytorch-nlp.html" rel="noreferrer">https://cs230-stanford.github.io/pytorch-nlp.html</a>. In there a neural model is created, using <code>nn.Module</code>, with an embedding layer, which is initialized here</p>
  
  <pre><code>self.embedding = nn.Embedding(params['vocab_size'], params['embedding_dim'])
  </code></pre>
  
  <p><code>vocab_size</code> is the total number of training samples, which is 4000.  <code>embedding_dim</code> is 50.  The relevant piece of the <code>forward</code> method is below</p>
  
  <pre><code>def forward(self, s):
          # apply the embedding layer that maps each token to its embedding
          s = self.embedding(s)   # dim: batch_size x batch_max_len x embedding_dim
  </code></pre>
  
  <p>I get this exception when passing a batch to the model like so
  <code>model(train_batch)</code>
  <code>train_batch</code> is a numpy array of dimension <code>batch_size</code>x<code>batch_max_len</code>.  Each sample is a sentence, and each sentence is padded so that it has the length of the longest sentence in the batch.</p>
  
  <blockquote>
    <p>File
    "/Users/liam_adams/Documents/cs512/research_project/custom/model.py",
    line 34, in forward
        s = self.embedding(s)   # dim: batch_size x batch_max_len x embedding_dim   File
    "/Users/liam_adams/Documents/cs512/venv_research/lib/python3.7/site-packages/torch/nn/modules/module.py",
    line 493, in <strong>call</strong>
        result = self.forward(*input, **kwargs)   File "/Users/liam_adams/Documents/cs512/venv_research/lib/python3.7/site-packages/torch/nn/modules/sparse.py",
    line 117, in forward
        self.norm_type, self.scale_grad_by_freq, self.sparse)   File "/Users/liam_adams/Documents/cs512/venv_research/lib/python3.7/site-packages/torch/nn/functional.py",
    line 1506, in embedding
        return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse) RuntimeError: index out of range at
    ../aten/src/TH/generic/THTensorEvenMoreMath.cpp:193</p>
  </blockquote>
  
  <p>Is the problem here that the embedding is initialized with different dimensions than those of my batch array?  My <code>batch_size</code> will be constant but <code>batch_max_len</code> will change with every batch. This is how its done in the tutorial.</p>