Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Balanced class weight #31

Open
javierrodenas opened this issue Jul 26, 2021 · 1 comment
Open

Balanced class weight #31

javierrodenas opened this issue Jul 26, 2021 · 1 comment

Comments

@javierrodenas
Copy link

javierrodenas commented Jul 26, 2021

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
Copy link
Author

javierrodenas commented Jul 27, 2021

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant