【发布时间】:2021-06-03 08:54:30
【问题描述】:
Focal Loss 是一种损失,旨在解决分类任务的类别不平衡问题。
这是我的尝试
class FocalLoss(nn.Module):
def __init__(
self,
weight=None,
gamma=2.,
reduction='none'
):
nn.Module.__init__(self)
self.weight = weight
self.gamma = gamma
self.reduction = reduction
def forward(self, input_tensor, target_tensor):
log_prob = F.log_softmax(input_tensor, dim=-1)
prob = torch.exp(log_prob)
return F.nll_loss(
((1 - prob) ** self.gamma) * log_prob,
target_tensor,
weight=self.weight,
reduction=self.reduction
)
loss.backward() 这给了
raise RuntimeError("grad can be implicitly created only for scalar outputs")
RuntimeError: grad can be implicitly created only for scalar outputs
这是对损失函数的调用:
loss = self._criterion(log_probs, label_batch)
self._criterion = nn.CrossEntropyLoss() 有效,self._criterion = FocalLoss() 报错。
如何使这种损失表现得像 CrossEntropyLoss API-wise?
【问题讨论】:
-
AFAIK 在尝试区分返回张量而不是标量的函数时会出现此类错误。因此,您的
forward函数可能会返回一个张量 -
@ForceBru 请参阅编辑。我只想要类似于
CrossEntropyLoss的行为
标签: python machine-learning deep-learning pytorch loss-function