Take a look on what torch.max() returns when 2 arguments is passed :
torch.max(input, dim, keepdim=False, *, out=None) -> (Tensor, LongTensor)
So, I think your function is right, except first line of code, it should be :
maxes, _ = torch.max(target, dim=1)