-
Notifications
You must be signed in to change notification settings - Fork 0
/
MLPVAE.py
308 lines (261 loc) · 10.1 KB
/
MLPVAE.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import torch
import torchvision
import torchvision.transforms as transforms
from torch.utils.data.sampler import SubsetRandomSampler
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import re
from torch.utils.data import Dataset
import glob
import os
import argparse
import random
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.utils.data
from torchvision.utils import save_image
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
# Import functions
import Metrics
import CustomDataset
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print(f'Is CUDA supported by this system?{torch.cuda.is_available()}')
print(f"CUDA version: {torch.version.cuda}")
# Storing ID of current CUDA device
cuda_id = torch.cuda.current_device()
print(f"ID of current CUDA device:{torch.cuda.current_device()}")
print(f"Name of current CUDA device:{torch.cuda.get_device_name(cuda_id)}")
# example getting the number of cpu cores
from multiprocessing import cpu_count
# get the number of logical cpu cores
n_cores = cpu_count()
# report the number of logical cpu cores
print(f'Number of Logical CPU cores: {n_cores}')
partname = "xc7v585tffg1157-3"
benchmark_names = ["syrk", "syr2k", "mvt", "k3mm", "k2mm","gesummv", "gemm", "bicg", "atax"]
i = 0
for benchmark_name in benchmark_names:
print(benchmark_name)
if i == 0 :
files = glob.glob("./20_polybench/" + benchmark_name + "/" + partname + "/*")
i += 1
else:
files += glob.glob("./20_polybench/" + benchmark_name + "/" + partname + "/*")
print("Total number of files: ", len(files))
print("Showing first 10 files...")
files[:10]
transform = transforms.Compose([transforms.ToTensor()])
# Set a seed for same validation set
random_seed = 42
torch.manual_seed(random_seed)
# Number of subprocesses to use for data loading
num_workers = 4
# Samples per batch
batch_size = 20
# Number of training epochs
num_epochs = 150
# Learning rate for optimizers
lr = 0.0001
# Number of GPUs available. Use 0 for CPU mode.
ngpu = 1
# Percentage of training set to use as validation
valid_size = 0.1
# latent space feature size
features = 16
# input binary bit size
bit_size = 32
temp = pd.read_csv(open(files[0],'r'))
number_of_variable = int(len(temp) / bit_size) # calculate number of variables may include directives
num_train = len(input_dataset)
indices = list(range(num_train))
np.random.seed(random_seed)
np.random.shuffle(indices)
split = int(np.floor(valid_size * num_train))
train_idx, valid_idx = indices[split:], indices[:split]
# Define samplers for obtaining training and validation batches
train_sampler = SubsetRandomSampler(train_idx)
valid_sampler = SubsetRandomSampler(valid_idx)
#custom dataset from all filenames
input_dataset = CustomDataset(filenames = files, batch_size = batch_size, bit_size = bit_size)
dataloader = torch.utils.data.DataLoader(input_dataset,batch_size = None, shuffle = True)
input_dataset_all = CustomDataset(filenames = files, batch_size = 1, bit_size = bit_size)
input_dataset_all_in_one = CustomDataset(filenames = files, batch_size = len(files), bit_size = bit_size)
dataloader_all = torch.utils.data.DataLoader(input_dataset_all,batch_size = None, shuffle = True)
dataloader_all_in_one = torch.utils.data.DataLoader(input_dataset_all_in_one,batch_size = None, shuffle = True)
# Define MLP VAE
class LinearVAE(nn.Module):
def __init__(self):
super(LinearVAE, self).__init__()
# encoder
self.enc1 = nn.Linear(in_features=number_of_variable*bit_size, out_features=256)
self.enc2 = nn.Linear(in_features=256, out_features=features*2)
# decoder
self.dec1 = nn.Linear(in_features=features, out_features=256)
self.dec2 = nn.Linear(in_features=256, out_features=number_of_variable*bit_size)
def reparameterize(self, mu, log_var):
"""
:param mu: mean from the encoder's latent space
:param log_var: log variance from the encoder's latent space
"""
std = torch.exp(0.5*log_var) # standard deviation
eps = torch.randn_like(std) # `randn_like` as we need the same size
sample = mu + (eps * std) # sampling as if coming from the input space
return sample
def forward(self, x):
# encoding
x = F.relu(self.enc1(x))
x = self.enc2(x).view(-1, 2, features)
# get `mu` and `log_var`
mu = x[:, 0, :] # the first feature values as mean
log_var = x[:, 1, :] # the other feature values as variance
# get the latent vector through reparameterization
z = self.reparameterize(mu, log_var)
# decoding
x = F.relu(self.dec1(z))
reconstruction = torch.sigmoid(self.dec2(x))
return reconstruction, mu, log_var
def final_loss(bce_loss, mu, logvar):
"""
This function will add the reconstruction loss (BCELoss) and the
KL-Divergence.
KL-Divergence = 0.5 * sum(1 + log(sigma^2) - mu^2 - sigma^2)
:param bce_loss: recontruction loss
:param mu: the mean from the latent vector
:param logvar: log variance from the latent vector
"""
BCE = bce_loss
KLD = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
return BCE + KLD
def fit(model, dataloader):
model.train()
running_loss = 0.0
for i, (data) in enumerate(dataloader):
#data, _ = data
data = data.to(device,dtype=torch.float)
data = data.view(data.size(0), -1)
optimizer.zero_grad()
reconstruction, mu, logvar = model(data)
bce_loss = criterion(reconstruction, data)
loss = final_loss(bce_loss, mu, logvar)
running_loss += loss.item()
loss.backward()
optimizer.step()
train_loss = running_loss/len(dataloader.dataset)
return train_loss
def validate(model, dataloader):
model.eval()
running_loss = 0.0
MMD_score = 0.0
fid_score = 0.0
SSD_score = 0.0
PRD_score = 0.0
COSS_score = 0.0
with torch.no_grad():
for i, data in enumerate(dataloader):
#data, _ = data
data = data.to(device,dtype=torch.float)
data = data.view(data.size(0), -1)
reconstruction, mu, logvar = model(data)
bce_loss = criterion(reconstruction, data)
loss = final_loss(bce_loss, mu, logvar)
running_loss += loss.item()
reconstruction = torch.round(reconstruction)
if data.size(0) == batch_size:
MMD_score += Metrics.mmd_rbf(data, reconstruction)
SSD_score += Metrics.SSD(data, reconstruction)
PRD_score += Metrics.PRD(data, reconstruction)
COSS_score += Metrics.COSS(data, reconstruction)
# if data.size(0) == batch_size:
# fid_score += calculate_fretchet(nn.functional.pad(data.view((batch_size,1,18,32)),(0, 0, 7, 7), "constant", 0).repeat(1, 3, 3, 3),
# nn.functional.pad(reconstruction.view((batch_size,1,18,32)),(0, 0, 7, 7), "constant", 0).repeat(1, 3, 3, 3))
# Save the last batch input and output of every epoch
if i == len(dataloader.dataset):
data = torch.round(data)
reconstruction = torch.round(reconstruction)
b_size = data.size(0)
num_rows = 8
both = torch.cat((data.view(b_size, 1, 20, 32)[:8],
reconstruction.view(b_size, 1, 20, 32)[:8]))
save_image(both.cpu(), f"./outputs/VAE_part1_20_output{epoch}.png", nrow=num_rows)
val_loss = running_loss/len(dataloader.dataset)
MDD = MMD_score.cpu().numpy()/(len(dataloader.dataset)-1)
# FID = fid_score/(len(dataloader.dataset)-1)
SSDscore = torch.sum(SSD_score/(len(dataloader.dataset)-1)).cpu().numpy()/batch_size
PRDscore = torch.sum(PRD_score/(len(dataloader.dataset)-1)).cpu().numpy()/batch_size
COSSscore = torch.sum(COSS_score/(len(dataloader.dataset)-1)).cpu().numpy()/batch_size
return val_loss, MDD, SSDscore, PRDscore, COSSscore
model = LinearVAE().to(device)
print(model)
optimizer = optim.Adam(model.parameters(), lr=lr)
criterion = nn.BCELoss(reduction='sum')
# Train
train_loss = []
val_loss = []
MMD_score = []
FID_score = []
SSD_score = []
PRD_score = []
COSS_score = []
for epoch in range(num_epochs):
train_epoch_loss = fit(model, dataloader)
val_epoch_loss, MMD_epoch, SSD_epoch ,PRD_epoch, COSS_epoch= validate(model, dataloader)
train_loss.append(train_epoch_loss)
val_loss.append(val_epoch_loss)
MMD_score.append(MMD_epoch)
# FID_score.append(FID_epoch)
SSD_score.append(SSD_epoch)
PRD_score.append(PRD_epoch)
COSS_score.append(COSS_epoch)
print('[%d/%d] \tLoss_train: %.4f\tLoss_val: %.4f\tMMD: %.4f\tSSD: %.4f \tPRD: %.4f \tCOSS: %.4f '
% (epoch, num_epochs, train_epoch_loss, val_epoch_loss, MMD_epoch,SSD_epoch,PRD_epoch,COSS_epoch))
# Plot and save
df1 = pd.DataFrame(MMD_score)
df2 = pd.DataFrame(SSD_score)
df3 = pd.DataFrame(PRD_score)
df4 = pd.DataFrame(COSS_score)
# concatenate dataframes
df = pd.concat([df1, df2,df3,df4], axis=1)
# save to csv
df.to_csv('VAE_MLP_part1_20_32_metrics.csv', index=False)
torch.save(model.state_dict(), 'VAE_MLP_part1_20_32.pt')
f = plt.figure()
f.add_subplot(1, 2, 1)
plt.plot(train_loss, '-bx')
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.legend(['Train'])
plt.title('Loss vs. epochs')
ax = f.add_subplot(1, 2, 2)
ax.yaxis.tick_right()
plt.plot(val_loss, '-rx')
plt.xlabel("Epoch")
plt.legend(['Valid'])
plt.title('Loss vs. epochs')
plt.show()
plt.plot(MMD_score, '-rx')
plt.xlabel("Epoch")
plt.ylabel("MDD")
plt.legend(['MDD'])
plt.savefig('./Figure/VAE_MLP_part1_20_32_MMD.png')
plt.show()
plt.plot(SSD_score, '-bx')
plt.xlabel("Epoch")
plt.ylabel("SSD")
plt.legend(['SSD'])
plt.savefig('./Figure/VAE_MLP_part1_20_32_SSD.png')
plt.show()
plt.plot(PRD_score, '-gx')
plt.xlabel("Epoch")
plt.ylabel("PRD")
plt.legend(['PRD'])
plt.savefig('./Figure/VAE_MLP_part1_20_32_PRD.png')
plt.show()
plt.plot(COSS_score, '-yx')
plt.xlabel("Epoch")
plt.ylabel("COSS")
plt.legend(['COSS'])
plt.savefig('./Figure/VAE_MLP_part1_20_32_COSS.png')
plt.show()