File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change 1+ """Example on how to freeze certain layers while training (in pytorch lightning)
2+ Author: Mohit Mayank
3+ """
4+
5+ # Before defining the optimizer, we need to freeze the layers
6+ # In pytorch lightning, as optimizer is defined in configure_optimizers, we freeze layers there.
7+ def configure_optimizers (self ):
8+ # iterate through the layers and freeze the one with certain name (here all BERT models)
9+ # note: the name of layer depends on the varibale name
10+ for name , param in self .named_parameters ():
11+ if 'BERTModel' in name :
12+ param .requires_grad = False
13+ # only pass the non-frozen paramters to optimizer
14+ optimizer = torch .optim .Adam (filter (lambda p : p .requires_grad , model .parameters ()), lr = 1e-3 )
15+ # return optimizer
16+ return optimizer
You can’t perform that action at this time.
0 commit comments