# GAN Theories

## 基本组件

Discriminator（判别器）: 用来判断图片是真实的还是生成的

Generator（生成器）: 生成使判别器无法判别真假的图片

## Objective

For the data set, after converting the data into vectors, the data of people has its own distribution, and the data of cars also has its own distribution. The data distribution of cars is definitely not the same as the data distribution of people. For two-dimensional data with two Gaussian distributions, the difference between the two data distributions is equivalent to the difference in expectation and variance. Of course, the actual data is much more complex than the above example, we can only understand their distribution abstractly
For traditional network, we need paired data to train the model (sample to sample). For example, if we want the model to de the colorization. we need a gray picture of chicken and corresponding color picture.
But if we let the model learn the distribution of color picture, we can use other color picture like car, horse and human to let the model do the colorize job for chicken picture.
But how can we prevent the model turning the chicken picture into horse? Remember, we can stack another loss function to discriminate the picture. In this way, we can colorize the picture and leave the object's appearance unchanged in space

## 代码实现

#### Problem

solutions

![image-20220522180623346](01 GAN 基础.assets/image-20220522180623346.png)

#### 训练代码示例（pytorch）

criterion = nn.BCELoss()

valid = Tensor(imgs.size(0), 1).fill_(1.0).detach()
fake = Tensor(imgs.size(0), 1).fill_(0.0).detach()
# detach means "requires_grad = False"

# Train Generator

z = Tensor(np.random.normal(0, 1, (imgs.shape, latent_dim)))
gen_imgs = generator(z)

g_loss = criterion(discriminator(gen_imgs), valid)
g_loss.backward()
optimizer_G.step()

# Train Discriminator

real_loss = criterion(discriminator(real_imgs), valid)
fake_loss = criterion(discriminator(gen_imgs.detach()), fake)
d_loss = (real_loss + fake_loss) / 2

d_loss.backward()
optimizer_D.step()


## 总结

### 最初GAN的问题

• 难以训练
• 容易得到相同的图片（以MNIST数据集为例非常容易生成1和7）
• 训练过程很不稳定，会出现效果倒退的情况

GAN相关代码已上传至GitHub RottenTangerine/ML/GAN at master · (github.com)