From 3defaddb0cc19fdf8129240917649386dbc873e8 Mon Sep 17 00:00:00 2001 From: AlessandroMiola Date: Fri, 20 Jan 2023 12:01:00 +0100 Subject: [PATCH] Fix lr, weight_decay defaults for ViT-* trained on ImageNet-1k --- 08_pytorch_paper_replicating.ipynb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/08_pytorch_paper_replicating.ipynb b/08_pytorch_paper_replicating.ipynb index 6c4bd61..80fdc1c 100644 --- a/08_pytorch_paper_replicating.ipynb +++ b/08_pytorch_paper_replicating.ipynb @@ -3457,6 +3457,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "id": "2554a736-ca45-4f83-abc3-41750c20ec58", "metadata": {}, @@ -3471,9 +3472,9 @@ "\n", "The authors set Adam's $\\beta$ (beta) values to $\\beta_{1}=0.9, \\beta_{2}=0.999$, these are the default values for the `betas` parameter in `torch.optim.Adam(betas=(0.9, 0.999))`.\n", "\n", - "They also state the use of [weight decay](https://paperswithcode.com/method/weight-decay) (slowly reducing the values of the weights during optimization to prevent overfitting), we can set this with the `weight_decay` parameter in `torch.optim.Adam(weight_decay=0.1)`.\n", + "They also state the use of [weight decay](https://paperswithcode.com/method/weight-decay) (slowly reducing the values of the weights during optimization to prevent overfitting), we can set this with the `weight_decay` parameter in `torch.optim.Adam(weight_decay=0.3)` (according to the setting of ViT-* trained on ImageNet-1k).\n", "\n", - "We'll set the learning rate of the optimizer to 0.1 as per Table 3.\n", + "We'll set the learning rate of the optimizer to 0.003 as per Table 3 (according to the setting of ViT-* trained on ImageNet-1k).\n", "\n", "And as discussed previously, we're going to use a lower batch size than 4096 due to hardware limitations (if you have a large GPU, feel free to increase this)." ] @@ -3544,9 +3545,9 @@ "\n", "# Setup the optimizer to optimize our ViT model parameters using hyperparameters from the ViT paper \n", "optimizer = torch.optim.Adam(params=vit.parameters(), \n", - " lr=1e-3, # Base LR from Table 3 \n", + " lr=3e-3, # Base LR from Table 3 for ViT-* ImageNet-1k\n", " betas=(0.9, 0.999), # default values but also mentioned in ViT paper section 4.1 (Training & Fine-tuning)\n", - " weight_decay=0.03) # from the ViT paper section 4.1 (Training & Fine-tuning)\n", + " weight_decay=0.3) # from the ViT paper section 4.1 (Training & Fine-tuning) and Table 3 for ViT-* ImageNet-1k\n", "\n", "# Setup the loss function for multi-class classification\n", "loss_fn = torch.nn.CrossEntropyLoss()\n",