# Difference between calling model() and using Trainer()?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdifference-between-calling-model-and-using-trainer%2F1696%3E%5C
**Category:** Beginners\
**Created:** [October 22, 2020, 2:55pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdifference-between-calling-model-and-using-trainer%2F1696 "2020-10-22T14:55:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![blagav](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/blagav/32/472_2.png) [@blagav](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fblagav)%5C
**Post date:** [October 22, 2020, 2:55pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdifference-between-calling-model-and-using-trainer%2F1696%2F1 "2020-10-22T14:55:58Z")

</div>

Hi All,

I was wondering if there is any tangible difference between calling `model()` and feeding data in manually, or using the `Trainer()` object?

If I run a program to batch my data and feed it manually, will the training results be the same as using `Trainer()`?

I only ask because I am getting some errors while applying the language modelling protocol (from: [https://colab.research.google.com/github/huggingface/blog/blob/master/notebooks/01\_how\_to\_train.ipynb#scrollTo=YZ9HSQxAAbme](https://colab.research.google.com/github/huggingface/blog/blob/master/notebooks/01_how_to_train.ipynb#scrollTo=YZ9HSQxAAbme)) with the `Trainer()` module but manually seems to work fine.

---

<div class="post-metadata">

**Author:** ![BramVanroy](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/bramvanroy/32/47360_2.png) [@BramVanroy](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FBramVanroy)%5C
**Post date:** [October 22, 2020, 9:34pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdifference-between-calling-model-and-using-trainer%2F1696%2F2 "2020-10-22T21:34:11Z")

</div>

This might be related, as it discusses a recently fixed bug with a Colan notebook.

> [@Using hyperparameter-search in Trainer](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fusing-hyperparameter-search-in-trainer%2F785%2F25)%3A
>
> Hi @sgugger, in case you’re not aware of it, it seems the latest commit on [master](https://github.com/huggingface/transformers/commit/e174bfeb340d3d3468d9c8eebce95c42aa2dcf84) broke the [Colab notebook](https://twitter.com/GuggerSylvain/status/1318215507242291202) you shared on Twitter Trying to run that notebook, I hit the following error when trying to run best\_run = trainer.hyperparameter\_search(n\_trials=10, direction="maximize") with the optuna backend. Stack trace: /usr/local/lib/python3.6/dist-packages/sklearn/metrics/\_classification.py:900: RuntimeWarning: invalid value encountered in double\_scalars [W 2020-10-22 14:58:41,815] Trial 0 f…

But to answer your question concerning the results: the results when using the Trainer or your own trainer should be the same as long as you use the same loss function and hyperparameters.

---

<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:** [October 23, 2020, 1:09pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdifference-between-calling-model-and-using-trainer%2F1696%2F3 "2020-10-23T13:09:37Z")

</div>

Trainer is mostly there to take the boilerplate out of your way, especially for mixed-precision training, distributed training and TPU training, but it only does the training loop (with good hyperparameter defaults) so it should match your manual training loop.

---

<div class="post-metadata">

**Author:** ![blagav](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/blagav/32/472_2.png) [@blagav](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fblagav)%5C
**Post date:** [October 23, 2020, 4:00pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdifference-between-calling-model-and-using-trainer%2F1696%2F4 "2020-10-23T16:00:31Z")

</div>

Wonderful, thanks for the clarification!

---

<div class="post-metadata">

**Author:** ![blagav](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/blagav/32/472_2.png) [@blagav](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fblagav)%5C
**Post date:** [October 23, 2020, 4:04pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdifference-between-calling-model-and-using-trainer%2F1696%2F5 "2020-10-23T16:04:03Z")

</div>

I was dealing with a different error: `RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn`

I will look into it!

---

<div class="post-metadata">

**Author:** ![abellaiche](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/abellaiche/32/609_2.png) [@abellaiche](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fabellaiche)%5C
**Post date:** [November 18, 2020, 12:20pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdifference-between-calling-model-and-using-trainer%2F1696%2F6 "2020-11-18T12:20:00Z")

</div>

Did you find out what was wrong?  
I’m running into the same kind of error with the `Trainer` object

---

<div class="post-metadata">

**Author:** ![blagav](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/blagav/32/472_2.png) [@blagav](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fblagav)%5C
**Post date:** [November 19, 2020, 12:38am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdifference-between-calling-model-and-using-trainer%2F1696%2F7 "2020-11-19T00:38:05Z")

</div>

Unfortunately I never got it to work with the Transformer-XL implementation I was working on, but I modified BERT to fit my application and it works with that instead
