在 PyTorch 中向训练数据添加自定义权重

Spi*_*ico 5 python machine-learning deep-learning pytorch

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

muj*_*iga 7

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

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)
Run Code Online (Sandbox Code Playgroud)