码迷,mamicode.com
首页 > 其他好文 > 详细

Pytorch技巧

时间:2018-08-24 22:54:57      阅读:427      评论:0      收藏:0      [点我收藏+]

标签:...   bsp   list   rate   map   get   单位   enumerate   ict   

one-hot encoding和常规label的转化

常规label指0,1,2,3,4,5,......(一个数代表一类)

#常规label转one-hot向量
def encode_onehot(labels):            #用单位矩阵来构建onehot向量
    classes = set(labels)
    classes_dict = {c: np.identity(len(classes))[i, :] for i, c in        #单位矩阵
                    enumerate(classes)}
    labels_onehot = np.array(list(map(classes_dict.get, labels)),
                             dtype=np.int32)
    return labels_onehot
#one-hot向量转常规label
labels = torch.LongTensor(np.where(labels)[1])

 

Pytorch技巧

标签:...   bsp   list   rate   map   get   单位   enumerate   ict   

原文地址:https://www.cnblogs.com/sbj123456789/p/9532182.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!