$npx -y skills add matlab/matlab-agentic-toolkit --skill matlab-train-networkTrain, 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). Use when training, fine-tuning, evalu
| 1 | # matlab-train-network |
| 2 | |
| 3 | Train, evaluate, and export neural networks to Simulink in MATLAB using the |
| 4 | recommended `dlnetwork`-based API (`trainnet`, `dlnetwork`, `minibatchpredict`, |
| 5 | `scores2label`, `testnet`, `imagePretrainedNetwork`) or, for tabular data, the |
| 6 | Statistics and Machine Learning Toolbox functions `fitcnet` and `fitrnet`. |
| 7 | |
| 8 | ## When to Use |
| 9 | |
| 10 | Activate this skill when a user asks to: |
| 11 | |
| 12 | - Train any neural network (classifier, regression, multi-output, LSTM, CNN, etc.) |
| 13 | - Fine-tune or use a pretrained model for transfer learning |
| 14 | - Evaluate a trained network on test data |
| 15 | - Run inference / predict with a trained network |
| 16 | - Export a trained network to Simulink |
| 17 | - Migrate existing legacy (patternnet, fitnet, narxnet, gensim) or discouraged |
| 18 | (trainNetwork, DAGNetwork, classify) code to recommended APIs |
| 19 | - Create a "pattern recognition network", "function fitting network", "NARX |
| 20 | network", or any task historically associated with the Neural Network Toolbox |
| 21 | shallow nets API |
| 22 | |
| 23 | ## When NOT to Use |
| 24 | |
| 25 | - Importing/exporting models (importNetworkFromPyTorch, exportONNXNetwork) |
| 26 | - Data loading and preprocessing (imageDatastore, transforms, augmentation) |
| 27 | - Network architecture design decisions (choosing CNN vs LSTM vs transformer) |
| 28 | - Reinforcement learning workflows (use Reinforcement Learning Toolbox) |
| 29 | - Object detection (use specialized detector training functions in Computer Vision Toolbox) |
| 30 | |
| 31 | ## Decision: fitrnet/fitcnet or trainnet |
| 32 | |
| 33 | Apply this check before starting any training workflow below. |
| 34 | |
| 35 | | Criterion | fitcnet/fitrnet | trainnet | |
| 36 | |-----------|----------------|----------| |
| 37 | | Ease of use | Simplest — one function call | Requires network definition + trainingOptions | |
| 38 | | Solver | L-BFGS | Adam, SGDM, RMSProp, L-BFGS, LM (R2024b+) | |
| 39 | | Loss functions | MSE and cross-entropy only | Any built-in or custom (pass function handle) | |
| 40 | | Multiple input/output branches | No | Yes | |
| 41 | | Custom architecture | Via `Network` argument (R2025a+) | Yes | |
| 42 | | Data type | Tabular data only (a table or a numeric matrix) | Tabular data plus everything else (sequences, images, multi-input) | |
| 43 | |
| 44 | Pass tables directly to `trainnet`, `fitcnet`, and `fitrnet`. If inputs have |
| 45 | categorical columns, pass them directly — they are encoded automatically |
| 46 | (`fitcnet`/`fitrnet` always; `trainnet`/`minibatchpredict`/`testnet` from R2025a). |
| 47 | |
| 48 | ```matlab |
| 49 | % Classification |
| 50 | mdl = fitcnet(tbl,responseName,LayerSizes=20); |
| 51 | [labels,score] = predict(mdl,tblTest); |
| 52 | L = loss(mdl,tblTest); |
| 53 | |
| 54 | % Regression |
| 55 | mdl = fitrnet(tbl,responseName,LayerSizes=[20 20]); |
| 56 | Y = predict(mdl,tblTest); |
| 57 | L = loss(mdl,tblTest); |
| 58 | |
| 59 | % Tabular data with trainnet (when fitcnet/fitrnet can't be used) |
| 60 | net = trainnet(tbl,net,"crossentropy",options); |
| 61 | accuracy = testnet(net,tblTest,"accuracy"); |
| 62 | scores = minibatchpredict(net,tblPredictors); |
| 63 | ``` |
| 64 | |
| 65 | - From R2024b, `fitrnet` supports multi-response variables. |
| 66 | - From R2025a, for custom architectures beyond `LayerSizes`, `Activations`, `LayerWeightsInitializer`, and `LayerBiasesInitializer`, pass a `dlnetwork` via the `Network` name-value argument. |
| 67 | |
| 68 | --- |
| 69 | |
| 70 | ## Conventions |
| 71 | |
| 72 | ### Training with trainnet + dlnetwork |
| 73 | |
| 74 | #### Data formats |
| 75 | |
| 76 | `trainnet` expects data in specific orientations by default: |
| 77 | |
| 78 | | Input layer | Expected data shape | |
| 79 | |-------------|-------------------| |
| 80 | | `featureInputLayer(C)` | observations×channels (e.g., 150×4) | |
| 81 | | `imageInputLayer([H W C])` | height×width×channels×observations (e.g., 28×28×1×5000) | |
| 82 | | `sequenceInputLayer(C)` | timesteps×channels×observations, or an observations×1 cell array where each element is a timesteps×channels time series | |
| 83 | |
| 84 | If your data has a different layout, use `InputDataFormats` and/or |
| 85 | `TargetDataFormats` in `trainingOptions` instead of transposing the data manually. |
| 86 | The format string describes your data's current layout — one letter per |
| 87 | dimension, not the desired layout. MATLAB handles the remapping internally. |
| 88 | For cell arrays, add `"B"` (batch) to the format string — e.g., |
| 89 | `InputDataFormats="CTB"` for cells of C×T matrices. Do not specify these |
| 90 | options when data already matches the input layer's default. |
| 91 | |
| 92 | #### What trainnet supports |
| 93 | |
| 94 | Use `trainnet` and `dlnetwork` for all Deep Learning Toolbox training. This includes: |
| 95 | |
| 96 | - Standard classification and regression |
| 97 | - Transfer learning |
| 98 | - Multi-input or multi-output networks |
| 99 | - Custom loss functions (pass a function handle to `trainnet`) |
| 100 | - Custom loss function backward passes via `DifferentiableFunction` |
| 101 | - Custom metrics (string, function handle, or `deep.Metric` subclass) |
| 102 | - Custom stopping crit |