Neural Network with custom performance function

9 views (last 30 days)
EngM
EngM on 9 Jan 2024
Commented: EngM on 10 Jan 2024
Although this question was asked before, but I didn't find a single statesfying answer! The question is:
Given a nueral network, regardless of its type, the desire is to train the neural network using an error function that, presumabely, positive definite or semi positive definite. Meaning, the function does have minima. The error function in the Deep learning toolbox always takes the neural network outputs and some targets. This is understood as the gradient calculation should start from the output of the netowrk. However, once the backprobagation starts, taking the derivative of performance function, say it is MSE, the result is simply the error. This means, you can easitly replace it with any error function you want regardless if it's calculated from the network outputs or not. The Main objective of this question is to have the ability to replace the calculation of the Error with calculation of ANY constrain function that we would like to minimize during training of the neural network.
A typical example is tunning the gains of a PID controller. The gains are the outputs of the Neural Network. The tunning uses the error calculated from the desired and current system outputs. So, I can't use the standard toolbox performance functions as they require using the outputs of the Neural network to get the error! In this example, there is no Target PID gains and the training is similar to what we do in adaptive control, i.e use the system error to tune the gains.
How do we supply an error function to the MSE peformance function that is NOT calculated from the Neural Netowrk Outputs/Targets?
Thanks.

Answers (1)

Yash
Yash on 10 Jan 2024
Hi EngM,
In the Deep Learning Toolbox, if the built-in performance metrics like Mean Squared Error (MSE) do not meet your specific needs, you have the option to create a custom performance metric. This involves defining a MATLAB function that accepts the network's predictions, the true targets, and other parameters, which then computes the error according to your custom criteria.
This custom function can then be integrated into the training process of the neural network, allowing you to optimize the network's weights based on your defined error metric. By doing so, you can tailor the learning process to focus on minimizing the error based on your custom performance function.
Here's an example of how you can define a custom performance function:
function error = myMetric(outputs, targets, additionalInputs)
% Calculate the error based on the desired constraint function and return the error value
error = yourCustomConstraintFunction(outputs, targets, additionalInputs);
end
Once you have defined your custom performance function, you can use it in the training process by specifying it as the performance function when creating the neural network:
net = feedforwardnet(hiddenSizes);
net.performFcn = 'myMetric';
By supplying your own error function, you can train the neural network to minimize any constraint or error metric that you define, even if it is not directly calculated from the network outputs or targets.
Hope this helps
  1 Comment
EngM
EngM on 10 Jan 2024
Hello, thanks for replying..
I tested the above code with simplified arguments.
function error = myMetric(outputs, targets)
% Calculate the error based on the desired constraint function and return the error value
error = outputs - targets;
end
net = feedforwardnet(10);
net.performFcn = 'myMetric';
However, I received an error :
Not enough input arguments.
Error in myMetric (line 3)
error = outputs - targets;
Error in network/subsasgn>getDefaultParam (line 2047)
param = struct(feval(fcn,'defaultParam'));
Error in network/subsasgn>setPerformFcn (line 1910)
net.performParam = getDefaultParam(performFcn);
Error in network/subsasgn>network_subsasgn (line 472)
if isempty(err), [net,err]=setPerformFcn(net,performFcn); end
Error in indexing (line 14)
net = network_subsasgn(net,subscripts,v,netname);
Please advice.. thanks.

Sign in to comment.

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!