# Use custom loss function for training ML task

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fuse-custom-loss-function-for-training-ml-task%2F11351%3E%5C
**Category:** Beginners\
**Created:** [November 4, 2021, 10:14am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fuse-custom-loss-function-for-training-ml-task%2F11351 "2021-11-04T10:14:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roman](https://avatars.discourse-cdn.com/v4/letter/r/ed8c4c/32.png) [@Roman](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FRoman)%5C
**Post date:** [November 4, 2021, 10:14am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fuse-custom-loss-function-for-training-ml-task%2F11351%2F1 "2021-11-04T10:14:44Z")

</div>

Hello.  
I’d like to train BERT from stratch on my custom corpus for the Masked Language Modeling task. But corpus has one specific - it is sequence of the numbers and absolute value of the difference of two words corresponds to its proximity. Therefore I guess I should use this difference(or some similar) as loss function during training. Is it possible to use custom loss function training BERT model fo ML task?

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsgugger)%5C
**Post date:** [November 4, 2021, 11:54am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fuse-custom-loss-function-for-training-ml-task%2F11351%2F2 "2021-11-04T11:54:50Z")

</div>

You can compute the loss outside of your model since it returns the logits, and apply any function you like.

If you question was related to the `Trainer`, you should definte your subclass with a `compute_loss` method. There is an example in the [documentation](https://huggingface.co/transformers/main_classes/trainer.html) (scroll a bit down).

---

<div class="post-metadata">

**Author:** ![bui](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@bui](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fbui)%5C
**Post date:** [March 17, 2022, 7:53am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fuse-custom-loss-function-for-training-ml-task%2F11351%2F3 "2022-03-17T07:53:39Z")

</div>

hi @sgugger,  
in the link you attached above, I have a question related to the example.  
why do we need this line of code when computing loss?

```auto
loss = loss_fct(logits.view(-1, self.model.config.num_labels), labels.view(-1))

```

I mean the .view() method. why do we have to reshape logits tensors?
