title: Pytorch select values from the last tensor dimension with indices from another tenor with a smaller dimension
body: <p>I have a tensor <code>a</code> with three dimensions. The first dimension corresponds to minibatch size, the second to the sequence length, and the third to the feature dimension. E.g.,</p>
  
  <pre><code>&gt;&gt;&gt; a = torch.arange(1, 13, dtype=torch.float).view(2,2,3)  # Consider the values of a to be random
  &gt;&gt;&gt; a
  tensor([[[ 1.,  2.,  3.],
           [ 4.,  5.,  6.]],
  
          [[ 7.,  8.,  9.],
           [10., 11., 12.]]])
  </code></pre>
  
  <p>I have a second, two-dimensional tensor. Its first dimension corresponds to the minibatch size and its second dimension to the sequence length. It contains values in the range of the indices of the third dimension of <code>a</code>. <code>a</code>s third dimension has size 3, so <code>b</code> can contain values 0, 1 or 2. E.g.,</p>
  
  <pre><code>&gt;&gt;&gt; b = torch.LongTensor([[0, 2],[1,0]])
  &gt;&gt;&gt; b
  tensor([[0, 2],
          [1, 0]])
  </code></pre>
  
  <p>I want to obtain a tensor <code>c</code> that has the shape of <code>b</code> and contains all the values of <code>a</code> that are referenced by <code>b</code>.
  In the upper scenario I would like to have:</p>
  
  <pre><code>c = torch.empty(2,2)
  c[0,0] = a[0, 0, b[0,0]]
  c[1,0] = a[1, 0, b[1,0]]
  c[0,1] = a[0, 1, b[0,1]]
  c[1,1] = a[1, 1, b[1,1]]
  
  &gt;&gt;&gt; c
  tensor([[ 1.,  5.],
          [ 8., 10.]])
  </code></pre>
  
  <p>How can I create the tensor <code>c</code> fast? Further, I also want c to be differentiable (be able to use <code>.backprob()</code>). I am not too familiar with pytorch, so I am not sure, if a differentiable version of this exists. </p>
  
  <p>As an alternative, instead of <code>c</code> having the same shape as <code>b</code> I could also use a <code>c</code> with the same shape of <code>a</code>, having only zeros, but at the places referenced by <code>b</code> ones. Then I could multiply <code>a</code> and <code>c</code> to obtain a differentiable tensor.</p>
  
  <p>Like follows:</p>
  
  <pre><code>c = torch.zeros(2,2,3, dtype=torch.float)
  c[0,0,b[0,0]] = 1
  c[1,0,b[1,0]] = 1
  c[0,1,b[0,1]] = 1
  c[1,1,b[1,1]] = 1
  
  &gt;&gt;&gt; a*c
  tensor([[[ 1.,  0.,  0.],
           [ 0.,  5.,  0.]],
  
          [[ 0.,  8.,  0.],
           [10.,  0.,  0.]]])
  </code></pre>