Neural Network
Generative Adversarial Network, GAN
2023-12-27
An autoencoder reconstructs its original input from a latent representation. A generative adversarial network (GAN), introduced here, can synthesize new data rather than simply reconstruct an existing input. This is why it is called a generative model.
GAN consists of two artificial neural networks: a generator (G) neural network to synthesize new samples and a discriminator (D) neural network to train the generator.
In a generative adversarial network (GAN), the goal is to train the generator and discriminator until the discriminator cannot distinguish the synthesized data from real data, by taking a randomly sampled latent vector (by a random number generation) from a given distribution, such as a uniform distribution or a Gaussian distribution in the generator, and passing it through the generator's coefficients to synthesize new data, which is then evaluated by the discriminator neural network.
Like ordinary artificial neural networks which is essentially represented by weights and thresholds, the generator's weights (v) and threshold (c) are used to generate new data, and based on this data, the discriminator's weights (w) and threshold (b) determine whether the result is real (i.e. 1) or fake (i.e. 0). By alternatively training real and fake data, the weights and thresholds of the generator and discriminator are obtained.
The picture shown below is the structure of GAN. On the left, in red, is the random vector z, comprising the generator neural network, consisting of an output layer with 4 nodes. On the right, in blue, is the discriminator neural network, consisting of an input layer with 4 nodes and an output layer with a single node. The discriminator aims to output a probability of 1 for labeled real input vector x, and a probability of 0 for fake input vector y generated from random numbers. The activation function for the discriminator used is Sigmoid. The generator uses random numbers generated from a normal distribution or a uniform distribution to create fake input vector y, hoping that the discriminator will classify it as 1. The overarching principle is to determine the weights (w, v) and thresholds (b, c) of each neural network to achieve this purpose.

The cost function for obtaining weights and threshold values uses cross entropy for binary classification, and the setting the cost function for each neural network input is as follows.
(1) If the generated fake data from the randomly sampled vector z in the distribution of the input value is denoted as y = [y1, y2, y3, y4] such that y=G(z), and its vector components are \(y_i=\phi(z*v_i-c_i)\). The corresponding discriminator is calculated by \(D(y')=D(G(z))=\phi(y\cdot w-b)=\phi(\sum _{\ }^{\ }y_iw_i-b)\). Here, the coefficients w and b must minimize the cost function of the discriminator, \(L=-\log (1-D(y'))\), because we would like the D(y') close to 0 (since it is fake data). Note that it must be 1-D(y') to be minimized because if D(y') is close to 0 (i.e. 0.001), then -ln(0.001) = 6.9, but -ln(1-0.001)= 0.001.
(2) For real data, x = [x1, x2, x3, x4], since the discriminator, \(D(x')=\phi(x \cdot w-b)=\phi(\sum _{\ }^{\ }x_iw_i-b)\) must is close to 1, the cost function to be minimized for the discriminator for the real data is defined as \(M=-\log (D(x'))\) to obtain the coefficients w, b.
(3) For the generator perspective, its coefficients v and c must be optimized for the discriminator output D(y') to be close to 1 for the given w, b at the discriminator. In other words, the cost function, \(N=-\log (D(y'))\) must be minimized to find v and c.
Step by step procedures are given below.
(step 1) Initialize the weights and thresholds of the generator and discriminator with random numbers.
(step 2) Generate a random number z within the distribution range and create fake data y = G(z).
(step 3) Calculate the discriminator cost function with arbitrary real data, denoted as x'.
\(M=-\log (D(x'))\)
(step 4) Update w and b to minimize the cost function, M.
\(w_i^{new}=w_i^{old}-\eta \partial M/\partial w_i\)
\(b^{new}=b^{old}-\eta \partial M/\partial b\)
(step 5) Calculate the discriminator cost function for the fake data, y' using improved w and b.
\(L=-\log (1-D(y'))\)
(step 6) Update w and b to minimize the cost function, L.
\(w_i^{new}=w_i^{old}-\eta \partial L/\partial w_i\)
\(b^{new}=b^{old}-\eta \partial L/\partial b\)
(step 7) Use the revised w, b to calculate the cost function, N, for the fake data, y'.
\(N=-\log (D(y'))\)
(step 8) Update v and c to minimize the cost function, N
\(v_i^{new}=v_i^{old}-\eta \partial N/\partial v_i\)
\(c_i^{new}=c_i^{old}-\eta \partial N/\partial c_i\)
(step 9) Repeat the (step 2)~(step 8) until w, v, b, and c get converged.
Throughout the steps, we introduced the 6 kinds of partial derivatives along with the learning rate \(\eta\):
\(\partial M/\partial w_i, \partial M/\partial b, \partial L/\partial w_i, \partial L/\partial b, \partial N/\partial v_i, \partial N/\partial c_i\)
These can be obtained by the chain rule which we described in the back propagation blog.
(1) The slope of the discriminator toward the real data, \(\partial M/\partial w_i, \partial M/\partial b\)
\(M=-\log (D(x'))\)
\(D(x')=\phi(x \cdot w-b)=\phi (\sum _{\ }^{\ }x_iw_i-b)\)
\(\phi'=\phi(1-\phi)\)
\(\partial M/\partial w_i=(\partial M/\partial D)(\partial D/\partial x')(\partial x'/\partial w_i)=\lbrace -1/D\rbrace \lbrace D(1-D)\rbrace x_i=-(1-D)x_i\)
\(\partial M/\partial b=(\partial M/\partial D)(\partial D/\partial x')(\partial x'/\partial b)=\lbrace -1/D\rbrace \lbrace D(1-D)\rbrace (-1)=(1-D)\)
(2) The slope of the discriminator toward the fake data, \(\partial L/\partial w_i, \partial L/\partial b\)
\(L=-\log (1-D(y'))\)
\(D(y)=\phi(y \cdot w-b)=\phi (\sum _{\ }^{\ }y_iw_i-b)\)
\(\partial L/\partial w_i=(\partial L/\partial D)(\partial D/\partial y')(\partial y'/\partial w_i)=\lbrace -1/(1-D)(-1)\rbrace \lbrace D(1-D)\rbrace y_i=Dy_i \)
\(\partial L/\partial b=(\partial L/\partial D)(\partial D/\partial y')(\partial y'/\partial b)=\lbrace -1/(1-D)(-1)\rbrace \lbrace D(1-D)\rbrace (-1)=-D\)
(3) The slope of the generator, \(\partial N/\partial v_i, \partial N/\partial c_i\)
\(N=-\log (D(y'))\)
\(D(y')=\phi(y \cdot w-b)=\phi (\sum _{\ }^{\ }y_iw_i-b) \text{, } y_i=\phi(z*v_i-c_i)\)
\(\partial N/\partial v_i=(\partial N/\partial D)(\partial D/\partial y')(\partial y'/\partial v_i)=\lbrace -1/D\rbrace \lbrace D(1-D)\rbrace \lbrace w_i\rbrace \lbrace y_i(1-y_i)\rbrace z=-(1-D)w_iy_i(1-y_i)z\)
\(\partial N/\partial c_i=(\partial N/\partial D)(\partial D/\partial y')(\partial y'/\partial c_i)=\lbrace -1/D\rbrace \lbrace D(1-D)\rbrace \lbrace w_i\rbrace \lbrace y_i(1-y_i)\rbrace (-1)=(1-D)w_iy_i(1-y_i)\)
To implement GAN in Excel, we will build the model to generate 3x4 pixel size of "O" shape like below.

(step 0) Prepare a set of Real data points.
The size of the nodes to be used on the output layer of the generator and the input layer of the discriminator is 12. Generates 10 real samples for training the discriminator, whose pixel values representing O.
Assign a near 1 value at the positions marked as "1" in the plot above:
= 1-rand()*0.2 at (1,2,3,4,6,7,9,10,11,12)
Assign a near 0 value at the positions marked as "0" in the plot above:
= 0+rand()*0.2 at (5,8)
Following table shows the 10 samples generated as the real data.

(step 1) In the first step, the weight w and the threshold b of the discriminator are initialized with random numbers, and in subsequent steps, the previous values are used.

(step 2) Calculate the probability of the real data, D(x), and its cost function, M,
D(x) =1/(1+EXP(-SUMPRODUCT(x1:x12,w1:w12)+bias))
M =-log(D(x))

(step 3) Compute the partial derivatives of M to the w and b, \(\partial M/\partial w_i, \partial M/\partial b\)

(step 4) Use the learning rate, \(\eta\) =0.01, to update the coefficients, w and b.

(step 5) Inputting a random value z which is from a uniform distribution between 0 and 1, generate a set of fake data by using the coefficients, v and c, which were initialized by random numbers, after then whatever from the previous iteration.



(step 6) Calculate the probability, D, for the fake y, and the cost function, L
D(y) =1/(1+EXP(-SUMPRODUCT(y1:y12,w1:w12)+bias))
L =-log(1-D(y)).

(step 7) Calculate the partial derivatives of L to the coefficients w and b, \(\partial L/\partial w_i, \partial L/\partial b\)

(step 8) Update w and b with the \(\eta\) =0.01

(step 9) Calculate the cost function, N for the generator, and its partial derivatives to the coefficients, v and c, \(\partial N/\partial v_i, \partial N/\partial c_i\).
N=-LOG(D(y'))


(step 10) Update the coefficients, v and c to minimize the cost function, N with \(\eta\)=0.01.


(step 11) Repeat the steps (step 1~step 10) using the next real data until all the coefficients get converged.
Fake images generated up to 500 epoch are shown in the below. As the epoch progresses, the fake image is getting close to the real "O" shape image.

Next plot is for the cost function for the generator, N, and the one for the discriminator, M+L, as a function of the iteration to show how quickly the GAN able to converge.
