# Extended Autoencoder for Novelty Detection with Reconstruction along Projection Pathway
## 3 Preliminaries of RaPP
### Architecture of Extended Autoencoder  
In this section, we describe the architecture of the extended autoencoder and the training procedure. The extended autoencoder consists of an autoencoder and a discriminator. Reconstruction loss and adversarial training loss are described in Equations (3) and (4). \[L_{mse}(x,g,f)=\|x-f(g(x))\|_{2}^{2} \tag{3}\]  
\[L_{D_{l}}(x,f,D_{l})=E_{x-p_{x}}[logD_{l}(g(x))]+E_{x-p_{x}}[log(1-D_{l}((g \circ f)(g(x)))] \tag{4}\]  
The first loss function is the mean squared error, which corresponds to the distance between the input and its reconstruction. We jointly optimize the encoder \(g\) and the decoder \(f\) to minimize the \(L_{mse}\) loss function. The second loss function is adversarial training loss to match the latent variables of the reconstructed input to the latent variables of the original input. The discriminator \(D_{l}\) tries to distinguish whether a sample arises from the latent variables of the input or from the latent variables of the reconstructed input. The decoder is trained to fool the discriminator \(D_{l}\), and the encoder and the decoder can be obtained by optimizing the following full objective:  
\[g,f=\underset{g,f}{\text{argmin}}\underset{D_{l}}{\max}(L_{mse}(x,g,f)+L_{D_{ l}}(x,f,D_{l}))\]  
The extended autoencoder can be combined with all variants of the autoencoder; the associated loss functions depend on the variant employed. For example, the extended autoencoder can combine with the VAE to create an extended VAE. Figure 5 depicts the architecture of the extended VAE, which has three loss functions:  
\[L_{mse}(x,g,f)=\|x-f(g(x))\|_{2}^{2}\]  
\[L_{KL}(x,g_{\mu},g_{\sigma})=E_{x-p_{x}}\Big{[}\frac{1}{2}(g_{\mu}(x)^{2}+e^{ g_{\sigma}(x)}-g_{\sigma}(x)-1)\Big{]}\]  
Figure 4: Architecture of the extended autoencoder, which is based on the autoencoder architecture with the addition of the discriminator \(D_{l}\). The dotted encoder is not updated during training. \[L_{D_{l}}(x,f,D_{l})=E_{x-p_{x}}[logD_{l}(g(x))]+E_{x-p_{x}}[log(1-D_{l}((g\circ f )(g(x)))]\]  
\[\text{where }g(x)=g_{\mu}(x)+e^{\frac{1}{2}g_{\sigma}(x)}.z,\ z\ \sim\ N(0,1)\]  
In this case, the second loss function is the Kullback-Leiber divergence loss, which is used to confer continuity in the latent space. \(L_{KL}\) is the closed form loss in the special case of a Gaussian latent. The VAE encoder consists of two neural networks: \(g_{\mu},g_{\sigma}\). The output of \(g_{\mu}\) is the mean, and the output of \(g_{\sigma}\) is log-variance. Using a reparameterization modification, it is possible for the encoder output \(g(x)\) to model the standard Gaussian distribution and the backpropagation. The remaining parts are the same as the case of the basic extended autoencoder. The final objective function of the extended VAE is as follows:  
\[g,f=\underset{g,f}{\text{argminmax}}(L_{mse}(x,g,f)+L_{KL}\big{(}x,g_{\mu},g_{ \sigma}\big{)}+L_{D_{l}}(x,f,D_{l}))\]  
The extended autoencoder can also combine with the AAE. Figure 6 describes the architecture of the extended AAE, which has three loss functions:  
\[L_{mse}(x,g,f)=\|x-f(g(x))\|_{2}^{2}\]  
\[L_{D_{l}}(x,f,D_{l})=E_{x-p_{x}}[logD_{l}(g(x))]+E_{x-p_{x}}[log(1-D_{l}((g \circ f)(g(x)))] \tag{4}\]0  
\[L_{D_{l}}(x,f,D_{l})=E_{x-p_{x}}[logD_{l}(g(x))]+E_{x-p_{x}}[log(1-D_{l}((g \circ f)(g(x)))] \tag{4}\]1  
Basically, the discriminator \(D_{z}\) tries to distinguish whether a sample arises from the latent variables or from a prior distribution. At the same time, the encoder is updated to fool the discriminator \(D_{z}\). In a similar way, the discriminator \(D_{l}\) tries to distinguish whether a sample arises from the latent variables of input or from the latent variables of the reconstructed input. The decoder is trained to fool the discriminator \(D_{l}\).