"size of predictions and target values must match" when using custom loop training for multi-input and multi-output
44 views (last 30 days)
Show older comments
Dear all,
I build a deep learning model with 2 input and 2 output and use a custom loop training. But I have an error when call dlfeval(): "size of predictions and target values must match" . What should I fix? Here is my code:
[grad,state,loss] = dlfeval(@modelGradientsMulti, dlnet, input1, input2, output1, output2);
%support function
function [gradients,state,loss] = modelGradientsMulti(dlnet,in1,in2,out1,out2)
[pre1,pre2,state] = forward(dlnet,in1,in2,'Outputs',["fc1" "fc2"]);
loss1 = mse(pre1,output1);
loss2 = mse(pre2,output2);
loss = loss1 + loss2;
gradients = dlgradient(loss,dlnet.Learnables);
end
Thank you for reading my question. I hope someone expert will give a help.
2 Comments
Katja Mogalle
on 21 Jan 2022
From the error message my first guess would be that pre1 and output1 don't have the same size, or that pre2 and output2 don't have the same size. Can you put a breakpoint at the line where you compute mse for the first time and check the sizes of pre1, output1, pre2, and output2? If there are inconsistencies between the sizes we need to look further if the data or the network is set up incorrectly.
To better understand the sizes of activations in the network, I always suggest using analyzeNetwork:
>> analyzeNetwork(dlnet)
Let me know how you get on and what you find.
Answers (0)
See Also
Categories
Find more on Statistics and Machine Learning Toolbox 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!