I want to update my answer because the previous solution was a little misleading. You can use the flatten() function. In my case, I used it as   target = torch.sparse.torch.eye(10).index_select(dim=0, index=target.long().flatten()) The problem with the previous solution is that it erases the error but will decrease your accuracy considerably.