Implementing a Neural Network with a Custom Objective Function

10 views (last 30 days)

I want to build a neural network that takes a matrix A as input and outputs a matrix B such that a constant C=f(A,B)is maximized as much as possible.(The function f()is a custom complex computation function involving random values,probability density,matrix norms,and a series of other calculations).

I tried to directly use 1/f(A,B)or-f(A,B)as the loss function,but I encountered an error stating:"The value to be differentiated is not tracked.It must be a tracked real number dlarray scalar.Use dlgradient to track variables in the function called by dlfeval."I suspect this is likely because f(A,B)is not differentiable.

However,I've also seen people say that no matter what function it is,the dlgradient function can differentiate it.

So,I'm not sure whether it's because the function f()is too complex to be used as a loss function to calculate gradients,or if there's an issue with my code.

If I can't directly use its reciprocal or negative as the loss function,how should I go about training this neural network?Currently,I only know how to implement:providing target values and using functions like mse or huber as loss functions.

  1 Comment
Torsten
Torsten on 10 Dec 2024
Edited: Torsten on 10 Dec 2024
The function f()is a custom complex computation function involving random values,probability density,matrix norms,and a series of other calculations
If new random values are created in f each time the function is called, you have a stochastic optimization problem that cannot be solved with one of the available MATLAB solvers. You could try "patternsearch", but with no guarantee:

Sign in to comment.

Answers (1)

Maksym Tymchenko
Maksym Tymchenko on 1 May 2025
Edited: Maksym Tymchenko on 1 May 2025
It looks like you are trying to define a loss function C=f(A,B) that uses both the input to the network (A) and the output of the network (B).
The recommended worflow for this is to build a dlnetwork object that takes A as input and returns B as the output and then use trainnet with a custom loss function. Because you want to use the input to your network (A) in the loss function, and the custom loss function signature only accepts the outputs and targets, (as a workaround) you can define the target as the inputs to your network. You can then define a custom loss function with the signature loss = myCustomLoss(Y, T) where Y=B is the output of your network and T=A is the input to your network.
Here is a simplified example script of how you can use the input XTrain to the network in your loss function:
% Example: Custom loss that uses the inputs to the network in the custom
% loss function.
%% 1. Generate synthetic data
numSamples = 1000;
inputDim = 3;
outputDim = 3; % For demonstration, input and output dims are the same
XTrain = randn(numSamples, inputDim);
%% 2. Define a simple network
layers = [
featureInputLayer(inputDim)
fullyConnectedLayer(20)
reluLayer
fullyConnectedLayer(outputDim)
];
%% 3. Define training options
options = trainingOptions('adam', ...
'MaxEpochs', 10, ...
'MiniBatchSize', 32, ...
'Plots', 'training-progress', ...
'Verbose', false);
%% 4. Define the custom loss function
function loss = myCustomLoss(Y, T)
% Y: Output of the network
% T: "Target", which is actually the input
X = T; % T is actually the input
loss = mean((Y - X).^2, 'all'); % Example: MSE between input and output
end
%% 5. Train the network using trainnet
% To be able to use the inputs in the loss function, we'll use the inputs
% as both the input and the "target".
net = trainnet(XTrain, XTrain, layers, @myCustomLoss, options);
%% 6. Test the network
XTest = randn(10, inputDim);
YPred = predict(net, XTest);
If the Deep Learning Toolbox does not provide the layers that you need to represent your custom loss function, you can use an alternative workflow where you define your own model loss function without using dlnetwork at all. See Train Network Using Model Function for more info.
The error that you are encountering:
"The value to be differentiated is not tracked.It must be a tracked real number dlarray scalar. Use dlgradient to track variables in the function called by dlfeval."
most likely suggest that you followed the alternative workflow but did not follow the necessary steps to use automatic differentiation in the Deep Learning Toolbox. The required workflow is the following:
  1. Convert the variables that you want to differentiate with respect to into dlarray objects.
  2. Create a dedicated myCustomLoss function that returns the gradient of the quantity that you want to minimize.
  3. Call the myCustomLoss function using dlfeval.
  4. Use a solver update function such as adamupdate which takes as input the learnable gradients and updates your learnable parameters.
Have a look at Define Model Loss Function for Custom Training Loop for an example of these steps.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!