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-measure-speedup.md

View on GitHub
# Measuring dlaccelerate Speedup

Measure the actual execution time improvement from dlaccelerate after verifying tracing correctness (Step 4 in [dlaccelerate-workflow.md](dlaccelerate-workflow.md)).

If the `matlab-optimize-performance` skill is available, use it to measure end-to-end speedup as it handles warmup, `timeit`/`gputimeit`, etc.

**Critical: Run benchmarks in isolation.** Do NOT launch multiple MATLAB sessions that measure performance simultaneously. CPU/GPU contention between parallel sessions produces meaningless timing data. Run one benchmark at a time.

**Critical: Prefer functions over scripts.** Wrap any benchmarking code to a function rather running it directly in a script.

## Manual A/B Test Using accFcn.Enabled

If `matlab-optimize-performance` is not available, use the `accFcn.Enabled` property to A/B test acceleration with adaptive stopping:

```matlab
nWarmup = 5;    % increase for very fast functions
nMaxRuns = 100; % upper bound
tolCoV = 0.05;  % stop early when coefficient of variation < 5%
nMinRuns = 10;  % minimum runs before checking convergence

% Measure CPU execution time.
accFcn.Enabled = false;
for i = 1:nWarmup, dlfeval(accFcn, x, y); end
times = zeros(1, nMaxRuns);
for i = 1:nMaxRuns
    tic; dlfeval(accFcn, x, y); times(i) = toc;
    if i >= nMinRuns && std(times(1:i))/mean(times(1:i)) < tolCoV
        times = times(1:i); break;
    end
end
tBase = median(times);

accFcn.Enabled = true;
clearCache(accFcn);
for i = 1:nWarmup, dlfeval(accFcn, x, y); end
times = zeros(1, nMaxRuns);
for i = 1:nMaxRuns
    tic; dlfeval(accFcn, x, y); times(i) = toc;
    if i >= nMinRuns && std(times(1:i))/mean(times(1:i)) < tolCoV
        times = times(1:i); break;
    end
end
tAccel = median(times);

fprintf('Speedup: %.3fx\n', tBase / tAccel);
```

## GPU Timing

For GPU execution, use `wait(gpuDevice)` to synchronize before timing:

```matlab
% Measure GPU execution time (requires wait for accurate timing).
accFcn.Enabled = false;
for i = 1:nWarmup, dlfeval(accFcn, x, y); end
times = zeros(1, nMaxRuns);
for i = 1:nMaxRuns
    wait(gpuDevice); tic; dlfeval(accFcn, x, y); wait(gpuDevice); times(i) = toc;
    if i >= nMinRuns && std(times(1:i))/mean(times(1:i)) < tolCoV
        times = times(1:i); break;
    end
end
tBase = median(times);

accFcn.Enabled = true;
clearCache(accFcn);
for i = 1:nWarmup, dlfeval(accFcn, x, y); end
times = zeros(1, nMaxRuns);
for i = 1:nMaxRuns
    wait(gpuDevice); tic; dlfeval(accFcn, x, y); wait(gpuDevice); times(i) = toc;
    if i >= nMinRuns && std(times(1:i))/mean(times(1:i)) < tolCoV
        times = times(1:i); break;
    end
end
tAccel = median(times);

fprintf('Speedup: %.3fx\n', tBase / tAccel);
```

## Interpreting Results

If tAccel > tBase, acceleration is harmful, likely due to constant re-tracing. Return to [dlaccelerate-workflow.md](dlaccelerate-workflow.md) Step 2 to identify the breaking pattern.

----

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