IndexError: Target 2 is out of bounds

Yes, you would need to map the values to the expected range.
One approach would be a manual mapping, where you can define which old class index maps to which new one and create the new target tensor by assigning the desired values.
Another valid approach would be to map new values from [0, nb_classes-1] to the unique old targets:

old_target = torch.randint(300, 320, (15,))
print(old_target)
> tensor([315, 308, 316, 310, 313, 314, 309, 304, 307, 303, 311, 311, 312, 302,
        308])

unique_targets = torch.unique(old_target)
print('unique targets: {}'.format(unique_targets))
> unique targets: tensor([302, 303, 304, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316])

new_target = torch.empty_like(old_target)
for idx, t in enumerate(unique_targets):
    print('replacing {} with {}'.format(t, idx))
    new_target[old_target == t] = idx

> replacing 302 with 0
 replacing 303 with 1
 replacing 304 with 2
 replacing 307 with 3
 replacing 308 with 4
 replacing 309 with 5
 replacing 310 with 6
 replacing 311 with 7
 replacing 312 with 8
 replacing 313 with 9
 replacing 314 with 10
 replacing 315 with 11
 replacing 316 with 12