首页 > 解决方案 > 在 PyTorch 中为训练数据添加自定义权重

问题描述

是否可以在 PyTorch 中为训练实例添加自定义权重?更明确地说,我想为我的数据集中的每一行添加一个自定义权重。默认情况下,权重为 1,这意味着每个数据对我的模型同样重要。

标签: pythonmachine-learningdeep-learningpytorch

解决方案


损失函数支持类别权重而不是样本权重。对于样本权重,您可以执行以下操作(内联注释):

import torch

x = torch.rand(8, 4)
# Ground truth
y = torch.randint(2, (8,))
# Weights per sample 
weights = torch.rand(8, 1) 

# Add weights as a columns, so that it will be passed trough
# dataloaders in case you want to use one
x = torch.cat((x, weights), dim=1)

model = torch.nn.Linear(4, 2)

loss_fn = torch.nn.CrossEntropyLoss(reduction='none')
def weighted_loss(y, y_hat, w):
  return (loss_fn(y, y_hat)*w).mean()

loss = weighted_loss(model(x[:, :-1]), y, x[:, -1])
print (loss)

推荐阅读