{'fraction_non_alphanumeric': 0.02635590636539178, 'fraction_numerical': 0.010671093194213896, 'mean_word_length': 4.790506080816006, 'pattern_counts': {'":': 0, '<': 2, '<?xml version=': 0, '>': 1, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
BinPlay: A Binary Latent Autoencoder for Generative Replay Continual Learning

We introduce a binary latent space autoencoder architecture to rehearse training samples for the continual learning of neural networks. The ability to extend the knowledge of a model with new data without forgetting previously learned samples is a fundamental requirement in continual learning. Existing solutions address it by either replaying past data from memory, which is unsustainable with growing training data, or by reconstructing past samples with generative models that are trained to generalize beyond training data and, hence, miss important details of individual samples. In this paper, we take the best of both worlds and introduce a novel generative rehearsal approach called BinPlay. Its main objective is to find a quality-preserving encoding of past samples into precomputed binary codes living in the autoencoder's binary latent space. Since we parametrize the formula for precomputing the codes only on the chronological indices of the training samples, the autoencoder is able to compute the binary embeddings of rehearsed samples on the fly without the need to keep them in memory. Evaluation on three benchmark datasets shows up to a twofold accuracy improvement of BinPlay versus competing generative replay methods.


Introduction
While mammal brains are capable of acquiring new knowledge without forgetting the skills learned in the past [7], artificial neural networks fail to copy this behavior. The resulting catastrophic forgetting phenomenon [7] manifests itself in the deteriorating performance of a model on a previously learned task upon learning a new one. This is due to the fact that in the training phase, a neural network assumes a stationary data distribution that does not change in time. Hence, re-training the network with new input data adjusts its Figure 1: Overview of our BinPlay architecture for continual learning. When a new batch of data arrives, we train the encoder to map new images to a set of precomputed binary codes in the latent space and then we train the decoder to reconstruct them back to their originals. Since we compute the binary codes using only the indices of training data samples, we know which part of the latent space is populated with the codes of previously seen data. We can also re-compute those codes on the fly, using the indices, and sample them uniformly to rehearse past samples. This way, our BinPlay model can encode information about the past data in the binary latent space of the autoencoder, without the need to store individual images or their codes in memory.
weights to improve the performance on the current objective, disregarding previously seen samples and the associated knowledge [15]. The assumption of stationary data distribution often fails in practice, as numerous applications, including experimental physics [32], autonomous driving [41], robotics [13], and recommendation engines [10], observe ever-growing datasets with changing data characteristics.
A domain of machine learning that attempts to address these requirements is referred to as continual learning. Methods of continual learning proposed in the literature include regularization of neural network weights [44,15], adjustments in the structure of a network to the next batches of data 1 [40,42,30,8,6,36], or replaying examples from previously seen batches while training the network with new data [28,21], also known as rehearsal training. As storing past data requires a growing buffer, recent methods for replaying past samples leverage generative neural architectures [9,14] to reconstruct previously seen data points from various probability distributions [33,38,27].
These models, referred to as generative rehearsal methods, theoretically allow infinite continual learning without inflating the buffer of past examples. However, their performance is upper-bounded by the quality of the training data created with generative models. Since the most frequently used generative models, such as Generative Adversarial Networks (GAN) [9] and Variational Autoencoders (VAE) [14], are trained to learn a continuous function that describes the entire dataset distribution, they often yield visually plausible yet lowquality results. For more challenging datasets, generative models often output blurry images that miss highfrequency details [20,1]. When used as samples for the rehearsal procedure, such examples lead to the deteriorated performance of neural network models [20].
In this paper, we address these shortcomings by introducing an autoencoder-based architecture named BinPlay 2 that is able to regenerate the samples seen in the previous batches out of a set of precomputed binary codes. Inspired by generative hashing approaches [4,25,43], we achieve that goal by designing a binary latent space autoencoder that learns the most quality-preserving mapping between current batch images and a set of binary vectors living in the autoencoder latent space. We allocate the pool of binary codes for a current batch using only a chronological ordering of training samples, i.e., the ordinal number at which samples are presented to the model. Since we know the number of data samples seen in previous batches, we can rehearse past images by first sampling binary codes uniformly from the pool of codes already allocated and then decoding them with an autoencoder. This way, we do not need to store any binary embeddings for previous batches in memory. At the same time, we avoid the problem faced by competing generative rehearsal methods, namely the implicit requirement for the generative model to generalize beyond a set of training samples. Fig. 1 illustrates the overview of our method.
Although our approach can be easily generalized to other computer vision applications, in this work, we focus on the image classification benchmarks. We evaluate our BinPlay method against competitive approaches on the most challenging class incremental learning scenario. BinPlay consistently outperforms state-of-the-art methods on three benchmark datasets while providing a comparable or lower model memory footprint.
To summarize, the contributions of this work are: • a new approach to continual learning problem called BinPlay that combines advantages of generative and buffer-based rehearsal methods by encoding previously seen samples not in memory but in the binary latent space of an autoencoder, • a novel binary latent space autoencoder architecture which learns a quality-preserving, one-to-one mapping between images and predefined binary codes, • a binary code assignment method based on the ordinal number of training images, which enables the computation of binary codes corresponding to rehearsal samples on the fly, without the need to store them in memory.
The remainder of this paper is organized as follows. Sec. 2 overviews the related work. Sec. 3 introduces our proposed BinPlay approach and in Sec. 4 we present the experiments confirming validity of our method. We conclude this work in Sec. 5.

Related work
Continual or lifelong learning, defined as the ability of a machine learning model to learn new knowledge without forgetting previously learned tasks, attracts increasing attention of the research community [26]. As simple re-training of neural systems on new data leads to catastrophic forgetting [7], there are many alternative approaches in the literature. We can classify them into three categories discussed below.
Methods based on regularization. The idea behind these methods is to train a model on the incoming batches of new data while at the same time imposing regularization, which enforces preservation of the model performance on the previous data. Different methods such as Synaptic Intelligence (SI) [44] and Elastic Weight Consolidation (EWC) [15] propose different regularization functions. However, their goal is to slow down the weights update procedure for weights that are considered most important for the previous batches of data. Recent experiments indicate that methods based on regularization do not prevent forgetting but usually slow it down [17].
Methods based on batch-specific model components. In these methods, different model versions are built for different batches of data, although sharing some common parts. During inference, the sample is first assigned to the proper batch (or their combination), to target the specific part of the network. In Reinforced Continual Learning (RCL) [40], Dynamically Expandable Networks (DEN) [42], and Progressive Neural Networks (PNN) [30] new structural elements are added to the model for each new data batch. Alternatively in [24,23,8,22] authors introduce different methods for selecting different submodels of the main model for consecutive batches. The methods in this category provide high accuracy in a multitask scenario, when test samples are given with a corresponding batch index [34]. Otherwise, the index needs to be estimated and current approaches solve that using heuristics, such as the minimization of classification entropy [11,37].
Methods based on replaying. This group of methods tries to preserve previous data and use it along with new samples when retraining the model. The first group of works including [29,1] employ a memory buffer to store all or possibly most relevant previous data examples. Although this approach directly solves the problem, for proper results, it needs to store at least several examples from each incoming batch. This requirement makes the solution inadequate for the general continual learning problem, in which we would like to retrain the model in the potentially infinite number of batches.
In [31], the authors propose to replace the buffer with a generative model based on the Generative Adversarial Networks (GAN) [9]. However, since any structure used to compress past data may also suffer from catastrophic forgetting, authors propose a self retraining procedure in which they retrain the generative model with both new data batches and regenerated ex-amples from the previous ones. In our BinPlay method we benefit from this approach while retraining our binary latent autoencoder as described in Sec. 3. Similarly to [31], [33] introduces a generative rehearsal model based on another successful generative method: Variational Autoencoder (VAE) [14]. Additionally, they combine the generative model with the base classifier, which reduces the cost of model retraining. VAE was also used in favor of GANs in [27], where authors successfully incorporated this model to the problem of open datasets. An interesting idea that we can place in between of generative and buffer based rehearsal is presented in [3], where authors incorporate VQ-VAE [35] architecture to compress the original data examples into a special representation that requires less memory the original images. In this work we extend this idea by proposing a method that compresses the data examples to the set of specific binary vectors which contrary to VQ-VAE does not have to be stored in the buffer.
Therefore, the method introduced in this paper falls into the category of generative rehearsal. Our approach is conceptually similar to that in [27]. We train an autoencoder with an increasing set of images to reproduce them for the rehearsal procedure.
However, all of the above-mentioned procedures are based on the generative models that are trained to approximate functions that produce images. While doing so with the mixtures of continuous distributions (e.g. gaussian), the resulting images are often averaged across samples and blurred. Continuous distributions come with benefits, allowing not only to reproduce previous examples but also to generate new ones as the interpolation between them [2] This valuable asset however, contributes little to the problem of rehearsal-based incremental learning. Therefore, in this work we propose to drop it in favor of high quality reconstructions of the original data examples.

Our method
The goal of our method is to continually train a base network while rehearsing the data from the past batches to avoid forgetting previously learned knowledge. For the rehearsal, we refer to the binary latent space autoencoder, an auxiliary neural network inspired by the hashing autoencoder approaches [4,25,43]. The objective of this autoencoder is to find an assignment of the input data to a set of predefined binary codes and then to reconstruct the codes back to the original data. We define the binary codes to live sparsely in the latent space to avoid hashing conflicts and simplify the reconstruction while populating only a limited part of a latent space. This allows us to Figure 2: Encoding and decoding BinPlay binary latent space codes. We first create a set of binary codes on the basis of consecutive indices (I). We then train the encoder to find the best mapping between original images and a set of binary codes, and the decoder to reconstruct them back to their originals (II). To generate all previously encoded images, we only need the decoder and the total number of encoded samples (equal to the last sample's index, if counted from 1), so we can recompute the set of binary codes and process them through the decoder (III). rehearse previously seen data by uniformly sampling populated latent space and reconstructing the original data from the sampled binary codes. We show an overview of this mechanism in Fig. 2. The next subsections discuss in more detail the building blocks of our approach, namely the binary latent autoencoder, definition of binary codes, and their assignment to input data.

Binary latent autoencoder
The objective of our autoencoder is to reconstruct a set of input images. Therefore, the encoder needs to learn their mapping to a set of predefined binary codes living in the autoencoder's latent space. The decoder, on the other hand, should be able to reproduce inputs from previous training batches based on the latent binary vectors assigned to them.
In order to train the binary latent autoencoder network to fulfill the above requirements, we follow the procedure introduced in [31] and use both data from a new batch and the sampled outputs from its current version. Thanks to this approach, we learn mappings and reconstructions for the new samples while preserving the knowledge of the previous ones at the same time. First, we create a copy, υ , of current decoder weights, υ, so that decoder with weights υ can serve as a source of previous training pairs for the network.
For the training of our autoencoder, we create a set of training samples by uniformly drawing indices i ∈ {1, . . . , K, . . . , N }, where K is the first index for the current batch, and N is the total number of images observed so far.
• If i is between 1 and K − 1, we recreate an image pair as c(i), d(c(i); υ ) , where c is a binary codes generator function and d is our decoder.
• If i is between K and N , we use a new training pair as c(i), x i , where x i is an original image assigned to the index i.
With combination of such pairs, we train the original weights of our decoder υ and learn image reconstructions from their binary codes. To ensure that the above sampling procedure in the latent space works, we propose an appropriate binary code definition.

Binary codes definition
As we want to rehearse previously seen data, we require the set of binary codes within the latent space to be parametrized by an index. Then, in order to produce a set of uniformly sampled random binary codes that can be used to reconstruct the previously seen data, we only need to sample a set of indices. Last but not least, we require the codes to be composed of a large number of bits that often change between the subsequent indices. This way, codes become easily recognizable inputs for the decoder network.
To fulfill the above requirements, we follow an observation presented below and proven in the supplementary material: Lemma 1. Let p be a prime number, p = 2, and i ∈ {1, . . . , 2 m } be coded as m least significant bits of the integer i · p m ln 2/ ln p .  As a result of this procedure, the binary codes for the data indices are different and sparsely distributed. Moreover, according to Lemma 1, active bits of the codes calculated with the proposed procedure often change between subsequent codes. Hence they fulfill our requirements.
In a typical continual learning scenarios, data samples within a training batch share some similarities, as the batches are constructed with the assumption of stationary data distribution. Therefore, we append a batch index transformed using the procedure above as a prefix of the binary codes defined for a given batch.

Algorithm 1: Designating regularization losses for binary codes assignment in BinPlay.
Input: C = set of binary codes, X = set of images, e = encoder, ν = encoders weights put elements of X in a random order for x i in X:

Binary codes assignment
Once we define the binary codes for a given data batch, we need a procedure to map the original data into this set of codes. For a decoder to rehearse past samples properly, we need the mapping to provide a single binary code in the latent space for each data sample. We, therefore, propose the following greedy binary code assignment algorithm.
For the set of images X, we generate a set of binary codes C, such that |X| = |C| 2 n , where n is a size of the latent space. Then, while training our binary latent autoencoder, we extend the original reconstruction loss with an additional regularization term. To compute this term, we assign a yet unassigned binary code to each sample that lies at the lowest distance to the sample. Then, we calculate the distance between data embedding in the latent space and its associated binary code as a L 2 norm. The final regularization is a sum of the distances computed across all of the images in the batch. Since this procedure is highly affected by the ordering of the samples, in each epoch, we shuffle the input images X. Alg. 1 presents a general overview of the assignment procedure.

Trainining
With the building blocks of BinPlay described above, we can now describe how to train our model. Our approach provides a general framework for various continual learning problems, yet here we focus on the incremental image classification as a representative one. Fig. 3 shows the overview of our method.
On the arrival of a new batch, we first train the binary latent autoencoder. We train its encoder to assign new examples to their binary codes and the decoder to reconstruct them back to the original form. To retain the knowledge derived from the previous data, we use the training procedure as defined in Sec. 3.1. To enable faster convergence, we start training autoencoder without enforcing the binary regularization of the latent space and turn the binary code assignment of Alg. 1 after a couple of warm-up rounds. This allows the autoencoder to cluster similar images in the latent space before mapping them to the allowed binary codes. Once the encoder converges on the mappings between the images and their binary codes, we save the mappings and omit the encoding loss to focus on the decoder. For training simplicity, we use the binary code values of {-1,1}, instead of {0,1}, as this facilitates the decoding procedure.
Our base network aims at minimizing the loss averaged over current data as well as a sample of the previous training batches. On the arrival of a new batch, we train the network with the new samples together with the samples regenerated from the binary codes sampled uniformly from the allocated pool in the autoencoder latent space. The new samples are processed through the autoencoder before using them as an input for training so that the base network does not focus on differences between the real and generated data but rather on the general image features that enable accurate classification. For the regenerated past samples, we train the base network to predict the same output distribution values as its copy -frozen before the training. Consequently, we use soft targets [12], instead of the ground truth labels, to better preserve the knowledge gained in the previous training batches. Tab. 1 shows the results of the ablation study supporting the design choices presented in this section.

Modification Results
Reference configuration 23.8 + processing new samples through autoencoder 54.0 + soft targets 63.3 Table 1: Ablation study for the training procedures of the base network. We report the average classification accuracy after the last batch on the CIFAR-10 dataset. As a reference configuration, we take a base network trained on both original (current batch) and generated (previous batches) data samples. Preprocessing incoming new samples with an autoencoder and using soft targets improves the performance of our final continual learning model.

Experimental study
We evaluate BinPlay on three commonly used benchmarks: MNIST [19], Fashion-MNIST [39], and CIFAR-10 [16]. To simulate the real setting of continual learning, we evaluate our model with the classincremental scenario. This means that in each batch of data, we introduce new classes to the scope of our model. In particular, we follow the split-MNIST split-FashionMNIST and split-CIFAR procedure that di-

MNIST and Fashion-MNIST
For both MNIST datasets, we propose the architecture based on a convolutional neural network. For the encoder and the decoder, we use 3 convolutional/transposed convolutional layers and 2 fully connected layers around the latent space of size 200.
Our classifier is based on the LeNet [18] architecture with 3 convolutional layers followed by two fully connected ones. We use batch normalization and dropout. Detailed implementation information can be found in the released codebase and in the appendix.

CIFAR-10
For the CIFAR-10 dataset, we employ similar network architectures as for MNIST and Fashion-MNIST but with a greater number of filters. Additionally, to simplify the encoding procedure for more complex images, we extend the binary latent size to 1000.

Results
We compare the accuracy of our models' predictions. For that purpose, we run the experiment through all 5 batches of data to report the average accuracy on the whole test-set after the final one. For the results of competing methods, we report the results from the original works. However, for several techniques, authors openly state that their methods do not scale well for more complex datasets such as CIFAR-10 [1,20]. Hence, in case of missing evaluations on any benchmarks, we ran the experiments with the code provided by the authors and report the results averaged across 3 runs. As presented in Tab. 2, those experiments demonstrate that our solution clearly outperforms other generative replay approaches on all benchmark datasets. Additionally, in Fig. 5 we show that our BinPlay is only slightly affected by the number of consecutive batches and the deterioration of the results is rather the effect of the increasing complexity of the problem. This is mainly thanks to the fact that our model can recreate sharp images with high-quality visual features. These are a valuable source of previous examples for the base classifier. To support this claim in Fig. 4, we show that our method is able to recreate rich images even after four batches of not related data examples. This is contrary to the other generative rehearsal procedures such as RtF [33] that produce blurry images already from the start.
We also measure the memory footprint of our generative model. We compare it with the current generative rehearsal methods and the space requirements for the Figure 4: Examples of the reconstructions (BinPlay) and generations (RtF [33]) of the images from the CIFAR-10 dataset. Our method is able to recreate high-quality images even after four following batches of data. On the contrary to the current state-of-theart generative replay method (RtF), images are sharp and allow the classifier to rehearse also high-frequency features from regenerated images. memory-buffer based ones. The results of this comparison are shown in Tab. 3. Our model is much more compact than the ones the other generative rehearsal procedures use. That is thanks to the fact that we have to store only the convolutional decoder of the two parts of the generative model. When compared to the rehearsal models with the buffer, our solution requires more space than experience replay methods. However, contrary to such approaches, space requirement for our BinPlay architecture is constant. Our model does not grow with the number of batches served, contrary to

Future work
The BinPlay approach for generative rehearsal presented in this work relies on a binary latent autoencoder and the construction of binary codes living in the latent space. Although the design choices we describe already lead to a significant performance improvement of BinPlay over the competing methods, we can envision several new research paths that are enabled by our work. The currently used binary encoding parametrization presented in Sec. 3 relies on a simple yet effective method to populate latent space. One potential direction of future research is the incorporation of other parametrization methods, including trainable models with divergence loss functions. Another path of future work can explore other than binary latent space types and the corresponding encodings. Finally, we believe that the work presented here is a stepping stone towards discovering the relation between generative models used in rehearsal and other compression techniques. Are generative models truly used to compress previously learned knowledge or rather to represent the pattern for generating images of the same kind? Our empirical evaluation indicates that it is rather the latter, yet further research can definitely shed more light on this question.

Conclusions
In this work, we proposed a novel approach for continual learning with generative replay. Our BinPlay approach uses binary latent space of autoencoder architecture to embed past data batches as binary codes that can be later reconstructed on the fly from a simple and deterministic parametrization function. Inspired by memory-buffer based models, we used binary latent space to store past images as efficient binary codes, while avoiding typical shortcomings of competing generative models that regenerated similar, but lowerquality samples from the past. The evaluation comparison of our approach against state-of-the-art models clearly shows the superiority of BinPlay in terms of average accuracy on three benchmark datasets at a similar or smaller memory footprint than the competing generative rehearsal methods.

Proof of Lemma 1
We have p m ln 2/ ln p < p m ln 2/ ln p = p (log p 2)m = 2 m , and p m ln 2/ ln p > p m ln 2/ ln p−1 = p (log p 2)m−1 = 2 m /p which proves point 1.
In order to prove point 2, we notice that since p is prime, and p = 2, p raised to any natural power is coprime with 2 m . Suppose there are two different i, j ∈ {1, . . . , 2 m } that have the same code in the form of m least significant bits of (1). That would mean that |i − j| * p m ln 2/ ln p mod 2 m = 0.
That can not be true since p m ln 2/ ln p has no common divisors with 2 m and |i − j| is not divisible by 2 m because |i − j| < 2 m .

MNIST and FashionMNIST
For the encoder, we use three convolutional layers with 32, 64, and 128 filters, respectively, with kernel size 3×3 and 2×2 stride. We compress the resulting feature map to the vector of 2048 values, which we translate to the binary latent space of 200 neurons. For the decoder we map features from the latent space through two fully connected layers with 800 neurons, then it is forwarded through 3 transposed convolutional layers with 32, 128, 192 filters with 3×3 kernel size, 2×2 stride, and the final output layer with 4×4 kernel size with no stride. For all activation functions, we use LeakyRelu. We also employ batch normalization after each convolutional layer.
Our classifier is based on the LeNet architecture with 3 convolutional layers with 32, 64, and 128 filters with 4×4 kernel size. We cast the output of the last convolution into the feature space of 1152 values, which we pass through the fully connected layer of 84 neurons to form the final output. We use an additional batch normalization after each convolutional layer, and dropout to prevent the network from overfitting.

CIFAR-10
For the CIFAR-10 dataset, we employ similar network architectures as for MNIST and Fashion-MNIST but with a greater number of filters and bigger latent size.
For the encoder, we use 3 convolutional layers with 32, 64, and 128 filers of 3×3 kernel size and 2×2 stride. After that, we encode the resulting feature map of 2048 features into the latent space of 1000 neurons for the index encoding and 10 neurons for the batch number encodings. In the decoder, similarly to the previous one, we use two fully connected layers to gradually map 1010 binary values from the latent space through the fully connected layer of 1500 neurons to the feature map of 6144 neurons.

Visualization of generated samples
To emphasize the quality of generated samples we show in higher resolution, the visualization of CIFAR-10 images generation with our BinPlay Binary Latent Autoencdeor after each batch. We visualize the same instances to show noticeable, but slow process of quality degradation.