remove unnecesarry gradient freeze/unfreeze for single optimizer setup (#719)

This commit is contained in:
Ayberk Aydın
2020-01-21 08:09:27 -05:00
committed by William Falcon
parent 9aad69d856
commit a2b20b46bc
+6 -5
View File
@@ -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():