How to understand "-input[range(target.shape[0]), target]"?
In the official example of PyTorch, it gives a loss function as follows.
def nll(input, target):
    return -input[range(target.shape[0]), target].mean()

loss_func = nll

How to understand the grammar of &quot;input[range(target.shape[0]), target]&quot; in the above function?
&quot;input&quot; has a torch.Size([64, 10]) and &quot;target&quot; has a torch.Size([64]). Why use &quot;range&quot; function here?

The range function is used as a shortcut to create a vector/list/generator starting from 0 up to 64. So it is shorthand for essentially [0,1,2,...64]
To make this explicit you could do the following:
def nll(input, target):
    minputlist = list(range(target.shape[0]))
    print(minputlist )
    return -input[minputlist, target].mean()

