Main Content

trainAutoencoder

(To be removed) Train an autoencoder

trainAutoencoder will be removed in a future release. For more information, see Transition Legacy Neural Network Code to dlnetwork Workflows.

For advice on updating your code, see Version History.

Description

autoenc = trainAutoencoder(X) returns an autoencoder, autoenc, trained using the training data in X.

example

autoenc = trainAutoencoder(X,hiddenSize) returns an autoencoder autoenc, with the hidden representation size of hiddenSize.

autoenc = trainAutoencoder(___,Name=Value) specifies options using one or more name-value arguments in addition to the input arguments in the previous syntaxes. For example, to train on a GPU, set UseGPU to "on".

Examples

collapse all

Load the sample data.

X = abalone_dataset;

X is an 8-by-4177 matrix defining eight attributes for 4177 different abalone shells: sex (M, F, and I (for infant)), length, diameter, height, whole weight, shucked weight, viscera weight, shell weight. For more information on the dataset, type help abalone_dataset in the command line.

Train a sparse autoencoder with default settings.

autoenc = trainAutoencoder(X);

Figure Neural Network Training (09-Apr-2026 05:41:26) contains an object of type uigridlayout.

Reconstruct the abalone shell ring data using the trained autoencoder.

XReconstructed = predict(autoenc,X);

Compute the mean squared reconstruction error.

mseError = mse(X-XReconstructed)
mseError = 
0.0167

Input Arguments

collapse all

Training data, specified as a matrix of training samples or a cell array of image data. If X is a matrix, then each column contains a single sample. If X is a cell array of image data, then the data in each cell must have the same number of dimensions. The image data can be pixel intensity data for gray images, in which case, each cell contains an m-by-n matrix. Alternatively, the image data can be RGB data, in which case, each cell contains an m-by-n-3 matrix.

Data Types: single | double | cell

Size of hidden representation of the autoencoder, specified as a positive integer value. This number is the number of neurons in the hidden layer.

Data Types: single | double

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: EncoderTransferFunction="satlin",L2WeightRegularization=0.05 specifies the transfer function for the encoder as the positive saturating linear transfer function and the L2 weight regularization as 0.05.

Transfer function for the encoder, specified as one the values listed in this table.

Transfer Function OptionDefinition
"logsig"

Logistic sigmoid function

f(z)=11+ez

"satlin"

Positive saturating linear transfer function

f(z)={0,if z0z,if 0<z<11,if z1

This argument sets the EncoderTransferFunction training parameter property of the output Autoencoder object as a character vector.

Example: EncoderTransferFunction="satlin"

Transfer function for the decoder, specified as one the values listed in this table.

Transfer Function OptionDefinition
"logsig"

Logistic sigmoid function

f(z)=11+ez

"satlin"

Positive saturating linear transfer function

f(z)={0,if z0z,if 0<z<11,if z1

"purelin"

Linear transfer function

f(z)=z

This argument sets the DecoderTransferFunction training parameter property of the output Autoencoder object as a character vector.

Example: DecoderTransferFunction="purelin"

Maximum number of training epochs or iterations, specified as a positive integer value.

Example: MaxEpochs=1200

The coefficient for the L2 weight regularizer in the cost function (LossFunction), specified as a positive scalar value.

Example: L2WeightRegularization=0.05

Loss function to use for training, specified as "msesparse". It corresponds to the mean squared error function adjusted for training a sparse autoencoder as follows:

E=1Nn=1Nk=1K(xknx^kn)2mean squared error+λ*ΩweightsL2regularization+β*Ωsparsitysparsityregularization,

where λ is the coefficient for the L2 regularization term and β is the coefficient for the sparsity regularization term. You can specify the values of λ and β by using the L2WeightRegularization and SparsityRegularization name-value arguments, respectively, while training an autoencoder.

This argument sets the LossFunction training parameter property of the output Autoencoder object as a character vector.

Indicator to show the training window, specified as numeric or logical 1 (true) or 0 (false).

Example: ShowProgressWindow=false

Desired proportion of training examples a neuron reacts to, specified as a positive scalar value. Sparsity proportion is a parameter of the sparsity regularizer. It controls the sparsity of the output from the hidden layer. A low value for SparsityProportion usually leads to each neuron in the hidden layer "specializing" by only giving a high output for a small number of training examples. Hence, a low sparsity proportion encourages higher degree of sparsity.

Example: SparsityProportion=0.01 is equivalent to saying that each neuron in the hidden layer should have an average output of 0.1 over the training examples.

Coefficient that controls the impact of the sparsity regularizer in the cost function, specified as a positive scalar value.

Example: SparsityRegularization=1.6

The algorithm to use for training the autoencoder, specified as "trainscg". It stands for scaled conjugate gradient descent [1].

This argument sets the TrainingAlgorithm training parameter property of the output Autoencoder object as a character vector.

Indicator to rescale the input data, specified as numeric or logical 1 (true) or 0 (false).

Autoencoders attempt to replicate their input at their output. For it to be possible, the range of the input data must match the range of the transfer function for the decoder. trainAutoencoder automatically scales the training data to this range when training an autoencoder. If the data was scaled while training an autoencoder, the predict, encode, and decode methods also scale the data.

Example: ScaleData=false

Option to use GPU for training, specified as one of these values:

  • "off" — Use the local CPU.

  • "on" — Use the local GPU.

  • "auto" — Use the local GPU if one is available. Otherwise, use the local CPU.

To use a GPU to accelerate computations in MATLAB®, you must have Parallel Computing Toolbox™ and a supported GPU device. For more information on supported devices, see GPU Computing Requirements (Parallel Computing Toolbox).

Before R2026a: Specify the UseGPU option as numeric or logical 0 (false) or 1 (true). These correspond to "off" and "auto", respectively.

Example: UseGPU="on"

Output Arguments

collapse all

Trained autoencoder, returned as an Autoencoder object. For information on the properties and methods of this object, see Autoencoder class page.

More About

collapse all

References

[1] Moller, M. F. “A Scaled Conjugate Gradient Algorithm for Fast Supervised Learning”, Neural Networks, Vol. 6, 1993, pp. 525–533.

[2] Olshausen, B. A. and D. J. Field. “Sparse Coding with an Overcomplete Basis Set: A Strategy Employed by V1.” Vision Research, Vol.37, 1997, pp.3311–3325.

Version History

Introduced in R2015b

expand all