Custom training loop, how to do regression for Dlarray type data, I want to do image to image regression,

4 vues (au cours des 30 derniers jours)
The code for the model gradient solution I defined is as follows:
function [gradients1,gradients2,gradients3,state1,state2,state3,loss] = modelGradients(dlnet1,dlnet2,dlnet3,dlX1,dlX2,dlY)
[T1,T2,T3,state1] =forward(dlnet1,dlX1);
[t1,t2,t3,state2] =forward(dlnet2,dlX2);
Tt1=T1+t1;
Tt2=T2+t2;
Tt3=T3+t3;
dlX3=cat(3,Tt1,Tt2,Tt3);
[T4,state3] = forward(dlnet3,dlX3);
t4=forward(dlnet1,dlX1,'Outputs','resize-output-size');
Tt4=T4+t4;
loss = mse(Tt4,dlY);%L1loss
[gradients1,gradients2,gradients3]=dlgradient(loss,dlnet1.Learnables,dlnet2.Learnables,dlnet3.Learnables);
end
  • How to do regression mapping for Dlarray data。
  • I want to add a regression before “loss = mse(Tt4,dlY);%L1loss”

Réponses (1)

Zuber Khan
Zuber Khan le 10 Mai 2024
Hi,
Regression mapping for dlarray data depends on the specific objective you are trying to achieve with the given dlnetwork.
In the case of Image to Image Regression, you would want your recovered image using dlnetwork to be as close as the target image. As you have specified that you want to add regression before the loss term, you would need to apply a meaningful transformation to the output of network before calculating the loss.
The choice of this transformation, in turn depends on the numerical pattern of the target data. Since this is a custom problem, there may not be a one-size fits all answer. You might need to experiment with multiple functions to find the best fit. For instance, this mapping could be as simple as linear regression, or as complex as a perceptron.
I will give you a general idea on how you can apply regression mapping to the network's output. Assuming that only linear scaling will help to decrease the training loss, you can add a linear regression term before calling the loss function as follows:
% Assuming 'c' and 'd' refer to regression coefficient and regression constant respectively
Tt4_mapped = c * Tt4 + d;
% Calculate loss using the mapped output
loss = mse(Tt4_mapped, dlY);
You can replace the linear regression function mentioned in the above code snippet with any other relevant function f(Tt4), based on the training results. However, please note that in case the operation is not directly applicable on the dlarray, you can extract the elements from the dlarray, apply the operation and then convert the extracted elements back to dlarray with appropriate data format and dimensions.
I hope this will resolve your query.
Regards,
Zuber

Community Treasure Hunt

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

Start Hunting!

Translated by