Your approach currently also filters out the class0 target indices. If you use groundtruth>=0, you should get the same results as my approach.
The gather operation selects the log probabilities at the target indices in dim1.