- Make sure that you are not updating the pretrained network weights in calculating the loss, as this can cause a problem in the computation.
- The gradients might not get calculated if the pretrained network includes a non-differentiable step.
- The dlnet.Learnables should contain all the parameters that you want to update. Check if all the relevant parameters are included in it.
- X and Y need to be "dlarray" objects for the gradient function to work. If not, please convert them to dlarray objects before passing them to the loss function.
dlgradients returning zeros, loss dependant of other already trained network
13 views (last 30 days)
Show older comments
Hello,
I'm facing a problem that might be similar to the one here https://uk.mathworks.com/matlabcentral/answers/1844083-why-happens-all-the-gradients-of-the-generator-are-zero-from-the-beginning-to-the-end-when-traini, but I don't understand why.
The loss I use depends on another already trained shallow network. Then I use dlgradient and the calculated loss to find the gradients, but this only returns zeros...
Here's the loss,gradients function:
function [loss,gradients] = modelLoss(dlnet,X,net2)
% Forward data through network.
[Y] = forward(dlnet,X);
% Data through trained network2.
X_2 = [extractdata(X(1:2,:));extractdata(Y)];
X_pred = net2(X_2);
% Convert to dlarray.
dlX_pred = dlarray(X_pred,'CB');
% Calculate loss.
loss = mean(mean((dlX_pred - X(3:end,:)).^2));
% Calculate gradients of loss with respect to learnable parameters.
gradients = dlgradient(loss,dlnet.Learnables);
end
And here is the use of the dlfeval:
[loss,gradients] = dlfeval(@modelLoss,dlnet,dLXMiniBatch,net2);
Any idea on what's missing?
Thanks!
0 Comments
Accepted Answer
Avadhoot
on 12 Feb 2024
Hi Marcos,
I see that you are calculating gradients for your custom loss function for your model. As mentioned in the example, you have correctly included all the calculations involved in the loss computation inside the loss function, including the pretrained network. Still there could be a few issues which might cause the gradient function to return 0. Below are the probable fixes for this issue:
As you mentioned that you are using a shallow network, the vanishing gradient problem should not trouble you. Please check on all the above factors to determine the cause of the problem.
I hope this helps.
More Answers (0)
See Also
Categories
Find more on Custom Training Loops in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!