-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtransformer_delay.py
396 lines (332 loc) · 12.4 KB
/
transformer_delay.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# Orignal author: Siddhant Ray
import argparse
import copy
import json
import math
import os
import pathlib
import random
import time as t
from datetime import datetime
from ipaddress import ip_address
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pytorch_lightning as pl
import torch
import yaml
from pytorch_lightning import loggers as pl_loggers
from pytorch_lightning.callbacks import ProgressBar
from pytorch_lightning.callbacks.early_stopping import EarlyStopping
from sklearn.model_selection import train_test_split
from tensorboard.backend.event_processing.event_accumulator import \
EventAccumulator
from torch import einsum, nn, optim
from torch.nn import functional as F
from torch.utils.data import DataLoader
from tqdm import tqdm
from utils import (PacketDataset, convert_to_relative_timestamp,
get_data_from_csv, ipaddress_to_number,
sliding_window_delay, sliding_window_features,
vectorize_features_to_numpy)
random.seed(0)
np.random.seed(0)
torch.manual_seed(0)
torch.set_default_dtype(torch.float64)
# Hyper parameters from config file
with open("configs/config-transformer.yaml") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
WEIGHTDECAY = float(config["weight_decay"])
LEARNINGRATE = float(config["learning_rate"])
DROPOUT = float(config["dropout"])
NHEAD = int(config["num_heads"])
LAYERS = int(config["num_layers"])
EPOCHS = int(config["epochs"])
BATCHSIZE = int(config["batch_size"])
LINEARSIZE = int(config["linear_size"])
LOSSFUNCTION = nn.MSELoss()
if "loss_function" in config.keys():
if config["loss_function"] == "huber":
LOSSFUNCTION = nn.HuberLoss()
if config["loss_function"] == "smoothl1":
LOSSFUNCTION = nn.SmoothL1Loss()
if config["loss_function"] == "kldiv":
LOSSFUNCTION = nn.KLDivLoss()
# Params for the sliding window on the packet data
SLIDING_WINDOW_START = 0
SLIDING_WINDOW_STEP = 1
SLIDING_WINDOW_SIZE = 10
SAVE_MODEL = False
MAKE_EPOCH_PLOT = True
TEST = True
if torch.cuda.is_available():
NUM_GPUS = torch.cuda.device_count()
print("Number of GPUS: {}".format(NUM_GPUS))
else:
print("ERROR: NO CUDA DEVICE FOUND")
NUM_GPUS = 0
# DO NOT USE (AS OF NOW)
class AbsPosEmb1DAISummer(nn.Module):
"""
Given query q of shape [batch heads tokens dim] we multiply
q by all the flattened absolute differences between tokens.
Learned embedding representations are shared across heads
"""
def __init__(self, tokens, dim_head):
"""
Output: [batch head tokens tokens]
Args:
tokens: elements of the sequence
dim_head: the size of the last dimension of q
"""
super().__init__()
scale = dim_head**-0.5
self.abs_pos_emb = nn.Parameter(torch.randn(tokens, dim_head) * scale)
def forward(self, q):
return einsum("b h i d, j d -> b h i j", q, self.abs_pos_emb)
# DO NOT USE (AS OF NOW)
class PositionalEncoding(nn.Module):
def __init__(self, d_model, dropout=DROPOUT, max_len=5000):
super(PositionalEncoding, self).__init__()
self.dropout = nn.Dropout(p=dropout)
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
div_term = torch.exp(
torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model)
)
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0).transpose(0, 1)
self.register_buffer("pe", pe)
def forward(self, x):
x = x + self.pe[: x.size(0), :]
return self.dropout(x)
# TRANSFOMER CLASS TO PREDICT DELAYS
class BaseTransformer(pl.LightningModule):
def __init__(self, input_size, target_size, loss_function):
super(BaseTransformer, self).__init__()
self.step = [0]
self.warmup_steps = 1000
# create the model with its layers
self.encoder_layer = nn.TransformerEncoderLayer(
d_model=LINEARSIZE, nhead=NHEAD, batch_first=True, dropout=DROPOUT
)
self.decoder_layer = nn.TransformerDecoderLayer(
d_model=LINEARSIZE, nhead=NHEAD, batch_first=True, dropout=DROPOUT
)
self.encoder = nn.TransformerEncoder(self.encoder_layer, num_layers=LAYERS)
self.decoder = nn.TransformerDecoder(self.decoder_layer, num_layers=LAYERS)
self.encoderin = nn.Linear(input_size, LINEARSIZE)
self.decoderin = nn.Linear(target_size, LINEARSIZE)
self.decoderpred = nn.Linear(LINEARSIZE, target_size)
self.model = nn.ModuleList(
[
self.encoder,
self.decoder,
self.encoderin,
self.decoderin,
self.decoderpred,
]
)
self.loss_func = loss_function
parameters = {
"WEIGHTDECAY": WEIGHTDECAY,
"LEARNINGRATE": LEARNINGRATE,
"EPOCHS": EPOCHS,
"BATCHSIZE": BATCHSIZE,
"LINEARSIZE": LINEARSIZE,
"NHEAD": NHEAD,
"LAYERS": LAYERS,
}
self.df = pd.DataFrame()
self.df["parameters"] = [json.dumps(parameters)]
def configure_optimizers(self):
self.optimizer = optim.Adam(
self.model.parameters(),
betas=(0.9, 0.98),
eps=1e-9,
lr=LEARNINGRATE,
weight_decay=WEIGHTDECAY,
)
return {"optimizer": self.optimizer}
def lr_update(self):
self.step[0] += 1
learning_rate = LINEARSIZE ** (-0.5) * min(
self.step[0] ** (-0.5), self.step[0] * self.warmup_steps ** (-1.5)
)
for param_group in self.optimizer.param_groups:
param_group["lr"] = learning_rate
def forward(self, input, target):
# used for the forward pass of the model
scaled_input = self.encoderin(input.double())
target = self.decoderin(target.double())
enc = self.encoder(scaled_input)
out = self.decoderpred(self.decoder(target, enc))
return out
def training_step(self, train_batch, train_idx):
X, y = train_batch
self.lr_update()
prediction = self.forward(X, y)
loss = self.loss_func(prediction, y)
self.log("Train loss", loss)
return loss
def validation_step(self, val_batch, val_idx):
X, y = val_batch
prediction = self.forward(X, y)
loss = self.loss_func(prediction, y)
self.log("Val loss", loss, sync_dist=True)
return loss
def test_step(self, test_batch, test_idx):
X, y = test_batch
prediction = self.forward(X, y)
loss = self.loss_func(prediction, y)
self.log("Test loss", loss, sync_dist=True)
return loss
def predict_step(self, test_batch, test_idx, dataloader_idx=0):
X, y = test_batch
prediction = self.forward(X, y)
return prediction
def training_epoch_end(self, outputs):
loss_tensor_list = [item["loss"].to("cpu").numpy() for item in outputs]
# print(loss_tensor_list, len(loss_tensor_list))
self.log(
"Avg loss per epoch",
np.mean(np.array(loss_tensor_list)),
on_step=False,
on_epoch=True,
)
def main():
path = "congestion_1/"
files = [
"endtoenddelay500s_1.csv",
"endtoenddelay500s_2.csv",
"endtoenddelay500s_3.csv",
"endtoenddelay500s_4.csv",
"endtoenddelay500s_5.csv",
]
sl_win_start = SLIDING_WINDOW_START
sl_win_size = SLIDING_WINDOW_SIZE
sl_win_shift = SLIDING_WINDOW_STEP
num_features = 15
input_size = sl_win_size * num_features
output_size = sl_win_size
model = BaseTransformer(input_size, output_size, LOSSFUNCTION)
full_feature_arr = []
full_target_arr = []
test_loaders = []
for file in files:
print(os.getcwd())
df = get_data_from_csv(path + file)
df = convert_to_relative_timestamp(df)
df = ipaddress_to_number(df)
feature_df, label_df = vectorize_features_to_numpy(df)
print(feature_df.head(), feature_df.shape)
print(label_df.head())
feature_arr = sliding_window_features(
feature_df.Combined, sl_win_start, sl_win_size, sl_win_shift
)
target_arr = sliding_window_delay(
label_df, sl_win_start, sl_win_size, sl_win_shift
)
print(len(feature_arr), len(target_arr))
full_feature_arr = full_feature_arr + feature_arr
full_target_arr = full_target_arr + target_arr
print(len(full_feature_arr), len(full_target_arr))
full_train_vectors, test_vectors, full_train_labels, test_labels = train_test_split(
full_feature_arr, full_target_arr, test_size=0.05, shuffle=True, random_state=42
)
# print(len(full_train_vectors), len(full_train_labels))
# print(len(test_vectors), len(test_labels))
train_vectors, val_vectors, train_labels, val_labels = train_test_split(
full_train_vectors, full_train_labels, test_size=0.1, shuffle=False
)
# print(len(train_vectors), len(train_labels))
# print(len(val_vectors), len(val_labels))
# print(train_vectors[0].shape[0])
# print(train_labels[0].shape[0])
train_dataset = PacketDataset(train_vectors, train_labels)
val_dataset = PacketDataset(val_vectors, val_labels)
test_dataset = PacketDataset(test_vectors, test_labels)
# print(train_dataset.__getitem__(0))
train_loader = DataLoader(
train_dataset, batch_size=BATCHSIZE, shuffle=True, num_workers=4
)
val_loader = DataLoader(
val_dataset, batch_size=BATCHSIZE, shuffle=False, num_workers=4
)
test_loader = DataLoader(
test_dataset, batch_size=BATCHSIZE, shuffle=False, num_workers=4
)
# print one dataloader item!!!!
train_features, train_lbls = next(iter(train_loader))
print(f"Feature batch shape: {train_features.size()}")
print(f"Labels batch shape: {train_lbls.size()}")
feature = train_features[0]
label = train_lbls[0]
print(f"Feature: {feature}")
print(f"Label: {label}")
val_features, val_lbls = next(iter(val_loader))
print(f"Feature batch shape: {val_features.size()}")
print(f"Labels batch shape: {val_lbls.size()}")
feature = val_features[0]
label = val_lbls[0]
print(f"Feature: {feature}")
print(f"Label: {label}")
test_features, test_lbls = next(iter(test_loader))
print(f"Feature batch shape: {test_features.size()}")
print(f"Labels batch shape: {test_lbls.size()}")
feature = test_features[0]
label = test_lbls[0]
print(f"Feature: {feature}")
print(f"Label: {label}")
print("Started training at:")
time = datetime.now()
print(time)
print("Removing old logs:")
os.system("rm -rf transformer_delay_logs/lightning_logs/")
tb_logger = pl_loggers.TensorBoardLogger(save_dir="transformer_delay_logs/")
if NUM_GPUS >= 1:
trainer = pl.Trainer(
precision=16,
gpus=-1,
strategy="dp",
max_epochs=EPOCHS,
check_val_every_n_epoch=1,
logger=tb_logger,
callbacks=[EarlyStopping(monitor="Val loss", patience=5)],
)
else:
trainer = pl.Trainer(
gpus=None,
max_epochs=EPOCHS,
check_val_every_n_epoch=1,
logger=tb_logger,
callbacks=[EarlyStopping(monitor="Val loss", patience=5)],
)
trainer.fit(model, train_loader, val_loader)
print("Finished training at:")
time = datetime.now()
print(time)
if SAVE_MODEL:
name = config["name"]
torch.save(model.model, f"./trained_transformer_{name}")
if not MAKE_EPOCH_PLOT:
t.sleep(5)
log_dir = "transformer_logs/lightning_logs/version_0"
y_key = "Avg loss per epoch"
event_accumulator = EventAccumulator(log_dir)
event_accumulator.Reload()
steps = {x.step for x in event_accumulator.Scalars("epoch")}
epoch_vals = list({x.value for x in event_accumulator.Scalars("epoch")})
epoch_vals.pop()
x = list(range(len(steps)))
y = [x.value for x in event_accumulator.Scalars(y_key) if x.step in steps]
fig, ax = plt.subplots()
ax.plot(epoch_vals, y)
ax.set_xlabel("epoch")
ax.set_ylabel(y_key)
fig.savefig("lossplot_perepoch.png")
if TEST:
trainer.test(model, dataloaders=test_loader)
if __name__ == "__main__":
main()