How to apply layer-wise learning rate in Pytorch?

后端 未结 1 1864
情书的邮戳
情书的邮戳 2021-02-01 19:25

I know that it is possible to freeze single layers in a network for example to train only the last layers of a pre-trained model. What I’m looking for is a way to apply certain

相关标签:
1条回答
  • 2021-02-01 19:40

    Here is the solution:

    from torch.optim import Adam
    
    model = Net()
    
    optim = Adam(
        [
            {"params": model.fc.parameters(), "lr": 1e-3},
            {"params": model.agroupoflayer.parameters()},
            {"params": model.lastlayer.parameters(), "lr": 4e-2},
        ],
        lr=5e-4,
    )
    

    Other parameters that are didn't specify in optimizer will not optimize. So you should state all layers or groups(OR the layers you want to optimize). and if you didn't specify the learning rate it will take the global learning rate(5e-4). The trick is when you create the model you should give names to the layers or you can group it.

    0 讨论(0)
提交回复
热议问题