-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
192 lines (157 loc) · 6 KB
/
models.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
import numpy
import scipy
import scipy.special
from Pipeline import Model
from Tools import logpdf_GAU_ND, mrow, mcol, vec, logpdf_GMM
class GenerativeModel(Model):
def __init__(self, K, mu, C):
super().__init__()
self.mu = mu
self.C = C
self.K = K # num Classes
self.prior = None
return
def setPrior(self, prior):
self.prior = mcol(numpy.array(prior))
return
def logLikelihood(self, ll, D, mu, C):
pass
def transform(self, D, L):
D, L = super().transform(D, L)
K = self.K
nSamples = D.shape[1]
ll = numpy.zeros((K, nSamples)) # array of log likelihoods vectors
if self.prior is None:
self.prior = mcol(numpy.ones(K) / float(K))
# Compute log-likelihood
ll = self.logLikelihood(ll, D, self.mu, self.C) # (K, N)
if K == 2:
# Binary
llr = ll[1] - ll[0]
return llr # (1, N)
logSJoint = ll + numpy.log(self.prior)
# Can we skip the below part in order to get time, see slide 36 of GenerativeLinearQuadratic
logSMarginal = mrow(scipy.special.logsumexp(logSJoint, axis=0))
logSPost = logSJoint - logSMarginal
SPost = numpy.exp(logSPost)
return ll
class MVGModel(GenerativeModel):
def __init__(self, K, mu, C):
super().__init__(K, mu, C)
def setPrior(self, prior):
super().setPrior(prior)
def logLikelihood(self, ll, D, mu, C):
# Compute log-likelihood
for i in range(self.K):
ll[i:i + 1, :] = mrow(logpdf_GAU_ND(D, self.mu[:, i], self.C[i]))
return ll
def transform(self, D, L):
return super().transform(D, L)
class TiedMVGModel(GenerativeModel):
def __init__(self, K, mu, C):
super().__init__(K, mu, C)
def setPrior(self, prior):
super().setPrior(prior)
def logLikelihood(self, ll, D, mu, C):
# Compute log-likelihood
for i in range(self.K):
ll[i:i + 1, :] = mrow(logpdf_GAU_ND(D, self.mu[:, i], self.C))
return ll
def transform(self, D, L):
return super().transform(D, L)
class LogRegModel(Model):
def __init__(self, w, b, isExpanded):
super().__init__()
self.w = w
self.b = b
self.isExpanded = isExpanded
def transform(self, D, L):
D, L = super().transform(D, L)
"""
Here we compute the score vector, is a vector with NumSample elements
it's computed by : s(xt) = w.T * xt + b
"""
if self.isExpanded:
nSamples = D.shape[1]
dim = D.shape[0]
phix = numpy.zeros((dim ** 2 + dim, nSamples))
for i in range(nSamples):
x = D[:, i:i + 1]
phix[:, i:i + 1] = numpy.vstack((vec(numpy.dot(x, x.T)), x))
D = phix
llrPost = numpy.dot(self.w.T, D) + self.b # (1, NumSamples)
return llrPost
class SVMModel(Model):
def __init__(self, a, K, kernel, isNoKern, DTR, LTR):
super().__init__()
self.a = mcol(a)
self.K = K
self.kernel = kernel
self.isNoKern = isNoKern
self.DTR = DTR
self.LTR = LTR
def transform(self, D, L):
D, L = super().transform(D, L)
N = D.shape[1]
"""
If we are using a non-linear SVM kernel the modified version is implicitly done inside the
calculus of the kernel, adding +K^2 factor, so we don't need to redo it again
Here I add the additional K feature only if the kernel is linear
"""
D = numpy.vstack((D, numpy.full((1, N), self.K))) if self.isNoKern else D
Z = mcol((self.LTR * 2.0) - 1.0) # 0 => -1 ; 1 => 1
## TODO
"""
TODO
The SVM decision rule is to assign a pattern to class HT if the score is greater than 0, and to
HF otherwise. However, SVM decisions are not probabilistic, and are not able to account for
different class priors and mis-classification costs. Bayes decisions thus require either a score post-
processing step, i.e. score calibration, or cross-validation to select the optimal threshold for a
specific application. Below we simply use threshold 0 and compute the corresponding accuracy.
"""
X = self.DTR
if not self.isNoKern:
"""
If we are doing a non linear SVM, the kernel MUST be computed with the normal Dataset
so here I'm removing the additional K feature
N.B.
Internally of the non linear kernels i.e. Poly or RBF, is added a constant eps = K^2
in order to regularize the bias that here we are removing
"""
X = X[:-1, :]
"""
Here I compute the score with the following formula
s(xt) = sum(from 1 to n) of[ a_i * z_i * k(x_i, x_t)]
I do everything with broadcasting
"""
S = numpy.dot((self.a * Z).T, self.kernel(X, D))
return S
class GMMModel(Model):
def __init__(self, K, GMMs):
super().__init__()
self.prior = None
self.K = K
self.GMMs = GMMs
def setPrior(self, prior):
self.prior = mcol(numpy.array(prior))
return
def transform(self, D, L):
D, L = super().transform(D, L)
K = self.K
nSamples = D.shape[1]
ll = numpy.zeros((K, nSamples)) # array of log likelihoods vectors
if self.prior is None:
self.prior = mcol(numpy.ones(K) / float(K))
# Compute log-likelihood
for c in range(K):
ll[c:c+1, :] = logpdf_GMM(D, self.GMMs[c])
if K == 2:
# Binary
llr = ll[1] - ll[0]
return llr
logSJoint = ll + numpy.log(self.prior)
# Can we skip the below part in order to get time, see slide 36 of GenerativeLinearQuadratic
logSMarginal = mrow(scipy.special.logsumexp(logSJoint, axis=0))
logSPost = logSJoint - logSMarginal
SPost = numpy.exp(logSPost)
return ll