- name
SKILL.md
The skill file stays in the publisher's repository.
Paste the copied command in a terminal. It installs this SKILL.md into that agent.
npx skills add matlab/matlab-agentic-toolkit --skill matlab-train-network -a cursor -yPaste the copied command in a terminal. It installs this SKILL.md into that agent.
npx skills add matlab/matlab-agentic-toolkit --skill matlab-train-network -a claude-code -yPaste the copied command in a terminal. It installs this SKILL.md into that agent.
npx skills add matlab/matlab-agentic-toolkit --skill matlab-train-network -a codex -yPaste the copied command in a terminal. It installs this SKILL.md into that agent.
npx skills add matlab/matlab-agentic-toolkit --skill matlab-train-network -a grok -ySend the copied message in a Grok Bot chat. The Bot fetches this SKILL.md from GitHub and adds it to the skills all your Bots share.
Install this skill: https://github.com/matlab/matlab-agentic-toolkit/tree/HEAD/skills-catalog/ai-and-statistics/matlab-train-networkFrom the publisher
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.
Activate this skill when a user asks to:
Apply this check before starting any training workflow below.
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);
fitrnet supports multi-response variables.LayerSizes, Activations, LayerWeightsInitializer, and LayerBiasesInitializer, pass a dlnetwork via the Network name-value argument.trainnet expects data in specific orientations by default:
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.
Use trainnet and dlnetwork for all Deep Learning Toolbox training. This includes:
trainnet)DifferentiableFunctiondeep.Metric subclass)OutputFcn in trainingOptionsCustom 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.
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.
See references/legacy-api-redirects.md for before/after code examples.
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.
minibatchpredict (or predict) + scores2label.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.testnet to calculate post-training metrics on a test dataset instead
of doing it manually."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).references/metrics-guidance.md.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).
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.
% 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:
inverseNormalizationLayer to the last layer and set
NormalizeTargets=true in trainingOptionsSee references/normalization.md for both workflows.
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 exportNetworkToSimulinkdlnetwork (large, or has layers unsupported by exportNetworkToSimulink): use the Predict block at library path deeplib/Predictfitcnet/fitrnet models: use the ClassificationNeuralNetwork Predict or RegressionNeuralNetwork Predict blocks from statsLibrary/See references/simulink-export.md for details.
Does NOT apply to:
fitcnet/fitrnet/nlarx workflowsApplies when:
dlfeval/dlgradient)trainnet with a custom loss function handle (R2026a+)Always attempt to dlaccelerate functions that:
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:
dlaccelerate with problems: start at Step 1 (identify
antipatterns, apply fixes, verify).dlaccelerate: start at Step 0 (requirements check,
wrap, verify).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.
references/dlaccelerate-workflow.md — full diagnostic/fix/improve processreferences/dlaccelerate-antipatterns.md — pattern catalog with BAD/GOOD examplesreferences/dlaccelerate-custom-training-loop.md — acceleration levels, L2, clippingreferences/dlaccelerate-trainnet-custom-loss.md — trainnet + custom loss (R2026a+)references/dlaccelerate-variable-length-sequences.md — padding/bucketing strategiesreferences/dlaccelerate-custom-layers.md — Acceleratable custom layers (nnet.layer.Acceleratable)references/dlaccelerate-measure-speedup.md — benchmarking methodologyCopyright 2026 The MathWorks, Inc.
>
---name: matlab-train-networkdescription: >---# skills-catalog/ai-and-statistics/matlab-train-network/SKILL.mdFAQ
Download skills-catalog/ai-and-statistics/matlab-train-network/SKILL.md from the publisher, or copy the path and open the repository. PlugMCP lists the skill and does not host the file.
The publisher keeps it at https://github.com/matlab/matlab-agentic-toolkit.
Claude, Cursor, Codex, and Grok Build can read a SKILL.md you add from the publisher's repository. Grok Bot installs it when you send the skill's GitHub folder link in a chat.
Manually converting network scores to labels (e.g., [~,idx] = max(scores)) | scores2label |
plotconfusion | confusionchart |
gensim | exportNetworkToSimulink (preferred), or Predict block |
preparets | nlarx (preferred, handles delays internally), or dlnetwork with sequenceInputLayer(C, MinLength=numDelays) + convolution1dLayer(numDelays, ..., Padding="causal") |
closeloop | forecast (preferred, with nlarx), or iterative predict loop feeding previous predictions back as input |