-
Notifications
You must be signed in to change notification settings - Fork 14
/
pre-training.py
153 lines (118 loc) · 4.28 KB
/
pre-training.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
import pdb
import torch
import numpy as np
import time
import random
import myDataset
import Model
import matplotlib.pyplot as plt
from torch.autograd import Variable
import torch.nn as nn
from tqdm import tqdm
from torch import optim
tqdm.monitor_interval = 0
# Some Global Variables
# Training Parameters
learning_rate = 0.001
batch_size = 32
Mode = 0 # 0:Encoder-Linear 1:Encoder-Decoder
# Network Parameters
time_steps = 45
#output_time_steps = 15
input_size = 2 #100byte of payload + length + direction + timestamp
output_size = 24
hidden_dim = 10 #number of features in hidden layer
num_epoches = 250
num_layers = 1 #number of hidden layer
display_step = 5
teacher_forcing_ratio = 0.5
unsuper_trainData = np.load("pretraining_trainData.npy")
unsuper_trainLabel = np.load("StatLabel.npy")
#pdb.set_trace()
sample_size = unsuper_trainData.shape[0]
perm = np.arange(sample_size)
#np.random.shuffle(perm)
#unsuper_trainData = unsuper_trainData[perm]
#unsuper_trainLabel = unsuper_trainLabel[perm]
testStart = int(sample_size * 0.96)
testData = unsuper_trainData[testStart:]
testLabel = unsuper_trainLabel[testStart:]
unsuper_trainData = unsuper_trainData[:int(testStart/1)]
unsuper_trainLabel = unsuper_trainLabel[:int(testStart/1)]
train_dataset = myDataset.unsuper_Dataset(unsuper_trainData,
unsuper_trainLabel)
test_dataset = myDataset.unsuper_Dataset(testData, testLabel)
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=batch_size, shuffle=True, drop_last=True)
#test_loader = torch.utils.data.DataLoader(
# test_dataset, batch_size=batch_size, shuffle=False, drop_last=True)
print("Dataset loaded!")
if Mode == 0:
use_gpu = True # GPU enable
encoder = Model.CNNEncoder(hidden_dim, output_size)
if use_gpu:
encoder = encoder.cuda()
#decoder = decoder.cuda()
start = time.time()
plot_losses = []
print_loss_total = 0 # Reset every print_every
plot_loss_total = 0 # Reset every plot_every
loss_function = nn.MSELoss()
# loss_function = nn.KLDivLoss()
#optimizer = optim.RMSprop(autoencoder.parameters(), lr = learning_rate, weight_decay = 0.005)
optimizer = optim.Adam(encoder.parameters(), lr=learning_rate)#, weight_decay = 0.005)
train_loss = []
# sample = torch.randn(1,2,time_steps)
# sample = Variable(sample).float()
# sample = sample.cuda()
# print(sample)
isFirst = 1
for epoch in range(num_epoches):
running_loss = 0.0
avg_loss = 0.0
for i, data in enumerate(train_loader, 1):
seq, target = data
#target = target[:,1,:]
#target = target[:,1,:]
#target = seq[:,1,15:30]
#target = torch.ones(target.shape[0],1) / 1 * 1
#pdb.set_trace()
seq = Variable(seq).float()
target = Variable(target).float()
if use_gpu:
seq = seq.cuda()
target = target.cuda()
if isFirst == 1:
sample = seq[:6,:]
sampleTarget = target[:6,:]
# pdb.set_trace()
# sample = seq[:6,:,:]
# sampleTarget = target[:6,:]
ifFirst = 0
# pdb.set_trace()
#print(encoder.enc_linear_1.weight.data)
out = encoder(seq)
loss = loss_function(out, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
running_loss += loss.item() * target.size(0)
#pdb.set_trace()
if epoch % display_step == 0:
avg_loss = running_loss / (batch_size * i)
print('[{}/{}] Loss: {:.6f}'.format(epoch + 1, num_epoches, avg_loss))
#pdb.set_trace()
train_loss.append(avg_loss)
eval_loss = 0.
eval_acc = 0.
torch.save(encoder, 'simple-45.pth')
torch.save(encoder, 'random-45.pth')
torch.save(encoder, 'incremental-45.pth')
print("Optimization Done!")
t = np.arange(len(train_loss))
plt.xlabel("Epochs")
plt.ylabel("MSE Loss")
plt.plot(t, train_loss, color="red", linewidth=2.5,
linestyle="-", label="Unsupervised Loss")
plt.legend(loc='upper right')
plt.show()