I understand that when training_step()
returns loss, the code for automatic optimization
(link) takes care of the loss.backward()
Can someone tell me what would be difference in the loss.backward() automatic optimization, for the following two scenarios for training_step():
Scenario 1:
def training_step(self, batch: list,epochidx):
x,y = batch
output = model(x)
loss = self.loss_func(output,y)
return loss
Scenario 2:
def training_step(self, batch: list,epochidx):
x,y = batch
output = model(x)
loss = self.loss_func(output,y)
metric = self.metric(output,y)
train_log = {"loss":loss,"metric":metric}
return train_log
What my worry is that loss.backward() in the 2nd scenario does backward for both loss and metric instead of just loss.
I opened the pytorch-lightning files in my conda environment to understand how the automatic optimization is happening if I send a dictionary instead of a Tensor but it didn’t lead to much.
Any help/hint is appreciated. Thanks!