How does dlgradient handle two output layers?
Mostra commenti meno recenti
I have a deep learning image analysis network with a custom training loop and loss function. In this loss function I take the output layer's output and the second to last layer's output from the net, perform mse (using the inbuilt function) on both, and sum the results. My output layer is a custom layer, so I have control over it's backwards function, but I cannot see the automatic backwards in the other layers.
When I use dlgradient on this new loss, how does Matlab handle two simultaneous rounds of differentiation? Does it sum the gradients for each learnable, or does one gradient calculation override the other?
I have tried both adam and SGDM methods of updating gradients, but I am suspecting since my network is not learning as expected that I need to adjust my backwards function in my custom layer to ensure a proper gradient backpropagation.
EDIT: Added some abstract code to demonstrate the context of my question. Specifically I'm asking if anyone knows what's going on under the hood as it does two backpropagations starting from two places. How it handles combining the propagated backwards gradients as it performs dlgradient, so I can better understand how to craft my backwards function.
function dlgradientForumQuestion()
% create a network
modelLayers = [
imageInputLayer([imageSize,imageSize],"Name","image_input")
convolution2dLayer([3,3],numFilters,"Name","processing")
customLayer("Name","output_layer")
];
% initialize network
myNetwork = dlnetwork(modelLayers);
myNetwork = initialize(myNetwork);
% do training
for i = 1:numTrainingIterations
% use custom loss function
[gradients,newState] = dlfeval(@customLossFunction,myNetwork,inputData,targetData1,targetData2);
myNetwork.State = newState;
% do the adamupdate here
[myNetwork,~,~] = adamupdate(myNetwork,gradients,averageGrad,averageSqGrad,iteration);
end
end
function [gradients,newState] = customLossFunction(myNetwork,inputData,targetData1,targetData2)
% forward through network
[customOutput,secondToLastOutput,newState] = forward(myNetwork,inputData,"Outputs",["output_layer","processing"]);
% do loss mse
loss1 = mse(customOutput,targetData1);
loss2 = mse(secondToLastOutput,targetData2);
% sum the losses
loss = loss1 + loss2;
% get gradients
gradients = dlgradient(loss,myNetwork.Learnables);
end
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Deep Learning Toolbox in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!