pytorch---之转成one-hot向量

对于分类问题,标签可以是类别索引值也可以是one-hot表示。以10类别分类为例,lable=[3] 和label=[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]是一致的.

现在给定索引标签,怎么将其转换为one-hot标签表示?

或者直接torch.LongTensor(data),然后再转为one-hot

>>>class_num = 10
>>>batch_size = 4
>>>label = torch.LongTensor(batch_size, 1).random_() % class_num
 3
 0
 0
 8

>>>one_hot = torch.zeros(batch_size, class_num).scatter_(1, label, 1)
    0     0     0     1     0     0     0     0     0     0
    1     0     0     0     0     0     0     0     0     0
    1     0     0     0     0     0     0     0     0     0
    0     0     0     0     0     0     0     0     1     0

---------------------

转载:https://blog.csdn.net/victoriaw/article/details/72874637

猜你喜欢

转载自blog.csdn.net/zxyhhjs2017/article/details/82842514