forked from kuqin12/spike-sorting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspike_svm.py
169 lines (143 loc) · 4.85 KB
/
spike_svm.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
import argparse
import logging
import os
import math
import numpy as np
import time
class SpikeSVMClassifier (object):
def __init__(self, spark, GD="MBGD"):
self.sp = spark
self.svms = {}
self.GD = GD
if GD == "BGD":
self.eta = 0.0000003
self.epsilon = 0.25
self.converge = SpikeSVMClassifier.BGD_Conv
elif GD == "SGD":
self.eta = 0.0001
self.epsilon = 0.001
self.converge = SpikeSVMClassifier.SGD_Conv
elif GD == "MBGD":
self.eta = 0.00001
self.epsilon = 0.01
self.converge = SpikeSVMClassifier.MBGD_Conv
else:
raise Exception ("Unsupported convergence condition specified.")
@staticmethod
def BGD_Conv (loss_arr, delta_loss_arr, epsilon):
old_loss = loss_arr [-2]
new_loss = loss_arr [-1]
delta_perc_loss = abs (old_loss - new_loss) / old_loss * 100
# print (delta_perc_loss)
if delta_perc_loss < epsilon:
return True
else:
return False
@staticmethod
def SGD_Conv (loss_arr, delta_loss_arr, epsilon):
old_loss = loss_arr [-2]
new_loss = loss_arr [-1]
delta_perc_loss = abs (old_loss - new_loss) / old_loss * 100
delta_loss = 0.5 * delta_loss_arr[-1] + 0.5 * delta_perc_loss
delta_loss_arr.append (delta_loss)
# print (delta_loss)
if delta_loss < epsilon:
return True
else:
return False
@staticmethod
def MBGD_Conv (loss_arr, delta_loss_arr, epsilon):
return SpikeSVMClassifier.SGD_Conv (loss_arr, delta_loss_arr, epsilon)
@staticmethod
def CalculateLoss (w, b, p_features, p_target, C):
loss = 0.5 * np.dot(w ,w)
n = len (p_features)
for i in range(n):
e_loss = p_target[i] * (np.dot(w, p_features[i]) + b)
loss += (C * max(0, 1 - e_loss))
return loss
@staticmethod
def unison_shuffled_copies(a, b):
assert len(a) == len(b)
p = np.random.permutation(len(a))
return a[p], b[p]
def SVM (self, features, target):
# print ("Start of SVM - %s" % self.GD)
# start_time = time.time()
n = len(features)
d = len(features[0])
if self.GD == 'BGD':
p_features = features
p_target = target
self.beta = n
elif self.GD == 'SGD':
p_features, p_target = SpikeSVMClassifier.unison_shuffled_copies (features, target)
self.beta = 1
elif self.GD == 'MBGD':
p_features, p_target = SpikeSVMClassifier.unison_shuffled_copies (features, target)
self.beta = 20
else:
raise Exception ("Unexpected gradient descent - %s!!!" % self.GD)
k = 0
w = [0] * d
b = 0
C = 100
ret_arr = []
loss = SpikeSVMClassifier.CalculateLoss (w, b, p_features, p_target, C)
ret_arr.append (loss)
# print (loss)
delta_loss = [0]
t = 1
while True:
# cache the old value
w_old = w.copy()
b_old = b
# print ("Iteration %d" % t)
e_loss = {}
p_b = 0
start = self.beta * k
end = min (self.beta * k + self.beta - 1, n - 1)
for i in range(start, end + 1):
# Within the range of B
e_loss[i] = p_target[i] * (np.dot(w_old, p_features[i]) + b_old)
# For optimization, sum up the result of equation 2
p_b += 0 if e_loss[i] >= 1 else (-p_target[i])
for j in range (d):
p_wj = 0
for i in e_loss:
# Sum up the result of equation 1
p_wj += 0 if e_loss[i] >= 1 else (-p_target[i] * p_features[i][j])
w[j] = w_old[j] - self.eta * (w_old[j] + C * p_wj)
b = b_old - self.eta * C * p_b
k = (k + 1) % math.ceil(n/self.beta)
loss = SpikeSVMClassifier.CalculateLoss (w, b, p_features, p_target, C)
ret_arr.append (loss)
if self.converge (ret_arr, delta_loss, self.epsilon):
break
t += 1
# done_time = time.time()
# elapsed = done_time - start_time
# print("Time elapsed %ds after %d iterations" % (elapsed, t))
return w, b
def Fit(self, data, label):
# The binary classification is "trivial" as noted above.
# This one we need to do a one-to-rest multiclass SVM
unique_label = np.unique(label)
for each in unique_label:
# specifically binarize the label
temp_label = np.array([1 if i == each else -1 for i in label])
w_each, b_each = self.SVM(data, temp_label)
self.svms [each] = (w_each, b_each)
def Predict(self, sample):
best_predict = None
max_predict = None
for each in self.svms:
w, b = self.svms[each]
predict = np.dot(sample, w) + b
if max_predict == None or predict > max_predict:
best_predict = each
max_predict = predict
# if max_predict < 0:
# # Something is off.. this prediction is the best but still may not be accurate
# # Do something...
return best_predict