# How to use Adaptive Learning rate during training?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-adaptive-learning-rate-during-training%2F9143%3E%5C
**Category:** 🤗Transformers\
**Created:** [August 10, 2021, 6:02pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-adaptive-learning-rate-during-training%2F9143 "2021-08-10T18:02:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anzaman](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@anzaman](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fanzaman)%5C
**Post date:** [August 10, 2021, 6:02pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-adaptive-learning-rate-during-training%2F9143%2F1 "2021-08-10T18:02:04Z")

</div>

Hi,  
I am trying to train a Siamese network and want to incorporate adaptive learning rate during training.  
So I have a SiameseTrainer which is a subclass of Trainer class. My question is am I doing something wrong that I get the error

```auto
TypeError: Object of type Tensor is not JSON serializable

```

in this line of my code:

```auto
train_result = trainer.train(resume_from_checkpoint=checkpoint)

```

My code runs for few epochs and then crashes. So what I am doing is below:

```auto
def compute_metrics(p: EvalPrediction):
  correct_predictions = p.predictions[1] if isinstance(p.predictions, tuple) else p.predictions
  labels = p.label_ids
  return {
    "MSE": np.mean((correct_predictions-labels)**2)
  }

optimizer = Adafactor(
            model.parameters(), 
            scale_parameter=True, # If True, learning rate is scaled by root mean square
        )
lr_scheduler = AdafactorSchedule(optimizer)

trainer = SiameseTrainer(
            model=model,
            args=training_args,
            train_dataset=train_dataset,
            eval_dataset=eval_dataset,
            data_collator=data_collator,
            compute_metrics=compute_metrics,
            tokenizer=tokenizer,
            callbacks=[EarlyStoppingCallback(early_stopping_patience=2)],
            optimizers=(optimizer, lr_scheduler)
)

```

When I comment out the **optimizers=(optimizer, lr\_scheduler)** parameter and run the code, it does not crash in the middle of epochs.

Appreciate any help!

---

<div class="post-metadata">

**Author:** ![poom-sci](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/poom-sci/32/3245_2.png) [@poom-sci](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fpoom-sci)%5C
**Post date:** [November 5, 2021, 9:45am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-adaptive-learning-rate-during-training%2F9143%2F2 "2021-11-05T09:45:35Z")

</div>

Hi, anzaman

I am stuck on the same problem as you do. Have You fixed this one yet?

This is mine error.

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

##########UPDATE  
I found a way to trick it by adding logging\_steps=9999999 in the trainer argument to make it not log between training , but is there any better solutions for this one.

---

<div class="post-metadata">

**Author:** ![enpassant](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@enpassant](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fenpassant)%5C
**Post date:** [February 11, 2022, 5:11pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-adaptive-learning-rate-during-training%2F9143%2F3 "2022-02-11T17:11:57Z")

</div>

your trick worked for me too. do you know if there are any updates that allow using adaFactor in training and successfully log without tripping `TypeError: Object of type Tensor is not JSON serializable`

---

<div class="post-metadata">

**Author:** ![mmalandro](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@mmalandro](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmmalandro)%5C
**Post date:** [August 20, 2022, 7:27pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-adaptive-learning-rate-during-training%2F9143%2F4 "2022-08-20T19:27:41Z")

</div>

I have the same problem.

---

<div class="post-metadata">

**Author:** ![jxm](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/jxm/32/29614_2.png) [@jxm](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fjxm)%5C
**Post date:** [June 2, 2023, 2:41pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-adaptive-learning-rate-during-training%2F9143%2F5 "2023-06-02T14:41:19Z")

</div>

I ran into an issue like this when I inadvertently logged a metric in the trainer that was a Tensor instead of just a float. I did something like `average_scores.mean()`, which returned a Tensor object, which was somehow affixed to `Trainer.state` and then broke json-saving. I changed the metric computation to `average_scores.mean().item()` and this fixed the issue.
