trainNetwork — Train a supported in-memory feed-forward network.
trainNetwork(X,Y,layers,options) trains a supported sequential feed-forward Deep Learning network over in-memory numeric or logical predictor data.
Syntax
net = trainNetwork(X, Y, layers, options)
[net, info] = trainNetwork(X, Y, layers, options)How trainNetwork works
- Supports the four-argument form with 2-D predictor matrices where rows are observations and columns are features.
- Layer collections may be layer objects, cell arrays of layer objects, object lists, or simple
layerGraphmetadata objects. - Supported trainable topology is the same sequential feed-forward subset used by
forward:featureInputLayer,fullyConnectedLayer,reluLayer,eluLayer,softmaxLayer,classificationLayer, andregressionLayer. - 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 returned trained network stores 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
SeriesNetworkfor layer arrays and aDAGNetworkfor simple layerGraph inputs. It includes updated layer weights,Learnables,TrainingInfo,TrainingLoss, andTrainedWithmetadata. - Datastores, image/sequence/recurrent/convolutional/normalization/custom layers, branching DAG execution, validation data, callbacks, checkpoints, plots, multi-GPU training, and automatic differentiation tracing remain outside this slice and raise explicit compatibility errors or are stored as inert options.
GPU memory and residency
trainNetwork 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 and provider-resident Deep Learning layer execution remain future acceleration work.
Examples
Train a small regression network
layers = {featureInputLayer(1,'Name','x'); fullyConnectedLayer(1,'Name','fc'); regressionLayer('Name','out')};
opts = trainingOptions('adam','MaxEpochs',120,'MiniBatchSize',4,'InitialLearnRate',0.05,'Shuffle','never');
net = trainNetwork([0;1;2;3], [1;3;5;7], layers, opts)Expected output:
net is a trained SeriesNetwork compatibility object usable with predict.Train a two-class network
layers = {featureInputLayer(2,'Name','in'); fullyConnectedLayer(2,'Name','fc'); softmaxLayer('Name','prob'); classificationLayer('Name','class')};
net = trainNetwork(X, labels, layers, opts)Expected output:
net.Classes records the sorted class labels.Using trainNetwork with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how trainNetwork changes the result.
Run a small trainNetwork 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 · trainnet
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.