How could I create one hot tensor while ignoring some label index

Hi,

I am creating one hot label with this function:

def one_hot_emb(logits, label):
    lb_one_hot = logits.data.clone().zero_().scatter_(1, label.unsqueeze(1), 1)
    return lb_one_hot

It works when there is no ignore labels. When there is ignore labels such as lb_ignore=255, I hope the one hot vector at this position to be all zeros. How could I do this please?

For example, there is a label=[0,1,2] whose one hot embedding is: [[1,0,0], [0,1,0], [0,0,1]]. If the label is label=[0,1,255], where 255 is the ignored label, the output should be: [[1,0,0], [0,1,0], [0,0,0]].

Do I have a way to do it?
The label tensor might not be 1d tensor, I can be tensor of shape N,C,H,W.

Waiting for the solutions… …

One possible approach would be to set the lb_ignore to nb_classes+1, create an approprialty shaped zero tensor, and remove this additional dimension using slicing.
Probably not the best approach, but it should work.

3 Likes

Have you solve that? I can only think of it as ptrblck said below.

def onehot_with_ignore_label(labels, num_class, ignore_label):
    dummy_label = num_class + 1
    mask = labels == ignore_label
    modified_labels = labels.clone()
    modified_labels[mask] = num_class
    # One-hot encode the modified labels
    one_hot_labels = torch.nn.functional.one_hot(modified_labels, num_classes=dummy_label)
    # Remove the last row in the one-hot encoding
    one_hot_labels = one_hot_labels[:, :-1]
    return one_hot_labels