title: Pytorch: Randomly subsample loss tensors using `torch.randperm`
body: <p>I'm trying to randomly subsample the <code>prediction</code> and <code>target</code> array for my loss calculation. </p>
  
  <pre><code>idx = torch.randperm(target.shape[0]) 
  target = target.index_select(0, idx[0, sample_size]
  </code></pre>
  
  <p>However I'm getting this error message.</p>
  
  <pre><code>index_select(): argument 'index' (position 2) must be Variable, not torch.LongTensor
  </code></pre>
  
  <p>Does anyone know how to fix this?</p>
  
  <p><strong>Edit:</strong>
  I got one step closer. It seems like torch.randperm does not return a torch variable, so one has to explicitly convert the output:</p>
  
  <pre><code>idx = torch.randperm(target.shape[0]) 
  idx = Variable(idx).cuda()
  target = target.index_select(0, idx[0, sample_size]
  </code></pre>
  
  <p>only problem is now that the backpropagation fails. Seems like the operation of randomly subsampling is causing an issue with the dimensions. 
  However the dimensions seem to be fine when calculating the loss:  </p>
  
  <pre><code>loss = F.nll_loss(prediction, target.view(-1)) # prediction shape is [Nx12] and target shape is N
  </code></pre>
  
  <p>Unfortunately when calling <code>loss.backward()</code> I get this error message:  </p>
  
  <pre><code>RuntimeError: The expanded size of the tensor (12) must match the existing size (217456) at non-singleton dimension 1
  </code></pre>