๐ ๋ชฉ์ฐจ
1. Energy-based Model
2. Energy ํจ์
3. ๊ธฐํ (BM, RBM)
4. ์์ฝ
๐ง preview:
EBM(Energy-Based Model)์ ์์ด๋์ด:
์ค์๊ฐ์ Energy ํจ์๋ฅผ 0๊ณผ 1์ฌ์ด๋ก ์ ๊ทํํ๋ Boltzmann Distribution์ผ๋ก ์ด๋ค ์ฌ๊ฑด์ ํ๋ฅ ์ ํํํ ์ ์๋ค๋ ๊ฒ.
1. Energy with Boltzmann
Boltzmann Distribution
EBM์ Boltzmann๋ถํฌ๋ก ์ค์ data์์ฑ๋ถํฌ๋ฅผ ๋ชจ๋ธ๋งํ๋ค.
์๋ ์๊ณผ ๊ฐ์ด Boltzmann๋ถํฌ์ Energyํจ์(์ ์)๋ฅผ ๋ํ๋ผ ์ ์๋ค:
Step 1. ์ ๊ฒฝ๋ง E(x)๋ฅผ ํ๋ จ:
→ ๊ฐ๋ฅ์ฑ์ด ๋์ sample์๋ ๋ฎ์ score → p(x)๊ฐ 1์ ๊ฐ๊น์์ง.
→ ๊ฐ๋ฅ์ฑ์ด ๋ฎ์ sample์๋ ๋์ score → p(x)๊ฐ 0์ ๊ฐ๊น์์ง.
Step 2. ํ๊ธฐ ์ด๋ ค์ด ์ ๋ถ์ด ์กด์ฌ:
→ ์ ์์ ์ ๊ทํ๋ถ๋ชจ๋ ๊ฐ๋จํ ๋ฌธ์ ๋ฅผ ์ ์ธ, ํ๊ธฐ ์ด๋ ค์ด ์ ๋ถ์ด ์กด์ฌ.
์ด ์ ๋ถ์ ๊ณ์ฐํ ์ ์์ผ๋ฉด, MLE๋ก ๋ชจ๋ธํ์ต์ด ๋ถ๊ฐ๋ฅํ๋ค.
โ EBM์ ํต์ฌ์์ด๋์ด: ๊ทผ์ฌ๊ธฐ๋ฒ์ ์ฌ์ฉ, ์ด ๋ถ๋ชจ๋ฅผ ๊ณ์ฐํ ํ์๊ฐ ์๋๋ก ๋ง๋๋ ๊ฒ.
์ด๋ ํ์ค์ ๊ท๋ถํฌ์ ์ ์ฉํ ๋ณํ์ด ์ฌ์ ํ ์ ํจํ ํ๋ฅ ๋ถํฌ๋ฅผ ์ถ๋ ฅํ๋๋ก ๋ง๋ค๋ ค๊ณ ์ ์ฐ๋ Normalizing Flow์๋ ๋์กฐ์ ์ด๋ค.
[Implicit Generation and Modeling with EBMs; 2019]์ ํต์ฌ์์ด๋์ด๋ ๋ค์๊ณผ ๊ฐ๋ค:
(for training): ๋์กฐ๋ฐ์ฐ๊ธฐ๋ฒ
(for sampling): Langevin Dynamics๊ธฐ๋ฒ
→ ๊น๋ค๋ก์ด ๋ถ๋ชจ๋ฌธ์ ๋ฅผ ํด๊ฒฐ.
2. Energy Based Model
Energy ํจ์
์๋์งํจ์ Eθ(x)๋ parameter๊ฐ θ์ด๊ณ ์ ๋ ฅ img๊ฐ x๋ฅผ ํ๋์ ์ค์นผ๋ผ๊ฐ์ผ๋ก ๋ณํํ๋ ์ ๊ฒฝ๋ง.
์ด๋, ์ฌ๊ธฐ์ Swishํ์ฑํํจ์๋ฅผ ์ฌ์ฉํ๋ค.
์ด Swishํจ์๋ Gradient Vanishing์ ์ํ์ํค๋๋ฐ, ํนํ EBM์์ ์ค์ํ๋ค.
์ด ์ ๊ฒฝ๋ง์ ์ผ๋ จ์ Conv2D๋ฅผ ์์ imgํฌ๊ธฐ๋ฅผ ์ค์ด๊ณ channelํฌ๊ธฐ๋ฅผ ๋๋ฆฐ๋ค.
๋ง์ง๋ง ์ธต์ Linear Activation์ด๊ธฐ์ ์ด ์ ๊ฒฝ๋ง์ (-∞,∞)์ ๋ฒ์๊ฐ์ ์ถ๋ ฅํ๋ค.
ํด๋น ์ฝ๋๋ ์๋์ ๊ฐ๋ค.
class Swish(nn.Module): def forward(self, x): return x * torch.sigmoid(x) class EBM(nn.Module): def __init__(self, hidden_features=32, out_dim=1, **kwargs): super().__init__() c_hid1 = hidden_features//2 c_hid2 = hidden_features c_hid3 = hidden_features*2 self.cnn_layers = nn.Sequential( nn.Conv2d(1, c_hid1, kernel_size=5, stride=2, padding=4), # [16x16] - Larger padding to get 32x32 image Swish(), nn.Conv2d(c_hid1, c_hid2, kernel_size=3, stride=2, padding=1), # [8x8] Swish(), nn.Conv2d(c_hid2, c_hid3, kernel_size=3, stride=2, padding=1), # [4x4] Swish(), nn.Conv2d(c_hid3, c_hid3, kernel_size=3, stride=2, padding=1), # [2x2] Swish(), nn.Flatten(), nn.Linear(c_hid3*4, c_hid3), Swish(), nn.Linear(c_hid3, out_dim) ) def forward(self, x): x = self.cnn_layers(x).squeeze(dim=-1) return xโ
Langevin Dynamic
Energyํจ์๋ ์ฃผ์ด์ง ์ ๋ ฅ์ ๋ํด 1๊ฐ ์ ์๋ง ์ถ๋ ฅํ๋ค.
๐ค How to... ์๋์ง์ ์๊ฐ ๋ฎ์ new sample์์ฑ?
๋์ฃผ๋ฑ ๋์ญํ๊ธฐ๋ฒ: ์ ๋ ฅ์ ๋ํ Energyํจ์์ Gradient๊ณ์ฐํ๋ ๊ธฐ๋ฒ
Step 1. Sample space์ ์์์ ์ง์ ์์ ์์
Step 2. ๊ณ์ฐ๋ Gradient์ ๋ฐ๋๋ฐฉํฅ์ผ๋ก ์กฐ๊ธ์ฉ ์ด๋, Energyํจ์๋ฅผ ๊ฐ์.
Step 3. train์ดํ, random noise๊ฐ trainset๊ณผ ์ ์ฌํ img๋ก ๋ณํ๋จ.
stochastic gradient Langevin Dynamic:
sample space ์ด๋ ์, ์ ๊ทธ๋ฆผ์ฒ๋ผ input์ ์๋์ random noise์ถ๊ฐ.
(๋ง์ฝ, ๊ทธ๋ ์ง ์์ผ๋ฉด local minima์ ๋น ์ง ์ ์์.)
๋ค๋ง, ์ผ๋ฐ SGD๋์ ๋น์ฐํ ์ฐจ์ด๊ฐ ์๋ค.
โ SGD: (-) ๊ธฐ์ธ๊ธฐ๋ฐฉํฅ์ผ๋ก ํ๋ผ๋ฏธํฐ ์กฐ๊ธ์ฉ update, Loss์ต์ํ
โ SGD with Langevin Dynamic:
์ฌ๊ธฐ์ Langevin Dynamic์ ์ฌ์ฉํ๋ฉด Gradient Weight๋ฅผ ๊ณ ์ ํ๊ณ ,
input์ ๋ํ ์ถ๋ ฅ์ Gradient๋ฅผ ๊ณ์ฐํ๋ค.
๊ทธ ํ (-) ๊ธฐ์ธ๊ธฐ๋ฐฉํฅ์ผ๋ก input์ ์กฐ๊ธ์ฉ update,
์ ์ง์ ์ผ๋ก ์ถ๋ ฅ(energy score)์ ์ต์ํํ๋ค.
๋ ๋ฐฉ์ ๋ชจ๋ ๋์ผํ ๊ฒฝ์ฌํ๊ฐ๋ฒ์ ํ์ฉํ๋, ๋ค๋ฅธ ๋ชฉ์ ํจ์์ ์ ์ฉ๋๋ค.
[Langevin Samplingํจ์]:
def generate_samples(model, inp_imgs, steps=60, step_size=10, return_img_per_step=False): """ Function for sampling images for a given model. Inputs: model - Neural network to use for modeling E_theta inp_imgs - Images to start from for sampling. If you want to generate new images, enter noise between -1 and 1. steps - Number of iterations in the MCMC algorithm. step_size - Learning rate nu in the algorithm above return_img_per_step - If True, we return the sample at every iteration of the MCMC """ # Before MCMC: set model parameters to "required_grad=False" # because we are only interested in the gradients of the input. is_training = model.training model.eval() for p in model.parameters(): p.requires_grad = False inp_imgs.requires_grad = True # Enable gradient calculation if not already the case had_gradients_enabled = torch.is_grad_enabled() torch.set_grad_enabled(True) # We use a buffer tensor in which we generate noise each loop iteration. # More efficient than creating a new tensor every iteration. noise = torch.randn(inp_imgs.shape, device=inp_imgs.device) # List for storing generations at each step (for later analysis) imgs_per_step = [] # Loop over K (steps) for _ in range(steps): # Part 1: Add noise to the input. noise.normal_(0, 0.005) inp_imgs.data.add_(noise.data) inp_imgs.data.clamp_(min=-1.0, max=1.0) # Part 2: calculate gradients for the current input. out_imgs = -model(inp_imgs) out_imgs.sum().backward() inp_imgs.grad.data.clamp_(-0.03, 0.03) # For stabilizing and preventing too high gradients # Apply gradients to our current samples inp_imgs.data.add_(-step_size * inp_imgs.grad.data) inp_imgs.grad.detach_() inp_imgs.grad.zero_() inp_imgs.data.clamp_(min=-1.0, max=1.0) if return_img_per_step: imgs_per_step.append(inp_imgs.clone().detach()) # Reactivate gradients for parameters for training for p in model.parameters(): p.requires_grad = True model.train(is_training) # Reset gradient calculation to setting before this function torch.set_grad_enabled(had_gradients_enabled) if return_img_per_step: return torch.stack(imgs_per_step, dim=0) else: return inp_imgs
Contrastive Divergence
์์ Sampling space์์ ๋ฎ์ผ energy๋ฅผ ๊ฐ๋ ์๋ก์ด point๋ฅผ samplingํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์์ผ๋ ๋ชจ๋ธ train์ชฝ์ ์์๋ณด์.
Energyํจ์๋ ํ๋ฅ ์ ์ถ๋ ฅํ์ง ์๋๋ค
= MLE๋ฅผ ์ ์ฉํ ์ ์๋ค. (๋ฌผ๋ก , ํญ์ ๊ทธ๋ ๋ฏ NLL Loss์ต์ํ๊ฐ ๋ชฉ์ ์ด๋ค.)
pθ(x)๊ฐ energyํจ์ Eθ(x)๋ฅผ ํฌํจํ๋ Boltzmann ๋ถํฌ์ผ ๋,
์ด ๊ฐ์ Gradient๋ ์๋์ ๊ฐ๋ค:
์ ์์ ์ง๊ด์ ์ผ๋ก ์ดํด๊ฐ๋ฅํ๋ค:
โ ์ค์ sample์ ๋ํด ํฐ ์์ energy score
โ ์์ฑ๋ ๊ฐ์ง sample์ ๋ํด ํฐ ์์ energy score๋ฅผ ์ถ๋ ฅํ๋๋ก ๋ชจ๋ธ ํ๋ จ
→ ๋ ๊ทน๋จ ๊ฐ์ ์ฐจ์ด๊ฐ ๊ฐ๋ฅํ ์ปค์ง๋๋กํ์ฌ ์ด๋ฅผ ์์คํจ์๋ก ์ด์ฉ.
์ด๋, ๊ฐ์ง sample์ Energy score๋ฅผ ๊ณ์ฐํ๋ ค๋ฉด ๋ถํฌ pθ(x)์์ ์ ํํ sampling๊ฐ๋ฅํด์ผํ๋ค.
๋ค๋ง, ๋ถ๋ชจ๊ณ์ฐ์ด ์ด๋ ค์ ๋ถ๊ฐ๋ฅํ๋ค.
→ ๋์ , Langevin Sampling๋ฐฉ๋ฒ์ ์ฌ์ฉํด ๋ฎ์ energy score๋ฅผ ๊ฐ๋ sample set ์์ฑ์ด ๊ฐ๋ฅํ๋ค.
๋ํ, ์ด์ ๋ฐ๋ณต sample์ buffer์ ์ ์ฅ,
๋ค์ batch์์์ ์ผ๋ก ์์noise๋์ ์ ์ด๋ฅผ ์ฌ์ฉํ๋ค.
def sample_new_exmps(self, steps=60, step_size=10): """ Function for getting a new batch of "fake" images. Inputs: steps - Number of iterations in the MCMC algorithm step_size - Learning rate nu in the algorithm above """ # Choose 95% of the batch from the buffer, 5% generate from scratch n_new = np.random.binomial(self.sample_size, 0.05) rand_imgs = torch.rand((n_new,) + self.img_shape) * 2 - 1 old_imgs = torch.cat(random.choices(self.examples, k=self.sample_size-n_new), dim=0) inp_imgs = torch.cat([rand_imgs, old_imgs], dim=0).detach().to(device) # Perform MCMC sampling inp_imgs = Sampler.generate_samples(self.model, inp_imgs, steps=steps, step_size=step_size) # Add new images to the buffer and remove old ones if needed self.examples = list(inp_imgs.to(torch.device("cpu")).chunk(self.sample_size, dim=0)) + self.examples self.examples = self.examples[:self.max_len] return inp_imgs
์ด๋, ๊ฐ step์ด ์ข ๋ฃ๋ ํ, score normalizationํ์ง ์๊ณ
์๊ณ ๋ฆฌ์ฆ์ ๋ฐ๋ผ sample score๋ ์๋๋ก ๋ด๋ ค๊ฐ๊ณ ,
๊ฐ์ง sample์ ์ ์๋ ์๋ก ์ฌ๋ผ๊ฐ๋ค.
์ต์ข ์ฝ๋๋ ๋งํฌ ์ฐธ๊ณ .
EBM์ ์ดํ score matching์ด๋ผ๋ ํ๋ จ๊ธฐ๋ฒ์ผ๋ก ๋ฐ์ ํ์๊ณ ,
์ด๋ Denoising Diffusion Probabilistic Model์ด๋ผ๋ ๋ชจ๋ธ๋ก ๋ฐ์ ํด
DALLEโ2 ๋ฐ Imagen๊ฐ์ ์ต์ฒจ๋จ ์์ฑ๋ชจ๋ธ ๊ตฌํ์ ์ฌ์ฉ๋๋ค.
3. ๊ธฐํ (BM, RBM)
Boltzmann Machine
EBM์ ์ด๊ธฐ ์ฌ๋ก์ค ํ๋๋ก fully connected undirected neural network์ด๋ค.
v: visible unit
h: hidden unit
Contrastive Divergence๋ก train.
์ด๋, ๊ท ํ์ ์ ์ฐพ์ ๋ ๊น์ง v์ h์ฌ์ด๋ฅผ ๋ฒ๊ฐ์ด์ Gibbs Sampling์ ์ํํ๋ค.
๋จ์ : train์๋๊ฐ ๋งค์ฐ ๋๋ฆฌ๊ณ hidden unit๊ฐ์๋ฅผ ํฌ๊ฒ ๋๋ฆด ์ ์๋ค.
Restricted Boltzmann Machine
์์ Boltzmann machine์ ํ์ฅํ RBM์ ๊ฐ์ ์ข ๋ฅ unit์ฌ์ด connection์ ์ ๊ฑฐ,
2๊ฐ ์ธต์ผ๋ก ๊ตฌ์ฑ๋ ์ด๋ถ๊ทธ๋ํ(bipartite graph)๋ฅผ ์์ฑํ๋ค.
์ด๋ฅผ ํตํด RBM์ ์์ ๋ ๋ณต์กํ ๋ถํฌ๋ฅผ ๋ชจ๋ธ๋งํ๋ ์ฌ์ธต์ ๋ขฐ์ ๊ฒฝ๋ง(Deep Belief Network)์ ๋ง๋ค ์ ์๋ค.
๋จ์ : Mixing Time(= Gibbs Sampling์ผ๋ก ๋ชฉํ์ํ์ ๋๋ฌํ๋๋ฐ ๊ฑธ๋ฆฐ์๊ฐ)์ด ๊ธด Sampling์ด ์ฌ์ ํ ํ์.
๋ฐ๋ผ์ ์ฌ์ ํ ๊ณ ์ฐจ์ data ๋ชจ๋ธ๋ง์๋ ํ์ค์ ์ผ๋ก ์ด๋ ต๋ค.
4. ์์ฝ
Deep EBM์ Sampling์ Langevin Dynamic์ผ๋ก ์ด๋ค์ง๋ค.
์ด ๊ธฐ๋ฒ์ input img์ ๋ํ ์ ์์ gradient๋ฅผ ์ฌ์ฉํ์ฌ
gradient๊ฐ ๊ฐ์ํ๋ ๋ฐฉํฅ์ผ๋ก ์กฐ๊ธ์ฉ input์ updateํ๋ค.
๊ฒฐ๊ณผ์ ์ผ๋ก random noise๋ ๊ทธ๋ด๋ฏํ sample๋ก ์ ์ง์ ๋ณํ์ด ์ด๋ค์ง๋ค.
'Gain Study > Generation' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[G]Part 3-1. Advanced GAN (0) | 2024.03.04 |
---|---|
[G]Part 2-6. Diffusion Models (0) | 2024.01.30 |
[G]Part 2-4. Normalizing Flows (2) | 2024.01.29 |
[G]Part 2-3. Auto Regressive Models (0) | 2024.01.26 |
[G]Part 2-2. GAN (0) | 2024.01.26 |