matlab/matlab-agentic-toolkit / skills-catalog/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.2 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-variable-length-sequences.md
View on GitHub# Variable-Length Sequence Data and dlaccelerate
**Problem:** Sequence data with varying lengths produces different dlarray sizes each batch. Since dlaccelerate keys on size, EVERY batch triggers a new trace.
**Symptoms:** `accFcn.HitRate` reads 0% or very low; `accFcn.Occupancy` grows steadily toward 100% (cache filling with unique traces).
## Step 0: Check Sequence Length Distribution
Before choosing a padding strategy, inspect the actual lengths:
```matlab
lengths = cellfun(@(x) size(x,2), sequences);
fprintf('Min: %d, Max: %d, Median: %d\n', min(lengths), max(lengths), median(lengths));
```
If max/min ratio > 3× or max/median ratio > 2×, warn about excessive padding and recommend bucketing or percentile-based truncation instead of padding to max length. Excessive padding wastes computation and may degrade training quality.
## Primary Fix: Pre-Pad All Sequences Once Before Training
Pre-pad the entire dataset to a single fixed length **before** the training loop. Every mini-batch then has the same tensor shape — dlaccelerate needs only one trace and HitRate converges near 100%:
```matlab
[XPad, mask] = padsequences(sequences, 2, Length="longest");
% Or pad/truncate to a specific fixed length:
[XPad, mask] = padsequences(sequences, 2, Length=200);
```
Use the returned `mask` (1=real data, 0=padded) inside the loss function to exclude padding from gradient contributions.
If a few outlier sequences are much longer than the majority, set `Length` to a percentile of the distribution (e.g., 95th percentile) — `padsequences` truncates sequences longer than the specified length.
## Alternative: Bucketing (Memory-Constrained Cases)
If a few very long sequences would force wasteful padding for the whole dataset, group sequences into a small number of discrete length buckets and pad each bucket to its target length:
```matlab
% Sort sequences by length, group into buckets (e.g., 50, 100, 150, 200)
[XBucket, mask] = padsequences(bucket, 2, Length=bucketLen);
% Result: only 4 traces needed instead of hundreds
```
Bucketing is a memory/HitRate tradeoff. Use it only when global pre-padding is impractical.
## Note: Per-Batch Padding with minibatchqueue
Padding each mini-batch to its own `maxLen` inside a `MiniBatchFcn` **does not eliminate re-tracing**, every unique batch-max-length still creates a new trace. Use a fixed `Length` argument to avoid this:
```matlab
mbq = minibatchqueue(ds, 3, MiniBatchSize=32, MiniBatchFcn=@preprocessMiniBatch);
function [XPad, YPad, mask] = preprocessMiniBatch(X, Y)
[XPad, mask] = padsequences(X, 2, Length=200); % fixed length → one trace
YPad = padsequences(Y, 2);
end
```
Without `Length=fixedVal`, per-batch padding is equivalent to the original problem.
## Impact on Validation
With variable-length data, more iterations are needed when validating acceleration with `CheckMode = "tolerance"`. The initial iterations will produce new traces (cache misses) rather than hits. Aim for enough iterations to see HitRate stabilize after the unique lengths have been cached.
----
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: 321k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 321k | 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: 321k | 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: 321k | Updated | Claude CodeCodex | Verified | Install |