ultralytics / yolov5

YOLOv5 πŸš€ in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.37k stars 16.26k forks source link

Question about weight decay #4104

Closed soyeollee closed 3 years ago

soyeollee commented 3 years ago

❔Question

Additional context

Thank you for your contributions.

I have a question about weight decay.

In train.py,

 for k, v in model.named_modules():
        if hasattr(v, 'bias') and isinstance(v.bias, nn.Parameter):
            pg2.append(v.bias)  # biases
        if isinstance(v, nn.BatchNorm2d):
            pg0.append(v.weight)  # no decay
        elif hasattr(v, 'weight') and isinstance(v.weight, nn.Parameter):
            pg1.append(v.weight)  # apply decay
...
if opt.adam:
        optimizer = optim.Adam(pg0, lr=hyp['lr0'], betas=(hyp['momentum'], 0.999))  # adjust beta1 to momentum
    else:
        optimizer = optim.SGD(pg0, lr=hyp['lr0'], momentum=hyp['momentum'], nesterov=True)

    optimizer.add_param_group({'params': pg1, 'weight_decay': hyp['weight_decay']})  # add pg1 with weight_decay
    optimizer.add_param_group({'params': pg2})  # add pg2 (biases)

there are different settings for parameters.

Only 'learning rate' is applied without 'weight_decay'.

What is a reason for this setting? Are there paper or ref for this issue?

Thank you.

github-actions[bot] commented 3 years ago

πŸ‘‹ Hello @SoYeol-Lee, thank you for your interest in YOLOv5 πŸš€! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a πŸ› Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python>=3.6.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

$ git clone https://github.com/ultralytics/yolov5
$ cd yolov5
$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

glenn-jocher commented 3 years ago

@SoYeol-Lee these is the YOLOv5 parameter groupings and decay schedules. Bias LR is initialized high to allow for fast improvements in bias during warmup.

vishnubanna commented 3 years ago

is weight decay applied to beta and gamma in the batch normalization layer

glenn-jocher commented 3 years ago

@vishnubanna no

github-actions[bot] commented 3 years ago

πŸ‘‹ Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 πŸš€ resources:

Access additional Ultralytics ⚑ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 πŸš€ and Vision AI ⭐!

ashwin-999 commented 7 months ago

@vishnubanna no

Hi @glenn-jocher understand it's based on settings from the paper. Going thru some older issues, I see you've done quite a lot of ablation studies with yolov5. Do you have any intuition of why this specific choice was made? Very curious

Edit: found this link that sorta tries to explain. https://stats.stackexchange.com/questions/576463/why-not-perform-weight-decay-on-layernorm-embedding. Do you see it the same way?

glenn-jocher commented 7 months ago

@ashwin-999, the decision not to apply weight decay to the parameters of batch normalization layers (specifically, the beta and gamma parameters) is indeed rooted in empirical findings and theoretical considerations. The primary intuition behind this choice is that these parameters are part of the normalization process, which aims to stabilize the distribution of activations throughout the network. Applying weight decay to these could potentially destabilize these distributions, leading to less effective training.

The link you found provides a good discussion on a related topic, focusing on why weight decay is not typically applied to normalization layers and embeddings. The reasoning aligns with our approach: normalization layers (like BatchNorm) and embeddings have a different role compared to typical weight matrices in a network. They are not directly involved in the "learning" of feature representations in the same way but are more about scaling and shifting those representations. Applying weight decay could hinder their ability to effectively scale and shift, which is why it's generally avoided.

In summary, the choice is based on both theoretical considerations and empirical evidence suggesting that excluding these parameters from weight decay leads to better training dynamics and model performance.