理论部分: CS231n 2022PPT笔记- 生成模型Generative Modeling_iwill323的博客-CSDN博客
目录
We can think of the generator (𝐺) trying to fool the discriminator (𝐷) and the discriminator trying to correctly classify real vs. fake as a minimax game:
where 𝑧∼𝑝(𝑧)are the random noise samples, 𝐺(𝑧) are the generated images using the neural network generator 𝐺, and 𝐷 is the output of the discriminator, specifying the probability of an input being real.
In this assignment, we will alternate the following updates:
- Update the generator (𝐺) to maximize the probability of the discriminator making the incorrect choice on generated data:
maximize 𝔼𝑧∼𝑝(𝑧)[log𝐷(𝐺(𝑧))]
- Update the discriminator (𝐷), to maximize the probability of the discriminator making the correct choice on real and generated data:
maximize 𝔼𝑥∼𝑝data[log𝐷(𝑥)]+𝔼𝑧∼𝑝(𝑧)[log(1−𝐷(𝐺(𝑧)))]
导包
# Setup cell.
import numpy as np
import torch
import torch.nn as nn
from torch.nn import init
import torchvision
import torchvision.transforms as transforms
import torch.optim as optim
from torch.utils.data import DataLoader
from torch.utils.data import sampler
import torchvision.datasets as datasets
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
%matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 8.0) # Set default size of plots.
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
%load_ext autoreload
%autoreload 2
def show_images(images):
# images: (N, C, H, W)
images = np.reshape(images, [images.shape[0], -1]) # Images reshape to (batch_size, D).
sqrtn = int(np.ceil(np.sqrt(images.shape[0])))
sqrtimg = int(np.ceil(np.sqrt(images.shape[1])))
fig = plt.figure(figsize=(sqrtn, sqrtn))
gs = gridspec.GridSpec(sqrtn, sqrtn)
gs.update(wspace=0.05, hspace=0.05)
for i, img in enumerate(images):
ax = plt.subplot(gs[i])
plt.axis('off')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_aspect('equal')
plt.imshow(img.reshape([sqrtimg,sqrtimg]))
return
dtype = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.FloatTensor
NOISE_DIM = 96
加载数据
NUM_TRAIN = 50000 # 总的训练数据其实是60,000个
NUM_VAL = 5000
NOISE_DIM = 96
batch_size = 128
mnist_train = datasets.MNIST(
'./cs231n/datasets/MNIST_data',
train=True,
download=True,
transform=transforms.ToTensor()
)
loader_train = DataLoader(
mnist_train,
batch_size=batch_size,
sampler=ChunkSampler(NUM_TRAIN, 0)
)
mnist_val = datasets.MNIST(
'./cs231n/datasets/MNIST_data',
train=True,
download=True,
transform=transforms.ToTensor()
)
loader_val = DataLoader(
mnist_val,
batch_size=batch_size,
sampler=ChunkSampler(NUM_VAL, NUM_TRAIN)
)
imgs = loader_train.__iter__().next()[0].view(batch_size, 784).numpy().squeeze()
print(imgs.shape) # (128, 784)
show_images(imgs) # 查看其中一个batch的图片
class ChunkSampler(sampler.Sampler):
"""Samples elements sequentially from some offset.
Arguments:
num_samples: # of desired datapoints
start: offset where we should start selecting from
"""
def __init__(self, num_samples, start=0):
self.num_samples = num_samples
self.start = start
def __iter__(self):
return iter(range(self.start, self.start + self.num_samples))
def __len__(self):
return self.num_samples
Vanilla GAN
Discriminator
The output of the discriminator should have shape [batch_size, 1]
, and