zrwangems/dsh-zrwang-plugin / skills/engineering/matlab/ai-and-statistics/matlab-train-network
matlab-train-network
VerifiedTrain, 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.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 19.4 KB |
| manifest.yaml | config | 533 B |
| references/dlaccelerate-antipatterns.md | markdown | 10.9 KB |
| references/dlaccelerate-custom-layers.md | markdown | 2.5 KB |
| references/dlaccelerate-custom-training-loop.md | markdown | 5.4 KB |
| references/dlaccelerate-measure-speedup.md | markdown | 2.9 KB |
| references/dlaccelerate-trainnet-custom-loss.md | markdown | 3.0 KB |
| references/dlaccelerate-variable-length-sequences.md | markdown | 3.0 KB |
| references/dlaccelerate-workflow.md | markdown | 11.4 KB |
| references/legacy-api-redirects.md | markdown | 4.0 KB |
| references/metrics-guidance.md | markdown | 2.1 KB |
| references/multi-output-training.md | markdown | 3.3 KB |
| references/normalization.md | markdown | 2.0 KB |
| references/simulink-export.md | markdown | 1.7 KB |
references/dlaccelerate-trainnet-custom-loss.md
View on GitHub# Accelerating Custom Loss Functions in trainnet (R2026a+)
Since R2026a, `trainnet` accepts an `AcceleratedFunction` object as the loss argument. This applies when the user passes a **function handle** (not a built-in loss string) to `trainnet`.
## When to Recommend
User calls `trainnet` with a custom loss like:
```matlab
lossFcn = @(Y1, Y2, T1, T2) crossentropy(Y1,T1) + mse(Y2,T2);
net = trainnet(ds, net, lossFcn, options);
```
## Fix
Wrap the loss with `dlaccelerate` and pass the accelerated object:
```matlab
lossFcn = @(Y1, Y2, T1, T2) crossentropy(Y1,T1) + mse(Y2,T2);
accLossFcn = dlaccelerate(lossFcn);
net = trainnet(ds, net, accLossFcn, options);
```
## Verification
trainnet only preserves the user's AcceleratedFunction object when `CheckMode` is set to `"tolerance"` before training. Use a short verification run to confirm acceleration works, then run full production training without the checking overhead.
Run a short verification by reducing `MaxEpochs` (1–2 epochs is sufficient — each epoch already processes the entire dataset) and optionally increasing `MiniBatchSize` to reduce the number of iterations further.
**Step 1: Short verification run**
```matlab
accLossFcn = dlaccelerate(lossFcn);
accLossFcn.CheckMode = "tolerance";
accLossFcn.CheckTolerance = 1e-4;
verifyOpts = trainingOptions('adam', MaxEpochs=2, MiniBatchSize=64, Verbose=false);
trainnet(ds, net, accLossFcn, verifyOpts);
fprintf('HitRate: %.1f%%\n', accLossFcn.HitRate);
fprintf('Occupancy: %.1f%%\n', accLossFcn.Occupancy);
```
If warnings appear ("Accelerated outputs differ from underlying function outputs"), the loss is NOT safe to accelerate — typically `extractdata` inside the function produces stale cached values. See Pattern 5 in [dlaccelerate-antipatterns.md](dlaccelerate-antipatterns.md).
**Step 2: Code ready to run the actual training**
```matlab
accLossFcn.CheckMode = "none";
clearCache(accLossFcn);
net = trainnet(ds, net, accLossFcn, options);
```
`CheckMode = "none"` removes the overhead of re-running the unaccelerated path each iteration. `clearCache` ensures any previous traces of the accelerated function are removed.
## When It Won't Help
If the loss function has variable-size inputs (e.g., variable-length sequences causing different input sizes each batch), the trace triggers every call. Check `HitRate`: if low, consider padding/bucketing inputs or accepting no acceleration benefit. See [dlaccelerate-variable-length-sequences.md](dlaccelerate-variable-length-sequences.md) for strategies.
## Pre-flight Checklist
Before wrapping, confirm the loss function is trace-compatible:
- No `extractdata` or `gather` inside the loss
- No data-dependent branches (`if`/`switch` on dlarray values)
- No side effects (`fprintf`, `disp`, file I/O, plotting)
- Fixed-size outputs (no logical indexing producing variable-length results)
If any of these apply, fix the loss function first. See [dlaccelerate-antipatterns.md](dlaccelerate-antipatterns.md) for the full catalog.
----
Copyright 2026 The MathWorks, Inc.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| 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 implementation | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| 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 fallback | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| 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 always | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |