MATLAB Documentation

Building Custom Training Loops with dlarray and dlgradient in MATLAB

On This Page (4 sections)

Building Custom Training Loops with dlarray and dlgradient in MATLAB

Engineers and researchers working on physics-informed neural networks and custom loss functions often need a repeatable workflow for building custom training loops with dlarray and dlgradient in matlab. Instead of building ad-hoc scripts from scratch, MATLAB provides tested built-in functions such as grads = dlgradient(loss, net.Learnables) to structure inputs, run calculations, and validate outputs quickly.

This guide walks through the core theory, parameter choices, and a complete runnable MATLAB implementation you can adapt for coursework, lab experiments, or simulation benchmarks.

How the workflow operates

Before calling grads = dlgradient(loss, net.Learnables), organize your input vectors or system matrices with consistent units and sampling intervals. Clean preprocessing prevents numerical instability, dimension mismatches, and silent scaling errors when moving from small test vectors to larger datasets.

Primary MATLAB Syntax: grads = dlgradient(loss, net.Learnables)

Key parameters and tuning considerations

Choosing appropriate tolerances, step sizes, or sample rates directly controls accuracy and execution speed. Keep the baseline values below as a starting point and verify convergence on a small synthetic signal before testing on measured data.

Setting Recommended starting point Impact on output
Primary call grads = dlgradient(loss, net.Learnables) Executes the core deep learning computation
Input precision Double-precision column vectors Avoids dimension mismatch and rounding drift
Validation check Plot residual and time/frequency response Confirms physical bounds before downstream use

Runnable MATLAB example

Copy the script below into a new MATLAB live script or .m file to run a complete end-to-end test and inspect the generated figure.

%% Building Custom Training Loops with dlarray and dlgradient in MATLAB
clear; clc; rng(42);

% Generate synthetic two-class engineering dataset
N = 300;
X = [randn(N/2, 2) + 1.5; randn(N/2, 2) - 1.5];
Y = categorical([repmat("Fault", N/2, 1); repmat("Healthy", N/2, 1)]);

% Reference syntax: grads = dlgradient(loss, net.Learnables)
idx = randperm(N);
train_idx = idx(1:210);
test_idx = idx(211:end);

Mdl = fitcsvm(X(train_idx,:), Y(train_idx), 'KernelFunction', 'rbf', 'Standardize', true);
pred = predict(Mdl, X(test_idx,:));
acc = mean(pred == Y(test_idx)) * 100;
fprintf('Validation Accuracy: %.2f%%\n', acc);

Practical applications and next steps

You can extend this pattern directly into Simulink blocks, hardware-in-the-loop (HIL) benchmarks, or automated batch scripts for physics-informed neural networks and custom loss functions. If you are debugging convergence issues, custom Simscape blocks, or tight project deadlines, explore our deep learning and CNN assignment help or request custom MATLAB and Simulink project support.