Skip to content

matlab/matlab-agentic-toolkit / skills-catalog/ai-and-statistics/matlab-train-network

matlab-train-network

Verified

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.

references/normalization.md

View on GitHub
# Normalization

Normalize inputs and targets per channel before training. Compute statistics
from training data only — never recompute on validation or test data.

## Classification Workflow

Set `Normalization` on the input layer. Statistics are computed per channel
from training data automatically.

```matlab
layers = [
    sequenceInputLayer(numChannels,Normalization="zscore")
    lstmLayer(100,OutputMode="last")
    fullyConnectedLayer(numClasses)
    softmaxLayer];

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

## Regression Workflow (since R2026a)

- **Inputs:** set the `Normalization` property on the input layer.
- **Targets:** append an `inverseNormalizationLayer` as the last layer, then
  set `NormalizeTargets=true` in `trainingOptions` so `trainnet` normalizes the
  targets during training and the `inverseNormalizationLayer` denormalizes
  predictions automatically at inference.

```matlab
layers = [
    sequenceInputLayer(numChannels,Normalization="zscore")
    lstmLayer(100,OutputMode="last")
    fullyConnectedLayer(numResponses)
    inverseNormalizationLayer];

options = trainingOptions("adam",MaxEpochs=50,NormalizeTargets=true);
net = trainnet(XTrain,TTrain,layers,"mse",options);
rmse = testnet(net,XTest,TTest,"rmse");
```

## Regression Workflow (before R2026a)

Normalize inputs and targets manually per channel. Denormalize predictions
at inference using training statistics.

```matlab
% Per-channel stats from training data only
muX = mean(XTrain,1);  sigmaX = std(XTrain,0,1);
muT = mean(TTrain,1);  sigmaT = std(TTrain,0,1);

XTrainNorm = (XTrain - muX) ./ sigmaX;
TTrainNorm = (TTrain - muT) ./ sigmaT;
XTestNorm = (XTest - muX) ./ sigmaX;

layers = [
    sequenceInputLayer(numChannels)
    lstmLayer(100,OutputMode="last")
    fullyConnectedLayer(numResponses)];

options = trainingOptions("adam",MaxEpochs=50);
net = trainnet(XTrainNorm,TTrainNorm,layers,"mse",options);

% Denormalize predictions
Y = minibatchpredict(net,XTestNorm) .* sigmaT + muT;
```

----

Copyright 2026 The MathWorks, Inc.

----
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 319kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 319kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 319kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 319kUpdated Claude CodeCodexVerifiedInstall