volo icon indicating copy to clipboard operation
volo copied to clipboard

Balanced class weight

Open javierrodenas opened this issue 2 years ago • 1 comments

Hello,

I was trying to compute the class weight "balanced". I see that there are two arguments:

parser.add_argument('--dense-weight', type=float, default=0.5,
                    help='Token labeling loss multiplier (default: 0.5)')
parser.add_argument('--cls-weight', type=float, default=1.0,
                    help='Cls token prediction loss multiplier (default: 1.0)')

How can I multiply the loss to get the balanced class weight?

Thank you in advance

javierrodenas avatar Jul 26 '21 14:07 javierrodenas

What I did so far:

from sklearn.utils import class_weight

class_weights = class_weight.compute_class_weight('balanced', np.unique(target_values), target_values.numpy())
class_weights = torch.tensor(class_weights, dtype=torch.float)

train_loss_fn = nn.CrossEntropyLoss(weight=class_weights).cuda()

See that I am changing the loss function, before I was using TokenLabelGTCrossEntropy :

train_loss_fn = TokenLabelGTCrossEntropy(dense_weight=args.dense_weight,\
    cls_weight = args.cls_weight, mixup_active = mixup_active).cuda()

javierrodenas avatar Jul 27 '21 10:07 javierrodenas