python - 在 PyTorch 中为训练数据添加自定义权重
问题描述
是否可以在 PyTorch 中为训练实例添加自定义权重?更明确地说,我想为我的数据集中的每一行添加一个自定义权重。默认情况下,权重为 1,这意味着每个数据对我的模型同样重要。
解决方案
损失函数支持类别权重而不是样本权重。对于样本权重,您可以执行以下操作(内联注释):
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)
推荐阅读
- azure - 使用 Microsoft.Graph 的 Azure Active Directory 中广告用户的扩展属性
- java - 如何使用 Webflux 上传多个文件?
- java - SSLSocket如何像Socket.sendUrgentData()一样发送心脏连接
- tensorflow - 反向传播时对象检测训练如何工作?
- php - 如何计算 Laravel 5.6 中的表列唯一值?
- c# - 为什么我无法再次打开字体对话框?
- powershell - 在每月的第一天运行 ScheduledTask?
- node.js - 使用 mocha / chai 测试服务:测试登录时出错
- javascript - 函数参数未声明变量中的 TDZ
- docker - Traefik:将多个 docker 服务聚合到一个逻辑后端