The batch size of your output and target should match.
After fixing this issue, the code runs fine using your target and nn.CrossentropyLoss:
criterion = nn.CrossEntropyLoss()

output = torch.randn(51, 22, requires_grad=True)
target = torch.tensor([17, 16, 8, 18, 9, 14, 11, 18, 9, 2, 15, 8, 11, 12, 12, 8, 5, 20,
3, 21, 2, 12, 3, 5, 18, 8, 14, 17, 6, 8, 21, 0, 20, 2, 8, 13,
21, 18, 8, 4, 2, 4, 9, 11, 3, 12, 15, 1, 7, 2, 4])

loss = criterion(output, target)
loss.backward()