ARENA-2016 ARENA-2016

11institutetext:  Vrije Universiteit Brussel (Belgium) 22institutetext:
Radboud University Nijmegen (The Netherlands) 33institutetext: University of
California Irvine (USA) 44institutetext: KVI-CART (The Netherlands)
55institutetext: ASTRON (The Netherlands) 66institutetext: University of
Groningen (The Netherlands) 77institutetext: NIKHEF (The Netherlands)

# Search for Cosmic Particles with the Moon and LOFAR

T. Winchen 11<EMAIL_ADDRESS>A. Bonardi 22 S. Buitink 11 A.
Corstanje 22 J. E. Enriquez 22 H. Falcke 225577 J. R. HÃrandel 22 P. Mitra 11
K. Mulrey 11 A. Nelles 33 J. P. Rachen 22 L. Rossetto 22 P. Schellart 22 O.
Scholten 4∂4∂ S. Thoudam 22 T.N.G. Trinh 44 6 6 S. ter Veen (The LOFAR Cosmic
Ray KSP) 55

###### Abstract

The low flux of the ultra-high energy cosmic rays (UHECR) at the highest
energies provides a challenge to answer the long standing question about their
origin and nature. A significant increase in the number of detected UHECR is
expected to be achieved by employing Earth’s moon as detector, and search for
short radio pulses that are emitted when a particle interacts in the lunar
rock. Observation of these short pulses with current and future radio
telescopes also allows to search for the even lower fluxes of neutrinos with
energies above $10^{22}$ eV, that are predicted in certain Grand-Unifying-
Theories (GUTs), and e.g. models for super-heavy dark matter (SHDM). In this
contribution we present the initial design for such a search with the LOFAR
radio telescope.

##  1 Introduction

The extremely low flux of ultra-high energy cosmic rays (UHECR) makes the
search for their sources a challenging task (e.g. Kotera2011 ). The same
challenge arises in testing specific theories of grand-unification
Bhattacharjee1997  and super heavy dark matter Aloisio2006 ; Aloisio2015  by
their predicted flux of neutrinos with extreme energies beyond
$1\text{\,}\mathrm{Z}\mathrm{eV}$. To test these theories and increase the
available UHECR data at the highest energies, detectors several orders of
magnitude larger than currently available are required. One approach to
achieve this necessary increase of detector area is to use Earth’s Moon as
detector. The Moon is ‘read-out’ by existing radio telescopes that record the
nanosecond radio pulses that are created when a particle interacts in the
lunar rock.

Several searches with radio telescopes operating in the GHz frequency regime
have so far yielded only upper limits on the neutrino flux and have not been
sensitive enough to observe the low UHECR flux Buitink2010 ; Hankins1996 ;
James2011 ; James2007 ; Bray2014 . The sensitivity of these searches was in
particular limited by the frequency range of the used telescopes that operate
in the GHz regime. While at GHz frequencies the expected pulse amplitudes
reach a maximum, the pulses cannot escape the moon for most part of its
surface; only pulses from particles hitting the limb are detectable at Earth.
Conversely, similar searches with the LOw Frequency ARray (LOFAR)
vanHaarlem2013  can use the full visible lunar surface as sensitive area, as
it operates in a frequency range that allows pulses to escape the lunar rock
in more geometries and is optimal for the lunar detection of particles
Scholten2006 .

##  2 Signal Processing at LOFAR

LOFAR consists of several thousand omni-directional antennas located in more
than 48 stations distributed throughout European. Twenty-four of the stations
are grouped into a dense core of approximately $2\text{\,}\mathrm{km}$
diameter in the Netherlands. All stations are equipped with 96 low-band
antennas (LBAs) with a frequency range from $1090\text{\,}\mathrm{MHz}$ and at
least 48 ‘tiles’ of 16 high-band antennas (HBAs) each, with a frequency range
from $110240\text{\,}\mathrm{MHz}$. By beamforming, i.e. coherent stacking the
signals of the individual antennas with a corresponding delay, the sensitivity
of a station is tuned towards a specific direction. A station thus assumes the
role of a single dish in a classical radio telescope.

Figure 1: Overview of the online data analysis processing steps for the
detection of ns-pulses with LOFAR.

As preparation for astronomical observations, the data is preprocessed in
several steps in LOFAR before it can be accessed by the user. An overview of
the default processing steps together with the online-analysis steps required
here is given in figure 1. After digitization, the data is first processed by
a polyphase filter (PPF), which splits the data stream into 256 sub-channels
of $195\text{\,}\mathrm{kHz}$ width. Compared to splitting data with a Fourier
transformation, the PPF allows efficient sampling conversion while preventing
signal-leakage between neighboring channels, thus allowing precise filtering.
However, within the individual sub-channels the time resolution is reduced
from $5\text{\,}\mathrm{ns}$ to only a few micro-seconds due to critical down-
sampling. The filtered data of the individual antennas of one station is then
combined into a ‘station beam’ before it is transmitted to a computing cluster
for further processing.

While the pre-filtering of the signal is convenient for typical astronomical
observations, here we require the full nano-second resolution. The thus
necessary inversion of the PPF is a lossy process, but we can use the thereby
approximately reconstructed signal to trigger storage of the raw voltage
traces which are buffered for 5 seconds. We therefore will first combine the
station beams to several tight-array beams pointing to different spots on the
moon, and then construct nanosecond time traces from the combined signals.
These are then corrected for ionospheric dispersion, and eventually trigger a
read-out of the buffer board.

##  3 Polyphase Synthesis

The polyphase filter analysis is an efficient implementation of an multi
channel Finite-Impulse-Response (FIR) filter with corresponding optimization
of the sampling rate to split the signal $x[n]$ into well defined sub-bands
$\hat{y}_{i}[m]$, with $i=0\cdots M$. The polyphase-filter mathematically
corresponds to the Fourier transform $\mathcal{F}(\mathbf{y})$ of the
transformed signal $\mathbf{y}=H\cdot\mathbf{x}$, with matrix $H$
corresponding to the single-channel filter. For a K-tap filter into M-sub-
channels $H$ is a $K\cdot M\times M$ matrix. The filter principle is sketched
in figure 2.

Figure 2: Sketch of the filter stage of an 2-tap 4-channel PPF before
application of a 4-point FFT to $\mathbf{y}$.

Inversion of the Fourier transformation in the polyphase analysis is possible
without loss of information. The inverse of the filter matrix $H^{-1}$,
however, does not exists as $H$ is not square. While methods exists (e.g.
Unser2000 ) to construct an approximate inverse $G$ so that $G\cdot H=1$, the
one-step inversion produces echos of short pulses as e.g. in Singh2012 . This
limits their usability here, as we thus expect a large number of false
triggers generated from RFI by this method. We here therefore investigate a
more robust but computationally more complex iterative method to solve the
linear-systems of equations $H\cdot\mathbf{x}=\mathbf{y}$.

As the system is under-determined, a side condition is required to constrain
the solutions. The reason for the ill-determination of the inverse is here,
that any $x[n]$ influences $y_{i}[m]$ for $K$ values of $m$. Thus, for a
filtered signal of finite length, $K\times M$ samples at the beginning,
respectively end, of the reconstructed input signal are not completely
determined. Searching for the minimum solution $\|\min{x}\|$ can thus be used
as side condition here, as this minimizes the signal in the under-determined
areas at cost of a slight damping of the reconstructed signal.

An algorithm using this side condition to solve under-determined linear
systems is the LSMR algorithm Fong2011 . Compared to alternatives as e.g. the
LSQR algorithm , the LSMR algorithm has also the advantage of practically
monotonically conversion that allows for safe early termination, which might
be advantageous for a real-time application.

Figure 3: Accuracy of PPF-Synthesis. (a) Input Signal, (b) reconstructed
signal, (c) difference between input and reconstructed signal, (d) zoom into
(c). The position of input pulses are marked by vertical gray lines.

In figure 3 an example of the reconstruction of a trace with a prototype
implementation of this method is given. The input signal shown in fig. 3 (a)
is composed of white noise and delta peaks of fixed height at random time
sets. After applying a polyphase filter and its inverse to the trace, the
signal is recovered almost perfectly, except for the edges of the trace as
shown in fig. 3 (b). The differences between the input signal and recovered
signal is exactly zero except for small time windows at intervals
corresponding to the number of sub-channels as shown in fig. 3(c, d). As these
windows are known, a necessary increase of the trigger sensitivity is limited
to a small time intervall. In particular, the uncertainty in the
reconstruction is not related to the position of the input pulses and thus not
critical for the trigger. The maximum amplitude found here corresponds to less
than 30% of the root-mean squared of the white noise.

##  4 Conclusion

The LOFAR radio telescope is a promising instrument for the search of cosmic-
particle induced radio-pulses from Earth’s moon. Such a search requires
triggering on preprocessed data and in particular the reconstruction of nano
second resolution time traces via a polyphase synthesis. The iterative
approach to polyphase synthesis investigated here is suitable for triggering
the data, as it does not introduce artefacts that are likely to increase the
trigger rate.

## References

  * (1) K. Kotera, A.V. Olinto, Annual Review of Astronomy and Astrophysics 49, 119 (2011)
  * (2) P. Bhattacharjee, AIP Conf. Proc. 433, 168 (1998)
  * (3) R. Aloisio, V. Berezinsky, M. Kachelriess, Phys. Rev. D74, 023516 (2006)
  * (4) R. Aloisio, S. Matarrese, A.V. Olinto, JCAP 1508, 024 (2015)
  * (5) S. Buitink et al., Astron. Astrophys. 521, A47 (2010)
  * (6) T.H. Hankins, R.D. Ekers, J.D. O’Sullivan, MNRAS 283, 1027 (1996)
  * (7) C.W. James et al., MNRAS 410, 885 (2011)
  * (8) C.W. James et al., MNRAS 379, 1037 (2007)
  * (9) J.D. Bray et al., Astropart. Phys. 65, 22 (2014)
  * (10) M.P. van Haarlem et al., Astronomy and Astrophysics 556, A2 (2013)
  * (11) O. Scholten et al., Astroparticle Physics 26, 219 (2006)
  * (12) M. Unser, Proceedings of the IEEE 88, 569 (2000)
  * (13) K. Singh et al., Nucl. Instrum. Meth. A664, 171 (2012)
  * (14) D. Fong, M. Saunders, SIAM Journal on Scientific Computing 33, 2950 (2011)


# Adaptive Compression of the Latent Space in Variational Autoencoders

Gabriela Sejnova Michal Vavrecka Karla Stepanova
Czech Institute of Informatics, Robotics and Cybernetics
Czech Technical University in Prague, Prague, Czech Republic
<EMAIL_ADDRESS>

###### Abstract

Variational Autoencoders (VAEs) are powerful generative models that have been
widely used in various fields, including image and text generation. However,
one of the known challenges in using VAEs is the model’s sensitivity to its
hyperparameters, such as the latent space size. This paper presents a simple
extension of VAEs for automatically determining the optimal latent space size
during the training process by gradually decreasing the latent size through
neuron removal and observing the model performance. The proposed method is
compared to traditional hyperparameter grid search and is shown to be
significantly faster while still achieving the best optimal dimensionality on
four image datasets. Furthermore, we show that the final performance of our
method is comparable to training on the optimal latent size from scratch, and
might thus serve as a convenient substitute.

##  1 Introduction

Variational Autoencoders (VAEs) (Kingma & Welling, 2014) are generative models
that learn a probabilistic mapping between the data and the latent space,
which allows for the generation of new, unseen data samples that are similar
to the training data. Although VAEs have proven to be highly effective in this
task, they are known for their sensitivity toward individual hyperparameters
(Hu & Greene, 2018) and particularly the size (dimensionality) of the latent
space. The selection of optimal latent dimensionality in VAEs is a crucial
step in achieving the desired trade-off between reconstruction quality and
clustering capability. Several works have studied and tried to mitigate this
problem (Higgins et al., 2016), (Kim & Mnih, 2018) (Shao et al., 2020),
however, in most current models, the optimal latent dimensionality is still
chosen based on the specific task and data at hand.

Traditionally, determining the optimal latent space size has been done through
a process of trial and error or by manually tuning the hyperparameters through
grid search and observing the model’s performance. However, this process can
be time-consuming (as there are several hyperparameters to tune in parallel)
and unreliable, making VAEs less practical for real-world applications (Way et
al., 2020),(Ahmed & Longo, 2022).

Figure 1: Overview of our training procedure with shrinking latent space size.
After each epoch, we encode a batch of 500 random samples from the validation
dataset and run K-means clustering. We then calculate the Silhouette score
based on the detected clusters. The Decision rule observes the approximate
slope of the Silhouette score, FID and the reconstruction loss over epochs. We
prune 1-5 neurons after each 5 epochs from the encoder’s $\bm{\mu}$ and
$\bm{\sigma}$ layers and the decoder’s first layer until the slopes meet our
criteria (see Section 3.3.1), then the latent space size remains fixed.

In this paper, we propose ALD-VAE (Adaptive Latent Dimensionality) - a novel,
automated approach for determining the optimal latent space size in VAEs. Our
approach involves gradually decreasing the latent space size by removing
neurons during the training process and observing the reconstruction loss,
Fréchet Inception Distance (FID) and cluster quality of the latent samples
using the Silhouette score.

We show that the ALD-VAE approximates the optimal number of dimensions and
thus avoids the need for manual tuning, plus reaches the same accuracy as if
it was trained on the optimal latent dimensionality from scratch. We
demonstrate the effectiveness of our approach on four image datasets ranging
from toy-level (SPRITES, MNIST) to real-world noisy data (EuroSAT). With our
proposed method, VAEs can be made more practical for real-world applications
and the need for manual tuning can be eliminated. The corresponding code and
more detailed results can be found in the GitHub repository
111https://github.com/gabinsane/ald-vae.

##  2 Related work

Hyperparameter tuning in VAEs has become a common subject of research in
recent years as it is usually a necessary step for obtaining optimal results
on the chosen dataset (Locatello et al., 2019), but the tuning process can
also influence the results of comparison between various VAE architectures (Hu
& Greene, 2018).

Several studies provided a comparison of VAE performance under various latent
space dimensionalities explored through grid search. For example, Ahmed &
Longo (2022) examine the optimal latent space size for electroencephalographic
(EEG) signals by comparing 25 different latent sizes, Way et al. (2020)
compare different latent space dimensionalities for gene expression data.
Considering the high computational costs of training multiple models with
different hyperparameters for comparison, other works focused on
hyperparameter tuning on the fly during a single training.

Mondal et al. (2020) propose a method to automatically estimate the optimal
latent dimensionality in adversarial auto-encoders by introducing an
additional trainable mask layer that learns to hide the least informative
dimensions. However, this method requires adopting a separate optimizer for
the mask layer and relies on a discriminator network which is not present in
VAEs. A different approach is the two-stage VAE (Dai & Wipf, 2019b), where
first a model with larger than optimal latent dimensionality is trained and
then another VAE is finetuned during the second stage to learn the correct
probability measure. Although close to our work, this approach does not focus
on the clustering capability in VAEs.

In this paper, we focus on an adaptive decrease of the latent space size in
VAEs during a single training which would be applicable to any VAE or its
adaptation without requiring extra computational costs. We gradually find the
optimal dimensionality based on the observation of the model’s reconstructing
and clustering capabilities in order to maximize the VAE’s performance.

##  3 Adaptively compressing latent space

In this section, we first formulate the problem of latent space compression
and the task we are focusing on. Next, we describe the general VAE
architecture. Finally, we define our proposed ALD-VAE training algorithm for
automatic estimation of the optimal latent dimensionality.

###  3.1 Problem formulation

Let $\mathcal{D}$ be a dataset consisting of datapoints $\mathbf{x}$ lying in
an $n$-dimensional manifold $\mathcal{X}$ embedded in $\mathbb{R}^{d}$.
Variational autoencoders (Kingma & Welling, 2014) then establish a
probabilistic relationship between $\mathcal{X}$ and a learned low-dimensional
latent space $\mathcal{Z}$. It is our goal to find the optimal latent
dimensionality $n_{z}$ within $\mathcal{Z}$ that matches the intrinsic
dimensionality $n$ of the data. In real-world datasets, the intrinsic
dimensionality $n$ has been shown to be much smaller than the dimensionality
of $\mathbb{R}^{d}$, i.e. $n<<d$ (Narayanan & Mitter, 2010), and using
$n_{z}>>n$ might thus lead to redundant dimensions and noisy outputs. One of
the metrics that can show the qualitative performance of the model is Fréchet
Inception Distance (FID) (Heusel et al., 2017) and, as demonstrated by Dai &
Wipf (2019a), too low as well as too high $n_{z}$ is non-optimal (see the
typical u-shape of FID for different latent dimensionality e.g., in Fig.2).

One way to find the optimal $n_{z}$ would be thus by finding the minimum of
FID. However, in many cases, it is also desirable to cluster the data in the
latent space based on the mutual similarity between the data, e.g., for image
datasets with multiple classes. Quality of the clusters in the latent space
can be evaluated using Silhouette score $S=\frac{1}{x}*\sum_{i=1}^{x}s(i)$
calculated over $x$ samples encoded by the encoder network. For each sample
$i$, $s_{i}$ can be computed as:

$s_{i}=\frac{b_{i}-a_{i}}{max(b_{i},a_{i})}$ (1)

where $b_{i}$ is the inter-cluster distance defined as the average distance to
the closest cluster $C$ of datapoint $i$ (except for the one it is part of):

$b_{i}=\min_{k\neq i}\frac{1}{|C_{k}|}\sum_{j\in C_{k}}{d(i,j)}$

and $a_{i}$ is the intra-cluster distance defined as the average distance to
all other points within the same cluster $C$:

$a_{i}=\frac{1}{|C_{i}|-1}\sum_{j\in C_{i},i\neq j}{d(i,j)}$

As shown in Fig. 2, the Silhouette score also changes with $n_{z}$, but can be
optimal in different dimensionality than FID.

Our objective is thus to find such latent dimensionality $n_{z}$, that
minimizes the FID score and reconstruction loss $\mathcal{L}_{r}$ (which is a
part of the VAE objective loss function, see Sec. 3.2), while maximizing the
Silhouette score $S$:

$n_{z}=\operatorname*{arg\,min}_{n_{z}\in\mathbb{Z}}FID(n_{z})\land\operatorname*{arg\,min}_{n_{z}\in\mathbb{Z}}\mathcal{L}_{r}(n_{z})\land\operatorname*{arg\,max}_{n_{z}\in\mathbb{Z}}S(n_{z}),$
(2)

###  3.2 VAE objective

As proposed by Kingma & Welling (2014), VAEs are trained end-to-end in an
unsupervised manner: first, the encoder maps the input data into a
distribution over the latent space, typically by using multiple layers of
neural networks. Second, samples are drawn from the distribution and the
decoder maps them back to the original data space. The objective of the VAE is
to maximize the evidence lower bound (ELBO), which is equivalent to maximizing
the likelihood of the data and regularizing the approximate posterior to be
close to the prior. The ELBO loss term is denoted as follows:

$\mathcal{L}_{ELBO}=\mathbb{E}_{q(z|x)}\log p(x|z)-KL(q(z|x)||p(z))$ (3)

where $\mathbb{E}_{q(z|x)}\log p(x|z)$ is the expected log-likelihood of the
data under the approximated posterior distribution, $KL(q(z|x)||p(z))$ is the
Kullback-Leibler divergence between the approximated posterior distribution
and the prior distribution over the latent variables.

The size of the latent space is equal to the dimensionality of the
multivariate distribution learned by the encoder. The parameters of the
distribution (a vector of means, $\bm{\mu}$, and a vector of variances,
$\bm{\sigma}$) are usually provided by the two fully connected output layers
of the encoder network, each of them having the dimensionality $n_{z}$. Since
the output size of the layer is conventionally decided at model
initialization, the latent space remains fixed during training.

Figure 2: The values of the four observed metrics (FID for reconstructions,
FID for generations, Silhouette score and Reconstruction loss) during training
when the latent space is gradually reduced until Dim=2. The upper axis shows
the dimensionality at the given epoch, the vertical red line shows when would
our ALD algorithm stop the compression. We show the results for four datasets.

###  3.3 Proposed Algorithm

We strive to minimize the latent space dimensionality $n_{z}$ during the VAE
training procedure to a value that is optimal in terms of the model’s
reconstruction accuracy and latent space regularization/clustering capability,
i.e. the Silhouette score (see Eq. 2). In this section, we first describe the
process of downsizing $n_{z}$ during training without the need of retraining
from scratch, then we summarize the stopping mechanisms used to determine
whether the adapted dimensionality is an optimal solution or not. For a high-
level overview of the whole procedure, see Figure 1 or the pseudocode in
Algorithm 1.

####  3.3.1 Latent dimensionality reduction

As mentioned in Section 3.2, the latent space size $n_{z}$ is given by the
output feature size of the two fully-connected layers $\bm{\mu}$ (learning the
means of the multivariate distribution) and $\bm{\sigma}$ (learning the
variances) which process the encoder’s output.

The latent dimensionality reduction works as follows (see also Algorithm 1):

  1. 1.

Start by choosing a random, high-enough initial dimensionality $n_{z}$.

  2. 2.

Train the VAE model for $p$ epochs.

  3. 3.

Check the stopping criterion (see Eq. 4). If stopping criterion is not met,
proceed to 4., otherwise fix the current $n_{z}$ and continue training.

  4. 4.

Remove $n$ neurons from each of the two last encoder layers of the VAE model
and proceed to 2.

In each reduction process (step 4), we replace the last two fully-connected
layers of the trained VAE encoder with new ones, which have the original
output feature dimension reduced by $n$. We then copy the learned weights and
biases from the old layer into the newly initialized layer and omit the $n$
values which are now redundant. We choose the weights and biases randomly,
although a possible additional metric might be used for selection such as the
dimension-specific KL divergence. As the latent vectors $z$ have a reduced
dimensionality after downsizing the encoder layers, we perform the same
operation on the first decoder network layer.

We perform the latent space compression reactively during the training. In the
beginning, we remove $n=5$ neurons (corresponds to the parameter
latent_decrease in Alg. 1) after each $p=5$ epochs (corresponds to the
parameter patience p in Alg. 1), until the slope of the Silhouette score over
the last 10 epochs becomes positive (see Eq. 5 for the slope calculation).
Afterwards, we slow down the pruning by reducing $n$ to 1 and prune only 1
neuron after each $p=5$ epochs, so that we can slowly approach the optimal
value. This is done iteratively until the stopping mechanism decides that the
current dimensionality is optimal and $n_{z}$ remains fixed. Please note that
although we choose the initial $n$ and $p$ empirically, we also evaluated the
algorithm with different initial values and the final optimal dimensionality
$n_{z}$ was similar, showing the robustness of the compression mechanism.

####  3.3.2 Stopping mechanism

As demonstrated in Fig. 2, the Silhouette score, as well as FID and the
reconstruction loss react to the latent space size throughout the training. We
seek such latent space dimensionality $n_{z}$, under which we get the maximal
Silhouette score ($S$) and minimal reconstruction loss
(${-\mathbb{E}_{q(z|x)}\log p(x|z)}$) as well as FID ($FID$). Since the
Silhouette score has an optimum in different $n_{z}$ than reconstruction loss
and FID, an optimal balance between them has to be found.

Algorithm 1 Adaptively compressing latent space

1: Input: data $x_{train}$, $x_{val}$, number of data classes $k\\_classes$

2: Parameters: init latent dims $n_{z}$, $latent\\_decrease$, patience $p$,
floating window $w$

3: s_scores, recon_losses, fid_g, fid_r $\leftarrow$ empty list

4: $compressing\leftarrow 1$

5: for $epoch$ in $num\\_epochs$ do

6: train_loop($x_{train}$)

7: val_loop($x_{val}$)

8: $labels$ = KMeans.fit($z\\_samples$, k=$k\\_classes$)

9: $s\\_score$ = silhouette_score($z\\_samples$, labels=$labels$)

10: $recon\\_loss$ = calculate_nll(model, data=$x_{val}$)

11: $FID\\_g$, $FID\\_r$ = calculate_fid(model, data=$x_{val}$)

12: append $s\\_score$ to s_scores

13: append $recon\\_loss$ to recon_losses

14: append $FID\\_g$ to fid_g, $FID\\_r$ to fid_r

15: if $epoch\mod p=0$ then

16: e1 = polyfit_slope(recon_losses[-$w$:], deg=1)

17: e2 = polyfit_slope(s_scores[-$w$:], deg=1)

18: e3 = polyfit_slope(fid_r[-$w$:], deg=1)

19: e4 = polyfit_slope(fid_g[-$w$:], deg=1)

20: if e1 $>$ 0 and e2 $>$ 0 and e3 $>$ 0 and e4 $>$ 0 then

21: // stop with optimal latent dimensionality n_z

22: $compressing\leftarrow 0$

23: else if e2 $>$0 and $latent\\_decrease$ ¿ 1 then

24: $latent\\_decrease\leftarrow 1$

25: end if

26: end if

27: if $compressing=1$ then

28: $n_{z}$ = $n_{z}$ \- $latent\\_decrease$

29: model.update_latent_dim(new_dim=$n_{z}$)

30: end if

31: end for

As stated in the previous subsection, we apply the decision mechanism for
possible neuron pruning after each $p=5$ training epochs. At each evaluation
epoch $e$, we calculate the slope of the Silhouette score and reconstruction
losses using a sliding window of 20 epochs. For each window, we calculate the
first-degree polynomial using the least squares polynomial fit to obtain the
slope of the curve. We use the slopes of the Silhouette score, reconstruction
loss, FID for reconstructed images and FID for generated images to determine
the stopping epoch $e$ after which we no longer reduce the latent
dimensionality. The stopping epoch $e_{s}$ is thus defined as follows:

$e_{s}=\min\\{e|(S^{\prime}(e)>0)\wedge(R^{\prime}(e)>0)\wedge(FID_{r}^{\prime}(e)>0)\wedge(FID_{g}^{\prime}(e)>0)\\}$
(4)

where $(S^{\prime},R^{\prime},FID_{r}^{\prime},FID_{g}^{\prime})$ are the
first-degree polynomial scores $p^{\prime}$ (for the given metric curve)
obtained through minimization of the squared error $E$:

$\begin{array}[]{l}E=\sum_{j=0}^{k}{|p(x_{j})-y_{j}|^{2}}\\\
p^{\prime}=\operatorname*{arg\,min}_{p}\sum_{j=0}^{k}{|p(x_{j})-y_{j}|^{2}}\end{array}$
(5)

where $k$ is the size of the sliding window (we choose $k$=20), $x$ is the
x-coordinate of each sample point, $y$ is the y-value of the given sample
point for the given metric (Silhouette score, Reconstruction loss, and FID for
reconstructed or generated images). In other words, we seek the first epoch
for which the slopes of the observed metrics calculated over the last k=20
epochs are all positive. This means that FID and Reconstruction loss have
already reached the optimal peak value and are starting to increase (i.e.,
deteriorate), while the Silhouette score is improving. For a different
overview of our method, see Algorithm 1.

In the following section, we demonstrate the effectiveness of our proposed
ALD-VAE on four standard vision datasets. We show that our adaptive algorithm
can save a significant amount of computational time as it achieves similar
results as if we would use extensive grid search to find the optimal latent
space dimensionality.

##  4 Experiments

To show the robustness and efficiency of our ALD-VAE approach, we compare its
performance to grid search for 4 image datasets of different complexity
(SPRITES, MNIST, FashionMNIST, EuroSAT, see Section 4.1) with known labels
that enable evaluation of the clustering capability. For each dataset, we
evaluate the model’s performance on generative and reconstructive FID,
Reconstruction loss and Silhouette score. We train the following models:

  1. 1.

A new VAE model for different (fixed) latent dimensionalities (grid search).

  2. 2.

The proposed ALD-VAE model with gradually compressing latent space.

We train all experiments on 5 different seeds with the same VAE architecture.
The number of epochs as well as the used encoder and decoder networks are
mentioned for each dataset. All encoder networks also include the two
additional fully-connected layers to produce the $\bm{\mu}$ and $\bm{\sigma}$
vectors.

###  4.1 Datasets description and VAE models specification

The SPRITES dataset (Li & Mandt, 2018) contains colour images of animated
characters (sprites). Each input is a sequence of 8 images showing random game
characters performing one of 9 actions. For the encoder and decoder networks,
we used a 3D convolutional layer adapted from VideoGPT (Yan et al., 2021). We
train all models for 300 epochs, the fixed models with latent sizes 12, 16, 24
and 32 and ALD-VAE with initial latent sizes $n_{z}=100,80,64$.

MNIST (Deng, 2012) is a widely used dataset comprising grayscale images with
written digits. For the encoder and decoder networks, we use 2 fully-connected
layers with 400 hidden dimensions. We train for 200 epochs with latent sizes
4, 8, 12, 16, 24 and 32 for the fixed dimensionality baseline scenarios and
initial latent sizes $n_{z}=100,80,64$ for our adaptive ALD-VAE scenario.

FashionMNIST (Xiao et al., 2017) contains 28x28 grayscale images of fashion
articles from 10 different categories. For this dataset, we use the same
encoder and decoder networks as for the MNIST dataset, i.e. 2 fully-connected
layers with 400 hidden dimensions. We trained the models for 200 epochs and we
compared the latent sizes 8, 16, 24, 32 and 64 in the fixed dimensionality
experiments and the initial latent sizes $n_{z}=100,80,64$ for ALD-VAE.

Figure 3: Comparison of the four metrics when using a fixed latent
dimensionality for the whole training (blue curves) or our ALD-VAE latent
space compression that converged to the same final dimensionality (orange
curves). We show FID for reconstructed and generated images (lower is better),
Silhouette score (higher is better) and Reconstruction loss (lower is better).
Shown on four different datasets.

EuroSAT (Helber et al., 2019) contains 64x64 RGB satellite images showing
pieces of land from 10 categories (e.g. forest, river, industrial etc.). For
the encoder and decoder networks, we use 4x 2D convolutional layers with 256
hidden dimensions and SiLU activation functions. Compared to the other
datasets, EuroSAT has a higher level of detail and variability in the images
and is thus more difficult to reconstruct and even cluster. We trained the
models again for 200 epochs and compared the latent sizes 16, 24, 32 and 64 in
the fixed dimensionality experiments. The initial latent sizes for ALD-VAE
were $n_{z}=100,80,64$.

##  5 Results

In this section, we show a comparison of the proposed ALD-VAE adaptive
approach to the standard grid search approach on four standard vision
datasets. We also show the effect of various initialization conditions and
seeds.

###  5.1 Comparison with random search

First, we show in Fig. 2 the quality of the latent space dimensionality
$n_{z}$ as detected online by the proposed ALD-VAE algorithm. To show the
quality of the online detection, we calculated the corresponding scores
(Silhouette score, FID, reconstruction losses) for the model as if it would
decrease $n_{z}$ until $n_{z}=2$, ignoring the stopping criterion, and
visualise the point, where the ALD-VAE actually decided to stop the reduction
of the latent space dimensionality. As can be seen in Fig.2, the ALD-VAE finds
a balance between the four observed metrics. As expected, the algorithm stops
for all the datasets at the minimum (or directly after) of FID and
Reconstruction loss, right after Silhouette score started increasing.

Figure 4: Training our ALD-VAE with different initial conditions. $ALD_{1}$
was trained with initial latent dimensionality $n_{z}=100$, $ALD_{2}$ was
trained with initial $n_{z}=80$ and $ALD_{3}$ was trained with initial
$n_{z}$=64. We show the results for the MNIST (left) and FashionMNIST (right)
datasets. The $Dim$ values in brackets for each ALD-VAE show the final
converged $n_{z}$.

Next, we compared the ALD-VAE algorithm with another model trained with a
fixed dimensionality that is the same as the final optimal $n_{z}$ found by
ALD-VAE. The results for the four datasets are shown in Fig. 3. On average, it
took 50 epochs until the model converged to the optimal dimensionality with
the exception of FashionMNIST, which took 100 epochs to converge. As seen in
the plots, the training curves either converge to very similar values (e.g.
for the EuroSAT dataset), or there is a trade-off between some of the metrics
- e.g., better Silhouette score for ALD-VAE in FashionMNIST, but worse final
reconstruction loss. However, the differences are in all cases marginal and
the overall outcome is almost identical when training with ALD-VAE compared to
the optimal dimensionality fixed from the beginning. There is thus no drawback
to using the significantly faster ALD-VAE compared to hyperparameter grid
search over multiple dimensionalities.

###  5.2 Various initialization conditions and seeds

To see how robust our ALD-VAE is, we trained several models with various
initialization conditions and various seeds. First, to see how the initial
$n_{z}$ influences the final optimal dimensionality, we trained three models
for each dataset. The first model had the initial $n_{z}$ = 100, the second
model had the initial $n_{z}$ = 80 and the third model had the initial
$n_{z}$=64. In Fig. 4, you can see results for MNIST and FashionMNIST. Both
figures show the final optimal $n_{z}$ for each model. As you can see, the
optimal dimensionalities only differ by 1-2 dimensions and the four observed
metrics have very similar values. The results were similar also for the
EuroSAT and SPRITES datasets. Note that we did not have to increase the number
of training epochs for larger $n_{z}$ as the adaptive neuron pruning can speed
up the initial latent compression based on the decreasing Silhouette score
(see Section 3.3.1 for the mechanism description).

We also trained ALD-VAE on 5 different seeds for each dataset. While the
resulting $n_{z}$ was relatively consistent for MNIST and FashionMNIST
(standard deviation sd=3 dimensions for MNIST and sd=2 dimensions for
FashionMNIST), there were more significant differences for the two remaining
datasets (sd=6 for EuroSAT and sd=7 dimensions for SPRITES). However, we also
found that the diverse final dimensionality $n_{z}$ was caused by a different
course of the observed metrics in the particular seeds. It might be thus
possible that, provided we are seeking a balance between the clustering and
reconstructing capabilities, there are different optima in different seeds due
to the noisy nature of VAEs.

##  6 Discussion

In Section 5, we have shown that the latent space compression with ALD-VAE
reaches the same performance as when training a model with fixed
dimensionality from scratch while being significantly faster. We have also
shown that our model is robust towards various initial $n_{z}$. Here we
discuss further findings and possible extensions.

Firstly, we found that the optimal latent dimensionality obtained with ALD-VAE
sometimes differs across different seeds. However, we observe that the
evaluation metrics (Reconstruction loss, FID, Silhouette score) have also
variable shapes during latent space compression. Considering that the optimum
dimensionality was compared with the hyperparameter grid search in each seed,
we attribute the differences across seeds to the stochastic and noisy nature
of VAEs.

Next, the stopping mechanism of ALD-VAE was designed to find a balance between
reconstruction and clustering quality. However, users have the flexibility to
adjust the decision thresholds to favour one quality over the other, providing
a customizable approach.

Finally, the experimental evaluation was performed on image datasets, allowing
for the straightforward calculation of the Fréchet Inception Distance (FID).
However, it is important to note that for datasets of other types, using FID
might not be appropriate or may need to be replaced with an alternative metric
more suitable for the specific data. We leave it for future research to
explore ALD-VAE, e.g., for text data or robotic actions.

##  7 Conclusion

We have introduced a new method for automatically determining the optimal size
of the latent space in Variational Autoencoders during a single training.
Compared to parameter grid search, our approach significantly speeds up the
process of finding the optimal latent space dimensionality.

Our model, called ALD-VAE, gradually reduces the size of the latent space
during the training process and evaluates the quality of the reconstruction
and clustering using the FID score, Reconstruction loss and Silhouette score.
We compare our method to the results obtained using a random hyperparameter
search on four different image datasets and show that our proposed method can
approximate the optimal size of the latent space and eliminate the need for
manual tuning. Furthermore, ALD-VAE reaches comparable accuracies as the
baselines trained with the same latent size from scratch. As ALD-VAE requires
only simple adjustments to the original architecture and is easy to implement,
it might be a convincing alternative to the classical models that require
manual tuning of the latent space dimensionality.

#### Acknowledgments

This work was supported by the Czech Science Foundation (GA ČR) grants no.
21-31000S and 22-04080L., and by CTU Student Grant Agency project
SGS21/184/OHK3/3T/37.

## References

  * Ahmed & Longo (2022) Taufique Ahmed and Luca Longo.  Examining the size of the latent space of convolutional variational autoencoders trained with spectral topographic maps of eeg frequency bands.  _IEEE Access_ , 10:107575–107586, 2022.
  * Dai & Wipf (2019a) Bin Dai and David Wipf.  Diagnosing and enhancing vae models.  _arXiv preprint arXiv:1903.05789_ , 2019a.
  * Dai & Wipf (2019b) Bin Dai and David P. Wipf.  Diagnosing and enhancing VAE models.  In _7th International Conference on Learning Representations, ICLR 2019, New Orleans, LA, USA, May 6-9, 2019_ , 2019b.
  * Deng (2012) Li Deng.  The mnist database of handwritten digit images for machine learning research.  _IEEE Signal Processing Magazine_ , 29(6):141–142, 2012.
  * Helber et al. (2019) Patrick Helber, Benjamin Bischke, Andreas Dengel, and Damian Borth.  Eurosat: A novel dataset and deep learning benchmark for land use and land cover classification.  _IEEE Journal of Selected Topics in Applied Earth Observations and Remote Sensing_ , 2019.
  * Heusel et al. (2017) Martin Heusel, Hubert Ramsauer, Thomas Unterthiner, Bernhard Nessler, and Sepp Hochreiter.  Gans trained by a two time-scale update rule converge to a local nash equilibrium.  _Advances in neural information processing systems_ , 30, 2017.
  * Higgins et al. (2016) Irina Higgins, Loic Matthey, Arka Pal, Christopher Burgess, Xavier Glorot, Matthew Botvinick, Shakir Mohamed, and Alexander Lerchner.  beta-vae: Learning basic visual concepts with a constrained variational framework.  2016\.
  * Hu & Greene (2018) Qiwen Hu and Casey S Greene.  Parameter tuning is a key part of dimensionality reduction via deep variational autoencoders for single cell rna transcriptomics.  In _BIOCOMPUTING 2019: Proceedings of the Pacific Symposium_ , pp. 362–373. World Scientific, 2018.
  * Kim & Mnih (2018) Hyunjik Kim and Andriy Mnih.  Disentangling by factorising.  In _International Conference on Machine Learning_ , pp. 2649–2658. PMLR, 2018.
  * Kingma & Welling (2014) Diederik P. Kingma and Max Welling.  Auto-Encoding Variational Bayes.  In _2nd International Conference on Learning Representations (ICLR)_ , 2014.
  * Li & Mandt (2018) Yingzhen Li and Stephan Mandt.  Disentangled sequential autoencoder.  In _International Conference on Machine Learning_ , 2018.
  * Locatello et al. (2019) Francesco Locatello, Stefan Bauer, Mario Lucic, Gunnar Raetsch, Sylvain Gelly, Bernhard Schölkopf, and Olivier Bachem.  Challenging common assumptions in the unsupervised learning of disentangled representations.  In _international conference on machine learning_ , pp. 4114–4124. PMLR, 2019.
  * Mondal et al. (2020) Arnab Mondal, Sankalan Pal Chowdhury, Aravind Jayendran, Himanshu Asnani, Parag Singla, and Prathosh A P.  Maskaae: Latent space optimization for adversarial auto-encoders.  In _Proceedings of the 36th Conference on Uncertainty in Artificial Intelligence (UAI)_ , volume 124, pp. 689–698. PMLR, 2020.
  * Narayanan & Mitter (2010) Hariharan Narayanan and Sanjoy Mitter.  Sample complexity of testing the manifold hypothesis.  _Advances in neural information processing systems_ , 23, 2010.
  * Shao et al. (2020) Huajie Shao, Shuochao Yao, Dachun Sun, Aston Zhang, Shengzhong Liu, Dongxin Liu, Jun Wang, and Tarek Abdelzaher.  Controlvae: Controllable variational autoencoder.  In _International Conference on Machine Learning_ , pp. 8655–8664. PMLR, 2020.
  * Way et al. (2020) Gregory P Way, Michael Zietz, Vincent Rubinetti, Daniel S Himmelstein, and Casey S Greene.  Compressing gene expression data using multiple latent space dimensionalities learns complementary biological representations.  _Genome biology_ , 21(1):1–27, 2020.
  * Xiao et al. (2017) Han Xiao, Kashif Rasul, and Roland Vollgraf.  Fashion-mnist: a novel image dataset for benchmarking machine learning algorithms, 2017.
  * Yan et al. (2021) Wilson Yan, Yunzhi Zhang, Pieter Abbeel, and Aravind Srinivas.  Videogpt: Video generation using vq-vae and transformers.  _arXiv preprint arXiv:2104.10157_ , 2021.