-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistributedDualAscentMethodWithMeka.py
More file actions
428 lines (373 loc) · 15.3 KB
/
Copy pathDistributedDualAscentMethodWithMeka.py
File metadata and controls
428 lines (373 loc) · 15.3 KB
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
from pyspark import SparkConf, SparkContext, AccumulatorParam
from pyspark.sql import SparkSession
import math
import numpy as np
from functools import partial
# filename = "a9a_1000/a9a_train_1000_40_with_label.txt"
# sc = SparkContext(master="local",appName="meka")
# print(sc.textFile(filename).first())
conf = SparkConf().setAppName('appName').setMaster('local')
sc = SparkContext(conf=conf)
numOfClusters = 4
totalData = 1000
testrow = 1000
nCols = 40
learnRate = 0.2
C = 1.0
threshold = 0.001
maxIteration = 100
numOfPartialData = int(math.ceil(totalData/numOfClusters))
optStepSize = (math.floor(1.0 / (learnRate * C)) - 0.5) * learnRate
thresholdSpark = 10
trainDataPath = "a9a_1000/a9a_train_1000_40_with_label.txt"
testDataPath = "a9a_1000/a9a_test_1000_40.txt"
testLabelPath = "a9a_1000/a9a_test_Label_1000.txt"
testLabelPositive = 1.0 #Positive label of the data set
testLabelNegative = -1.0 #Negative label of the data set
rowNumberCount = 0
#Accumulator Defination
class VectorAccumulatorParam(AccumulatorParam):
def zero(self, initialValue):
return initialValue
def addInPlace(self, v1, v2):
#v1 += v2
return np.add(v1,v2)
#Accumulator Definition Completes
recordCountInClusters = sc.accumulator(np.zeros((numOfClusters,1)), VectorAccumulatorParam())
Rgatherac = sc.accumulator(np.zeros((numOfClusters*nCols,nCols)), VectorAccumulatorParam())
xAcc = sc.accumulator(np.zeros((totalData, 1)), VectorAccumulatorParam())
alphaList = sc.accumulator(np.zeros((totalData, 1)), VectorAccumulatorParam())
betaList = sc.accumulator(np.zeros((totalData, 1)), VectorAccumulatorParam())
# This part is for HouseHolder QRDecomposition
def column_convertor(x):
"""
Converts 1d array to column vector
"""
x.shape = (1, x.shape[0])
return x
def get_norm(x):
"""
Returns Norm of vector x
"""
return np.sqrt(np.sum(np.square(x)))
def householder_transformation(v):
"""
Returns Householder matrix for vector v
"""
size_of_v = v.shape[1]
e1 = np.zeros_like(v)
e1[0, 0] = 1
vector = get_norm(v) * e1
if v[0, 0] < 0:
vector = - vector
u = (v + vector).astype(np.float32)
norm2 = get_norm(u)
u = u / norm2
H = np.identity(size_of_v) - ((2 * np.matmul(np.transpose(u), u)) / np.matmul(u, np.transpose(u)))
return H, u
def qr_step_factorization(q, r, iter, n):
"""
Return Q and R matrices for iter number of iterations.
"""
v = column_convertor(r[iter:, iter])
Hbar, reflect = householder_transformation(v)
H = np.identity(n)
H[iter:, iter:] = Hbar
r = np.matmul(H, r)
q = np.matmul(q, H)
return q, r,reflect
def QR_Factorization(A,n,m):
Q = np.identity(n)
R = A.astype(np.float32)
Reflectors = np.zeros((n, m))
for i in range(min(n, m)):
# For each iteration, H matrix is calculated for (i+1)th row
Q, R, reflect = qr_step_factorization(Q, R, i, n)
Reflectors[i:n, i] = np.transpose(reflect).ravel()
min_dim = min(m, n)
R = np.around(R, decimals=6)
R = R[:min_dim, :min_dim]
Q = np.around(Q, decimals=6)
return Q,R,Reflectors
#HouseHolder QRDecomposition ends here
def inputFuncTrain(dataLine):
X = np.zeros((1, nCols))
dataLine = dataLine.strip()
splitBySpace = dataLine.split(' ')
for j in range(nCols):
X[0, j] = float(splitBySpace[j])
return X
def inputFuncTest(dataLine):
X = np.zeros((1, nCols))
dataLine = dataLine.strip()
splitByComma = dataLine.split(',')
for j in range(nCols):
X[0, j] = float(splitByComma[j])
return X
def inputFuncTestLabel(dataLine):
X = np.zeros((1, 1))
dataLine = dataLine.strip()
splitBySpace = dataLine.split(' ')
X[0, 0] = float(splitBySpace[0])
return X
def LocalQtX(localReflector, subX):
(rowInCluster,colInCluster) = subX.shape
for k in range(nCols):
value = np.matmul(np.transpose(localReflector[0:rowInCluster,k]),subX[0:rowInCluster,0])
temp = (2*value)*localReflector[0:rowInCluster,k]
subX[0:rowInCluster,0] = (subX[0:rowInCluster,0]-temp).ravel()
return subX[0:rowInCluster,0]
def GlobalQtX(globalReflector, subX):
(rowInReflector,colInReflector) = globalReflector.shape
for k in range(nCols):
value = np.matmul(np.transpose(globalReflector[0:rowInReflector,k]),subX[0:rowInReflector,0])
temp = (2*value)*globalReflector[0:rowInReflector,k]
subX[0:rowInReflector,0] = (subX[0:rowInReflector,0]-temp).ravel()
return subX[0:rowInReflector,0]
def LocalQtXPerCluster(num, list_of_lists,fullX, recordCountInClusters):
partitionedMatrix = np.zeros((numOfPartialData + thresholdSpark, nCols))
final_iterator = [5]
currentRow = 0
for x in list_of_lists:
partitionedMatrix[currentRow, :] = x
currentRow += 1
reflector = partitionedMatrix[0:currentRow, :]
rowInCluster = int(recordCountInClusters[num][0])
previousRows = 0
for x in range(num):
previousRows += int(recordCountInClusters[x][0])
subX = fullX[previousRows:previousRows+rowInCluster,:]
updatedSubX = LocalQtX(reflector,subX)
temp = np.zeros((totalData, 1))
temp[previousRows:previousRows+rowInCluster, 0] = updatedSubX.ravel()
xAcc.add(temp)
return iter(final_iterator)
def Dist_QtX(x, globalReflector, localReflector, recordCountInClusters):
dummy = localReflector.mapPartitionsWithIndex(
partial(LocalQtXPerCluster, fullX=x, recordCountInClusters=recordCountInClusters))
dummy.count()
global xAcc
finalX = xAcc.value
xAcc = sc.accumulator(np.zeros((totalData, 1)), VectorAccumulatorParam())
partialX = np.zeros((numOfClusters*nCols, 1))
fullIterator = 0
partialIterator = 0
for k in range(numOfClusters):
temp = finalX[fullIterator:fullIterator+nCols,0]
partialX[partialIterator:partialIterator+nCols,0] = temp.ravel()
fullIterator += int(recordCountInClusters[k][0])
partialIterator += nCols
updatedPartialX = GlobalQtX(globalReflector, partialX)
rowNumberCount = 0
partialIterator = 0
for k in range(numOfClusters):
finalX[rowNumberCount:rowNumberCount+nCols,0] = updatedPartialX[partialIterator:partialIterator+nCols].ravel()
rowNumberCount += int(recordCountInClusters[k][0])
partialIterator += nCols
return finalX
def LocalQX(localReflector, subX):
(rowInCluster,colInCluster) = subX.shape
for k in range(nCols-1, -1,-1):
value = np.matmul(np.transpose(localReflector[0:rowInCluster,k]),subX[0:rowInCluster,0])
temp = (2*value)*localReflector[0:rowInCluster,k]
subX[0:rowInCluster,0] = (subX[0:rowInCluster,0]-temp).ravel()
return subX[0:rowInCluster,0]
def GlobalQX(globalReflector, subX):
(rowInReflector,colInReflector) = globalReflector.shape
for k in range(nCols-1, -1,-1):
value = np.matmul(np.transpose(globalReflector[0:rowInReflector,k]),subX[0:rowInReflector,0])
temp = (2*value)*globalReflector[0:rowInReflector,k]
subX[0:rowInReflector,0] = (subX[0:rowInReflector,0]-temp).ravel()
return subX[0:rowInReflector,0]
def LocalQXPerCluster(num, list_of_lists,fullX, recordCountInClusters):
partitionedMatrix = np.zeros((numOfPartialData + thresholdSpark, nCols))
final_iterator = [5]
currentRow = 0
for x in list_of_lists:
partitionedMatrix[currentRow, :] = x
currentRow += 1
reflector = partitionedMatrix[0:currentRow, :]
rowInCluster = int(recordCountInClusters[num][0])
previousRows = 0
for x in range(num):
previousRows += int(recordCountInClusters[x][0])
subX = fullX[previousRows:previousRows+rowInCluster,:]
updatedSubX = LocalQX(reflector,subX)
temp = np.zeros((totalData, 1))
temp[previousRows:previousRows+rowInCluster, 0] = updatedSubX.ravel()
xAcc.add(temp)
return iter(final_iterator)
def Dist_QX(x, globalReflector, localReflector, recordCountInClusters):
partialX = np.zeros((numOfClusters*nCols, 1))
fullIterator = 0
partialIterator = 0
for k in range(numOfClusters):
temp = x[fullIterator:fullIterator+nCols,0]
partialX[partialIterator:partialIterator+nCols,0] = temp.ravel()
fullIterator += int(recordCountInClusters[k][0])
partialIterator += nCols
updatedPartialX = GlobalQX(globalReflector, partialX)
rowNumberCount = 0
partialIterator = 0
for k in range(numOfClusters):
x[rowNumberCount:rowNumberCount+nCols,0] = updatedPartialX[partialIterator:partialIterator+nCols].ravel()
rowNumberCount += int(recordCountInClusters[k][0])
partialIterator += nCols
dummy = localReflector.mapPartitionsWithIndex(partial(LocalQXPerCluster,fullX = x,recordCountInClusters = recordCountInClusters))
dummy.count()
global xAcc
updatedX = xAcc.value
xAcc = sc.accumulator(np.zeros((totalData, 1)), VectorAccumulatorParam())
return updatedX
def AlphaBetaUpdate(F, betaCapOld, Ecap):
alphaCap = np.matmul(np.linalg.inv(F),Ecap-betaCapOld)
betaCap = betaCapOld - (optStepSize * alphaCap)
return alphaCap, betaCap
def AlphaBetaUpdatePerCluster(num, list_of_lists,betaBroadcast, enBroadcast, recordCountInClusters):
for x in list_of_lists:
F = x
#partitionedMatrix = np.zeros((numOfPartialData + thresholdSpark, nCols))
final_iterator = [5]
rowInCluster = int(recordCountInClusters[num][0])
previousRows = 0
for x in range(num):
previousRows += int(recordCountInClusters[x][0])
fullBeta = betaBroadcast.value
fullEn = enBroadcast.value
subBeta = fullBeta[previousRows:previousRows+rowInCluster,:]
subEn = fullEn[previousRows:previousRows+rowInCluster,:]
alphaCap, betaCap = AlphaBetaUpdate(F, subBeta, subEn)
temp = np.zeros((totalData, 1))
temp[previousRows:previousRows+rowInCluster, 0] = alphaCap.ravel()
alphaList.add(temp)
temp = np.zeros((totalData, 1))
temp[previousRows:previousRows+rowInCluster, 0] = betaCap.ravel()
betaList.add(temp)
return iter(final_iterator)
def QRDecompositionPerCluster(num, list_of_lists):
partitionedMatrix = np.zeros((numOfPartialData+thresholdSpark,nCols))
#final_iterator = [5]
currentRow = 0
for x in list_of_lists:
partitionedMatrix[currentRow,:] = x
currentRow += 1
trimmedMatrix = partitionedMatrix[0:currentRow,:]
Q, R, Reflectors = QR_Factorization(trimmedMatrix,currentRow,nCols)
temp = np.zeros((numOfClusters,1))
temp[num,0] = currentRow
recordCountInClusters.add(temp)
temp = np.zeros((numOfClusters*nCols,nCols))
temp[num*nCols:(num+1)*nCols,:] = R
Rgatherac.add(temp)
return iter(Reflectors)
def dummyFunc(s):
return s
def TestData(alphaTotalMat, finalR, testDataMat, testLabelMat):
weightMat = np.matmul(np.transpose(finalR), alphaTotalMat[0:nCols,:])
transposeWeightMat = np.transpose(weightMat)
result = np.matmul(transposeWeightMat,np.transpose(testDataMat))
correct = 0
wrong = 0
correntPos = 0
correntNeg = 0
wrongPos = 0
wrongNeg = 0
for i in range(testrow):
if result[0][i] >0:
if testLabelMat[i][0] == testLabelPositive:
correct += 1
correntPos += 1
else:
wrong += 1
wrongPos += 1
else:
if testLabelMat[i][0] == testLabelNegative:
correct += 1
correntNeg += 1
else:
wrong += 1
wrongNeg += 1
accuracy = (correct *100)/ testrow
print("corrent is :" + str(accuracy))
print("wrong is :" + str(accuracy))
print("Accuracy is :" + str(accuracy))
def main():
#sc = SparkContext(master="local", appName="meka")
spark = SparkSession(sc)
#global numOfPartialData
#numOfPartialData = math.ceil(totalData/numOfClusters)
#print(numOfPartialData)
trainingDataRDD = sc.textFile(trainDataPath,numOfClusters)
trainingData = trainingDataRDD.map(inputFuncTrain).persist()
testDataRDD = sc.textFile(testDataPath, numOfClusters)
testData = testDataRDD.map(inputFuncTest).persist()
testLabelRDD = sc.textFile(testLabelPath, numOfClusters)
testLabel = testLabelRDD.map(inputFuncTestLabel).persist()
testdatamat = np.zeros((testrow, nCols))
testlabelmat = np.zeros((testrow, 1))
count = 0
for x in testData.take(testrow):
for j in range(nCols):
testdatamat[count,j] = x[0,j]
count+=1
count = 0
for y in testLabel.take(testrow):
testlabelmat[count, 0] = y[0, 0]
count += 1
#trainingData.foreachPartition(filter_out_2_from_partition)
# rdd = sc.parallelize(range(1, 4)).map(lambda x: (x, "a" * x))
# print(rdd.collect())
localReflectors = trainingData.mapPartitionsWithIndex(QRDecompositionPerCluster)
localReflectors.count()
#print(recordCountInClusters.value)
#print(Rgatherac.value)
RgatherMat = Rgatherac.value
Q, finalR, globalReflector = QR_Factorization(RgatherMat, numOfClusters*nCols, nCols)
recordCountInClustersMat = recordCountInClusters.value
betaCapmat = np.full((totalData,1),1.0)
betaBroadCast = sc.broadcast(betaCapmat)
Ecap = Dist_QtX(np.full((totalData,1),-1.0),globalReflector,localReflectors,recordCountInClustersMat)
enBroadCast = sc.broadcast(Ecap)
fullRfinalTransposeMat = np.transpose(finalR)
RgRgTmat = np.matmul(finalR,fullRfinalTransposeMat)
Fs = []
F = np.zeros((int(recordCountInClustersMat[0][0]), int(recordCountInClustersMat[0][0])))
F[0:nCols,0:nCols] = (-1)*RgRgTmat
for i in range(int(recordCountInClustersMat[0][0])):
F[i][i] += (-1.0/(2*C))
Fs.append(F)
for p in range(numOfClusters-1):
F = np.zeros((int(recordCountInClustersMat[p+1][0]), int(recordCountInClustersMat[p+1][0])))
for i in range(int(recordCountInClustersMat[p+1][0])):
F[i][i] += (-1.0 / (2 * C))
Fs.append(F)
FsRDD = sc.parallelize(Fs, numOfClusters)
prevBetaCap = np.full((totalData,1),1.0)
for it in range(maxIteration):
global alphaList
alphaList = sc.accumulator(np.zeros((totalData, 1)), VectorAccumulatorParam())
global betaList
betaList = sc.accumulator(np.zeros((totalData, 1)), VectorAccumulatorParam())
dummy = FsRDD.mapPartitionsWithIndex(partial(AlphaBetaUpdatePerCluster, betaBroadcast = betaBroadCast, enBroadcast = enBroadCast, recordCountInClusters = recordCountInClustersMat))
dummy.count()
Betamat = Dist_QX(betaList.value, globalReflector, localReflectors, recordCountInClustersMat)
for i in range(totalData):
if Betamat[i][0] < 0:
Betamat[i][0] = 0
betaCapmat = Dist_QtX(Betamat, globalReflector, localReflectors, recordCountInClustersMat)
diffBeta = betaCapmat - prevBetaCap
error = np.linalg.norm(diffBeta,ord=1)
prevBetaCap = betaCapmat
print("####################Here is Iteration : "+str(it)+" ############################")
print("This is error :"+str(error))
betaBroadCast = sc.broadcast(betaCapmat)
if it% 5 == 0:
TestData(alphaList.value, finalR, testdatamat, testlabelmat)
if error<threshold:
break
TestData(alphaList.value, finalR, testdatamat, testlabelmat)
'''dummyMap = filtered_lists.map(dummyFunc)
print(dummyMap.collect())
dummyReduce = dummyMap.reduce(lambda a, b: a + b)
print(dummyReduce)'''
main()