Encoder decoder lr split

From wenmin-wu/ds-skills · View interactive version

Encoder decoder lr split is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Use separate learning rates for pretrained backbone (low) and randomly initialized classification head (high)

How to use this skill

Skill file

Encoder-Decoder Learning Rate Split

Overview

Pretrained transformer backbones need a lower learning rate to avoid catastrophic forgetting, while the randomly initialized classification head needs a higher learning rate to converge quickly. Split optimizer param groups into encoder vs decoder with different LRs.

Quick Start

def get_optimizer_params(model, encoder_lr=2e-5, decoder_lr=1e-3, weight_decay=0.01):
    no_decay = ["bias", "LayerNorm.bias", "LayerNorm.weight"]
    return [
        {"params": [p for n, p in model.backbone.named_parameters()
                    if not any(nd in n for nd in no_decay)],
         "lr": encoder_lr, "weight_decay": weight_decay},
        {"params": [p for n, p in model.backbone.named_parameters()
                    if any(nd in n for nd in no_decay)],
         "lr": encoder_lr, "weight_decay": 0.0},
        {"params": [p for n, p in model.named_parameters()
                    if "backbone" not in n],
         "lr": decoder_lr, "weight_decay": 0.0},
    ]

optimizer = torch.optim.AdamW(get_optimizer_params(model))

Key Decisions

Workflow

  1. Define param groups: encoder-with-decay, encoder-no-decay, head
  2. Assign different LRs per group
  3. Use single AdamW optimizer with all groups
  4. Scheduler operates on all groups (each group can have its own warmup)

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.