rng(123);
X_ori=[4,163,80;5,164,75];
X=permute(X_ori,[3,4,2,1]);
dlX = dlarray(X, 'SSCB');
Y_ori=[0, 0, 0, 1; 0, 1, 0, 0];
myModel = [
imageInputLayer([1 1 3],'Normalization','none','Name','in')
fullyConnectedLayer(7,'Name','Layer 1')
fullyConnectedLayer(4,'Name','Layer 2')];
MyLGraph = layerGraph(myModel);
myDLnet = dlnetwork(MyLGraph);
gradients1 = dlfeval(@modelGradients1, myDLnet, dlX, Y_ori);
gradients2 = dlfeval(@modelGradients2, myDLnet, dlX, Y_ori);
gradients_sum = gradients1+gradients2;
function [gradients1] = modelGradients1(myModel, modelInput, CorrectLabels)
CorrectLabels_transpose=transpose(CorrectLabels);
[modelOutput,state] = forward(myModel,modelInput);
loss = -31*sum(sum(CorrectLabels_transpose.*log(sigmoid(modelOutput/100))));
gradients1 = dlgradient(loss, myModel.Learnables);
end
function [gradients2] = modelGradients2(myModel, modelInput, CorrectLabels)
CorrectLabels_transpose=transpose(CorrectLabels);
[modelOutput,state] = forward(myModel,modelInput);
loss = -42*sum(sum(CorrectLabels_transpose.*log(sigmoid(modelOutput/100))));
gradients2 = dlgradient(loss, myModel.Learnables);
end