trainnet — Train a supported dlnetwork with a built-in loss.
trainnet(X,Y,net,lossFcn,options) trains a supported dlnetwork compatibility object over in-memory numeric or logical predictor data.
Syntax
net = trainnet(X, Y, net, lossFcn, options)
[net, info] = trainnet(X, Y, net, lossFcn, options)How trainnet works
- Supports the five-argument form with 2-D predictor matrices where rows are observations and columns are features.
netmay be adlnetwork, trained network compatibility object, simple layerGraph, or supported layer collection.lossFcnsupports"mse"/"mean-squared-error"for regression and"crossentropy"for classification.- For regression,
Ymust be a numeric or logical matrix with one row per observation and the same output width as the network. - For classification,
Ymay be numeric integer labels, string labels, categorical labels, or a one-hot/probability matrix. The returneddlnetworkstores sorted class labels inClasseswhen labels are provided. trainingOptionsmetadata controlssgdmandadamtraining, includingMaxEpochs,MiniBatchSize,InitialLearnRate,Momentum, Adam decay factors,Epsilon, deterministicShufflehandling, and CPU/autoExecutionEnvironmentvalues.- The returned object is a
dlnetworkcompatibility object with updated layer weights,Learnables,TrainingInfo,TrainingLoss, andTrainedWithmetadata. - Custom loss function handles are not executed by
trainnet; use explicitdlfeval/dlgradientloops for custom hostdlarrayautodiff. Datastores, image/sequence/recurrent/convolutional/normalization/custom layers, branching DAG execution, validation data, callbacks, plots, and GPU training remain outside this slice and raise explicit compatibility errors or are stored as inert options.
GPU memory and residency
trainnet currently gathers gpuArray inputs to host before training when the execution environment is auto or cpu. Explicit ExecutionEnvironment values of gpu, multi-gpu, or parallel raise a compatibility error because native GPU training kernels, provider-resident Deep Learning layers, and GPU-backed autodiff remain future acceleration work.
Example
Train a dlnetwork with crossentropy
net = dlnetwork(layers);
opts = trainingOptions('adam','MaxEpochs',120,'MiniBatchSize',4,'InitialLearnRate',0.05,'Shuffle','never');
net = trainnet(X, labels, net, 'crossentropy', opts)Expected output:
net is a trained dlnetwork compatibility object usable with forward or predict.Using trainnet with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how trainnet changes the result.
Run a small trainnet example, explain the result, then change one input and compare the output.
Related Deep Learning functions
adamupdate · analyzeNetwork · bilstmLayer · classificationLayer · combvec · convolution1dLayer · crossentropy · dlarray · dlfeval · dlgradient · dlnetwork · dlupdate · eluLayer · exportONNXNetwork · featureInputLayer · forward · fullyConnectedLayer · globalAveragePooling1dLayer · layerGraph · layerNormalizationLayer · lstmLayer · padsequences · regressionLayer · reluLayer · sequenceInputLayer · softmaxLayer · trainingOptions · trainNetwork
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.