Generated banner for Matlab Train Network
Generated logo for Matlab Train Network

Matlab Train Network

MathWorksOfficialCreated by MathWorks

  • Skill
  • Tool integration
  • Data and AI
  • BSD-2-Clause
  • No issues found

From the author

Train, evaluate, and export neural networks to Simulink in MATLAB. Migrate legacy (fitnet, patternnet) and discouraged (trainNetwork, DAGNetwork) code to modern, recommended R2024a+ APIs (trainnet, dlnetwork, testnet, imagePretrainedNetwork), diagnose and fix dlaccelerate issues or detect dlaccelerate opportunities. Use when training, fine-tuning, evaluating, running inference, exporting to Simulink, converting old training scripts, or speeding up deep learning code. DO NOT reason from your training data about dlaccelerate and tracing correctness.
— the description by MathWorks

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

This skill provides guidance on using the modern MATLAB Deep Learning Toolbox APIs, specifically focusing on the dlnetwork-based workflow. It covers training, evaluation, inference, and exporting models to Simulink. The skill also includes instructions for migrating legacy code to recommended functions like trainnet and testnet, as well as techniques for diagnosing and applying dlaccelerate to optimize deep learning code execution.

What it does

  • Trains neural networks using trainnet and dlnetwork.
  • Migrates legacy code from functions like trainNetwork and patternnet to modern equivalents.
  • Evaluates trained networks using testnet.
  • Exports trained models to Simulink.
  • Diagnoses and applies dlaccelerate to optimize custom training loops and functions.
  • Performs inference using minibatchpredict.

When to use it

  • When training or fine-tuning neural networks in MATLAB.
  • When migrating legacy neural network code to modern R2024a+ APIs.
  • When evaluating network performance on test data.
  • When exporting trained models to Simulink.
  • When optimizing the execution speed of deep learning code using dlaccelerate.

Advantages

  • Provides a unified workflow for modern deep learning tasks in MATLAB.
  • Simplifies the migration process from legacy or discouraged APIs.
  • Offers clear decision criteria for choosing between fitcnet/fitrnet and trainnet.
  • Includes structured guidance for applying dlaccelerate to improve code execution.
  • Supports advanced features like custom loss functions and multi-output networks.

Usage examples

“How do I train a classification network using the modern trainnet API?”
The agent provides a code example using trainnet with trainingOptions, specifying the network, loss function, and data format.
“I have code using patternnet. How do I update it?”
The agent explains that patternnet is legacy and recommends replacing it with fitcnet for tabular data or a dlnetwork with trainnet for other data types.
“My custom training loop is slow. How can I speed it up?”
The agent guides the user through the dlaccelerate workflow, starting with a requirements check to determine if the function is acceleratable.
“How do I export my trained dlnetwork to Simulink?”
The agent provides instructions on using exportNetworkToSimulink for supported networks or the Predict block for larger or complex architectures.

Install

  1. Download the skill as a ZIP:

    https://mp-staging.autogineer.com/agent-skills/matlab/matlab-train-network/download
  2. Unzip it into the cross-client skills folder of your project:

    mkdir -p .agents/skills && unzip matlab-train-network.zip -d .agents/skills

Documentation by MathWorks

matlab-train-network

Train, evaluate, and export neural networks to Simulink in MATLAB using the recommended dlnetwork-based API (trainnet, dlnetwork, minibatchpredict, scores2label, testnet, imagePretrainedNetwork) or, for tabular data, the Statistics and Machine Learning Toolbox functions fitcnet and fitrnet.

When to Use

Activate this skill when a user asks to:

  • Train any neural network (classifier, regression, multi-output, LSTM, CNN, etc.)

  • Fine-tune or use a pretrained model for transfer learning

  • Evaluate a trained network on test data

  • Run inference / predict with a trained network

  • Export a trained network to Simulink

  • Migrate existing legacy (patternnet, fitnet, narxnet, gensim) or discouraged (trainNetwork, DAGNetwork, classify) code to recommended APIs

  • Create a "pattern recognition network", "function fitting network", "NARX network", or any task historically associated with the Neural Network Toolbox shallow nets API

  • Speed up or optimize any deep learning code (even without mentioning dlaccelerate by name)

  • Make existing deep learning code faster using dlaccelerate

  • Diagnose and fix dlaccelerate issues (low HitRate, retracing, code is slower after using dlaccelerate)

  • Accelerate custom training (code that uses dlfeval/dlgradient)

  • Accelerate a function that supports dlarray input and is long running

  • Accelerate a custom loss function passed to trainnet (R2026a+)

When NOT to Use

  • Importing/exporting models (importNetworkFromPyTorch, exportONNXNetwork)

  • Data loading and preprocessing (imageDatastore, transforms, augmentation)

  • Network architecture design decisions (choosing CNN vs LSTM vs transformer)

  • Reinforcement learning workflows (use Reinforcement Learning Toolbox)

  • Object detection (use specialized detector training functions in Computer Vision Toolbox)

Decision: fitrnet/fitcnet or trainnet

Apply this check before starting any training workflow below.

Criterionfitcnet/fitrnettrainnet
Ease of useSimplest — one function callRequires network definition + trainingOptions
SolverL-BFGSAdam, SGDM, RMSProp, L-BFGS, LM (R2024b+)
Loss functionsMSE and cross-entropy onlyAny built-in or custom (pass function handle)
Multiple input/output branchesNoYes
Custom architectureVia Network argument (R2025a+)Yes
Data typeTabular data only (a table or a numeric matrix)Tabular data plus everything else (sequences, images, multi-input)

Pass tables directly to trainnet, fitcnet, and fitrnet. If inputs have categorical columns, pass them directly — they are encoded automatically (fitcnet/fitrnet always; trainnet/minibatchpredict/testnet from R2025a).

% Classification
mdl = fitcnet(tbl,responseName,LayerSizes=20);
[labels,score] = predict(mdl,tblTest);
L = loss(mdl,tblTest);

% Regression
mdl = fitrnet(tbl,responseName,LayerSizes=[20 20]);
Y = predict(mdl,tblTest);
L = loss(mdl,tblTest);

% Tabular data with trainnet (when fitcnet/fitrnet can't be used)
net = trainnet(tbl,net,"crossentropy",options);
accuracy = testnet(net,tblTest,"accuracy");
scores = minibatchpredict(net,tblPredictors);
  • From R2024b, fitrnet supports multi-response variables.

  • From R2025a, for custom architectures beyond LayerSizes, Activations, LayerWeightsInitializer, and LayerBiasesInitializer, pass a dlnetwork via the Network name-value argument.


Conventions

Training with trainnet + dlnetwork
Data formats

trainnet expects data in specific orientations by default:

Input layerExpected data shape
featureInputLayer(C)observations×channels (e.g., 150×4)
imageInputLayer([H W C])height×width×channels×observations (e.g., 28×28×1×5000)
sequenceInputLayer(C)timesteps×channels×observations, or an observations×1 cell array where each element is a timesteps×channels time series

If your data has a different layout, use InputDataFormats and/or TargetDataFormats in trainingOptions instead of transposing the data manually. The format string describes your data's current layout — one letter per dimension, not the desired layout. MATLAB handles the remapping internally. For cell arrays, add "B" (batch) to the format string — e.g., InputDataFormats="CTB" for cells of C×T matrices. Do not specify these options when data already matches the input layer's default.

What trainnet supports

Use trainnet and dlnetwork for all Deep Learning Toolbox training. This includes:

  • Standard classification and regression

  • Transfer learning

  • Multi-input or multi-output networks

  • Custom loss functions (pass a function handle to trainnet)

  • Custom loss function backward passes via DifferentiableFunction

  • Custom metrics (string, function handle, or deep.Metric subclass)

  • Custom stopping criteria via OutputFcn in trainingOptions

  • Custom layers

Custom training loops (dlfeval/dlgradient/update functions) are appropriate when the workflow requires customizations impossible via trainingOptions or the specific workflow — multi-model adversarial training, alternating updates, or custom weight update rules. Note that trainingOptions supports L-BFGS (R2023b+) and Levenberg-Marquardt "lm" (R2024b+).

When a user has a working custom training loop and asks to speed it up, apply dlaccelerate directly. Mention that their workflow may also be expressible with trainnet (which handles acceleration internally), but do not push the conversion — focus on accelerating the code they have.

NEVER use these legacy or discouraged APIs

If the user has existing code using these APIs, migrate it to the recommended replacement and briefly explain which APIs were replaced and what the modern equivalents are. If the user asks for a legacy or discouraged API by name, acknowledge their request and explain that the function has been replaced with a recommended alternative before providing the solution.

Legacy or discouraged APIRecommended replacement
trainNetworktrainnet
patternnetfitcnet (preferred), or dlnetwork + trainnet
fitnetfitrnet (preferred), or dlnetwork + trainnet
feedforwardnetdlnetwork + trainnet
narxnet, timedelaynetnlarx (preferred), or dlnetwork + trainnet
train() (shallow network object)trainnet
classifyminibatchpredict + scores2label
activationsminibatchpredict(net,data,Outputs=layer)
predictAndUpdateState, classifyAndUpdateState[Y,state] = predict(net,X); net.State = state;
classificationLayerNot required — use trainnet with "crossentropy" as the loss
regressionLayerNot required — use trainnet with "mse" as the loss
DAGNetwork, SeriesNetwork, layerGraphdlnetwork — supports addLayers, connectLayers, and replaceLayer for multi-branch architectures, anything layerGraph can do, dlnetwork can do directly
resnet18, googlenet, squeezenet, etc. (pretrained network functions that return DAGNetwork)imagePretrainedNetwork("resnet18", ...) — returns a dlnetwork and handles head replacement automatically
Manually converting network scores to labels (e.g., [~,idx] = max(scores))scores2label
plotconfusionconfusionchart
gensimexportNetworkToSimulink (preferred), or Predict block
preparetsnlarx (preferred, handles delays internally), or dlnetwork with sequenceInputLayer(C, MinLength=numDelays) + convolution1dLayer(numDelays, ..., Padding="causal")
closeloopforecast (preferred, with nlarx), or iterative predict loop feeding previous predictions back as input

See references/legacy-api-redirects.md for before/after code examples.

Inference — use minibatchpredict (or predict)

If you see a for-loop calling predict or forward on batches for inference, replace the entire loop with minibatchpredict. It handles batching, GPU transfer, dlarray conversion, and acceleration automatically. Output format (numeric array, table, or cell array) depends on the input type and network.

Exception: If the loop includes custom pre- or postprocessing around the predict call that cannot be separated from it, minibatchpredict cannot replicate the full pipeline. In that case, wrap the entire custom function with dlaccelerate instead (see references/dlaccelerate-workflow.md). Do not split the function into a minibatchpredict call plus separate accelerated pre/postprocessing — a single dlaccelerate boundary around the full function produces one unified trace.

  • For classification: use minibatchpredict (or predict) + scores2label.

  • For regression or when you need raw scores: use minibatchpredict or predict.

  • predict is for single-batch/small-batch use and accepts plain numeric arrays directly — do not wrap inputs in dlarray or call extractdata on outputs.

Evaluation — use testnet
  • Use testnet to calculate post-training metrics on a test dataset instead of doing it manually.

  • For single-output networks, use string metrics: "accuracy", "rmse".

  • trainnet and testnet accept targets as a separate argument only for in-memory data (testnet(net,XTest,TTest,"accuracy")). When passing a datastore, targets must already be embedded in it (e.g., labeled imageDatastore or combined datastore with targets in a second column).

  • For multi-output networks or advanced metric customization, see references/metrics-guidance.md.

Transfer learning — use imagePretrainedNetwork
net = imagePretrainedNetwork("squeezenet",NumClasses=5);

options = trainingOptions("adam", ...
    MaxEpochs=10, ...
    MiniBatchSize=16, ...
    InitialLearnRate=1e-4, ...
    ValidationData=imdsVal, ...
    Metrics="accuracy", ...
    Plots="training-progress");

net = trainnet(augimdsTrain,net,"crossentropy",options);

% Inference — class names come from training data, not the pretrained net
classNames = categories(imdsTrain.Labels);
scores = minibatchpredict(net,imdsTest);
labels = scores2label(scores,classNames);

imagePretrainedNetwork returns class names only when both NumClasses and NumResponses are unset (pretrained mode, no transfer learning).


Workflow: Training

Check the Decision section above first — tabular data goes to fitrnet/fitcnet unless you need a non-LBFGS solver or a non-MSE/cross-entropy loss.

Standard training
% Define network
numChannels = 3;
numClasses = 5;
layers = [
    sequenceInputLayer(numChannels,Normalization="zscore")
    lstmLayer(100,OutputMode="last")
    fullyConnectedLayer(numClasses)
    softmaxLayer];

% Training options
options = trainingOptions("adam", ...
    MaxEpochs=30, ...
    MiniBatchSize=128, ...
    ValidationData={XVal,TVal}, ...
    Metrics="accuracy", ...
    Plots="training-progress");

% Train
net = trainnet(XTrain,TTrain,layers,"crossentropy",options);

Always normalize inputs. Set Normalization on the input layer (see example above). For regression, also normalize targets:

  • R2026a+: append inverseNormalizationLayer to the last layer and set NormalizeTargets=true in trainingOptions

  • Pre-R2026a: manually z-score targets before training and denormalize predictions at inference

See references/normalization.md for both workflows.

Custom loss function for multi-output

The function handle receives network outputs then targets, in order. Pass categorical targets directly — trainnet encodes them automatically.

lossFcn = @(Y1,Y2,T1,T2) crossentropy(Y1,T1) + mse(Y2,T2);

net = trainnet(ds,net,lossFcn,options);

For the full multi-output recipe (OutputNames alignment, combined datastores, testnet evaluation), see references/multi-output-training.md.

Since the multi-output loss is a function handle, it can be accelerated using dlaccelerate. Always inform the user that their custom loss function handle can be accelerated with dlaccelerate for faster training — even if they did not ask to speed up training. See references/dlaccelerate-trainnet-custom-loss.md for the verification and production workflow.


  • dlnetwork (small, all layers supported): use exportNetworkToSimulink

  • dlnetwork (large, or has layers unsupported by exportNetworkToSimulink): use the Predict block at library path deeplib/Predict

  • fitcnet/fitrnet models: use the ClassificationNeuralNetwork Predict or RegressionNeuralNetwork Predict blocks from statsLibrary/

See references/simulink-export.md for details.


Workflow: Deep Learning Code Acceleration with dlaccelerate — Diagnose, Fix, or Apply

Does NOT apply to:

  • Simulink export

  • MEX/Coder compilation

  • fitcnet/fitrnet/nlarx workflows

Applies when:

  • User has a custom training loop (dlfeval/dlgradient)

  • User has a function that supports dlarray input and is long running

  • User calls trainnet with a custom loss function handle (R2026a+)

Always attempt to dlaccelerate functions that:

  • Are long-running.

  • Have dlarray objects, structures of dlarray objects, or dlnetwork objects as inputs.

  • Only perform operations that affect the output variables. Operations that do not affect the output variables include plotting, displaying output, and writing to files.

Check references/dlaccelerate-workflow.md Step 0 to determine whether the function is acceleratable before recommending.

Follow references/dlaccelerate-workflow.md for the full diagnostic/fix/improve process. Entry points:

  • User says "make faster" or "speed up" and the code has a custom training loop: start at Step 0.

  • Code already uses dlaccelerate with problems: start at Step 1 (identify antipatterns, apply fixes, verify).

  • Custom training loop without dlaccelerate: start at Step 0 (requirements check, wrap, verify).

  • Any function with dlarray input that is called repeatedly: start at Step 0. This includes custom model functions used for prediction.

  • Custom inference function called repeatedly: same Step 0 applies. dlaccelerate is not training-specific — any repeatedly-called dlarray function benefits. Use minibatchpredict when the loop only calls predict with no custom pre- or postprocessing; use dlaccelerate when custom operations surround the predict call.

  • trainnet + custom loss function handle (R2026a+): wrap the loss with dlaccelerate and pass the AcceleratedFunction to trainnet. See references/dlaccelerate-trainnet-custom-loss.md.

For antipatterns that break tracing and their fixes, see references/dlaccelerate-antipatterns.md.

dlaccelerate References
  • references/dlaccelerate-workflow.md — full diagnostic/fix/improve process

  • references/dlaccelerate-antipatterns.md — pattern catalog with BAD/GOOD examples

  • references/dlaccelerate-custom-training-loop.md — acceleration levels, L2, clipping

  • references/dlaccelerate-trainnet-custom-loss.md — trainnet + custom loss (R2026a+)

  • references/dlaccelerate-variable-length-sequences.md — padding/bucketing strategies

  • references/dlaccelerate-custom-layers.md — Acceleratable custom layers (nnet.layer.Acceleratable)

  • references/dlaccelerate-measure-speedup.md — benchmarking methodology


Key Functions

FunctionPurpose
fitcnetTrain neural network classifier for tabular data (Statistics and Machine Learning Toolbox)
fitrnetTrain neural network for regression on tabular data (Statistics and Machine Learning Toolbox)
nlarxNonlinear ARX model for NARX / time-delay time series (System Identification Toolbox)
trainnetTrain any dlnetwork with built-in or custom loss
dlnetworkModern network object (replaces DAGNetwork/SeriesNetwork/LayerGraph)
trainingOptionsConfigure solver, epochs, validation, metrics
minibatchpredictBatch inference (handles batching automatically)
scores2labelConvert score matrix to categorical labels
testnetEvaluate network with metrics on a dataset (handles batching automatically)
predictSingle-batch inference on dlnetwork, ClassificationNeuralNetwork, RegressionNeuralNetwork
imagePretrainedNetworkLoad pretrained model with automatic head replacement
exportNetworkToSimulinkExport dlnetwork to Simulink as layer blocks
analyzeNetworkInspect network: info = analyzeNetwork(net) returns layer info, parameter counts, and architecture issues
minibatchqueueManage mini-batches with custom per-batch preprocessing
dlaccelerateAccelerate a deep learning function by tracing and caching its execution graph

Common Mistakes

What the agent might tryWhy it's wrongDo this instead
predict(net,dlarray(X,"TCB"))Unnecessary — predict on a dlnetwork accepts plain arrayspredict(net,X)
Manual accuracy/RMSE after trainingCovered by existing functionalitytestnet(net,XTest,TTest,"accuracy")
squeezenet + layerGraph + replaceLayerDiscouraged manual layer surgery for transfer learningimagePretrainedNetwork("squeezenet",NumClasses=N)
Custom training loop for multi-outputUnnecessary complexitytrainnet with function handle loss
Transposing data to match the default layout (e.g., cellfun(@transpose,...))Unnecessary complexityInputDataFormats, TargetDataFormats — arrange letters to match your data's actual dimension order
testnet(net,ds,labels,"accuracy")testnet does not accept separate targets with datastorestestnet(net,ds,"accuracy")
trainnet for tabular dataUnnecessary complexity when using MSE/cross-entropy loss and LBFGS solverfitrnet or fitcnet
analyzeNetwork(net) without capturing outputLoses programmatic access to layer info, parameter counts, and issuesinfo = analyzeNetwork(net)
Manually encoding categorical columns before passing to trainnet/fitcnet/fitrnetUnnecessary complexity when these functions encode categorical data automaticallyPass categorical data directly
Manual for-loop batching for inferenceUnnecessary complexityminibatchpredict(net,X) — handles batching, GPU transfer, and acceleration automatically
Manual for-loop batching for custom preprocessingUnnecessary complexityminibatchqueue — handles batching, GPU transfer, dlarray conversion, and custom transforms

Copyright 2026 The MathWorks, Inc.


Files

references/dlaccelerate-workflow.md

dlaccelerate Diagnostic/Fix/Improve Workflow

Step-by-step process for diagnosing and fixing dlaccelerate tracing issues, and improving execution speed of deep learning workflows.

How Tracing Works

dlaccelerate wraps a function and caches its execution trace. On the first call it records every operation and builds an optimized computation graph. On subsequent calls, if the signature matches a cached trace, the cached version is reused without re-executing the original code.

Signature matching rules:

Input TypeWhat triggers a NEW trace
dlarrayDifferent size, format, or datatype (value changes are fine)
dlnetworkDifferent learnable/state sizes, formats, or datatypes
Scalars, strings, other non-dlarray variablesANY change in value (each unique value = new trace)
Function outputsDifferent number of output arguments requested

Key insight: dlarray values DON'T trigger re-tracing EXCEPT if their shape/type/format change, but non-dlarray values DO. This is the source of most performance bugs.

A non-dlarray input that is constant (same value every call) is perfectly safe — it produces exactly one cached trace. Only values that change between calls cause retracing. Do not defensively wrap constants as dlarray.

Cache properties: Default CacheSize is 50. Occupancy is a percentage (0–100) indicating how full the cache is. Each unique signature occupies one slot. clearCache(accFcn) forces re-tracing on next call.

Step 0: Assess Whether dlaccelerate Applies

Before measuring or fixing, determine whether dlaccelerate is already in use or should be added.

If the code already uses dlaccelerate → proceed to Step 1.

If trainnet with a custom loss function handle (R2026a+) → see dlaccelerate-trainnet-custom-loss.md.

If the code uses dlfeval/dlgradient WITHOUT dlaccelerate:

  1. Check the "Scenarios Where dlaccelerate Provides No Benefit" table below. If any scenario applies, stop and inform the user that dlaccelerate is not suitable here

  2. Identify the function passed to dlfeval (typically the gradient/loss function)

  3. Check these requirements before adding dlaccelerate:

    • No extractdata inside the function (would produce stale cached values, see Pattern 5 fix strategy in dlaccelerate-antipatterns.md)

    • No data-dependent control flow inside forward(net, x) if a network is involved (check custom layers for if/while/for/break/continue on dlarray values)

    • Non-dlarray arguments that CHANGE between calls are wrapped as dlarray (constant non-dlarray arguments are safe as-is, they produce one trace)

    • Custom layers in the network inherit nnet.layer.Acceleratable (or accept fragmented acceleration)

    • If any check fails, fix the issue first. Do NOT wrap with dlaccelerate until all checks pass

  4. Wrap it with dlaccelerate:

    accFcn = dlaccelerate(@modelGradient);
  5. Replace the dlfeval call to use the accelerated function:

    [loss, gradients, state] = dlfeval(accFcn, net, X, T);
  6. Scan the function for antipatterns in Step 1 before running and fix any obvious issues first

  7. Proceed to Step 3 to verify acceleration works

Step 1: Identify Breaking Patterns

Scan the traced function for antipatterns that cause retracing. See dlaccelerate-antipatterns.md for the full catalog with BAD/GOOD code examples.

#PatternWhat breaksQuick fix
1Data-dependent branchesif/switch on dlarray values or rand(), branch baked into cacheBranch-free arithmetic: min(t/norm, 1)
2Logical indexing with count-dependent opsmean/var/std/numel/size on selection baked as constantMask-based: sum(x.*mask,'all') / sum(mask,'all')
3Random numbers without tracingrand(sz) without 'like' becomes a constantrand(sz, 'like', x)
4Side effectsdisp, fprintf, file I/O skipped after first traceMove outside the traced function
5extractdata inside traced functionRemoves value from the trace, becomes stale constantTry stripdims first; move outside dlfeval if not needed in accelerated function
6Enclosing workspace variablesCaptured as constants at trace time, stale if changedPass as explicit function inputs
7Recreated anonymous function handlesEach new instance triggers retraceAssign to a variable once, reuse
8Variable-length sequence inputsDifferent sizes = new trace per batchSee dlaccelerate-variable-length-sequences.md
9Custom layer without AcceleratableFragmented acceleration, multiple boundary crossingsSee dlaccelerate-custom-layers.md

Note: Patterns 6 and 7 are special cases of a general rule: non-dlarray inputs are matched by exact value/identity. This is only a problem when the value CHANGES between calls. Constant non-dlarray inputs (fixed hyperparameters, static strings, named function handles) are safe and should NOT be wrapped as dlarray.

Step 2: Apply Fixes

Apply the matching fix patterns from Step 1. Safe patterns that need no fix:

  • Branching on constant non-dlarray inputs: if ~isinf(gradientThreshold) is evaluated at trace time, correct if the value never changes between calls

  • Constant non-dlarray inputs (scalars, strings, function handles assigned once): If the value never changes between calls, it produces one cached trace and is safe. Only wrap as dlarray if the value changes between calls.

  • forward(net, x) / predict(net, x) inside a traced function: Network execution is compatible, provided all custom layers in the network are trace-compatible (no data-dependent control flow such as if extractdata(...) < tol, break inside their predict/forward methods). If a custom layer has data-dependent branches, the first branch path gets baked into the trace and subsequent calls with different paths silently produce wrong results.

  • rand(sz, 'like', x): With x being a dlarray, it is traced correctly because it's linked to a dlarray.

  • sum(x.^2, 'all'): Format-safe reduction (assuming x being a dlarray)

Step 3: Verify Acceleration

After applying fixes (or after adding dlaccelerate for the first time), verify with three checks. All three are required, skipping any one can hide a different type of bug.

Check 1: HitRate and Occupancy — confirm traces are being reused:

accFcn = dlaccelerate(@myFunction);
clearCache(accFcn);

for i = 1:20
    x = ...;  % generate new inputs each iteration
    y = ...;
    result = dlfeval(accFcn, x, y);
    ...       % update network weights if applicable
end

fprintf('HitRate: %.1f%%\n', accFcn.HitRate);
fprintf('Occupancy: %.1f%%\n', accFcn.Occupancy);

Both are percentages (0–100). With default CacheSize=50, an Occupancy of 10% means 5 cached traces.

Interpreting results:

ObservationMeaning
Occupancy stable, HitRate converging upwardHealthy
HitRate = 0%Every call retraces (performance disaster)
Occupancy growing toward 100%Inputs vary too much, cache filling with unique traces
HitRate declining over timeGrowing number of unique signatures evicting each other
  • Healthy: Occupancy stops growing AND HitRate converges upward (normally >90%, but see dlaccelerate-variable-length-sequences.md if using bucketed or variable-length inputs).

  • Unhealthy: If HitRate stays low or Occupancy keeps growing, the software spends time creating new caches that do not get reused often. Revisit Step 1.

Check 2: CheckMode — confirm cached results are numerically correct:

accFcn.CheckMode = "tolerance";
accFcn.CheckTolerance = 1e-4;
clearCache(accFcn);

for i = 1:20
    x = ...;
    y = ...;
    result = dlfeval(accFcn, x, y);
    ...       % update network weights if applicable
end

accFcn.CheckMode = "none";  % disable after verification
clearCache(accFcn);

If warnings appear ("Accelerated outputs differ from underlying function outputs"), the function is NOT safe to accelerate — typically extractdata inside the function produces stale cached values. See Pattern 5 in dlaccelerate-antipatterns.md.

Why CheckMode is essential: A function can show 90%+ HitRate yet produce wrong outputs. HitRate only confirms the trace is reused, not that the reused result is correct. For training loops, always include weight updates between calls so CheckMode can detect stale-value bugs.

Step 4: Measure Speedup

See dlaccelerate-measure-speedup.md for the full benchmarking methodology (CPU and GPU timing, adaptive stopping, A/B via accFcn.Enabled). If tAccel > tBase, acceleration is harmful — return to Step 1.

Step 5: Optimize Custom Training Loop Structure

If the code uses a custom training loop (manual dlfeval/dlgradient calls rather than trainnet), see dlaccelerate-custom-training-loop.md for strategies that maximize the accelerated boundary, including:

  • 4 acceleration levels (model-only → full iteration including solver update)

  • dlaccelerate-compatible L2 regularization and gradient clipping implementations

  • Critical rules for wrapping iteration-varying inputs as dlarray

Scenarios Where dlaccelerate Provides No Benefit

ScenarioReason
Input sizes change almost every callConstant re-tracing, no speedup
Function has heavy side effects and requires themSide effects lost after tracing
Function uses data-dependent control flowOnly one branch captured per trace
Very short functions (< 1ms)Tracing overhead exceeds savings
trainnet with a built-in loss string or minibatchpredictRedundant. trainnet and minibatchpredict handle it internally
Debugging / developmentHides errors, harder to step through
Very few iterationsTracing cost may be greater than its performance benefit
Simulink export workflows (exportNetworkToSimulink, Predict block)Different execution model, dlaccelerate does not apply
MEX/Coder/compilation workflowsCode generation has its own optimization path
fitcnet / fitrnet / nlarx modelsThese use different solvers and execution paths, not dlarray-based training
When dlaccelerate Doesn't Apply — Speed-Up Alternatives

For custom training loops (manual dlfeval/dlgradient), speed up without dlaccelerate by:

OptionHow
GPU trainingUse gpuArray on input data, or minibatchqueue(..., OutputEnvironment="gpu") to send mini-batches to the GPU
Larger mini-batchesIncrease batch size to improve GPU utilisation (try 2–4× current value)
Solver tuningIncrease InitialLearnRate with a decay schedule

For trainnet with a built-in loss string, larger mini-batches and solver tuning still apply — configure via trainingOptions, plus:

OptionHow
GPU accelerationExecutionEnvironment="gpu" or "auto"
Background preprocessingPreprocessingEnvironment="background" or "parallel" — fetches and preprocesses data asynchronously (stochastic solvers only, requires subsettable datastore)

Copyright 2026 The MathWorks, Inc.

Requirements and compatibility

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Security

No issues found by our static scan· No issues found

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Created by
MathWorks
Publisher
MathWorks (Official)
Source
matlab/matlab-agentic-toolkit · skills-catalog/ai-and-statistics/matlab-train-network · commit 2a7f215
Licence
BSD 2-Clause License (redistribution allowed)
  • LICENSE (added by AutoGineer from LICENSE.md)
  • ATTRIBUTION.md (written by AutoGineer)
Synced

AutoGineer is not affiliated with or endorsed by MathWorks.

Download ZIP

The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download matlab-train-network.zip