mirror of
https://github.com/wassname/pytorch-lightning.git
synced 2026-09-10 12:21:57 +08:00
remove unnecesarry gradient freeze/unfreeze for single optimizer setup (#719)
This commit is contained in:
committed by
William Falcon
parent
9aad69d856
commit
a2b20b46bc
@@ -460,11 +460,12 @@ class TrainerTrainLoopMixin(ABC):
|
||||
for opt_idx, optimizer in enumerate(self.optimizers):
|
||||
# make sure only the gradients of the current optimizer's paramaters are calculated
|
||||
# in the training step to prevent dangling gradients in multiple-optimizer setup.
|
||||
for param in self.get_model().parameters():
|
||||
param.requires_grad = False
|
||||
for group in optimizer.param_groups:
|
||||
for param in group['params']:
|
||||
param.requires_grad = True
|
||||
if len(self.optimizers) > 1:
|
||||
for param in self.get_model().parameters():
|
||||
param.requires_grad = False
|
||||
for group in optimizer.param_groups:
|
||||
for param in group['params']:
|
||||
param.requires_grad = True
|
||||
|
||||
# wrap the forward step in a closure so second order methods work
|
||||
def optimizer_closure():
|
||||
|
||||
Reference in New Issue
Block a user