-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
273 lines (216 loc) · 11.6 KB
/
model.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import time
import itertools
from itertools import chain
import numpy as np
import torch
from torch import nn, tensor
from util import OneHot
import matplotlib.pyplot as plt
#Define generator and discriminator network
class Generator(nn.Module):
def __init__(self, batch_size, dim_y, dim_z, dim_W1, dim_W2, dim_W3, dim_channel, device):
super().__init__()
self.batch_size = batch_size
self.dim_y = dim_y
self.dim_z = dim_z
self.dim_W1 = dim_W1
self.dim_W2 = dim_W2
self.dim_W3 = dim_W3
self.dim_channel = dim_channel
self.device = device
self.layer1 = nn.Sequential(nn.Linear(dim_z + dim_y, dim_W1, bias=False),
nn.BatchNorm1d(dim_W1, eps=1e-8),
nn.ReLU())
self.layer2 = nn.Sequential(nn.Linear(dim_W1 + dim_y, dim_W2*6*6, bias=False),
nn.BatchNorm1d(dim_W2*6*6, eps=1e-8),
nn.ReLU())
self.layer3 = nn.Sequential(nn.ConvTranspose2d(in_channels=dim_W2 + dim_y, out_channels=dim_W3, kernel_size=5, stride=2, padding=2, output_padding=1),
nn.BatchNorm2d(dim_W3, eps=1e-8),
nn.ReLU())
self.layer4 = nn.Sequential(nn.ConvTranspose2d(in_channels=dim_W3 + dim_y, out_channels=dim_channel, kernel_size=5, stride=2, padding=2, output_padding=1),
nn.BatchNorm2d(dim_channel, eps=1e-8))
def forward(self, z, y):
yb = torch.reshape(y, (self.batch_size, self.dim_y, 1, 1))
z = torch.cat((z, y), 1)
h1 = self.layer1(z)
h1 = torch.cat((h1, y), 1)
h2 = self.layer2(h1)
h2 = torch.reshape(h2, (self.batch_size, self.dim_W2, 6, 6))
h2 = torch.cat((h2, torch.mul(yb, torch.ones((self.batch_size, self.dim_y, 6, 6), dtype=torch.float32, device=self.device))), 1)
n = torch.mul(yb, torch.ones((self.batch_size, self.dim_y, 6, 6), dtype=torch.float32, device=self.device))
h3 = self.layer3(h2)
h3 = torch.cat((h3, torch.mul(yb, torch.ones((self.batch_size, self.dim_y, 12, 12), dtype=torch.float32, device=self.device))), 1)
h4 = self.layer4(h3)
return h4
class Discriminator(nn.Module):
def __init__(self, batch_size, dim_y, dim_z, dim_W1, dim_W2, dim_W3, dim_channel, device):
super().__init__()
self.batch_size = batch_size
self.dim_y = dim_y
self.dim_z = dim_z
self.dim_W1 = dim_W1
self.dim_W2 = dim_W2
self.dim_W3 = dim_W3
self.dim_channel = dim_channel
self.device = device
self.layer1 = nn.Sequential(nn.Conv2d(in_channels=dim_channel + dim_y, out_channels=dim_W3, kernel_size=5, stride=2, padding=2),
nn.LeakyReLU(negative_slope=0.2, inplace=True))
self.layer2 = nn.Sequential(nn.Conv2d(in_channels=dim_W3 + dim_y, out_channels=dim_W2, kernel_size=5, stride=2, padding=2),
nn.BatchNorm2d(dim_W2, eps=1e-8),
nn.LeakyReLU(negative_slope=0.2, inplace=True))
self.layer3 = nn.Sequential(nn.Linear(dim_W2*6*6 + dim_y, dim_W1, bias=False),
nn.BatchNorm1d(dim_W1, eps=1e-8),
nn.LeakyReLU(negative_slope=0.2, inplace=True))
def forward(self, image, y):
yb = torch.reshape(y, (self.batch_size, self.dim_y, 1, 1))
x = torch.cat((image, torch.mul(yb, torch.ones((self.batch_size, self.dim_y, 24, 24), dtype=torch.float32, device=self.device))), 1)
h1 = self.layer1(x)
h1 = torch.cat((h1, torch.mul(yb, torch.ones((self.batch_size, self.dim_y, 12, 12), dtype=torch.float32, device=self.device))), 1)
h2 = self.layer2(h1)
h2 = torch.reshape(h2, (self.batch_size, -1))
h2 = torch.cat((h2, y), 1)
h3 = self.layer3(h2)
return h3
#Define loss functions
def generator_cost(raw_gen2):
return -torch.mean(raw_gen2)
def discriminator_cost(raw_real2, raw_gen2):
return torch.sum(raw_gen2) - torch.sum(raw_real2)
#Define GAN architecture
class GAN(nn.Module):
def __init__(self, epochs=10, batch_size=32, image_shape=[1, 24, 24], dim_y=6, dim_z=100, dim_W1=1024, dim_W2=128, dim_W3=64, dim_channel=1, learning_rate=1e-4, device='cpu'):
super().__init__()
self.epochs = epochs
self.batch_size = batch_size
self.image_shape = image_shape
self.dim_y = dim_y
self.dim_z = dim_z
self.dim_W1 = dim_W1
self.dim_W2 = dim_W2
self.dim_W3 = dim_W3
self.dim_channel = dim_channel
self.learning_rate = learning_rate
self.device = device
self.normal = (0, 0.1)
self.generator = Generator(batch_size, dim_y, dim_z, dim_W1, dim_W2, dim_W3, dim_channel, device=device)
self.discriminator = Discriminator(batch_size, dim_y, dim_z, dim_W1, dim_W2, dim_W3, dim_channel, device=device)
self.optimizer_g = torch.optim.RMSprop(list(self.generator.parameters()), lr=learning_rate)
self.optimizer_d = torch.optim.RMSprop(list(self.discriminator.parameters()), lr=learning_rate)
#Initialization of weights
for weights in chain(self.generator.parameters(), self.discriminator.parameters()):
torch.nn.init.normal_(weights, mean=0.0, std=0.02)
self.fitting_time = None
def fit(self, x, y):
self.fitting_time = time.time()
iterations = 0
#Control balance of training discriminator vs generator
k = 4
gen_loss_all = []
p_real = []
p_fake = []
p_distri = []
discrim_loss = []
for epoch in range(self.epochs):
if (epoch + 1) % (0.1*self.epochs) == 0:
print('Epoch:', epoch + 1)
index = np.arange(len(y))
np.random.shuffle(index)
x = x[index]
y = y[index]
y2 = OneHot(y, n=self.dim_y)
for start, end in zip(range(0, len(y), self.batch_size), range(self.batch_size, len(y), self.batch_size)):
xs = x [start:end].reshape([-1, 1, 24, 24])
ys = y2[start:end]
zs = np.random.normal(self.normal[0], self.normal[1], size=(self.batch_size, self.dim_z)).astype(np.float32)
xs = tensor(xs, dtype=torch.float32, device=self.device)
ys = tensor(ys, dtype=torch.float32, device=self.device)
zs = tensor(zs, dtype=torch.float32, device=self.device)
self.train()
if iterations % k == 0:
self.optimizer_g.zero_grad()
h4 = self.generator(zs, ys)
image_gen = nn.Sigmoid()(h4)
raw_gen2 = self.discriminator(image_gen, ys)
p_gen_val = torch.mean(raw_gen2)
gen_loss_val = generator_cost(raw_gen2)
raw_real2 = self.discriminator(xs, ys)
p_real_val = torch.mean(raw_real2)
discrim_loss_val = discriminator_cost(raw_real2, raw_gen2)
gen_loss_val.backward()
self.optimizer_g.step()
else:
self.optimizer_d.zero_grad()
h4 = self.generator(zs, ys)
image_gen = nn.Sigmoid()(h4)
raw_gen2 = self.discriminator(image_gen, ys)
p_gen_val = torch.mean(raw_gen2)
gen_loss_val = generator_cost(raw_gen2)
raw_real2 = self.discriminator(xs, ys)
p_real_val = torch.mean(raw_real2)
discrim_loss_val = discriminator_cost(raw_real2, raw_gen2)
discrim_loss_val.backward()
self.optimizer_d.step()
p_real.append(p_real_val.cpu().item())
p_fake.append(p_gen_val.cpu().item())
discrim_loss.append(discrim_loss_val.cpu().item())
if iterations % 1000 == 0:
print('Iterations',
iterations,
'| Average P(real):', f'{p_real_val.cpu().item():12.9f}',
'| Average P(fake):', f'{p_gen_val.cpu().item():12.9f}',
'| Discriminator loss:', f'{discrim_loss_val.cpu().item():12.9f}')
# self.eval()
# with torch.no_grad():
# y_np_sample = OneHot(np.random.randint(events_num, size=[self.batch_size]), n=events_num)
# z_np_sample = np.random.normal(self.normal[0], self.normal[1], size=[self.batch_size, self.dim_z]).astype(np.float32)
# y_np_sample = tensor(y_np_sample, dtype=torch.float32, device=device)
# z_np_sample = tensor(z_np_sample, dtype=torch.float32, device=device)
# generated_samples = nn.Sigmoid()(self.generator(z_np_sample, y_np_sample))
# generated_samples = generated_samples.reshape([-1, 576])
# generated_samples = generated_samples * max_value
# with open(data_saving_path + '%s.csv' %iterations, 'w') as csvfile:
# # spamwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
# writer = csv.writer(csvfile)
# writer.writerows(generated_samples.cpu().detach().numpy())
# csvfile.close()
iterations += 1
self.fitting_time = np.round(time.time() - self.fitting_time, 3)
print('\nElapsed Training Time: ' + time.strftime('%Hh %Mmin %Ss', time.gmtime(self.fitting_time)))
# print('P_real', p_real)
# print('P_fake', p_fake)
#Plotting
fig, ax = plt.subplots()
ax.plot(p_real, label='real')
ax.plot(p_fake, label='fake')
ax.legend()
ax.set_xlim(0, len(p_real))
ax.set_xlabel('Iteration')
ax.set_ylabel('Wasserstein Distance')
ax.grid(True)
fig.show()
fig, ax = plt.subplots()
ax.plot(discrim_loss)
ax.set_xlim(0, len(discrim_loss))
ax.set_xlabel('Iteration')
ax.set_ylabel('Discriminator Loss')
ax.grid(True)
fig.show()
def predict(self):
self.eval()
with torch.no_grad():
y_np_sample = OneHot(np.random.randint(self.dim_y, size=[self.batch_size]), n=self.dim_y)
zs = np.random.normal(self.normal[0], self.normal[1], size=[self.batch_size, self.dim_z]).astype(np.float32)
y_np_sample = tensor(y_np_sample, dtype=torch.int8, device=self.device)
zs = tensor(zs, dtype=torch.float32, device=self.device)
generated_samples = nn.Sigmoid()(self.generator(zs, y_np_sample))
#Image shape 24x24 = 576
generated_samples = generated_samples.reshape([-1, 576])
return generated_samples.cpu().detach().numpy(), y_np_sample.cpu().detach().numpy()
# with open(f'{path_data}' if path_data.endswith('csv') else f'{path_data}.csv', 'w') as csvfile:
# writer = csv.writer(csvfile)
# writer.writerows(generated_samples.cpu().detach().numpy())
# csvfile.close()
# with open(f'{path_labels}' if path_labels.endswith('csv') else f'{path_labels}.csv', 'w') as csvfile:
# writer = csv.writer(csvfile)
# writer.writerows(y_np_sample.cpu().detach().numpy())
# csvfile.close()