# Specify Loss for Trainer / TrainingArguments

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fspecify-loss-for-trainer-trainingarguments%2F10481%3E%5C
**Category:** 🤗Transformers\
**Created:** [October 3, 2021, 10:30pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fspecify-loss-for-trainer-trainingarguments%2F10481 "2021-10-03T22:30:33Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnielsr)%5C
**Post date:** [October 4, 2021, 8:34am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fspecify-loss-for-trainer-trainingarguments%2F10481%2F2 "2021-10-04T08:34:44Z")

</div>

You can overwrite the `compute_loss` method of the Trainer, like so:

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

class RegressionTrainer(Trainer):
    def compute_loss(self, model, inputs, return_outputs=False):
        labels = inputs.get("labels")
        outputs = model(**inputs)
        logits = outputs.get('logits')
        loss_fct = MSELoss()
        loss = loss_fct(logits.squeeze(), labels.squeeze())
        return (loss, outputs) if return_outputs else loss

```

However, [several models in the library](https://huggingface.co/transformers/master/main_classes/configuration.html?highlight=problem_type) have an attribute of their config called `problem_type`, which you can set to “regression”. In that case, you shouldn’t overwrite anything, and you can just use the default loss of the model.

---

_[View the full topic](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fspecify-loss-for-trainer-trainingarguments%2F10481)._
