IndexError: index 20 is out of bounds for dimension 1 with size 20

username: JYIJ_94
cooked: <p>I was trying to train a model and getting the following error. I am confused why I am getting this error.<br>
And the shape of all the variables are:<br>
Data shape:  torch.Size([321, 20, 14])<br>
Mask shape: torch.Size([321, 20])<br>
Decay shape: torch.Size([321, 20])<br>
Redecay shape:  torch.Size([321, 20])</p>
<p>The code is given below -</p>
<pre><code class="lang-auto">data = T.as_tensor(data.values.astype(float), dtype=T.float32)
data=data.view(int(data.shape[0]/args.seq_len), args.seq_len, data.shape[1])
mask = T.as_tensor(mask.values.astype(float), dtype=T.float32)
mask=mask.view(int(mask.shape[0]/args.seq_len), args.seq_len, mask.shape[1])
decay=mask[:,:,1]
rdecay=mask[:,:,2]
mask=mask[:,:,0]

data=data.squeeze()
mask=mask.squeeze()
decay=decay.squeeze()
rdecay=rdecay.squeeze()
ret_f, ret, disc = run_on_batch(model,discriminator,data,mask,decay,rdecay, args, optimizer=None,optimizer_d=None,epoch=None)

</code></pre>
<p>Error message:</p>
<pre><code class="lang-auto">IndexError                                Traceback (most recent call last)
&lt;ipython-input-161-f02c9fd37373&gt; in &lt;module&gt;
      3 #oBmi, oBmiF, iBmi, oAge, oSex = run(ARGS)
      4 
----&gt; 5 run(ARGS)
      6 
&lt;ipython-input-160-636128776031&gt; in run(args)
     26 
     27     elif args.train:
---&gt; 28         trainLoss,discLoss,gLoss, valLoss,discValLoss,gValLoss = run_epoch(args, model, discriminator)
     29         # return trainLoss,discLoss,gLoss, valLoss,discValLoss,gValLoss
     30 
&lt;ipython-input-159-922ded87c953&gt; in run_epoch(args, model, discriminator)
     84 
     85 
---&gt; 86         ret_f, ret, disc = run_on_batch(model,discriminator,data,mask,decay,rdecay, args, optimizer,optimizer_d,epoch)#,bmi_norm)
     87         print(ret_f)
     88         RLoss=RLoss+ret['loss'].item()
~/Work/deep-learning-based-packet-imputation/BiGAN/biGan/bgan_i_ganOrig.ipynb in run_on_batch(model, discriminator, data, mask, decay, rdecay, args, optimizer, optimizer_d, epoch)
~/.local/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--&gt; 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

~/Work/deep-learning-based-packet-imputation/BiGAN/biGan/bgan_i_ganOrig.ipynb in forward(self, data, mask, decay, rdecay, args)

~/.local/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--&gt; 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

~/Work/deep-learning-based-packet-imputation/BiGAN/biGan/ugan_i_ganOrig.ipynb in forward(self, values, masks, deltas, args, direct)

IndexError: index 20 is out of bounds for dimension 1 with size 20
</code></pre>
<p>Thank you for your help.</p>

username: ptrblck
cooked: <p>Python and PyTorch use 0-based indexing so a tensor with a shape <code>[batch_size, 20]</code> can be indexed with indices in <code>[0, 19]</code> in <code>dim1</code> while you are passing <code>20</code>:</p>
<pre><code class="lang-python">x = torch.randn(6, 20)
x[:, 19] # works
x[:, 20] # fails
# IndexError: index 20 is out of bounds for dimension 1 with size 20
</code></pre>

username: JYIJ_94
cooked: <p>Thanks, it is solved. But I have another error (<a href="https://discuss.pytorch.org/t/runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-cuda-floattensor-5-1-which-is-output-0-of-tbackward-is-at-version-2-expected-version-1-instead/164522">link</a>), if you have a look and help me to solve then it will be very much helpful.</p>

username: zahra97
cooked: <p>May I know how you solve this problem. I have a same issue with same code and repo.</p>