Recently, I accidentally came across a very meaningful work called “Relativistic GAN,” or RSGAN for short, from the paper “The relativistic discriminator: a key element missing from standard GAN.” It is said that this article even received a like from the founder of GANs, Ian Goodfellow. This paper proposes replacing the original discriminator of the standard GAN with a relativistic discriminator, making the generator’s convergence faster and the training more stable.
Unfortunately, this paper only discusses the results from the perspective of training and experiments, without conducting a deeper analysis. As a result, many people feel that this is just a trick for GAN training. However, in my view, RSGAN has a more profound meaning; it can even be seen as having pioneered a new school of GANs. Therefore, I decided to provide a basic introduction to the RSGAN model and the connotations behind it. It should be noted, however, that except for the identical results, the introduction process in this article has almost no overlap with the original paper.
The “Turing Test” Concept
SGAN
SGAN refers to the Standard GAN. Even for readers who have not conducted research on GANs, I believe they have learned the general principle of GANs through various channels: The “counterfeiter” constantly creates forgeries, trying to fool the “discriminator”; the “discriminator” constantly improves its identification techniques to distinguish between genuine and fake goods. The two compete and progress together until the “discriminator” can no longer distinguish between the real and the fake, at which point the “counterfeiter” has succeeded.
In modeling, this process is achieved through alternating training: fix the generator and train a discriminator (a binary classification model) to output 1 for real samples and 0 for forged samples; then fix the discriminator and train the generator to make the forged samples output 1 as much as possible. This latter step does not involve real samples.
The Problem
However, this modeling process seems to be too demanding on the discriminator because the discriminator operates in isolation: When training the generator, real samples do not participate, so the discriminator must remember all the attributes of the real samples to guide the generator in producing more realistic samples.
In real life, we don’t do it this way. As the saying goes, “there is no harm without comparison, and no progress without harm.” We often distinguish based on the comparison between real and fake products. For example, to identify a counterfeit bill, you might need to compare it with a real one; to identify a knock-off phone, you only need to compare it with the original; and so on. Similarly, if you want to make a fake more realistic, you need to have the real product by your side for constant comparison and improvement, rather than relying solely on the real product in your “memory.”
“Comparison” makes it easier for us to identify real and fake products, thereby allowing us to manufacture better fakes. In the field of Artificial Intelligence, we know the famous “Turing Test,” which refers to a tester communicating with both a robot and a human simultaneously without prior knowledge. If the tester cannot successfully distinguish between the human and the robot, it means the robot has (in some aspect) human intelligence. The “Turing Test” also emphasizes the importance of comparison: if the robot and the human are mixed and cannot be distinguished, then the robot has succeeded.
Next, we will see that RSGAN is based on the idea of the “Turing Test”: If the discriminator cannot identify mixed real and fake images, then the generator has succeeded; and to generate better images, the generator also needs to directly leverage real images.
The Basic Framework of RSGAN
SGAN Analysis
First, let’s review the process of a standard GAN. Let the real sample distribution be \tilde{p}(x) and the forged sample distribution be q(x). After fixing the generator, we optimize the discriminator T(x): \min_{T} -\mathbb{E}_{x\sim \tilde{p}(x)}[\log \sigma(T(x))] - \mathbb{E}_{x\sim q(x)}[\log(1-\sigma(T(x)))] \label{eq:sgan-d} Here, \sigma is the sigmoid activation function. Then, fixing the discriminator, we optimize the generator G(z): \min_{G} \mathbb{E}_{x=G(z),z\sim q(z)}[h(T(x))] \label{eq:sgan-g} Note that we have an uncertain h here; we will analyze it immediately.
From [eq:sgan-d], we can solve for the optimal discriminator (supplementary proof provided later): \frac{\tilde{p}(x)}{q(x)} = \frac{\sigma(T(x))}{1 - \sigma(T(x))} = e^{T(x)} Substituting this into [eq:sgan-g], we find the result is: \min_{G} \mathbb{E}_{x=G(z),z\sim q(z)}\left[h\left(\log\frac{\tilde{p}(x)}{q(x)}\right)\right] = \min_{G} \int q(x)\left[h\left(\log\frac{\tilde{p}(x)}{q(x)}\right)\right] dx Written as the last equality, we can see it has the form of an f-divergence by simply setting f(t)=h(\log(t)). That is to say, minimizing [eq:sgan-g] is equivalent to minimizing the corresponding f-divergence. Regarding f-divergence, you can refer to my previous post “Introduction to f-GAN: The Production Workshop of GAN Models.” The essential requirement for f in f-divergence is that f is a convex function, so we only need to choose h such that h(\log(t)) is a convex function. The simplest case is h(t)=-t, corresponding to h(\log(t))=-\log t, which is convex. In this case, [eq:sgan-g] becomes: \min_{G} \mathbb{E}_{x=G(z),z\sim q(z)}[-T(x)] There are many similar choices. For example, when h(t)=-\log \sigma(t), h(\log(t))=\log \left(1+\frac{1}{t}\right) is also a convex function (for t > 0), so: \min_{G} \mathbb{E}_{x=G(z),z\sim q(z)}[-\log\sigma(T(x))] is also a reasonable choice, and it is one of the commonly used generator losses in GANs. Similarly, there is h(t)=\log(1-\sigma(t)), and these choices are not enumerated here.
RSGAN Objective
Here, we first directly give the optimization objective of RSGAN: after fixing the generator, we optimize the discriminator T(x): \min_{T} -\mathbb{E}_{x_r\sim \tilde{p}(x), x_f\sim q(x)}[\log \sigma(T(x_r)-T(x_f))] \label{eq:rsgan-d} Here, \sigma is the sigmoid activation function. Then, fixing the discriminator, we optimize the generator G(z): \min_{G} \mathbb{E}_{x_r\sim \tilde{p}(x), x_f=G(z),z\sim q(z)}[h(T(x_f) - T(x_r))] \label{eq:rsgan-g} As with SGAN, we keep a general h here, and the requirements for h are consistent with the previous discussion of SGAN. The choice in the original RSGAN paper is: \min_{G} -\mathbb{E}_{x_r\sim \tilde{p}(x), x_f=G(z),z\sim q(z)}[\log\sigma(T(x_f) - T(x_r))]
It looks like the two terms of the SGAN discriminator have been replaced by a single relativistic discriminator. How do the related analytical results change?
Theoretical Results
Through the calculus of variations (supplementary proof provided later), we can obtain the optimal solution for [eq:rsgan-d]: \frac{\tilde{p}(x_r)q(x_f)}{\tilde{p}(x_f)q(x_r)} = \frac{\sigma(T(x_r)-T(x_f))}{\sigma(T(x_f)-T(x_r))} = e^{T(x_r)-T(x_f)} Substituting this into [eq:rsgan-g], the result is: \begin{aligned} &\min_{G} \mathbb{E}_{x_r\sim \tilde{p}(x), x_f=G(z),z\sim q(z)}\left[h\left(\log\frac{\tilde{p}(x_f)q(x_r)}{\tilde{p}(x_r)q(x_f)}\right)\right] \\ =& \min_{G} \iint \tilde{p}(x_r)q(x_f)\left[h\left(\log\frac{\tilde{p}(x_f)q(x_r)}{\tilde{p}(x_r)q(x_f)}\right)\right] dx_r dx_f \end{aligned} \label{eq:rsgan-gg} This result is the essence of the entire RSGAN: it optimizes the f-divergence between \tilde{p}(x_r)q(x_f) and \tilde{p}(x_f)q(x_r)!
What does this mean? It means that if I sample a x_r from the real samples and a x_f from the forged samples, and then swap them—treating the fake as real and the real as fake—can they still be distinguished? In other words: is there a significant change in \tilde{p}(x_f)q(x_r)?
If there is no change, it means that real and fake samples can no longer be distinguished, and the training is successful. If they can still be distinguished, it means that real samples are still needed to improve the forged samples. Therefore, Equation [eq:rsgan-gg] is the embodiment of the “Turing Test” idea in RSGAN: If the data is shuffled, can it still be distinguished?
Model Performance Analysis
The author also proposed RaSGAN in the original paper, where “a” stands for average, meaning the average of the entire batch is used to replace a single real/fake sample. However, I don’t think this is a particularly elegant approach, and the paper also shows that the effect of RaSGAN is not always better than RSGAN, so I won’t introduce it here. Interested readers can check the original paper.
As for the results, the performance list in the paper shows that RSGAN improves the generation quality of the model in many tasks, but this is not always the case; on average, there is a slight improvement. The author specifically points out that RSGAN can speed up the training of the generator. I personally experimented with it, and it is faster than SGAN and SNGAN.
My reference code:
https://github.com/bojone/gan/blob/master/keras/rsgan_sn_celeba.py
Borrowing a chart from MingtaoGuo to compare the convergence speed of RSGAN:
Intuitively, RSGAN is faster because it also uses information from real samples when training the generator, rather than just relying on the discriminator’s “memory.” Theoretically, by taking the difference between T(x_r) and T(x_f), the discriminator only depends on their relative values, which simply improves the potential bias situation of the discriminator T, making the gradient more stable. I even think that introducing real samples into the generator’s training might (though I haven’t proven it carefully) improve the diversity of forged samples, because with various real samples for comparison, if the model only generates a single sample, it would be difficult to satisfy the discriminator’s comparative criteria.