"size of predictions and target values must match" when using custom loop training for multi-input and multi-output
이전 댓글 표시
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
Katja Mogalle
2022년 1월 21일
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
2022년 1월 24일
답변 (1개)
Jaimin
2024년 12월 3일
Hi @Van Vy
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.
카테고리
도움말 센터 및 File Exchange에서 Classification Trees에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!