# How can I use class\_weights when training?

**URL:** <https://discuss.huggingface.co/t/how-can-i-use-class-weights-when-training/1067>\
**Category:** 🤗Transformers\
**Created:** [September 9, 2020, 2:29pm UTC](https://discuss.huggingface.co/t/how-can-i-use-class-weights-when-training/1067 "2020-09-09T14:29:03Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](https://discuss.huggingface.co/u/nielsr)\
**Post date:** [October 29, 2021, 8:32am UTC](https://discuss.huggingface.co/t/how-can-i-use-class-weights-when-training/1067/7 "2021-10-29T08:32:36Z")

</div>

Overwriting the Trainer can be done as follows (this is also explained in the [docs](https://huggingface.co/transformers/main_classes/trainer.html)):

```auto
from torch import nn
from transformers import Trainer

class CustomTrainer(Trainer):
    def compute_loss(self, model, inputs, return_outputs=False):
        labels = inputs.get("labels")
        # forward pass
        outputs = model(**inputs)
        logits = outputs.get('logits')
        # compute custom loss
        loss_fct = nn.CrossEntropyLoss(weight=torch.tensor([0.2, 0.3]))
        loss = loss_fct(logits.view(-1, self.model.config.num_labels), labels.view(-1))
        return (loss, outputs) if return_outputs else loss

```

---

_[View the full topic](https://discuss.huggingface.co/t/how-can-i-use-class-weights-when-training/1067)._
