# How to show the learning rate during training

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-show-the-learning-rate-during-training%2F13914%3E%5C
**Category:** Beginners\
**Created:** [January 19, 2022, 11:44pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-show-the-learning-rate-during-training%2F13914 "2022-01-19T23:44:39Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![milyiyo](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/milyiyo/32/59140_2.png) [@milyiyo](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmilyiyo)%5C
**Post date:** [January 19, 2022, 11:44pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-show-the-learning-rate-during-training%2F13914%2F1 "2022-01-19T23:44:39Z")

</div>

Hi everyone 🙂

I would like to know if it is possible to include the `learning rate` value as part of the information presented during the training.

The columns `Accuracy`, `F1`, `Precision` and `Recall` were added after setting a custom `compute_metrics` function. And I would like to have the `Learning Rate` as well.

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/2X/6/63f4c44fe21fd1d16cb6ddd852a3aa301912d57e.png)

Is it possible to add it there?

Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![Amalq](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@Amalq](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FAmalq)%5C
**Post date:** [January 22, 2022, 4:29pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-show-the-learning-rate-during-training%2F13914%2F2 "2022-01-22T16:29:56Z")

</div>

Hi Milyiyo,

I am curious how did you add accuracy, F1 Precision and Recall?

---

<div class="post-metadata">

**Author:** ![milyiyo](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/milyiyo/32/59140_2.png) [@milyiyo](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmilyiyo)%5C
**Post date:** [January 22, 2022, 7:36pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-show-the-learning-rate-during-training%2F13914%2F3 "2022-01-22T19:36:36Z")

</div>

Hi @Amalq,

I used this:

```python
from sklearn.metrics import precision_recall_fscore_support, accuracy_score

def compute_metrics(pred):
    labels = pred.label_ids
    preds = pred.predictions.argmax(-1)
    precision, recall, f1, _ = precision_recall_fscore_support(labels, preds, average='weighted')
    acc = accuracy_score(labels, preds)
    return {
        'accuracy': acc,
        'f1': f1,
        'precision': precision,
        'recall': recall
    }

```

That function was provided to the Trainer like this:

```python
from transformers import Trainer

trainer = Trainer(model=model,
                  args=training_args,
                  compute_metrics=compute_metrics,
                  tokenizer=tokenizer,
                  eval_dataset=dataset['validation'],
                  train_dataset=dataset['train'],
                  data_collator=data_collator)

```

I hope this can help 🙂

---

<div class="post-metadata">

**Author:** ![Amalq](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@Amalq](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FAmalq)%5C
**Post date:** [January 22, 2022, 7:38pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-show-the-learning-rate-during-training%2F13914%2F4 "2022-01-22T19:38:25Z")

</div>

Thank you so much @milyiyo , that is really helpful.

---

<div class="post-metadata">

**Author:** ![marshmellow77](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/marshmellow77/32/3279_2.png) [@marshmellow77](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmarshmellow77)%5C
**Post date:** [January 23, 2022, 9:04am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-show-the-learning-rate-during-training%2F13914%2F5 "2022-01-23T09:04:56Z")

</div>

Hi Alberto, yes it is possible to include `learning rate` in the evaluation logs!

Fortunately, the `log()` method of the Trainer class is one of the methods that you can “subclass” to inject custom behaviour: [Trainer](https://huggingface.co/docs/transformers/main_classes/trainer#trainer)

So, all you have to do is create your own Trainer subclass and override the `log()` method like so:

```auto
class MyTrainer(Trainer):
    def log(self, logs: Dict[str, float]) -> None:
        logs["learning_rate"] = self._get_learning_rate()
        super().log(logs)

trainer = MyTrainer(...)
trainer.train()

```

You should now see the learning rate in the eval logs.

Hope that helps, let me know if any questions.

Cheers  
Heiko

---

<div class="post-metadata">

**Author:** ![milyiyo](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/milyiyo/32/59140_2.png) [@milyiyo](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmilyiyo)%5C
**Post date:** [January 23, 2022, 5:01pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-show-the-learning-rate-during-training%2F13914%2F6 "2022-01-23T17:01:55Z")

</div>

Thanks @marshmellow77 🙂

That solved my problem, look the new column:

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/2X/1/1f8fd0763b1769aaec1852f3865740c622b85953.png)

Thanks a lot for your help 🤗

---

<div class="post-metadata">

**Author:** ![Vighnesh](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@Vighnesh](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FVighnesh)%5C
**Post date:** [May 17, 2022, 3:15pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-show-the-learning-rate-during-training%2F13914%2F7 "2022-05-17T15:15:20Z")

</div>

> [@marshmellow77](#):
>
> ```auto
> def log(self, logs: Dict[str, float]) -> None:
> logs["learning_rate"] = self._get_learning_rate()
> super().log(logs)
> 
> ```

Hi @marshmellow77 ,  
I am trying to use the same logic to log the learning rates at each epoch. But, I am getting the following error:  
AttributeError: ‘LMCL\_Trainer’ object has no attribute ‘\_get\_learning\_rate’

LMCL\_Trainer is the name of the subclass extended from Trainer. Providing the class below →

```auto
class LMCL_Trainer(Trainer):
    
    def __init__ (self, *args, **kwargs):
        super(). __init__ (*args, **kwargs)
        
    
    def log(self, logs: Dict[str, float]) -> None:
        logs["learning_rate"] = self._get_learning_rate()
        super().log(logs)

```

I have the transformers version 3.3.0 installed.

---

<div class="post-metadata">

**Author:** ![marshmellow77](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/marshmellow77/32/3279_2.png) [@marshmellow77](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmarshmellow77)%5C
**Post date:** [May 18, 2022, 10:37am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-show-the-learning-rate-during-training%2F13914%2F8 "2022-05-18T10:37:51Z")

</div>

Hi @Vighnesh , the transformers library is currently at version 4.19. Lots has changed since v3.3, I recommend upgrading to the latest version to see if this solution works for you.

Cheers  
Heiko

---

<div class="post-metadata">

**Author:** ![Vighnesh](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@Vighnesh](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FVighnesh)%5C
**Post date:** [May 19, 2022, 6:07am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-show-the-learning-rate-during-training%2F13914%2F9 "2022-05-19T06:07:04Z")

</div>

Thanks a lot. Yes the latest version has a lot of changes/improvements.

The solution worked with transformers version 4.19

---

<div class="post-metadata">

**Author:** ![matanle](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@matanle](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmatanle)%5C
**Post date:** [September 26, 2023, 1:44pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-show-the-learning-rate-during-training%2F13914%2F10 "2023-09-26T13:44:10Z")

</div>

Hi 🙂

I’ve tried the suggested solution with transformers version: 4.31.0, but for some reason the learning rate is not plotted as part of the evaluation logs.

Any ideas why? (I do see that it is saved in the log history)

---

<div class="post-metadata">

**Author:** ![artyomboyko](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/artyomboyko/32/31062_2.png) [@artyomboyko](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fartyomboyko)%5C
**Post date:** [November 14, 2023, 5:21pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-show-the-learning-rate-during-training%2F13914%2F11 "2023-11-14T17:21:48Z")

</div>

@marshmellow77 Hello. I want to use your solution to infer the learning rate. I am currently using Python 3.10. I get the following error:

```consloe
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[36], line 1
----> 1 class MyTrainer(Trainer):
      2 def log(self, logs: Dict[str, float]) -> None:
      3 logs["learning_rate"] = self._get_learning_rate()

Cell In[36], line 2, in MyTrainer()
      1 class MyTrainer(Trainer):
----> 2 def log(self, logs: Dict[str, float]) -> None:
      3 logs["learning_rate"] = self._get_learning_rate()
      4 print(logs["learning_rate"])

NameError: name 'Dict' is not defined

```

Which specific module do you need to import it from?

---

<div class="post-metadata">

**Author:** ![milyiyo](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/milyiyo/32/59140_2.png) [@milyiyo](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmilyiyo)%5C
**Post date:** [January 2, 2024, 1:36pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-show-the-learning-rate-during-training%2F13914%2F12 "2024-01-02T13:36:34Z")

</div>

Hi @artyomboyko,

Did you solve the error?

Maybe was missing something like: `from typing import Dict`

---

<div class="post-metadata">

**Author:** ![artyomboyko](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/artyomboyko/32/31062_2.png) [@artyomboyko](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fartyomboyko)%5C
**Post date:** [January 2, 2024, 3:33pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-show-the-learning-rate-during-training%2F13914%2F13 "2024-01-02T15:33:19Z")

</div>

@milyiyo Hello and Happy New year! Yes, you can find solution [here](https://github.com/huggingface/transformers/issues/27631).
