Prev: L12, Next: L14, Zoom: Link

Wisc NetID for In-class Quiz: @wisc.edu
4-digit Passcode: (anything is okay, use the same one for all lectures or use based on your id) 
Save submission to file? File name: (leave it blank to not save anything)


Slide:  



# Generative Adversarial Networks

📗 Generative adversarial networks (GAN) use two neural networks: Link, Wikipedia.
➩ The generator (deconvolutional neural network): input is noise, output is fake images (or text).
➩ The discriminator (convolutional neural network): input the fake or real image, output is binary class whether the image is real.
➩ The two networks are trained together, or competing in a zero-sum game: the generator tries to maximize the discriminator loss, and the discriminator tries to minimize the same loss: Wikipedia.
➩ The solution to the zero-sum game, Nash equilibrium, is the solution to \(\displaystyle\min_{w^{\left(G\right)}} \displaystyle\max_{w^{\left(D\right)}} C\left(w^{\left(G\right)}, w^{\left(D\right)}\right)\), where \(C\) is the loss of the discriminator.
📗 Example (deepfake) faces generated by GAN: Link, Link, Wikipedia.
In-class Discussion
📗 [1 points] Let the reward function of the two players be \(u_{i}\left(x_{i}, x_{-i}\right) = a x_{i} x_{-i} + \dfrac{b}{2} x_{i}^{2} + \dfrac{b}{2} x_{-i}^{2}\) where \(\left(x_{i}, x_{-i}\right)\) are actions taken by the two players. Move the sliders to change the values of \((a, b)\) so that (simultaneous) gradient ascent-descent will lead to a stable Nash.
a: 0
b: 0


Note: the gradient steps are \(x' = x - \alpha \left(a y + b x\right)\) and \(y' = y - \alpha \left(a x - b y\right)\) with learning rate \(\alpha\).
[L13Q1]
 

# Variational Auto-encoder

📗 Variational auto-encoder is another generative model that maps input into a lowest dimensional latent space (encoder) that can be used to reconstruct the original input (decoder), similar to PCA: Wikipedia, Wikipedia.
➩ An auto-encoder with encoder \(z = E\left(x\right)\) and decoder \(\hat{x} = D\left(z\right)\) minimizes the reconstruction loss \(\left\|x - \hat{x}\right\|^{2}\) = \(\left\|x - D\left(E\left(x\right)\right)\right\|^{2}\).
➩ Unlike regular auto-encoder, for VAE, the latent space (encoder) is forced to look Gaussian \(N\left(0, I\right)\), so it is possible to sample in the latent space, so the encoder is \(\left(\mu, \sigma\right) = E\left(x\right)\) with \(z = \mu + \sigma \varepsilon\) with \(\varepsilon \sim N\left(0, I\right)\): Link
➩ The loss function is regularized by the similarity between the encoding vector \(z\) and a Gaussian distribution: \(\left\|x - \hat{x}\right\| + \lambda KL\left(z, N\left(0, I\right)\right)\), where KL is the Kullback Leibler divergence (relative entropy) between two distributions, \(KL\left(P, Q\right) = \displaystyle\sum_{z} P\left(z\right) \log \dfrac{P\left(z\right)}{Q\left(z\right)}\).
📗 In VAE, \(\left(\mu, \sigma\right) = E\left(x\right)\) is used instead of \(z = E\left(x\right)\) and \(z = \mu + \sigma \varepsilon\) is generated with \(\varepsilon \sim N\left(0, I\right)\) so that every step in the neural network is differentiable (gradient descent can be used). Random sampling operation is not differentiable.
In-class Discussion
📗 [1 points] Click on the diagram on the left to regularize the auto-encoder so that the encoding follows a Gaussian distribution. Click to restart.


[L13Q2]
 



# Diffusion Model

📗 Diffusion model is a generative model that gradually add or remove noise (adding noise is similar to encoder, removing noise is similar to decoder): Wikipedia.
➩ Forward process (add noise): start with original image \(x_{0}\), at each step \(x_{t} = \sqrt{1 - \beta_{t}} x_{t-1} + \sqrt{\beta_{t}} \varepsilon\) where \(\varepsilon \sim N\left(0, I\right)\).
➩ Reverse process (remove noise): network inputs \(\left(x_{t}, t\right)\) and outputs \(\hat{\varepsilon}\) with loss \(\left\|\varepsilon - \hat{\varepsilon}\right\|^{2}\) during training. The the original image can be recovered as \(x_{t} - \hat{\varepsilon}\).
➩ The neural network training is similar to CNN training in supervised learning minimizing the loss by gradient descent.
📗 When generating images, sample \(x_{T} \sim N\left(0, I\right)\), predict \(x_{T-1} = x_{T} - \hat{\varepsilon}\left(x_{T}, T\right)\), and repeat until \(x_{0}\) which recovers the original image.
➩ Conditional diffusion conditions the noise removal process on some condition \(y\), for example, text prompt converted to an embedding vector. In this case, the network takes in \(\left(x_{t}, t, y\right)\) and predicts \(\hat{\varepsilon}\) as a function of \(y\).
Math Note
📗 Noise does not need to be added step by step, the following are equivalent:
➩ \(x_{t} = \sqrt{1 - \beta_{t}} x_{t-1} + \sqrt{\beta_{t}} \varepsilon\)
➩ \(x_{t} = \sqrt{\displaystyle\prod_{s=1}^{t} \left(1 - \beta_{t}\right)} x_{0} + \sqrt{1 - \displaystyle\prod_{s=1}^{t} \left(1 - \beta_{t}\right)} \varepsilon\)
In-class Discussion
📗 [1 points] Upload an image and try diffusion with different parameters: \(\beta\) = and \(t\) = . Image:
. Click on the image to reverse the diffusion process.


[L13Q3]
 

# Stable Diffusion

📗 Running diffusion on pixels is expensive: an (variational) auto-encoder can be used to reduce the dimensions.
➩ Given encoder \(z = E\left(x\right)\) and decoder \(x = D\left(z\right)\), the diffusion networks are applied to \(z_{t} = \sqrt{1 - \beta_{t}} z_{t-1} + \sqrt{\beta_{t}} \varepsilon\).
➩ When generating images, diffusion model turns prompt (text embedding vector) to image in the latent space (image embedding vector), then VAE converts it back to pixels of an image.
📗 During training, VAE is trained first and frozen, then the diffusion model is trained using the output of the VAE encoder.




# Questions?

📗 If you have questions, please (i) Ask during or after the lecture or the break, (ii) Piazza: Link, (iii) Office hours and discussion sessions. Please do NOT use Canvas mail and use email only to the course instructor (not TAs) for grading issues.
Additional In-class Discussion
📗 Sometimes a question not in the notes will be asked during the lecture, you can submit your answer here:

[Notes] (not visible to other students):
[L13Q4]

Submit your answer to see other students answers (click the submit button to refresh): 

Additional In-class Quiz
📗 Sometimes a question not in the notes will be asked during the lecture, you can submit your answer here:
A.
B.
C.
D.
E.
[Notes] (not visible to other students):
[L13Q5]

Submit your answer to see other students answers (click the submit button to refresh): 






# In-class Quiz Instructions

📗 To get full points on the in-class quizzes for a lecture:
➩ Submit relevant answers to the questions discussed during the lecture: incorrect answers are okay.
➩ Some questions require [notes] to earn the point.
➩ Some questions require special ID (given during the lecture) to earn the point.
➩ Do not submit answers to questions that are not discussed during the lectures. Each such submission will result in a deduction of one point.
➩ The grade on Canvas Assignment W13 is computed as number of points divided by the number of questions asked (and multiplied by 4, out of 4) and updated on Canvas every weekend.
📗 Notes and code adapted from the course taught by Professors Jerry Zhu, Blerina Gkotse, Yudong Chen, Yingyu Liang, Charles Dyer. Some content are generated using Copilot .

Prev: L12, Next: L14





Last Updated: November 25, 2026 at 3:04 AM