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/dlaccelerate-custom-layers.md

View on GitHub
# Custom Layers and dlaccelerate

Custom layers should inherit `nnet.layer.Acceleratable` to participate in acceleration:

```matlab
classdef MyLayer < nnet.layer.Layer & nnet.layer.Acceleratable
    methods
        function Y = predict(layer, X)
            Y = myOp(X);
        end
    end
end
```

## Without `Acceleratable`

- The layer forces `Acceleration="auto"` to break the network into multiple accelerated functions (one per contiguous group of acceleratable layers)
- Performance degrades significantly due to multiple boundary crossings

## With `Acceleratable`

- The layer participates in the same accelerated function as surrounding layers
- Performance is close to built-in layers

## Important Distinction

`Acceleratable` only applies to automatic acceleration (`Acceleration="auto"` inside `forward`/`predict`). If you place the entire network execution inside your own `dlaccelerate` call, all layers are accelerated regardless of whether they inherit `Acceleratable`.

## Data-Dependent Control Flow in Custom Layers

A custom layer's `predict`/`forward` method must be trace-compatible. If the method contains data-dependent control flow (`if`/`while`/`for`/`break`/`continue` on dlarray values or values derived from `extractdata`), the first branch path gets baked into the trace and subsequent calls with different paths silently produce wrong results.

```matlab
% BAD: data-dependent iteration count, not trace-compatible
classdef SinkhornLayer < nnet.layer.Layer & nnet.layer.Acceleratable
    methods
        function Y = predict(layer, X)
            for i = 1:layer.MaxIter
                X = normalize(X);
                if extractdata(gather(norm(X - Xprev))) < layer.Tol
                    break;  % iteration count depends on data
                end
            end
            Y = X;
        end
    end
end
```

**Fix:** Remove data-dependent stopping before adding `Acceleratable`. Use a fixed iteration count:

```matlab
% GOOD: fixed iteration count, trace-compatible
classdef SinkhornLayer < nnet.layer.Layer & nnet.layer.Acceleratable
    methods
        function Y = predict(layer, X)
            for i = 1:layer.NumIter  % fixed, not data-dependent
                X = normalize(X);
            end
            Y = X;
        end
    end
end
```

If the layer cannot be made trace-compatible (the algorithm fundamentally requires data-dependent stopping), do NOT add `Acceleratable`. Accept fragmented acceleration or place the layer outside the accelerated boundary.

----

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