Thanks for explaination. SORRY  @ptrblck, may be we have misunderstood the question. In my question, given a targets (labels) with ignored class (=num_class+1), I want to convert the targets/labels to the one hot encoding and ignore the ignored class during converting. I can do it by using the second solution make_one_hot_v2 but it takes time-consuming than the first way.
import torch
import random
import numpy as np

def make_one_hot(labels):
    labels.unsqueeze_(1)
    one_hot = torch.cuda.FloatTensor(labels.size(0), num_classes, labels.size(2), labels.size(3)).zero_()
    target = one_hot.scatter_(1, labels.data, 1)

    return target

def make_one_hot_v2(labels):
    labels = labels.data.cpu().numpy()
    one_hot = np.zeros((labels.shape[0], num_classes,labels.shape[1], labels.shape[2]), dtype=labels.dtype)
    # handle ignore labels
    for class_id in range(num_classes):
        one_hot[:, class_id,...] = (labels==class_id)
    return torch.cuda.FloatTensor(one_hot)

num_classes=5
h,w=24,24
batch_size=1
#+1 because ignore class=num_classes+1
labels = torch.empty(batch_size,  h, w, dtype=torch.long).random_(num_classes+1)
labels= labels.cuda()
make_one_hot (labels)

labels = torch.empty(batch_size,  h, w, dtype=torch.long).random_(num_classes+1)
labels= labels.cuda()
make_one_hot_v2(labels)

If I used make_one_hot_v2, we have no error because I handle the ignore class by using loop, however, it may slow, thus my expected way is the first function make_one_hot but it got error because the ignored class issue.
RuntimeError: cuda runtime error (59) : device-side assert triggered at /opt/conda/conda-bld/pytorch_1524590031827/work/aten/src/THC/generic/THCTensorCopy.c:21

Do you have any suggestion to fix the first way?