Custom Training Using Automatic Differentiation - MATLAB & Simulink (original) (raw)

Train deep learning networks using custom training loops

If the trainingOptions function does not provide the training options that you need for your task, or you have a loss function that the trainnet function does not support, then you can define a custom training loop. For models that cannot be specified as networks of layers, you can define the model as a function. To learn more, see Define Custom Training Loops, Loss Functions, and Networks.

Functions

expand all

dlnetwork Deep learning neural network
imagePretrainedNetwork Pretrained neural network for images (Since R2024a)
resnetNetwork 2-D residual neural network (Since R2024a)
resnet3dNetwork 3-D residual neural network (Since R2024a)
addLayers Add layers to neural network
removeLayers Remove layers from neural network
replaceLayer Replace layer in neural network
connectLayers Connect layers in neural network
disconnectLayers Disconnect layers in neural network
addInputLayer Add input layer to network (Since R2022b)
initialize Initialize learnable and state parameters of neural network (Since R2021a)
networkDataLayout Deep learning network data layout for learnable parameter initialization (Since R2022b)
setL2Factor Set L2 regularization factor of layer learnable parameter
getL2Factor Get L2 regularization factor of layer learnable parameter
setLearnRateFactor Set learn rate factor of layer learnable parameter
getLearnRateFactor Get learn rate factor of layer learnable parameter
plot Plot neural network architecture
summary Print network summary (Since R2022b)
analyzeNetwork Analyze deep learning network architecture
checkLayer Check validity of custom or function layer
isequal Check equality of neural networks (Since R2021a)
isequaln Check equality of neural networks ignoring NaN values (Since R2021a)
forward Compute deep learning network output for training
predict Compute deep learning network output for inference
adamupdate Update parameters using adaptive moment estimation (Adam)
rmspropupdate Update parameters using root mean squared propagation (RMSProp)
sgdmupdate Update parameters using stochastic gradient descent with momentum (SGDM)
lbfgsupdate Update parameters using limited-memory BFGS (L-BFGS) (Since R2023a)
lbfgsState State of limited-memory BFGS (L-BFGS) solver (Since R2023a)
dlupdate Update parameters using custom function
trainingProgressMonitor Monitor and plot training progress for deep learning custom training loops (Since R2022b)
updateInfo Update information values for custom training loops (Since R2022b)
recordMetrics Record metric values for custom training loops (Since R2022b)
groupSubPlot Group metrics in training plot (Since R2022b)
deep.gpu.deterministicAlgorithms Set determinism of deep learning operations on the GPU to get reproducible results (Since R2024b)
padsequences Pad or truncate sequence data to same length (Since R2021a)
minibatchqueue Create mini-batches for deep learning (Since R2020b)
onehotencode Encode data labels into one-hot vectors (Since R2020b)
onehotdecode Decode probability vectors into class labels (Since R2020b)
next Obtain next mini-batch of data from minibatchqueue (Since R2020b)
reset Reset minibatchqueue to start of data (Since R2020b)
shuffle Shuffle data in minibatchqueue (Since R2020b)
hasdata Determine if minibatchqueue can return mini-batch (Since R2020b)
partition Partition minibatchqueue (Since R2020b)
dlarray Deep learning array for customization
dlgradient Compute gradients for custom training loops using automatic differentiation
dljacobian Jacobian matrix deep learning operation (Since R2024b)
dldivergence Divergence of deep learning data (Since R2024b)
dllaplacian Laplacian of deep learning data (Since R2024b)
dlfeval Evaluate deep learning model for custom training loops
dims Data format of dlarray object
finddim Find dimensions with specified label
stripdims Remove dlarray data format
extractdata Extract data from dlarray
isdlarray Check if object is dlarray (Since R2020b)
crossentropy Cross-entropy loss for classification tasks
indexcrossentropy Index cross-entropy loss for classification tasks (Since R2024b)
l1loss L1 loss for regression tasks (Since R2021b)
l2loss L2 loss for regression tasks (Since R2021b)
huber Huber loss for regression tasks (Since R2021a)
ctc Connectionist temporal classification (CTC) loss for unaligned sequence classification (Since R2021a)
mse Half mean squared error
dlaccelerate Accelerate deep learning function for custom training loops (Since R2021a)
AcceleratedFunction Accelerated deep learning function (Since R2021a)
clearCache Clear accelerated deep learning function trace cache (Since R2021a)

Topics

Custom Training Loops

Automatic Differentiation

Generative Adversarial Networks

Graph Neural Networks

Deep Learning Function Acceleration

Generate Images Using Diffusion

Generate Images Using Diffusion

Generate new images using a diffusion model.

Train Variational Autoencoder (VAE) to Generate Images

Train Variational Autoencoder (VAE) to Generate Images

Train a deep learning variational autoencoder (VAE) to generate images.

Train Fast Style Transfer Network

Train Fast Style Transfer Network

Train a network to transfer the style of an image to a second image. It is based on the architecture defined in [1].

Train a Twin Network for Dimensionality Reduction

Train a Twin Network for Dimensionality Reduction

Train a twin neural network with shared weights to compare handwritten digits using dimensionality reduction.

Train Bayesian Neural Network

Train Bayesian Neural Network

Train a Bayesian neural network (BNN) for image regression using Bayes by Backpropagation.

Train Image Classification Network Robust to Adversarial Examples

Train Image Classification Network Robust to Adversarial Examples

Train a neural network that is robust to adversarial examples using fast gradient sign method (FGSM) adversarial training.

Train Robust Deep Learning Network with Jacobian Regularization

Train Robust Deep Learning Network with Jacobian Regularization

Train a neural network that is robust to adversarial examples using a Jacobian regularization scheme.

Train a Twin Neural Network to Compare Images

Train a Twin Neural Network to Compare Images

Train a twin neural network with shared weights to identify similar images of handwritten characters.

Train Network Using Cyclical Learning Rate for Snapshot Ensembling

Train Network Using Cyclical Learning Rate for Snapshot Ensembling

Train a network to classify images of objects using a cyclical learning rate schedule and snapshot ensembling for better test accuracy. In the example, you learn how to use a cosine function for the learning rate schedule, take snapshots of the network during training to create a model ensemble, and add L2-norm regularization (weight decay) to the training loss.

Train Network Using Federated Learning

Train Network Using Federated Learning

Train a network using federated learning. Federated learning is a technique that enables you to train a network in a distributed, decentralized way [1].

Image Captioning Using Attention

Image Captioning Using Attention

Train a deep learning model for image captioning using attention.