- 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 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Avadhoot
2024-2-12
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Custom Training Loops 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!