-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomModels.py
165 lines (136 loc) · 4.47 KB
/
CustomModels.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
from math import *
from DataOperations import *
from datetime import datetime
from tools.misc import *
from IModel import *
from random import random
######################################################################################
## CUSTOM MODELS
class OnlineLinearLearning(Model):
PARAMS_KEYS = {
"alpha" : float,
}
def __init__(self,params,**kwargs):
Model.__init__(self,params,**kwargs)
self.n = [0] * D
self.name = "Online method"
def loop(self,p,x,y):
for i in x:
self.n[i] += abs(p - y)
self.w[i] -= (p - y) * 1. * self.alpha / sqrt(self.n[i])
class LogOnlineLinearLearning(Model):
PARAMS_KEYS = {
"alpha" : float,
}
def __init__(self,params,**kwargs):
Model.__init__(self,params,**kwargs)
self.n = [0] * D
self.name = "Log Online method"
def loop(self,p,x,y):
for i in x:
self.n[i] += abs(p - y)
self.w[i] -= max(min((1 - y) / (1 - p) - y / p,10 ** 8),-10 ** 8) * 1. * self.alpha / sqrt(self.n[i])
class ZALMS(Model):
def __init__(self,params,**kwargs):
Model.__init__(self,params,**kwargs)
self.name = "ZALMS"
def loop(self,p,x,y):
for i in x:
self.w[i] -= self.delta * ((p - y) * 1. + self.rho * copysign(self.w[i],1))
class OLBI(Model):
def __init__(self,params,**kwargs):
Model.__init__(self,params,**kwargs)
self.name = "OLBI"
def loop(self,p,x,y):
for i in x:
self.w[i] -= self.delta * (p - y) * 1.
self.w[i] = shrink(self.w[i], self.gamma)
class Perceptron(Model):
def __init__(self,params,**kwargs):
Model.__init__(self,params,**kwargs)
self.name = "Perceptron"
def predict(self,x):
wTx,_ = self.innerProduct(x)
p = sigmoid(self.approx * wTx)
return p
def loop(self,p,x,y):
if (y - 0.5) * (p - 0.5) <= 0: # if the predictions disagree
for i in x: # contribution of each feature is corrected
self.w[i] += (y - 0.5) * 2.
class Perceptron2(Model):
def __init__(self,params,**kwargs):
Model.__init__(self,params,**kwargs)
self.name = "Perceptron2"
def predict(self,x):
wTx,_ = self.innerProduct(x)
p = sigmoid(self.approx * wTx)
return p
def loop(self,p,x,y):
if (y - 0.5) * (p - 0.5) <= 0: # if the predictions disagree
for i in x: # contribution of each feature is corrected
self.w[i] += min(max(-1, (y - 0.5) * 2.),1)
class RandomNeuralNetwork(Model):
def __init__(self,params,**kwargs):
Model.__init__(self,params,**kwargs)
self.name = "RandomNeuralNetwork"
self.neurons = []
self.signals = []
self.n = []
self.proba = self.maxNeurons * 1. / 30000000
def neuronActivate(self,neuron,x):
res = 0
for i in x :
self.w[i] = 1
for n in neuron:
res = res + self.w[n]
for i in x :
self.w[i] = 0
return res > self.threshold
def predict(self, x) :
sumSignals = 0
nbNeurons = len(self.neurons)
if(nbNeurons > 0):
for i in range(nbNeurons) :
isActive = self.neuronActivate(self.neurons[i],x)
if isActive :
sumSignals += self.signals[i]
return (sumSignals * 1. / (2 * nbNeurons) + 0.5)
else :
return 0.5
def loop(self,p,x,y):
if (len(self.neurons) < self.maxNeurons) and (random() < self.proba):
self.neurons.append(x)
if len(self.neurons) == self.maxNeurons :
print("Maximum number of neurons reached")
self.signals.append((y - 0.5) * 2)
class FTRLProximal(Model):
PARAMS_KEYS = {
"alpha" : float,
"beta" : float,
"lambda1" : float,
"lambda2" : float,
"approx" : float,
}
def __init__(self,params,**kwargs):
Model.__init__(self,params,**kwargs)
self.name = "FTRLProximal"
self.z = [0] * D
self.sigma = [0] * D
self.g = [0] * D
self.n = [0] * D
def predict(self,x):
wTx,_ = self.innerProduct(x)
p = sigmoid(self.approx * wTx)
return p
def loop(self,p,x,y):
error = (p - y)
for i in x: # contribution of each feature is corrected
if copysign(self.z[i],1) <= self.lambda1:
self.w[i] = 0
else:
self.w[i] = (self.lambda1 * copysign(1,self.z[i]) - self.z[i]) / (((self.beta + sqrt(self.n[i])) / self.alpha) + self.lambda2)
for i in x :
self.g[i] = error * 1.
self.sigma[i] = 1 / self.alpha * (sqrt(self.n[i] + self.g[i] ** 2) - sqrt(self.n[i]))
self.z[i] += self.g[i] - self.sigma[i] * self.w[i]
self.n[i] += self.g[i] ** 2