How to import input into the custom forwardLoss and backwardLoss function?
9 views (last 30 days)
Show older comments
In my custom regression output layer, both of the forwardLoss and backwardLoss function depend on the input X. function loss = forwardLoss(layer,Y T) , function dLdY=backwardLoss(layer,Y,T). How can I import input X into the forwardLoss and backwardLoss function.
3 Comments
Rebecca Plant
on 29 Oct 2021
Hi Hongbo Xu, I'm struggling with the same thing. Did you find a solution that worked for you?
Answers (1)
Ayush Aniket
on 6 May 2025
In addition to @BartGoris's approach, another way to achieve this is to write your own training loop using dlnetwork and dlfeval. This way, you can compute the loss with access to X, Y, and T as needed, as shown below:
% Forward pass
Y = forward(net, X);
% Compute custom loss using X, Y, and T
loss = myCustomLoss(X, Y, T);
% Backward pass
gradients = dlgradient(loss, net.Learnables);
Refer to the following documentation link to read more about the process of writing custom training loops: https://www.mathworks.com/help/deeplearning/ug/define-model-gradients-function-for-custom-training-loop.html
0 Comments
See Also
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!