matlab-train-network

Train, evaluate, and export neural networks in MATLAB using modern dlnetwork-based APIs.

995|122|Updated Apr 3, 2026
One-click install
npx skills add https://github.com/matlab/matlab-agentic-toolkit --skill matlab-train-network
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: matlab-train-network
Source: https://github.com/matlab/matlab-agentic-toolkit/tree/main/skills-catalog/ai-and-statistics/matlab-train-network
Command: npx skills add https://github.com/matlab/matlab-agentic-toolkit --skill matlab-train-network

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

MATLAB deep learning code often relies on legacy or discouraged APIs (trainNetwork, patternnet, fitnet, DAGNetwork) that miss modern features, and dlaccelerate usage frequently suffers from silent tracing bugs that destroy performance or produce wrong results. This Skill guides AI agents to use the recommended R2024a+ APIs and correctly diagnose, fix, and apply dlaccelerate.

Core Features & Use Cases

  • Modern training workflows: Train classifiers and regressors with trainnet, dlnetwork, fitcnet, and fitrnet, including multi-output networks, custom loss functions, and transfer learning via imagePretrainedNetwork.
  • Legacy code migration: Convert patternnet, fitnet, narxnet, trainNetwork, DAGNetwork, and gensim code to recommended replacements with before/after examples.
  • dlaccelerate diagnostics: Detect and fix tracing antipatterns (data-dependent branches, extractdata misuse, side effects), verify with HitRate/Occupancy/CheckMode, and measure speedup.
  • Simulink export: Export trained networks using exportNetworkToSimulink or Predict blocks.
  • Use Case: A user has a slow custom training loop with dlfeval/dlgradient. The Skill identifies antipatterns, wraps the function with dlaccelerate, verifies cache hit rates, and benchmarks the resulting speedup.

Quick Start

Ask the agent to train a neural network classifier on your tabular data in MATLAB and evaluate its accuracy on a test set.

Frequently Asked Questions about matlab-train-network

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I train a neural network in MATLAB with the modern API?

Use trainnet with a dlnetwork or layer array, a loss such as "crossentropy" or "mse", and trainingOptions. For tabular data, fitcnet and fitrnet provide one-call training for classification and regression.

How do I migrate trainNetwork or patternnet code to current MATLAB APIs?

Replace trainNetwork with trainnet, patternnet with fitcnet, fitnet with fitrnet, and DAGNetwork or layerGraph with dlnetwork. Pretrained functions like squeezenet are replaced by imagePretrainedNetwork, which handles head replacement automatically.

Why is my dlaccelerate code slower or producing wrong results?

Common causes are data-dependent branches, extractdata inside the traced function, side effects like fprintf, and non-dlarray inputs that change every call. Check HitRate, Occupancy, and CheckMode="tolerance" to confirm traces are reused and numerically correct.

Can I accelerate a custom loss function passed to trainnet?

Yes, from R2026a trainnet accepts an AcceleratedFunction as the loss argument. Wrap the loss handle with dlaccelerate, verify with CheckMode="tolerance" on a short run, then set CheckMode="none" for production training.

How do I export a trained MATLAB network to Simulink?

Use exportNetworkToSimulink for small dlnetwork objects with supported layers, or the Predict block from deeplib for larger networks. Models from fitcnet and fitrnet use the dedicated predict blocks in the Statistics and Machine Learning Toolbox library.

When should I use fitcnet or fitrnet instead of trainnet?

Use fitcnet or fitrnet for tabular data when MSE or cross-entropy loss and the L-BFGS solver are sufficient. Choose trainnet when you need custom losses, multiple inputs or outputs, sequence or image data, or solvers like Adam.