Yes they are equivalent because, if no gradient is passed in to .backward when the output is a scalar, it is implicitly Tensor(1.), and backprop through sum will into the same shape as the final output before you reduced it, which recovers torch.ones(labels.shape[0])). Doing .sum() is slightly more efficient though because expand won’t materialize the entire ones tensor, it just creates a view. Or even better you could just do loss.backward(tensor(1.).expand_as(labels.shape[0])).