Implementing a Control-Oriented Cost Function with Internal Neural Network Terms in MATLAB

34 views (last 30 days)
Hi! I have a problem regarding the implementation of a custom performance function.
I am working with the following state-space model:
where gn() and hn() are neural networks designed using the MATLAB Deep Learning Toolbox.
To train this model, I would like to use the following performance function:
This cost function minimizes:
  1. the error between the real system output and the model output, and
  2. the magnitude of the nonlinear residual term gn(x,u).
Our objective is not only to minimize the nonlinear term gn(x,u), but rather to ensure that it is negligible compared to the linear part of the dynamics, i.e., the model behavior is dominated by the linear term.
In other words, we aim at minimizing .
In practice, I am unsure how to implement such a performance function in MATLAB when training neural networks, especially when the loss depends on an internal neural-network-based term like gn.
I first tried to solve this using trainlm, but since MSE is fixed as the performance function, I could not include the additional penalty term.
I then attempted the solution proposed here:
However, this approach did not work in my case because I cannot access the internal neural network outputs (e.g., the gn(x,u) term) inside the performance function. The custom performance function only receives the final network output, while my loss depends on intermediate NN signals.
Any guidance on how to implement or approximate this type of performance function would be appreciated.
  1 Comment
Broy
Broy on 15 Jan 2026 at 9:21
To implement a loss function that regularizes the ratio between the nonlinear term g_n and the linear dynamics, you should move from the standard shallow network training workflow to a Custom Training Loop using the MATLAB Deep Learning framework (dlnetwork, dlarray, dlgradient).
This approach gives you full control over the forward pass and the loss calculation. In a custom training loop, you define the forward pass of your model explicitly. This allows your model function to return multiple outputs: the final prediction y_hat and the value of the intermediate term g_n.
Here are the key resources to help you set up the custom loop:

Sign in to comment.

Answers (0)

Categories

Find more on Sequence and Numeric Feature Data Workflows 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!