I don’t know if I got it right, but maybe what you want to do is:
true_positives = torch.zeros_like(labels)
idx_ones_pred = (pred == 1).nonzero().squeeze(1)
idx_labels_filtered = (labels[ones_pred] == 1).nonzero().squeeze(1)
true_positives[idx_labels_filtered] = 1