# Finetuning BART using custom loss

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffinetuning-bart-using-custom-loss%2F4060%3E%5C
**Category:** Beginners\
**Created:** [March 1, 2021, 7:21pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffinetuning-bart-using-custom-loss%2F4060 "2021-03-01T19:21:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![himanshu](https://avatars.discourse-cdn.com/v4/letter/h/ac91a4/32.png) [@himanshu](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fhimanshu)%5C
**Post date:** [March 1, 2021, 7:21pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffinetuning-bart-using-custom-loss%2F4060%2F1 "2021-03-01T19:21:07Z")

</div>

Hi everyone,

I want o fine tune BART using custom loss. What I want to do is take the output text generated by the BART model, feed it to a classifier and update weights of the BART model using the classification loss. Please note that I do not want to train the classifier, rather I want to train the BART model using the classification loss on the generated text. Can someone give me pointers on how to do it?

TIA

---

<div class="post-metadata">

**Author:** ![lewtun](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lewtun/32/30995_2.png) [@lewtun](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flewtun)%5C
**Post date:** [March 1, 2021, 8:22pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffinetuning-bart-using-custom-loss%2F4060%2F2 "2021-03-01T20:22:51Z")

</div>

Hi @himanshu, the simplest way to implement custom loss functions is by subclassing the `Trainer` class and overriding the `compute_loss` function, e.g.

```auto
from transformers import Trainer

class BartTrainer(Trainer):
    def compute_loss(self, model, inputs):
        # implement custom logic here
        custom_loss = ...
        return custom_loss

```

You can find more details in the docs here: [Trainer — transformers 4.3.0 documentation](https://huggingface.co/transformers/main_classes/trainer.html?highlight=trainer#trainer)

---

<div class="post-metadata">

**Author:** ![theudster](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@theudster](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Ftheudster)%5C
**Post date:** [March 2, 2021, 9:44am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffinetuning-bart-using-custom-loss%2F4060%2F3 "2021-03-02T09:44:54Z")

</div>

Hi, this may be a bit naive, but do you then pass on the parameters to BartTrainers as you would to Trainer, as such;

```
trainer = BartTrainer(
    model=model, # the instantiated Transformers model to be trained
    args=training_args, # training arguments, defined above
    train_dataset=train_dataset, # training dataset
    eval_dataset=val_dataset, # evaluation dataset
   compute_metrics=compute_metrics,
)
```

---

<div class="post-metadata">

**Author:** ![lewtun](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lewtun/32/30995_2.png) [@lewtun](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flewtun)%5C
**Post date:** [March 2, 2021, 10:02am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffinetuning-bart-using-custom-loss%2F4060%2F4 "2021-03-02T10:02:59Z")

</div>

yep, that’s it - by subclassing the `Trainer` class, `BartTrainer` inherits all the attributes and functions 🙂

strictly speaking, you might need to include `super()` in the subclass e.g.

```auto
class BartTrainer(Trainer):
    def __init__ (self, *args, **kwargs):
        super(). __init__ (*args, **kwargs)

    def compute_loss(self, model, inputs):
        # implement custom logic here
        custom_loss = ...
        return custom_loss

```

there’s a nice template of a custom trainer for question-answering here that you could work from: [transformers/trainer\_qa.py at master · huggingface/transformers · GitHub](https://github.com/huggingface/transformers/blob/master/examples/question-answering/trainer_qa.py)

---

<div class="post-metadata">

**Author:** ![himanshu](https://avatars.discourse-cdn.com/v4/letter/h/ac91a4/32.png) [@himanshu](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fhimanshu)%5C
**Post date:** [March 2, 2021, 11:22am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffinetuning-bart-using-custom-loss%2F4060%2F5 "2021-03-02T11:22:35Z")

</div>

Hi @lewtun thanks for the reply. Actually, I am looking for how to compute the loss i.e. how to “implement custom logic”, what I mean by that is the output of BART is some text, I want to feed that text to the classifier and update weights of BART based on classification loss. How do I code such a system. I hope that make sense 🙂 .  
TIA

---

<div class="post-metadata">

**Author:** ![lewtun](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lewtun/32/30995_2.png) [@lewtun](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flewtun)%5C
**Post date:** [March 2, 2021, 12:33pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffinetuning-bart-using-custom-loss%2F4060%2F6 "2021-03-02T12:33:13Z")

</div>

Hi @himanshu, could you provide a code snippet of what you have in mind (even if it’s pseudo-code)? For example, can you show what the inputs / outputs to the classifiers should look like and how the classification loss is computed?

---

<div class="post-metadata">

**Author:** ![himanshu](https://avatars.discourse-cdn.com/v4/letter/h/ac91a4/32.png) [@himanshu](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fhimanshu)%5C
**Post date:** [March 2, 2021, 5:35pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffinetuning-bart-using-custom-loss%2F4060%2F7 "2021-03-02T17:35:48Z")

</div>

Hi @lewtun, I have something like this in mind.

```auto
def compute_loss(self,model,inputs,classifier):
    output = model.generate(inputs)
    text = tok.decode(output)
    # convert text to ids
    classifier_output = classifier(text)
    loss = loss_function(classifier_output, targets)
    return loss

```

Now, when I do loss.backward(), I want to update the weights of model and not classifier. I understand that there is non-differentiability involved as soon as I use the model to generate text, I want to know how to handle that?

---

<div class="post-metadata">

**Author:** ![minji](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/minji/32/42669_2.png) [@minji](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fminji)%5C
**Post date:** [June 15, 2022, 1:15pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffinetuning-bart-using-custom-loss%2F4060%2F8 "2022-06-15T13:15:52Z")

</div>

@lewtun @himanshu Is there any idea about that?

---

<div class="post-metadata">

**Author:** ![halamvac](https://avatars.discourse-cdn.com/v4/letter/h/e47774/32.png) [@halamvac](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fhalamvac)%5C
**Post date:** [September 24, 2022, 9:34am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffinetuning-bart-using-custom-loss%2F4060%2F9 "2022-09-24T09:34:44Z")

</div>

@himanshu @minji Did any of you make it work?

---

<div class="post-metadata">

**Author:** ![minji](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/minji/32/42669_2.png) [@minji](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fminji)%5C
**Post date:** [September 25, 2022, 7:19am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffinetuning-bart-using-custom-loss%2F4060%2F10 "2022-09-25T07:19:22Z")

</div>

I did it to use pytorch lightening  
and change this part

```auto
outputs = self(batch[0], batch[1], batch[2])
loss = outputs["loss"]

```

```auto
y_hat, y = outputs["logits"].view(-1, outputs["logits"].shape[-1]), batch[2].view(-1)
cross_entropy_loss = F.cross_entropy(y_hat, y, reduction="mean") # you can change it to your own custom loss

```
