Skip to content

Instantly share code, notes, and snippets.

View f1recracker's full-sized avatar

Harsh Mohan f1recracker

  • Seattle, WA
View GitHub Profile
@f1recracker
f1recracker / focal_loss.py
Last active September 3, 2023 09:11
PyTorch implementation of focal loss that is drop-in compatible with torch.nn.CrossEntropyLoss
# pylint: disable=arguments-differ
import torch
import torch.nn as nn
import torch.nn.functional as F
class FocalLoss(nn.CrossEntropyLoss):
''' Focal loss for classification tasks on imbalanced datasets '''