这篇文章给大家分享的是有关在Pytorch中如何使用样本权重sample_weight的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
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] *100
train_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”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!