Pytorch技巧

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])

增减layer:

参考 https://www.cnblogs.com/marsggbo/p/8781774.html

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