title: How does `torch.gather()` construct new tensors?
body: <p>I'm struggling to understand what the function <a href="https://pytorch.org/docs/stable/generated/torch.gather.html" rel="nofollow noreferrer"><code>torch.gather</code></a> is doing.</p>
  <p>If I have:</p>
  <pre><code>a = torch.tensor([[4,5,6],[7,8,9],[10,11,12]])
  b = torch.tensor([[1,1],[1,2]])
  </code></pre>
  <p>Then</p>
  <pre><code>&gt;&gt;&gt; torch.gather(input=a, dim=1, index=b)
  tensor([[5, 5],
          [8, 9]])
  </code></pre>
  <p>While</p>
  <pre><code>&gt;&gt;&gt; torch.gather(input=a, dim=0, index=b)
  tensor([[ 7,  8],
          [ 7, 11]])
  </code></pre>
  <p>Can somebody explain <strong>very simply</strong> how these output tensors are actually constructed?</p>