Memory GAN Reading Notes

Introduction

This paper mainly solves two problems in training unsupervised GANs.

First, since generative adversarial networks use only a continuous latent distribution to represent multiple classes or clusters of data, they usually cannot correctly handle the structurally discontinuous nature between different classes in the latent space. (Caused by the mode collapse problem.) For example, if a GAN embeds buildings and cats in the same continuous latent distribution, the GAN may generate realistic images in the transition region between the two classes.

Second, the discriminator of a generative adversarial network very easily forgets the samples generated in the past, which causes instability during adversarial training.

The authors believe that these two main problems can be solved by a learnable memory network accessible to both the generator and the discriminator. The generator can effectively learn representations of the training samples to understand the underlying clustering distribution of the data, thereby alleviating the structural discontinuity problem. Meanwhile, the discriminator can better memorize the clusters of previously generated samples, which can alleviate the forgetting problem.

This paper proposes an end-to-end generative adversarial network model, Memory GAN, which involves an unsupervised memory network integrated with existing generative adversarial network models.

Von Mises-Fisher (vMF) mixture model. The memory module can effectively alleviate the instability problem. First, to alleviate the structural discontinuity problem, the memory can learn representations of the training samples, helping the generator better understand the class and cluster distributions. Therefore, we can separate the modeling of discrete clusters from the embedding of data attributes on the continuous latent space, which can alleviate the discontinuity problem.

Second, the memory network can alleviate the forgetting problem by learning to memorize the clusters of previously generated sample data, including very rare samples.

Architecture

The overall architecture is divided into the memory discriminative network (DMN) and the memory conditional generative network (MCGN).

Memory Discriminative Network

The memory discriminative network consists of a feedforward network μ\mu and a memory network.

The feedforward network μ\mu is a convolutional neural network, whose input is xRDx \in \mathbb{R}^{D} and whose output is a normalized vector q=μ(x)RMq=\mu(x) \in \mathbb{R}^{M}, where q=1\|q\|=1. This is then used as the input of the memory module, which outputs the discriminative result.

The memory network is formulated as M=(K,v,a,h)\mathcal{M}=(K, v, a, h), where KRN×MK \in \mathbb{R}^{N \times M} is the memory key matrix, NN is the memory size and MM is the dimension. v{0,1}Nv \in\{0,1\}^{N} is the memory value matrix. Conceptually, each key vector stores a representation of a clustering center learned through the vMF mixture model, and its corresponding key value is the real/fake judgment. aRNa \in \mathbb{R}^{N} denotes the age of the object stored in each memory block. And hRNh \in \mathbb{R}^{N} denotes the slot histogram, where each hih_{i} represents the amount of valid data belonging to the i-th memory slot.

life-long memory network: the memory space can grow freely, and there is no need to reset it during training.

k-nearest neighbor indexing for efficient memory lookup: k-nearest neighbor indexing is used for lookup.

least recently used (LRU) scheme for memory update. The LRU scheme is used for memory update.

Innovations:

  • Probabilistic derivation: the data likelihood, as well as the prior and posterior distributions of the categorical distribution over memory indices, can be computed.
  • The memory learns an approximate distribution of queries by maximizing the likelihood with an incremental EM algorithm.
  • The memory is optimized through the GAN loss rather than through a memory loss.
  • The contribution of each sample is decided by tracking the slot histogram.

Discriminator Output

For each input xx, we first need to decide which memory slot to use to compute the discriminative probability.

c{1,2,,N}c \in\{1,2, \ldots, N\} denotes the memory slot index.

A Von Mises-Fisher (vMF) mixture model is used to represent the posterior distribution of the memory index.

p(c=ix)=p(xc=i)p(c=i)j=1Np(xc=j)p(c=j)=exp(κKiTμ(x))p(c=i)j=1Nexp(κKjTμ(x))p(c=j)p(c=i | x)=\frac{p(x | c=i) p(c=i)}{\sum_{j=1}^{N} p(x | c=j) p(c=j)}=\frac{\exp \left(\kappa K_{i}^{T} \mu(x)\right) p(c=i)}{\sum_{j=1}^{N} \exp \left(\kappa K_{j}^{T} \mu(x)\right) p(c=j)}

In p(xc=i)=C(κ)exp(κKiTμ(x))p(x | c=i)=C(\kappa) \exp \left(\kappa K_{i}^{T} \mu(x)\right), κ=1\kappa=1 is a constant attention parameter.

The vMF is effectively equivalent to a properly normalized Gaussian distribution defined on the unit sphere.

The categorical distribution over the memory index, p(c)p(c), is obtained by normalizing the slot histogram, p(c=i)=hi+βj=1N(hj+β)p(c=i)=\frac{h_{i}+\beta}{\sum_{j=1}^{N}\left(h_{j}+\beta\right)}, where β(=108)\beta\left(=10^{-8}\right) is a smoothing constant for numerical stability. By using p(y=1c=i,x)=vip(y=1 | c=i, x)=v_{i}, we marginalize the joint probability p(y=1,cx)p(y=1, c | x) over c to obtain p(y=1x)p(y=1 | x):

p(y=1x)=i=1Np(y=1c=i,x)p(c=ix)=i=1Nvip(c=ix)=Eip(cx)[vi] p(y=1 | x)=\sum_{i=1}^{N} p(y=1 | c=i, x) p(c=i | x)=\sum_{i=1}^{N} v_{i} p(c=i | x)=\mathbb{E}_{i \sim p(c | x)}\left[v_{i}\right]

However, for each sample x, this is not scalable for the whole memory of size N.

The maximum a posteriori probability is adopted to consider the top-k slots S={s1,,sk}S=\left\{s_{1}, \dots, s_{k}\right\}:

S=argmaxc1,,ckp(cx)=argmaxc1,,ckp(xc)p(c)=argmaxc1,,ckexp(κKcTμ(x))(hc+β) S=\underset{c_{1}, \ldots, c_{k}}{\operatorname{argmax}} p(c | x)=\underset{c_{1}, \ldots, c_{k}}{\operatorname{argmax}} p(x | c) p(c)=\underset{c_{1}, \ldots, c_{k}}{\operatorname{argmax}} \exp \left(\kappa K_{c}^{T} \mu(x)\right)\left(h_{c}+\beta\right)

where p(xc)p(x | c) is the vMF likelihood and p(c)p(c) is the prior distribution of the memory index.

Here we omit the normalization of the vMF likelihood and the denominator of the prior, because they are both constants. Once we have obtained S, we can compute

p(yx)iSvip(xc=i)p(c=i)jSp(xc=j)p(c=j) p(y | x) \approx \frac{\sum_{i \in S} v_{i} p(x | c=i) p(c=i)}{\sum_{j \in S} p(x | c=j) p(c=j)}

Memory Update Mechanism

The memory keys and values are updated during training. The update mechanism includes a traditional memory update mechanism and an incremental EM algorithm.

Let the sample be xx with label yy, where real is 1 and fake is 0. For each x, we first find the k-nearest slots SyS_{y}, but using the conditional posterior p(cx,vc=y)p\left(c | x, v_{c}=y\right). This is so that in the subsequent EM algorithm, only the slots belonging to the same class as y are considered.

After that, we update the memory in different ways depending on whether SyS_y contains the correct label.

Without the correct label:

The oldest memory slot is found via na=argmaxi{1,,N}ain_{a}=\operatorname{argmax}_{i \in\{1, \ldots, N\}} a_{i}, and the information of x is copied to the corresponding position: Knaq=μ(x)K_{n_{a}} \leftarrow q=\mu(x), vnayv_{n_{a}} \leftarrow y, ana0a_{n_{a}} \leftarrow 0, hna1Ni=1Nhih_{n_{a}} \leftarrow \frac{1}{N} \sum_{i=1}^{N} h_{i}.

With the correct label:

The memory keys are updated through the following custom incremental EM algorithm run for T iterations, which partly incorporates the information of the new sample.

In the inference step, by applying the keys of the previous moment K^it1\hat{K}_{i}^{t-1} and h^it1\hat{h}_{i}^{t-1} to Equation 1, the posterior distribution γit=p(cix)\gamma_{i}^{t}=p\left(c_{i} | x\right) is computed, where iSyi \in S_{y}. In the maximization step, the following updates are performed:

h^ith^it1+γtγt1,K^itK^it1+γtγt1h^it(qiK^it) \hat{h}_{i}^{t} \leftarrow \hat{h}_{i}^{t-1}+\gamma^{t}-\gamma^{t-1}, \quad \hat{K}_{i}^{t} \leftarrow \hat{K}_{i}^{t-1}+\frac{\gamma^{t}-\gamma^{t-1}}{\hat{h}_{i}^{t}}\left(q_{i}-\hat{K}_{i}^{t}\right)

where t1,,T,γ0=0,K^i1=Ki,h^i1=αhit \in 1, \ldots, T, \gamma^{0}=0, \hat{K}_{i}^{1}=K_{i}, \hat{h}_{i}^{1}=\alpha h_{i}, and α=0.5\alpha=0.5.

After T iterations, the slots of SyS_y are updated through KiK^itK_{i} \leftarrow \hat{K}_{i}^{t} and hih^ith_{i} \leftarrow \hat{h}_{i}^{t}.

The decay rate α\alpha controls the degree to which the contribution of old queries to the slot position in the average direction of the mixture component is exponentially reduced.

α\alpha is crucial for performance, because the old queries used to update the keys no longer fit the current mixture distribution, as the feedforward network itself is also being updated.

Finally, it is worth noting that this memory update mechanism is orthogonal to the adversarial training algorithm, because when the discriminator is updated, the memory update is carried out independently. In addition, adding the memory module does not affect the speed of the model at test time, because the memory is only updated during training.

Memory Conditional Generative Network

The memory conditional generative network is based on the generator of InfoGAN. The difference is that it is conditioned not only on random noise but also on memory information.

In other words, the generator not only samples randomly from the noise distribution, but also samples the memory index ii from P(c=ivc=1)=hivijNhjvjP\left(c=i | v_{c}=1\right)=\frac{h_{i} v_{i}}{\sum_{j}^{N} h_{j} v_{j}}. The formula above represents the frequency of occurrence of the unit i that stores real data. The final input is [Ki,z][K_i, z], where KiK_i is the key vector of memory index i.

The difference from other CGANs is that MCGN does not require extra annotations or an extra encoder. Instead, MCGN can make full use of the memory information learned by the DMN in an unsupervised way. The DMN learns the vMF mixture memory using only each sample in the sequence and its label.

The training procedure of the whole MemoryGAN is:

for number of training iterations do Sample a minibatch of samples from the training set Sample a minibatch from the noise distribution and the memory indices Update the discriminator loss Find S_y for each sample in the minibatch Initialize the key, h and gamma for each slot in S_y for number of EM iterations do Estimate γs\gamma_s for each s Update hsh_s Update KsK_s Update the vMF mixture model, hsyh^syT,KsyK^syTh_{s_{y}} \leftarrow \hat{h}_{s_{y}}^{T}, K_{s_{y}} \leftarrow \hat{K}_{s_{y}}^{T} for sySys_{y} \in S_{y} Sample a minibatch from the noise distribution and the memory indices Update the generator loss

objective function

The goal of MemoryGAN is based on the objective of InfoGAN, which is to maximize the mutual information between the latent variables and the observed content. (For details, see InfoGAN.)

A mutual information loss is added between KiK_i and G(z,Ki)G(z, K_i) to ensure the continuity between the sampled memory information and the structured information of the generated samples:

I(Ki;G(z,Ki))H(Ki)I^logC(κ) I\left(K_{i} ; G\left(z, K_{i}\right)\right) \geq H\left(K_{i}\right)-\hat{I}-\log C(\kappa)

where I^\hat{I} denotes the expectation of the negative cosine similarity, I^=ExG(z,Ki)[κKiTμ(x)]\hat{I}=-E_{x \sim G\left(z, K_{i}\right)}\left[\kappa K_{i}^{T} \mu(x)\right].

To be continued