在Pytorch中使用样本权重(sample_weight)的正确方法

时间:2021-05-22

step:

1.将标签转换为one-hot形式。

2.将每一个one-hot标签中的1改为预设样本权重的值

即可在Pytorch中使用样本权重。

eg:

对于单个样本:loss = - Q * log(P),如下:

P = [0.1,0.2,0.4,0.3]Q = [0,0,1,0]loss = -Q * np.log(P)

增加样本权重则为loss = - Q * log(P) *sample_weight

P = [0.1,0.2,0.4,0.3]Q = [0,0,sample_weight,0]loss_samle_weight = -Q * np.log(P)

在pytorch中示例程序

train_data = np.load(open('train_data.npy','rb'))train_labels = []for i in range(8): train_labels += [i] *100train_labels = np.array(train_labels)train_labels = to_categorical(train_labels).astype("float32")sample_1 = [random.random() for i in range(len(train_data))]for i in range(len(train_data)): floor = i / 100 train_labels[i][floor] = sample_1[i]train_data = torch.from_numpy(train_data) train_labels = torch.from_numpy(train_labels) dataset = dataf.TensorDataset(train_data,train_labels) trainloader = dataf.DataLoader(dataset, batch_size=batch_size, shuffle=True)

对应one-target的多分类交叉熵损失函数如下:

def my_loss(outputs, targets): output2 = outputs - torch.max(outputs, 1, True)[0] P = torch.exp(output2) / torch.sum(torch.exp(output2), 1,True) + 1e-10 loss = -torch.mean(targets * torch.log(P)) return loss

以上这篇在Pytorch中使用样本权重(sample_weight)的正确方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章