-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearn_MMmK.py
413 lines (317 loc) · 15.1 KB
/
learn_MMmK.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
'''
Created on Oct 27, 2018
@author: mohame11
'''
import pandas as pd
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.optim
import random
import copy
import sys
import math
class MMmK_pytorch(nn.Module):
def __init__(self, lr, gradientClip = 10.0, gradientClipType = 'inf', optim = 'SGD'):
super(MMmK_pytorch, self).__init__()
#K has to be greater than m for the queueing eqn to work (K>m)
self.default_m = 1.0
self.default_K = 5.0
self.default_mu = 5.0
self.m_min, self.m_max = 1.0, 1e10
self.K_min, self.K_max = 5.0, 1e10
self.mu_min, self.mu_max = 5.0, 1e10
self.clampMin = -1e37
self.clampMax = 1e37
self.m = nn.Parameter(torch.FloatTensor([self.default_m]),requires_grad = True)
self.mu = nn.Parameter(torch.FloatTensor([self.default_mu]),requires_grad = True)
self.K = nn.Parameter(torch.FloatTensor([self.default_K]),requires_grad = True)
self.loss = nn.MSELoss(size_average = True)
self.lr = lr
self.gradientClip = gradientClip
self.gradientClipType = gradientClipType
self.optim = optim
self.optimiser = getattr(torch.optim, self.optim)(self.parameters(), lr = self.lr)
#self.optimiser = torch.optim.SGD(self.parameters(), self.lr)
paramsCount=0
for p in list(self.parameters()):
if len(p.shape) == 1:
paramsCount += p.shape[0]
else:
paramsCount += p.shape[0] * p.shape[1]
print '**************************************************'
print 'Number of parameters in the model = ',paramsCount
print '**************************************************'
def toStr(self):
s = 'm0=%.1f_K0=%.1f_mu0=%.1f_4cores'%(self.default_m, self.default_K, self.default_mu)
return s
def paramClip(self):
#if self.m.item() <= 0:
self.m.data.clamp_(min=self.m_min, max=self.m_max)
#self.m = nn.Parameter(torch.FloatTensor([self.default_m]),requires_grad = True)
#if self.K.item() <= 0:
self.K.data.clamp_(min=self.K_min, max=self.K_max)
#self.K = nn.Parameter(torch.FloatTensor([self.default_K]),requires_grad = True)
#if self.mu.item() <= 0:
self.mu.data.clamp_(min=self.mu_min, max=self.mu_max)
#self.mu = nn.Parameter(torch.FloatTensor([self.default_K_mu]),requires_grad = True)
def forwardPass_new(self, inp):
rho = torch.div(inp, self.mu)
#x = inp / self.mu
c1 = self.K * torch.log(rho)
c2 = torch.lgamma(self.m + 1)
c3 = (self.K - self.m) * torch.log(self.m)
logC_K = c1 - c2 - c3
part2 = torch.zeros(rho.size())
for n in range(1,self.m):
f1 = torch.pow(rho , n)
f1 = torch.clamp(f1, min=self.min, max=self.max)
f2 = torch.exp(torch.lgamma(torch.FloatTensor([n+1])))
f2 = torch.clamp(f2, min=self.min, max=self.max)
f = f1 / f2
f = torch.clamp(f, min=self.min, max=self.max)
part2 += f
part2 = torch.clamp(part2, min=self.min, max=self.max)
part2 = torch.clamp(part2, min=self.min, max=self.max)
'''
part3 = torch.zeros(rho.size())
for n in range(self.m, self.K+1):
part3 += torch.exp( (n * torch.log(rho)) - ((n-self.m) * torch.log(self.m)) )
part3 = part3 / (torch.exp(torch.lgamma(self.m+1)))
'''
part3 = torch.exp((self.K+1) * torch.log(rho))
part3 = torch.clamp(part3, min=self.min, max=self.max)
p = torch.exp((self.m - self.K) * torch.log(self.m))
p = torch.clamp(p, min=self.min, max=self.max)
part3 = part3 * p
part3 = torch.clamp(part3, min=self.min, max=self.max)
p = (self.m * torch.exp(self.m * torch.log(rho)))
p = torch.clamp(p, min=self.min, max=self.max)
part3 = part3 - p
part3 = torch.clamp(part3, min=self.min, max=self.max)
p = (self.m - rho)
p = torch.clamp(p, min=self.min, max=self.max)
part3 = part3 / p
part3 = torch.clamp(part3, min=self.min, max=self.max)
p = (torch.exp(torch.lgamma(self.m+1)))
p = torch.clamp(p, min=self.min, max=self.max)
part3 = part3 / p
part3 = torch.clamp(part3, min=self.min, max=self.max)
part3 = part3 * (-1)
part3 = torch.clamp(part3, min=self.min, max=self.max)
#part3a = (self.m-self.K)*torch.log(self.m) + (self.K+1) * torch.log(rho) - torch.log(rho - self.m) - torch.lgamma(self.m+1)
#part3b = torch.log(self.m) + (self.m * torch.log(rho)) - torch.log(self.m-rho) - torch.lgamma(self.m+1)
#part3 = torch.exp(part3a) + torch.exp(part3b)
p = 1.0 + part2 + part3
p = torch.clamp(p, min=1.0/1e25, max=self.max)
logP0 = -1 * torch.log(p)
logP_K = logC_K + logP0
return logP_K
def forwardPass(self, inp, clampNumbers = False):
#cmin = self.clampMin
#cmax = self.clampMax
cmin = -1e35
cmax = 1e35
rho = torch.div(inp, self.mu)
#x = inp / self.mu
c1 = self.K * torch.log(rho)
c2 = torch.lgamma(self.m + 1)
c3 = (self.K - self.m) * torch.log(self.m)
logC_K = c1 - c2 - c3
part2 = torch.zeros(rho.size())
for n in range(1,self.m):
if clampNumbers:
n1 = torch.pow(rho , n)
n1 = torch.clamp(n1, min=cmin, max=cmax)
n2 = torch.exp(torch.lgamma(torch.FloatTensor([n+1])))
n2 = torch.clamp(n2, min=cmin, max=cmax)
part2 = part2 + (n1/n2)
part2 = torch.clamp(part2, min = cmin, max = cmax)
else:
part2 = part2 + torch.pow(rho , n) / torch.exp(torch.lgamma(torch.FloatTensor([n+1])))
'''
part3 = torch.zeros(rho.size())
for n in range(self.m, self.K+1):
part3 += torch.clamp ( torch.exp( (n * torch.log(rho)) - ((n-self.m) * torch.log(self.m)) ) , min = self.clampMin, max = self.clampMax)
part3 = torch.clamp(part3, min = self.clampMin, max = self.clampMax)
part3 = part3 / (torch.exp(torch.lgamma(self.m+1)))
'''
part3 = torch.zeros(rho.size())
for n in range(self.m, self.K+1):
if clampNumbers:
a = torch.exp( (n * torch.log(rho)) - ((n-self.m) * torch.log(self.m)) )
a = torch.clamp(a, min = cmin, max = cmax)
part3 = part3 + a
part3 = torch.clamp(part3, min = cmin, max = cmax)
else:
part3 = part3 + torch.exp( (n * torch.log(rho)) - ((n-self.m) * torch.log(self.m)) )
if clampNumbers:
part3 = part3 / (torch.exp(torch.lgamma(self.m+1)))
part3 = torch.clamp(part3, min = cmin, max = cmax)
else:
part3 = part3 / (torch.exp(torch.lgamma(self.m+1)))
'''
#default way of cal part3 (it has a mistake of forgetting to divide by m! in the end
part3 = torch.zeros(rho.size())
for n in range(self.m, self.K+1):
part3 += torch.pow(rho , n) / torch.pow(self.m,(n - self.m))
'''
logP0 = -1 * torch.log(1.0 + part2 + part3)
logP_K = logC_K + logP0
#logP_K = torch.clamp(logP_K, min = self.clampMin, max = math.log(self.clampMax))
return logP_K
def predict(self, inp, clampNumbers):
logPK = self.forwardPass(torch.FloatTensor([inp]), clampNumbers)
if clampNumbers:
PK = torch.exp(logPK)
PK = torch.clamp(PK, min = 0.0, max = 1.0)
else:
PK = torch.exp(logPK)
return PK.item()
def parseDataFile(fpath, inputPacketsCols, droppedPacketsCols):
df = pd.read_csv(fpath, usecols = inputPacketsCols+droppedPacketsCols)
df.fillna(0, inplace=True) # replace missing values (NaN) to zero9
return df
def getTrainingData(dir, summaryFile, minDropRate, maxDropRate):
sfile = dir+summaryFile
inputPacketsCols = ['CallRate(P)']
droppedPacketsCols = ['FailedCall(P)']
df = pd.read_csv(sfile, usecols = ['Rate File', ' Failed Calls'])
df.fillna(0, inplace=True)
train_X = []
train_Y = []
for i, row in df.iterrows():
if row[' Failed Calls'] < minDropRate or row[' Failed Calls'] > maxDropRate:
continue
fname = 'sipp_data_' + row['Rate File'] + '_1.csv'
simulationFile = dir + fname #sipp_data_UFF_Perdue_01_1_reduced_1.csv UFF_Perdue_01_12_reduced
curr_df = pd.read_csv(simulationFile, usecols = inputPacketsCols+droppedPacketsCols)
curr_df.fillna(0, inplace=True) # replace missing values (NaN) to zero9
for j, curr_row in curr_df.iterrows():
try:
the_lambda = float(curr_row['CallRate(P)'])
failed = float(curr_row['FailedCall(P)'])
if failed > the_lambda:
continue
PK = failed/the_lambda
except:
continue
train_X.append(Variable(torch.FloatTensor([the_lambda])))
train_Y.append(Variable(torch.FloatTensor([PK])))
return train_X, train_Y
def evaluate(MMmK, data_X, data_Y):
MMmK.eval()
totalLoss = 0
for i in range(len(data_X)):
logPK = MMmK.forwardPass(data_X[i])
PK = torch.exp(logPK)
#PK = torch.clamp(PK, min=0, max=1.0)
loss = MMmK.loss(PK, data_Y[i])
totalLoss += loss.item()
return totalLoss
def train(MMmK, train_X, train_Y, valid_X, valid_Y, test_X, test_Y, epochs, batchsize, annelingAt = 20, shuffleData = False, showBatches = False):
MMmK.train()
train_X = Variable(torch.FloatTensor(train_X))
train_Y = Variable(torch.FloatTensor(train_Y))
valid_X = Variable(torch.FloatTensor(valid_X))
valid_Y = Variable(torch.FloatTensor(valid_Y))
test_X = Variable(torch.FloatTensor(test_X))
test_Y = Variable(torch.FloatTensor(test_Y))
prevValidLoss = 1e100
bestValidLoss = 1e100
test_bestValidLoss = 1e100
train_bestValidLoss = 1e100
bestModel = None
numOfBatchs = train_X.size(0) // batchsize
for e in range(epochs):
totalLoss = 0
#shuffle training data
if shuffleData:
rows = len(train_X)
idxs = list(range(0,rows))
random.shuffle(idxs)
idxs = torch.LongTensor(idxs)
train_X = train_X[idxs]
train_Y = train_Y[idxs]
for b in range(numOfBatchs):
if b == 821:
dbg = 1
batch_X = train_X[b:b+batchsize]
batch_Y = train_Y[b:b+batchsize]
logPK = MMmK.forwardPass(batch_X)
PK = torch.exp(logPK)
#PK = torch.clamp(PK, min=0, max = 1.0)
loss = MMmK.loss(PK, batch_Y)
loss.backward()
totalLoss += loss.item()*batchsize
nn.utils.clip_grad_norm_(MMmK.parameters(), MMmK.gradientClip , norm_type = MMmK.gradientClipType)
MMmK.optimiser.step()
MMmK.optimiser.zero_grad()
MMmK.paramClip()
if showBatches:
print 'Batch=',b, ' MSE loss = ', loss.item(), 'lr=',MMmK.lr
print 'm=',MMmK.m.item(), 'K=',MMmK.K.item(), 'mu=',MMmK.mu.item()
print
#trainLoss = evaluate(MMmK, train_X, train_Y)
validLoss = evaluate(MMmK, valid_X, valid_Y) / float(len(valid_X))
if len(test_X) != 0:
testLoss = evaluate(MMmK, test_X, test_Y) / float(len(test_X))
else:
testLoss = -1
trainLoss = totalLoss / float(len(train_X))
print 'Epoch',e,'--------------------------------------'
print 'Total Train MSE loss = ', trainLoss, 'lr=', MMmK.lr
print 'Total Valid MSE loss = ', validLoss
print 'Total Test MSE loss = ', testLoss
print 'm=',MMmK.m.item(), 'K=',MMmK.K.item(), 'mu=',MMmK.mu.item()
print '--------------------------------------'
#if validLoss > prevValidLoss:
if MMmK.optim == 'SGD' and e % annelingAt == 0 and e != 0:
prevValidLoss = validLoss
MMmK.lr /= 2.0
for param_group in MMmK.optimiser.param_groups:
param_group['lr'] = MMmK.lr
prevValidLoss = validLoss
if (validLoss < bestValidLoss) or (validLoss == bestValidLoss and trainLoss < train_bestValidLoss) :
bestValidLoss = validLoss
bestModel = copy.deepcopy(MMmK)
test_bestValidLoss = testLoss
train_bestValidLoss = trainLoss
#params = '_m=%0.3f_K=%0.3f_mu=%0.3f'%(bestModel.m.item(), bestModel.K.item(), bestModel.mu.item())
torch.save(bestModel, 'MMmK_model_'+MMmK.toStr())
print '\t', 'train loss=',train_bestValidLoss, 'best valid=',bestValidLoss, 'test loss=', test_bestValidLoss, ' m=',bestModel.m.item(), 'K=',bestModel.K.item(), 'mu=',bestModel.mu.item()
torch.save(bestModel, 'MMmK_model')
def main():
#dir = '/Users/mohame11/Documents/myFiles/Career/Work/Purdue/PhD_courses/projects/queueing/results_24_2018.10.20-13.31.38_client_server/sipp_results/'
dir = '/Users/mohame11/Documents/myFiles/Career/Work/Purdue/PhD_courses/projects/queueing/results_CORES_4_K_DEFT_SCALE_86_2018.10.29-22.19.41/sipp_results/'
#dir = '/Users/mohame11/Documents/myFiles/Career/Work/Purdue/PhD_courses/projects/queueing/results_CORES_2_K_100000_SCALE_43_2018.11.03-13.38.21/sipp_results/'
summaryFile = 'summary_data_dump.csv'
random.seed(1111)
trainQuota = 0.85
validQuota = 0.15
data_X, data_Y = getTrainingData(dir, summaryFile, minDropRate=1, maxDropRate=1e10)
#shuffle data
combined = list(zip(data_X, data_Y))
random.shuffle(combined)
data_X[:], data_Y[:] = zip(*combined)
trainLen = int(trainQuota*len(data_X))
validLen = int(validQuota*len(data_X))
print 'trainLen=', trainLen, 'validLen=', validLen
train_X = data_X[:trainLen]
train_Y = data_Y[:trainLen]
valid_X = data_X[trainLen:trainLen+validLen]
valid_Y = data_Y[trainLen:trainLen+validLen]
test_X = data_X[trainLen+validLen:]
test_Y = data_Y[trainLen+validLen:]
MMmK = MMmK_pytorch(lr=0.01, gradientClip = 0.25, gradientClipType = 2, optim = 'Adam')
train(MMmK,
train_X, train_Y,
valid_X, valid_Y,
test_X, test_Y,
epochs = 10000, batchsize = 32, annelingAt = 500,
shuffleData = True,
showBatches = False
)
if __name__ == "__main__":
main()
print('DONE!')