When I use the "dlnetwork" type deep neural network model to make predictions, the results of the two functions are very different, except that using the predict function will freeze the batchNormalizationLayer and dropout layers.While forward does not freeze the parameters, he is the forward transfer function used in the training phase. From the two pictures above, there are orders of magnitude difference in the output of the previous 10 results. Where does the problem appear?
Kshitij Singh answered .
2025-11-20
[gradients,loss] = dlfeval(@modelGradients,dlnet,dlX,Ylabel); [dlnet,otherOutputs]=rmspropupdate(dlnet,gradients,otherInputs); function [gradients,loss] = modelGradients(dlnet,dlX,Ylabel) Y=forward(dlnet,dlX); loss=myLoss(Y,Ylabel); gradients=dlgradient(loss,dlnet.Learnables); end The code above is wrong if you have batchNorms, it won't update them. The batchNorms are updated through the State property returnet from forward and assigned to dlnet: [gradients,state,loss] = dlfeval(@modelGradients,dlnet,dlX,Ylabel); dlnet.State=state; % THIS!!! [dlnet,otherOutputs]=rmspropupdate(dlnet,gradients,otherInputs); function [gradients,state,loss] = modelGradients(dlnet,dlX,Ylabel) [Y,state]=forward(dlnet,dlX); % THIS!!! loss=myLoss(Y,Ylabel); gradients=dlgradient(loss,dlnet.Learnables); end