-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
124 lines (96 loc) · 4.19 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import torch
import torch.optim
import torchvision.datasets as datasets
import torchvision.transforms as transforms
import torchvision.utils
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc, rcParams
from vae import VariationalAutoEncoder
from sampler import Sampler
data_path = '/Users/matthieu/Projets/Data/'
image_size = 28
n_channels = 1
batch_size = 8
n_epochs = 20
input_dim = image_size * image_size * n_channels
embedding_dim = 10
learning_rate = 1e-3
train_loader = torch.utils.data.DataLoader(
torchvision.datasets.MNIST(data_path, train=True, download=False,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
(0.1307,), (0.3081,))
])),
batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(
torchvision.datasets.MNIST(data_path, train=False, download=False,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
(0.1307,), (0.3081,))
])),
batch_size=batch_size, shuffle=True)
def train(model, optimizer, n_epochs):
training_loss_values = []
print(f'Training loop')
for epoch_index in range(n_epochs):
print(f'Epoch {epoch_index}')
epoch_loss = 0
for batch_index, (image_input, label) in enumerate(train_loader):
# if batch_index > 5000:
# break
if batch_index % 100 == 0:
print(f'Batch index {batch_index}')
input_vector = image_input.view(batch_size, input_dim)
generated_sample, embedded_mean, embedded_log_var = model(input_vector)
loss = model.loss(input_vector, generated_sample,
embedded_mean, embedded_log_var)
optimizer.zero_grad()
loss.backward()
optimizer.step()
epoch_loss += loss
training_loss_values.append(epoch_loss)
return training_loss_values
def generate_image(model):
embedding_sample = model.sample(torch.zeros(embedding_dim),
torch.ones(embedding_dim))
generated_sample = model.decode(embedding_sample)
return vector_to_image(generated_sample.data.numpy())
def vector_to_image(vector):
return vector.reshape((image_size, image_size, n_channels))
batch = next(iter(train_loader))
# TRAIN
model = VariationalAutoEncoder(input_dim, embedding_dim)
print('optimizer')
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
print(n_epochs)
training_loss_values = train(model, optimizer, n_epochs)
# RECONSTRUCTION
def reconstruct(model, x0, original_vector, missing_data_indices, T, method='pseudo_Gibbs'):
sampler = Sampler(model, x0.clone(), missing_data_indices, T)
sampling_method = sampler.sample_pseudo_gibbs if method=='pseudo_Gibbs' else sampler.sample_metropolis
sampled_vectors = sampling_method()
sampled_images = np.array([vector_to_image(vector)
for vector in sampled_vectors])
error = (original_vector - torch.Tensor(sampled_vectors)).norm(dim=1)
return sampled_images, error
T = 20
t_sample_values = np.linspace(0, T-1, num=10, dtype=int)
missing_data_indices = np.arange(200, 500)
# input
x = batch[0][0].view(input_dim)
original_vector = x.data.clone()
# add noise
x.data[missing_data_indices] = torch.abs(torch.randn(size=(len(missing_data_indices),)))
# output
gibbs_sampled_images, gibbs_error = reconstruct(model, x, original_vector, missing_data_indices, T)
metropolis_sampled_images, metropolis_error = reconstruct(model, x, original_vector, missing_data_indices, T, method='Metropolis')
def plot_errors():
plt.plot(metropolis_error, label='Metropolis within Gibbs', lw=1, color='black')
plt.plot(gibbs_error, label='Pseudo Gibbs', lw=1)
plt.xlabel('iterations')
plt.ylabel('reconstruction error')
plt.legend()