PyTorch Workflow: How does the loss function know what is going on with the model parameters? #714
Replies: 2 comments
-
There is no easy answer to your question. Torch builds the computational graph for each computation you perform in the forward pass of the model. Loss function takes in the output variable from the forward pass that references the computational graph. Torch moves backwards (loss.backwards()) from the loss to each gradient in the computational graph using chain rule. Optimizer advances the weights with the gradients at the optimizer.step(). |
Beta Was this translation helpful? Give feedback.
-
The When you call The optimizer does not need to know the loss function explicitly. Instead, it uses the gradients that are computed by the I hope this explanation helps |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to understand the training loop. Everything makes sense to me except for the loss function. I understand that we calculate the loss in a given epoch with
loss = loss_fn(y_pred, y_train)
. I'm confused about two things:how does
loss.backward()
know what the model parameters are? We don't connect it to the model. We defineloss_fn = nn.L1Loss()
and then we put in two tensors into the loss function withloss = loss_fn(y_pred, y_train)
. Does it know the parameters from the y_pred, y_train? I'm used to scikit learn where these are just vectors, but maybe the tensors have more information attached to them?How does the optimizer know what the loss is? We never pass the loss function into the optimizer.
Beta Was this translation helpful? Give feedback.
All reactions