ในกรณีที่จำนวนข้อมูลตัวอย่าง ในแต่ละ Class แตกต่างกันมาก เรียกว่า Class Imbalance แทนที่เราจะใช้ Cross Entropy Loss ตามปกติที่เรามักจะใช้ในงาน Classification ในเคสนี้เราจะเปลี่ยนไปใช้ Loss Function พิเศษ ที่ออกแบบมาเพื่อแก้ปัญหานี้ เรียกว่า Focal Loss ดังสมการด้านล่าง
\(\text{FL}(p_t) = -\alpha_t (1 – p_t)^{\gamma} \, \text{log}(p_t)\)Focal Loss จะให้ความสำคัญ Class ที่มีจำนวนตัวอย่างน้อย หรือจะ Penalty ให้ Prediction ที่มั่นใจมาก (แปลว่าง่ายสำหรับโมเดล หรือ มีข้อมูลตัวอย่างเป็นจำนวนมาก) ให้มีน้ำหนักน้อยลง เทียบกับสัดส่วน Loss ทั้ง Batch
`p_t` is the model’s estimated probability for each class.
ตัวอย่าง kornia.losses.focal
Arguments:
- alpha (float): Weighting factor :math:`\alpha \in [0, 1]`.
- gamma (float): Focusing parameter :math:`\gamma >= 0`.
- reduction (str, optional): Specifies the reduction to apply to the output: ‘none’ | ‘mean’ | ‘sum’. ‘none’: no reduction will be applied, ‘mean’: the sum of the output will be divided by the number of elements in the output, ‘sum’: the output will be summed. Default: ‘none’.
Shape:
- Input: :math:`(N, C, *)` where C = number of classes.
- Target: :math:`(N, *)` where each value is :math:`0 ≤ targets[i] ≤ C−1`.
Examples
N = 5 # num_classes
kwargs = {"alpha": 0.5, "gamma": 2.0, "reduction": 'mean'}
loss = kornia.losses.FocalLoss(**kwargs)
input = torch.randn(1, N, 3, 5, requires_grad=True)
target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)
output = loss(input, target)
output.backward()
ตัวอย่างการประยุกต์ใช้ Focal Loss
อ่านต่อ AI วินิจฉัยโรคมะเร็งผิวหนัง 7 ชนิด ความแม่นยำ 94%
References:
- https://arxiv.org/abs/1708.02002
- https://kornia.readthedocs.io/en/latest/_modules/kornia/losses/focal.html