I am trying to train my model and my code is listed below:
import torch.nn.functional as F
data = Variable(data,requires_grad=True)
output = model(data)
predict = F.softmax(output,0)
final_predict = 1.2 * predict + 1.3 * predict + 2.5 * predict
loss = F.mse_loss(final_predict, ground_truth)
However, the parameters of my model remain the same and the gradient is none.
So, how to solve this issue? Thanks.
Your predict is not a parameter so it by default doesn’t retain gradients.
How should I modify this code?
Now, I slightly modify my code:
optimizer = optim.SGD(model.parameters(), lr = lr, momentum = momentum)
self.classifier = nn.Sequential(
def forward(self, x):
output = self.classifier(x)
final_predict = 1.2 * output + 1.3 * output + 2.5 * output
predict = model(data)
loss = F.mse_loss(predict, ground_truth)
Obviously, the optimizer still can not see
final_predict. So, how can I add
final_predict to the existing optimizer or convert it to a nn.parameter()? Because I need to use
final_predict to calculate the loss so as to update my model.
Did you read the examples in the link I gave above?