# Autoencoder

An autoencoder is a type of artificial neural network used to learn efficient codings of unlabeled data (unsupervised learning). An autoencoder learns two functions: an encoding function that transforms the input data, and a decoding function that recreates the input data from the encoded representation. The autoencoder learns an efficient representation (encoding) for a set of data, typically for dimensionality reduction, to generate lower-dimensional embeddings for subsequent use by other machine learning algorithms.
Variants exist which aim to make the learned representations assume useful properties. Examples are regularized autoencoders (sparse, denoising and contractive autoencoders), which are effective in learning representations for subsequent classification tasks, and variational autoencoders, which can be used as generative models. Autoencoders are applied to many problems, including facial recognition, feature detection, anomaly detection, and learning the meaning of words. In terms of data synthesis, autoencoders can also be used to randomly generate new data that is similar to the input (training) data.

## Mathematical principles
### Definition

An autoencoder is defined by the following components:
   Two sets: the space of decoded messages ${\mathcal {X}}$; the space of encoded messages ${\mathcal {Z}}$. Typically ${\mathcal {X}}$ and ${\mathcal {Z}}$ are Euclidean spaces, that is, ${\mathcal {X}}=\mathbb {R} ^{m},{\mathcal {Z}}=\mathbb {R} ^{n}$ with $m>n.$
   Two parametrized families of functions: the encoder family $E_{\phi }:{\mathcal {X}}\rightarrow {\mathcal {Z}}$, parametrized by $\phi$; the decoder family $D_{\theta }:{\mathcal {Z}}\rightarrow {\mathcal {X}}$, parametrized by $\theta$.
For any $x\in {\mathcal {X}}$, we usually write $z=E_{\phi }(x)$, and refer to it as the code, the latent variable, latent representation, latent vector, etc. Conversely, for any $z\in {\mathcal {Z}}$, we usually write $x'=D_{\theta }(z)$, and refer to it as the (decoded) message.
Usually, both the encoder and the decoder are defined as multilayer perceptrons (MLPs). For example, a one-layer-MLP encoder $E_{\phi }$ is:
    $E_{\phi }(\mathbf {x} )=\sigma (Wx+b)$
where $\sigma$ is an element-wise activation function, $W$ is a "weight" matrix, and $b$ is a "bias" vector.

### Training an autoencoder

An autoencoder, by itself, is simply a tuple of two functions. To judge its quality, we need a task. A task is defined by a reference probability distribution $\mu _{ref}$ over ${\mathcal {X}}$, and a "reconstruction quality" function $d:{\mathcal {X}}\times {\mathcal {X}}\to [0,\infty ]$, such that $d(x,x')$ measures how much $x'$ differs from $x$.
With those, we can define the loss function for the autoencoder as$L(\theta ,\phi ):=\mathbb {\mathbb {E} } _{x\sim \mu _{ref}}[d(x,D_{\theta }(E_{\phi }(x)))]$The optimal autoencoder for the given task $(\mu _{ref},d)$ is then $\arg \min _{\theta ,\phi }L(\theta ,\phi )$. The search for the optimal autoencoder can be accomplished by any mathematical optimization technique, but usually by gradient descent. This search process is referred to as "training the autoencoder".
In most situations, the reference distribution is just the empirical distribution given by a dataset $\{x_{1},...,x_{N}\}\subset {\mathcal {X}}$, so that$\mu _{ref}={\frac {1}{N}}\sum _{i=1}^{N}\delta _{x_{i}}$
where $\delta _{x_{i}}$ is the Dirac measure, the quality function is just L2 loss: $d(x,x')=\|x-x'\|_{2}^{2}$, and $\|\cdot \|_{2}$ is the Euclidean norm. Then the problem of searching for the optimal autoencoder is just a least-squares optimization:$\min _{\theta ,\phi }L(\theta ,\phi ),\qquad {\text{where }}L(\theta ,\phi )={\frac {1}{N}}\sum _{i=1}^{N}\|x_{i}-D_{\theta }(E_{\phi }(x_{i}))\|_{2}^{2}$

### Interpretation

An autoencoder has two main parts: an encoder that maps the message to a code, and a decoder that reconstructs the message from the code. An optimal autoencoder would perform as close to perfect reconstruction as possible, with "close to perfect" defined by the reconstruction quality function $d$.
The simplest way to perform the copying task perfectly would be to duplicate the signal. To suppress this behavior, the code space ${\mathcal {Z}}$ usually has fewer dimensions than the message space ${\mathcal {X}}$.
Such an autoencoder is called undercomplete. It can be interpreted as compressing the message, or reducing its dimensionality.
At the limit of an ideal undercomplete autoencoder, every possible code $z$ in the code space is used to encode a message $x$ that really appears in the distribution $\mu _{ref}$, and the decoder is also perfect: $D_{\theta }(E_{\phi }(x))=x$. This ideal autoencoder can then be used to generate messages indistinguishable from real messages, by feeding its decoder arbitrary code $z$ and obtaining $D_{\theta }(z)$, which is a message that really appears in the distribution $\mu _{ref}$.
If the code space ${\mathcal {Z}}$ has dimension larger than (overcomplete), or equal to, the message space ${\mathcal {X}}$, or the hidden units are given enough capacity, an autoencoder can learn the identity function and become useless. However, experimental results found that overcomplete autoencoders might still learn useful features.
In the ideal setting, the code dimension and the model capacity could be set on the basis of the complexity of the data distribution to be modeled. A standard way to do so is to add modifications to the basic autoencoder, to be detailed below.

## Variations
### Variational autoencoder (VAE)

Variational autoencoders (VAEs) belong to the families of variational Bayesian methods. Despite the architectural similarities with basic autoencoders, VAEs are architected with different goals and have a different mathematical formulation. The latent space is, in this case, composed of a mixture of distributions instead of fixed vectors.
Given an input dataset $x$ characterized by an unknown probability function $P(x)$ and a multivariate latent encoding vector $z$, the objective is to model the data as a distribution $p_{\theta }(x)$, with $\theta$ defined as the set of the network parameters so that $p_{\theta }(x)=\int _{z}p_{\theta }(x,z)dz$.

### Sparse autoencoder (SAE)

Inspired by the sparse coding hypothesis in neuroscience, sparse autoencoders (SAE) are variants of autoencoders, such that the codes $E_{\phi }(x)$ for messages tend to be sparse codes, that is, $E_{\phi }(x)$ is close to zero in most entries. Sparse autoencoders may include more (rather than fewer) hidden units than inputs, but only a small number of the hidden units are allowed to be active at the same time. Encouraging sparsity improves performance on classification tasks.
There are two main ways to enforce sparsity. One way is to simply clamp all but the highest-k activations of the latent code to zero. This is the k-sparse autoencoder.
The k-sparse autoencoder inserts the following "k-sparse function" in the latent layer of a standard autoencoder:$f_{k}(x_{1},...,x_{n})=(x_{1}b_{1},...,x_{n}b_{n})$where $b_{i}=1$ if $|x_{i}|$ ranks in the top k, and 0 otherwise.
Backpropagating through $f_{k}$ is simple: set gradient to 0 for $b_{i}=0$ entries, and keep gradient for $b_{i}=1$ entries. This is essentially a generalized ReLU function.
The other way is a relaxed version of the k-sparse autoencoder. Instead of forcing sparsity, we add a sparsity regularization loss, then optimize for$\min _{\theta ,\phi }L(\theta ,\phi )+\lambda L_{\text{sparse}}(\theta ,\phi )$where $\lambda >0$ measures how much sparsity we want to enforce.
Let the autoencoder architecture have $K$ layers. To define a sparsity regularization loss, we need a "desired" sparsity ${\hat {\rho }}_{k}$ for each layer, a weight $w_{k}$ for how much to enforce each sparsity, and a function $s:[0,1]\times [0,1]\to [0,\infty ]$ to measure how much two sparsities differ.
For each input $x$, let the actual sparsity of activation in each layer $k$ be$\rho _{k}(x)={\frac {1}{n}}\sum _{i=1}^{n}a_{k,i}(x)$where $a_{k,i}(x)$ is the activation in the $i$ -th neuron of the $k$ -th layer upon input $x$.
The sparsity loss upon input $x$ for one layer is $s({\hat {\rho }}_{k},\rho _{k}(x))$, and the sparsity regularization loss for the entire autoencoder is the expected weighted sum of sparsity losses:$L_{\text{sparse}}(\theta ,\phi )=\mathbb {\mathbb {E} } _{x\sim \mu _{X}}\left[\sum _{k\in 1:K}w_{k}s({\hat {\rho }}_{k},\rho _{k}(x))\right]$Typically, the function $s$ is either the Kullback-Leibler (KL) divergence, as
       $s(\rho ,{\hat {\rho }})=KL(\rho ||{\hat {\rho }})=\rho \log {\frac {\rho }{\hat {\rho }}}+(1-\rho )\log {\frac {1-\rho }{1-{\hat {\rho }}}}$
or the L1 loss, as $s(\rho ,{\hat {\rho }})=|\rho -{\hat {\rho }}|$, or the L2 loss, as $s(\rho ,{\hat {\rho }})=|\rho -{\hat {\rho }}|^{2}$.
Alternatively, the sparsity regularization loss may be defined without reference to any "desired sparsity", but simply force as much sparsity as possible. In this case, one can define the sparsity regularization loss as $L_{\text{sparse}}(\theta ,\phi )=\mathbb {\mathbb {E} } _{x\sim \mu _{X}}\left[\sum _{k\in 1:K}w_{k}\|h_{k}\|\right]$where $h_{k}$ is the activation vector in the $k$-th layer of the autoencoder. The norm $\|\cdot \|$ is usually the L1 norm (giving the L1 sparse autoencoder) or the L2 norm (giving the L2 sparse autoencoder).

### Denoising autoencoder (DAE)

Denoising autoencoders (DAE) try to achieve a good representation by changing the reconstruction criterion.
A DAE, originally called a "robust autoassociative network" by Mark A. Kramer, is trained by intentionally corrupting the inputs of a standard autoencoder during training. A noise process is defined by a probability distribution $\mu _{T}$ over functions $T:{\mathcal {X}}\to {\mathcal {X}}$. That is, the function $T$ takes a message $x\in {\mathcal {X}}$, and corrupts it to a noisy version $T(x)$. The function $T$ is selected randomly, with a probability distribution $\mu _{T}$.
Given a task $(\mu _{\text{ref}},d)$, the problem of training a DAE is the optimization problem:$\min _{\theta ,\phi }L(\theta ,\phi )=\mathbb {\mathbb {E} } _{x\sim \mu _{X},T\sim \mu _{T}}[d(x,(D_{\theta }\circ E_{\phi }\circ T)(x))]$That is, the optimal DAE should take any noisy message and attempt to recover the original message without noise, thus the name "denoising".
Usually, the noise process $T$ is applied only during training and testing, not during downstream use.
The use of DAE depends on two assumptions:
* There exist representations to the messages that are relatively stable and robust to the type of noise we are likely to encounter;
* The said representations capture structures in the input distribution that are useful for our purposes.
Example noise processes include:
* additive isotropic Gaussian noise,
* masking noise (a fraction of the input is randomly chosen and set to 0)
* salt-and-pepper noise (a fraction of the input is randomly chosen and randomly set to its minimum or maximum value).

### Contractive autoencoder (CAE)

A contractive autoencoder (CAE) adds the contractive regularization loss to the standard autoencoder loss:$\min _{\theta ,\phi }L(\theta ,\phi )+\lambda L_{\text{cont}}(\theta ,\phi )$where $\lambda >0$ measures how much contractive-ness we want to enforce. The contractive regularization loss itself is defined as the expected square of Frobenius norm of the Jacobian matrix of the encoder activations with respect to the input:$L_{\text{cont}}(\theta ,\phi )=\mathbb {E} _{x\sim \mu _{ref}}\|\nabla _{x}E_{\phi }(x)\|_{F}^{2}$To understand what $L_{\text{cont}}$ measures, note the fact$\|E_{\phi }(x+\delta x)-E_{\phi }(x)\|_{2}\leq \|\nabla _{x}E_{\phi }(x)\|_{F}\|\delta x\|_{2}$for any message $x\in {\mathcal {X}}$, and small variation $\delta x$ in it. Thus, if $\|\nabla _{x}E_{\phi }(x)\|_{F}^{2}$ is small, it means that a small neighborhood of the message maps to a small neighborhood of its code. This is a desired property, as it means small variation in the message leads to small, perhaps even zero, variation in its code, like how two pictures may look the same even if they are not exactly the same.
The DAE can be understood as an infinitesimal limit of CAE: in the limit of small Gaussian input noise, DAEs make the reconstruction function resist small but finite-sized input perturbations, while CAEs make the extracted features resist infinitesimal input perturbations.

### Minimum description length autoencoder (MDL-AE)

A minimum description length autoencoder (MDL-AE) is an advanced variation of the traditional autoencoder, which leverages principles from information theory, specifically the Minimum Description Length (MDL) principle. The MDL principle posits that the best model for a dataset is the one that provides the shortest combined encoding of the model and the data. In the context of autoencoders, this principle is applied to ensure that the learned representation is not only compact but also interpretable and efficient for reconstruction.
The MDL-AE seeks to minimize the total description length of the data, which includes the size of the latent representation (code length) and the error in reconstructing the original data. The objective can be expressed as $L_{\text{code}}+L_{\text{error}}$, where $L_{\text{code}}$ represents the length of the compressed latent representation and $L_{\text{error}}$ denotes the reconstruction error.

### Concrete autoencoder (CAE)

The concrete autoencoder is designed for discrete feature selection. A concrete autoencoder forces the latent space to consist only of a user-specified number of features. The concrete autoencoder uses a continuous relaxation of the categorical distribution to allow gradients to pass through the feature selector layer, which makes it possible to use standard backpropagation to learn an optimal subset of input features that minimize reconstruction loss.

## Advantages of depth

Autoencoders are often trained with a single-layer encoder and a single-layer decoder, but using many-layered (deep) encoders and decoders offers many advantages.
* Depth can exponentially reduce the computational cost of representing some functions.
* Depth can exponentially decrease the amount of training data needed to learn some functions.
* Experimentally, deep autoencoders yield better compression compared to shallow or linear autoencoders.

### Training

Geoffrey Hinton developed the deep belief network technique for training many-layered deep autoencoders. His method involves treating each neighboring set of two layers as a restricted Boltzmann machine so that pretraining approximates a good solution, then using backpropagation to fine-tune the results.
Researchers have debated whether joint training (i.e. training the whole architecture together with a single global reconstruction objective to optimize) would be better for deep auto-encoders. A 2015 study showed that joint training learns better data models along with more representative features for classification as compared to the layerwise method. However, their experiments showed that the success of joint training depends heavily on the regularization strategies adopted.

## History

(Oja, 1982) noted that PCA is equivalent to a neural network with one hidden layer with identity activation function. In the language of autoencoding, the input-to-hidden module is the encoder, and the hidden-to-output module is the decoder. Subsequently, in (Baldi and Hornik, 1989) and (Kramer, 1991) generalized PCA to autoencoders, which they termed as "nonlinear PCA".
Immediately after the resurgence of neural networks in the 1980s, it was suggested in 1986 that a neural network be put in "auto-association mode". This was then implemented in (Harrison, 1987) and (Elman, Zipser, 1988) for speech and in (Cottrell, Munro, Zipser, 1987) for images. In (Hinton, Salakhutdinov, 2006), deep belief networks were developed. These train a pair restricted Boltzmann machines as encoder-decoder pairs, then train another pair on the latent representation of the first pair, and so on.
The first applications of AE date to early 1990s. Their most traditional application was dimensionality reduction or feature learning, but the concept became widely used for learning generative models of data. Some of the most powerful AIs in the 2010s involved autoencoder modules as a component of larger AI systems, such as VAE in Stable Diffusion, discrete VAE in Transformer-based image generators like DALL-E 1, etc.
During the early days, when the terminology was uncertain, the autoencoder has also been called identity mapping, auto-associating, self-supervised backpropagation, or Diabolo network.

## Applications

The two main applications of autoencoders are dimensionality reduction and information retrieval (or associative memory), but modern variations have been applied to other tasks.

### Dimensionality reduction

Dimensionality reduction was one of the first deep learning applications.
For Hinton's 2006 study, he pretrained a multi-layer autoencoder with a stack of RBMs and then used their weights to initialize a deep autoencoder with gradually smaller hidden layers until hitting a bottleneck of 30 neurons. The resulting 30 dimensions of the code yielded a smaller reconstruction error compared to the first 30 components of a principal component analysis (PCA), and learned a representation that was qualitatively easier to interpret, clearly separating data clusters.
Representing dimensions can improve performance on tasks such as classification. Indeed, the hallmark of dimensionality reduction is to place semantically related examples near each other.

#### Principal component analysis

If linear activations are used, or only a single sigmoid hidden layer, then the optimal solution to an autoencoder is strongly related to principal component analysis (PCA). The weights of an autoencoder with a single hidden layer of size $p$ (where $p$ is less than the size of the input) span the same vector subspace as the one spanned by the first $p$ principal components, and the output of the autoencoder is an orthogonal projection onto this subspace. The autoencoder weights are not equal to the principal components, and are generally not orthogonal, yet the principal components may be recovered from them using the singular value decomposition.
However, the potential of autoencoders resides in their non-linearity, allowing the model to learn more powerful generalizations compared to PCA, and to reconstruct the input with significantly lower information loss.

### Information retrieval and Search engine optimization

Information retrieval benefits particularly from dimensionality reduction in that search can become more efficient in certain kinds of low dimensional spaces. Autoencoders were indeed applied to semantic hashing, proposed by Salakhutdinov and Hinton in 2007. By training the algorithm to produce a low-dimensional binary code, all database entries could be stored in a hash table mapping binary code vectors to entries. This table would then support information retrieval by returning all entries with the same binary code as the query, or slightly less similar entries by flipping some bits from the query encoding.
The encoder-decoder architecture, often used in natural language processing and neural networks, can be scientifically applied in the field of SEO (Search Engine Optimization) in various ways:
* Text Processing: By using an autoencoder, it's possible to compress the text of web pages into a more compact vector representation. This can help reduce page loading times and improve indexing by search engines.
* Noise Reduction: Autoencoders can be used to remove noise from the textual data of web pages. This can lead to a better understanding of the content by search engines, thereby enhancing ranking in search engine result pages.
* Meta Tag and Snippet Generation: Autoencoders can be trained to automatically generate meta tags, snippets, and descriptions for web pages using the page content. This can optimize the presentation in search results, increasing the Click-Through Rate (CTR).
* Content Clustering: Using an autoencoder, web pages with similar content can be automatically grouped together. This can help organize the website logically and improve navigation, potentially positively affecting user experience and search engine rankings.
* Generation of Related Content: An autoencoder can be employed to generate content related to what is already present on the site. This can enhance the website's attractiveness to search engines and provide users with additional relevant information.
* Keyword Detection: Autoencoders can be trained to identify keywords and important concepts within the content of web pages. This can assist in optimizing keyword usage for better indexing.
* Semantic Search: By using autoencoder techniques, semantic representation models of content can be created. These models can be used to enhance search engines' understanding of the themes covered in web pages.
In essence, the encoder-decoder architecture or autoencoders can be leveraged in SEO to optimize web page content, improve their indexing, and enhance their appeal to both search engines and users.

### Anomaly detection

Another application for autoencoders is anomaly detection. By learning to replicate the most salient features in the training data under some of the constraints described previously, the model is encouraged to learn to precisely reproduce the most frequently observed characteristics. When facing anomalies, the model should worsen its reconstruction performance. In most cases, only data with normal instances are used to train the autoencoder; in others, the frequency of anomalies is small compared to the observation set so that its contribution to the learned representation could be ignored. After training, the autoencoder will accurately reconstruct "normal" data, while failing to do so with unfamiliar anomalous data. Reconstruction error (the error between the original data and its low dimensional reconstruction) is used as an anomaly score to detect anomalies.
Recent literature has however shown that certain autoencoding models can, counterintuitively, be very good at reconstructing anomalous examples and consequently not able to reliably perform anomaly detection.

### Image processing

The characteristics of autoencoders are useful in image processing.
One example can be found in lossy image compression, where autoencoders outperformed other approaches and proved competitive against JPEG 2000.
Another useful application of autoencoders in image preprocessing is image denoising.
Autoencoders found use in more demanding contexts such as medical imaging where they have been used for image denoising as well as super-resolution. In image-assisted diagnosis, experiments have applied autoencoders for breast cancer detection and for modelling the relation between the cognitive decline of Alzheimer's disease and the latent features of an autoencoder trained with MRI.

### Drug discovery

In 2019 molecules generated with variational autoencoders were validated experimentally in mice.

### Popularity prediction

Recently, a stacked autoencoder framework produced promising results in predicting popularity of social media posts, which is helpful for online advertising strategies.

### Machine translation

Autoencoders have been applied to machine translation, which is usually referred to as neural machine translation (NMT). Unlike traditional autoencoders, the output does not match the input - it is in another language. In NMT, texts are treated as sequences to be encoded into the learning procedure, while on the decoder side sequences in the target language(s) are generated. Language-specific autoencoders incorporate further linguistic features into the learning procedure, such as Chinese decomposition features. Machine translation is rarely still done with autoencoders, due to the availability of more effective transformer networks.

### Communication Systems

Autoencoders in communication systems are important because they help in encoding data into a more resilient representation for channel impairments, which is crucial for transmitting information while minimizing errors. In Addition, AE-based systems can optimize end-to-end communication performance. This approach can solve the several limitations of designing communication systems such as the inherent difficulty in accurately modeling the complex behavior of real-world channels.