"size of predictions and target values must match" when using custom loop training for multi-input and multi-output

17 visualizaciones (últimos 30 días)
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 comentarios
Katja Mogalle
Katja Mogalle el 21 de En. de 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.
Van Vy
Van Vy el 24 de En. de 2022
@Katja Mogalle: Thank you for your reply, I checked my network and there was no problem. Now I'm trying to re-organize my code in another way because I stucked in this point long time already.

Iniciar sesión para comentar.

Respuestas (1)

Jaimin
Jaimin el 3 de Dic. de 2024
To resolve the error "size of predictions and target values must match" in a deep learning model with two inputs and two outputs using a custom training loop, I have created a simplified demo code.
Kindly refer to it for understanding.
% Create dummy data
input1 = dlarray(randn(10, 32), 'CB'); % 10 features, 32 samples
input2 = dlarray(randn(10, 32), 'CB'); % 10 features, 32 samples
output1 = dlarray(randn(3, 32), 'CB'); % 3 outputs, 32 samples
output2 = dlarray(randn(3, 32), 'CB'); % 3 outputs, 32 samples
% Custom training loop
numEpochs = 5;
learningRate = 0.01;
for epoch = 1:numEpochs
% Evaluate the model gradients and loss using dlfeval
[gradients, state, loss] = dlfeval(@modelGradientsMulti, dlnet, input1, input2, output1, output2);
% Update the network parameters using SGD
dlnet = dlupdate(@(w, g) w - learningRate * g, dlnet, gradients);
% Display the loss
fprintf('Epoch %d, Loss: %.4f\n', epoch, loss);
end
% Support function for computing gradients and loss
function [gradients, state, loss] = modelGradientsMulti(dlnet, in1, in2, out1, out2)
% Forward pass
[pre1, pre2, state] = forward(dlnet, in1, in2, 'Outputs', ["output1", "output2"]);
% Compute losses
loss1 = mse(pre1, out1);
loss2 = mse(pre2, out2);
loss = loss1 + loss2;
% Compute gradients
gradients = dlgradient(loss, dlnet.Learnables);
end
For more information kindly refer following MathWorks documentation.
I hope this will be helpful.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by