I want to use a loss function I created with a shallow neural network.

1 回表示 (過去 30 日間)
智輝 小林
智輝 小林 2021 年 11 月 7 日
回答済み: Rushil 2025 年 4 月 29 日
I want to use the app's neural network fitting to create a network that modifies only the loss function used for training.
In the app, MSE is used to evaluate the error.
I would like to use my own loss function instead of using MSE to evaluate the error.
When trainning, I want to use the loss function I created, but I don't know how to create the loss function and how to incorporate the created loss function.
How can I create a loss function in matlab? How can I use the loss function I created during training?

回答 (1 件)

Rushil
Rushil 2025 年 4 月 29 日
Hello
The Neural Net Fitting App is used for creating shallow networks to solve simple data fitting tasks, most of which involve numbers. So, it may not be possible to add a custom loss function using the Neural Net Fitting app.
As an alternative, one may consider using the Deep Network Designer app, which allows designing a network visually. Consider the following steps to train a network with a custom loss function:
1) Design a network using the Deep Network Designer app. The network can be exported as a “dlnetwork” object to the MATLAB workspace.
2) Define a custom loss function as a separate MATLAB function file (for example, myCustomLoss.m):
function loss = myCustomLoss(Y, T)
loss = mean(abs(Y - T), 'all'); % mean absolute error (MAE)
end
3) Implement a custom training loop in MATLAB. In a loop, use the exported network and call the custom loss function during training. Below is an example of how it could be done:
for epoch = 1:numEpochs
% forward pass
dlY = forward(net, dlX);
% compute custom loss
loss = myCustomLoss(dlY, dlT);
% compute gradients and parameter update
gradients = dlgradient(loss, net.Learnables);
net = dlupdate(@(w, g) w - learningRate * g, net, gradients);
end
To find more information about the app and programmatic workflows in the Deep Network Designer, refer to the following documentation link:
Hope it helps

カテゴリ

Help Center および File ExchangeDeep Learning Toolbox についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by