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/metrics-guidance.md

View on GitHub
# Metrics Guidance

How to choose the right metric format for `testnet` and `trainingOptions`.

## Decision Tree

1. **Single-output network, built-in metric?** → Use a string: `"accuracy"`,
   `"rmse"`, `"fscore"`.

2. **Multi-output network?** → See `references/multi-output-training.md` for the
   full recipe including metric objects with `NetworkOutput`.

3. **Need customization of a built-in metric?** → Use metric objects with
   name-value arguments (e.g., `AverageType="macro"/"micro"/"weighted"` for
   classification metrics):
   ```matlab
   metrics = precisionMetric(AverageType="micro");
   ```

4. **Custom metric where weighted average across minibatches is acceptable?** →
   Define a custom metric function:
   ```matlab
   options = trainingOptions("adam",Metrics=@myMetricFcn);
   ```
   The custom metric function receives all network outputs followed by all
   targets, in order: `metric = myMetricFcn(Y1,Y2,...,T1,T2,...)`.

5. **Custom metric requiring accumulation of intermediate values across
   minibatches** (e.g., accumulating true negatives/false positives to compute
   an exact metric over the full dataset)? → Subclass `deep.Metric`. Key aspects:
   - Expose `Name` and `NetworkOutput` as constructor arguments so users can configure them; set `Maximize` internally (`true` if higher is better, `false` if lower is better)
   - Implement `reset` to zero out accumulated state
   - Implement `update(metric,batchY,batchT)` to accumulate per-minibatch values
   - Implement `aggregate(metric,metric2)` to combine results across workers
   - Implement `evaluate` to return the final scalar metric value from accumulated state

## Using Metrics with trainnet

Metrics specified in `trainingOptions` are displayed during training:

```matlab
options = trainingOptions("adam",Metrics="accuracy");
```

For multi-output networks, see `references/multi-output-training.md`.

## Using Metrics with testnet

```matlab
accuracy = testnet(net,XTest,TTest,"accuracy");
rmse = testnet(net,XTest,TTest,"rmse");
```

For multi-output networks, see `references/multi-output-training.md`.

----

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: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated 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: 322kUpdated 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: 322kUpdated Claude CodeCodexVerifiedInstall