-
Notifications
You must be signed in to change notification settings - Fork 2
/
transporter.py
190 lines (151 loc) · 6.33 KB
/
transporter.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from copy import deepcopy
import numpy as np
import os
import torch
import torch.autograd as autograd
import torch.nn as nn
import torch.optim as optim
from torch.distributions import uniform, normal
from utils import *
torch.set_default_tensor_type('torch.cuda.FloatTensor')
'''
Transporter Class. Uses optimal transport to learn the mapping from
a noise distribution to some latent space distribution.
'''
class Transporter():
def __init__(self, latent, noise='uniform', path='.', batch_size=512):
'''
Initializes the Transporter, including creating the model.
latent: (np array) Latent space distribution to map to. Must be an
array of one dimensional vectors.
noise: (str) Noise distribution to map from. Must be either 'uniform',
'normal', or 'gaussian'
path: (str) Path to store any images/weights of the model
batch_size: (int) Batch Size
'''
self.latent = torch.Tensor(latent)
self.dim = len(latent[0])
if noise.lower() == 'uniform':
self.noise = uniform.Uniform(-1, 1)
elif noise.lower() == 'normal' or noise.lower() == 'gaussian':
self.noise = normal.Normal(0, 1)
else:
raise Exception("{} has not been implemented yet".format(noise))
self.path = path
self.batch_size = batch_size
self.create_model()
def create_model(self):
'''
Creates a model of 7 fully connected layers with LeakyReLU activation.
All layers but last have 512 neurons.
'''
H = 512
self.model = torch.nn.Sequential(
torch.nn.Linear(self.dim, H),
torch.nn.LeakyReLU(),
torch.nn.Linear(H, H),
torch.nn.LeakyReLU(),
torch.nn.Linear(H, H),
torch.nn.LeakyReLU(),
torch.nn.Linear(H, H),
torch.nn.LeakyReLU(),
torch.nn.Linear(H, H),
torch.nn.LeakyReLU(),
torch.nn.Linear(H, H),
torch.nn.LeakyReLU(),
torch.nn.Linear(H, self.dim),
torch.nn.Sigmoid()
)
print("Sigmoid is added at the end of the generator")
def save_weights(self, name):
'''
Weights saved under self.path defined above.
'''
full_path = os.path.join(self.path, name)
torch.save(self.model.state_dict(), full_path)
def load_weights(self, name):
'''
Weights saved under self.path defined above.
'''
full_path = os.path.join(self.path, name)
self.model.load_state_dict(torch.load(full_path))
def train(self, steps, lr=0.001, images=False):
'''
Train transporter using optimal transport for any number of iterations.
steps: (int) Number of iterations to train
lr: (int) Learning Rate
images: (bool) Whether to store images or not. NOTE: Images will be
unable to be generated if dimension is > 2. This function is for the
toy datasets only!!!
'''
print("Beginning Transporter Training!")
loss_fn = torch.nn.L1Loss()
optimizer = optim.Adam(self.model.parameters(), betas=[0.5,0.999], lr=lr)
for i in range(steps):
# Samples inputs from noise distribution
inputs = self.noise.sample((self.batch_size, self.dim))
# Samples latent distribution
indices = np.random.choice(len(self.latent), size=self.batch_size)
real_vecs = self.latent[indices]
# Computes optimal transport from inputs to latent. answers[i] will
# be the latent vector that inputs[i] should be mapped to.
answer_indices = optimal_transport(inputs.cpu().numpy(), real_vecs.cpu().numpy())
answers = real_vecs[answer_indices]
generated = self.model(inputs)
loss = loss_fn(generated, answers)
loss.backward()
optimizer.step()
optimizer.zero_grad()
if i % 100 == 0:
print("Loss at step {}: {}".format(i, loss.item()))
if images and i % 1000 == 0:
save_points(generated.detach().cpu(), answers.cpu(), self.path, name="points_{}".format(i))
if i % 1000 == 0:
print("Saving checkpoint at 'transporter_{}'".format(i))
self.save_weights("transporter_{}".format(i))
print("Saving Transporter Weights...")
self.save_weights("transporter")
def generate(self, batches=1):
'''
Generate realistic latent vectors by sampling noise and using the
model to map them.
batches: (int) The number of batches of realistic latent vectors to
generate.
'''
outputs = None
for i in range(batches):
inputs = self.noise.sample((self.batch_size, self.dim))
latent_vec = self.model(inputs).detach().cpu().numpy()
if outputs is None:
outputs = latent_vec
else:
outputs = np.concatenate((outputs, latent_vec), 0)
return outputs
def interpolate(self, vec1, vec2, intermediate):
'''
Interpolate between two noise vectors.
vec1: (np array) First noise vector
vec2: (np array) Second noise vector
intermediate: (int) Number of intermediate steps when interpolating.
'''
increment = (vec1 - vec2) / intermediate
current = deepcopy(vec1)
inputs = [deepcopy(current)]
for i in range(size):
current = current + increment
inputs.append(deepcopy(current))
return self.model(torch.Tensor(inputs)).detach().cpu().numpy()
def evaluate(self):
'''
Evaluates how good the model is doing by calculating the optimal
transport mapping from the model's predictions to some true data
and then taking the average distance.
'''
generated = self.generate(batches=1)
# Samples latent distribution
indices = np.random.choice(len(self.latent), size=self.batch_size)
real_vecs = self.latent[indices].cpu().numpy()
answer_indices = optimal_transport(generated, real_vecs)
distances = np.linalg.norm(generated - real_vecs[answer_indices], axis=1)
mean_distance = np.mean(distances)
print("Mean distance: {}".format(mean_distance))