1 """
2 This module contains generic utilities that can be used
3 by any model.
4 """
5
6 from math import *
7
8 -def grad(model, w, b):
9 """
10 Calculates the gradient
11 @type model: vector
12 @param model: model
13 @type w: vector
14 @param w: model vector
15 @type b: double
16 @param b: bias
17
18 @rtype: vector
19 @return: gradient
20 """
21 n = min(len(model),len(w))
22 err = sum([model[i]*w[i] for i in range(n)]) - b
23 return [ wi * err for wi in w]
24
26 """
27 Computes and returns the dot product of given two vectors
28 @type u: vector
29 @param u: vector1
30 @type v: vector
31 @param v: vector2
32
33 @rtype: double
34 @return: dot product value of the vectors
35 """
36 assert(len(u) == len(v))
37 return sum([u[i]*v[i] for i in range(len(u))])
38
40 err = dot(w,x) + b - y
41 return (scale(w,err), err*b)
42
43
45 """
46 Scales the given vector with the given scale factor and return it
47 @type w: vector
48 @param w: vector to be scaled
49 @type b: double
50 @param b: scale factor
51
52 @rtype: vector
53 @return: scaled vector
54 """
55 return [wi*b for wi in w]
56
58 """
59 Scales given vector y and add it to the x and return it
60
61 @type x: vector
62 @param x: vector to be added with the scaled one
63 @type y: vector
64 @param y: vector to be scaled
65 @type s: double
66 @param s: scale factor
67
68 @rtype: vector
69 @return: x + y * s
70 """
71 n = len(x)
72 assert( n == len(y))
73 return [x[i] + y[i]*s for i in range(n)]
74
76 """
77 Computes and returns dot product of two vectors (at least one of them is sparse)
78 @type dx: vector
79 @param dx: first component of the dot product
80 @type sy: vector
81 @param sy: second component of the dot product
82
83 @rtype: double
84 @return: dot product of dx and sy
85 """
86 s = 0.0
87 for (k,v) in sy:
88 s = s + dx[k]*v
89 return
90
92 """
93 Returns sigma valu
94
95 @type v: double
96 @param v: v
97
98 @rtype: double
99 @return: sigma value of v
100 """
101 if(v > 30):
102 return 1.0/(1.0 + exp(-v))
103 else:
104 return exp(v)/(1+exp(v))
105
106
107
109 """
110 Scales the given vector
111 @type x: vector
112 @param x: vector to be scaled
113 @type c: double
114 @param c: scale factor
115 """
116 for i in range(len(x)):
117 x[i] = x[i] * c
118
120 """
121 Scales y and adds it to x
122 x += y * c
123
124 @type x: vector
125 @param x: vector to be modified
126 @type y: vector
127 @param y: vector to be scaled and added to x
128 @type c: double
129 @param c: scale factor
130
131 """
132 n = min(len(x), len(y))
133 for i in range(n):
134 x[i] = x[i] + y[i] * c
135
137 """
138 Scales sz (which is sparse) and adds it to x
139 dx += sz * c
140
141 @type dx: vector
142 @param dx: vector to be modified
143 @type sz: vector
144 @param sz: vector to be scaled and added to dx
145 @type c: double
146 @param c: scale factor
147 """
148 for (k,v) in sz:
149 dx[k] = dx[k] + v * c
150
151
153 """
154 Calculates and returns dot products of w and vs
155 @type w: vector
156 @param w: first component of the dot product
157 @type ks: vector
158 @param ks: indexes of the sparse vector
159 @type vs: vector
160 @param vs: values of the sparse vector
161
162 @rtype: double
163 @return: dot product of two vectors
164 """
165 s = 0
166 for i in range(len(ks)):
167 s = s + w[ks[i]]*vs[i]
168 return s
169
171 """
172 Scales vectors and adds it to w
173
174 @type w: vector
175 @param w: vector to be modified
176 @type index: vector
177 @param index: indexes of the sparse vector
178 @type vectors: vector
179 @param vectors: values of the sparse vector
180 @type c: double
181 @param c: scale factor
182 """
183 for i in range(len(index)):
184 w[index[i]] = w[index[i]] + vectors[i]*c
185
186
188 """
189 l2 project function
190 @type x: vector
191 @param x: x
192 @type B: double
193 @param B: B
194 """
195 norm_sq = sum([xi*xi for xi in x])
196 if norm_sq > B*B:
197 norm = sqrt(norm_sq)
198 for i in range(len(x)):
199 x[i] = x[i]*B/norm
200
201
202
203
205 """
206 l1 shrink function
207 @type x: vector
208 @param x: x
209 @type u: double
210 @param u: u
211 """
212 for i in range(len(x)):
213 if x[i] > u:
214 x[i] = x[i] - u
215 else:
216 if x[i] < -u:
217 x[i] = x[i] + u
218 else:
219 x[i] = 0
220
221
223 """
224 l1 shrink function
225 @type x: vector
226 @param x: x
227 @type u: double
228 @param u: B
229 @type indexes: vector
230 @param indexes: indexes of the sparse vector
231 """
232 for i in indexes:
233 if x[i] > u:
234 x[i] = x[i] - u
235 else:
236 if x[i] < -u:
237 x[i] = x[i] + u
238 else:
239 x[i] = 0
240
241
242
243
245 """
246 Incremental average function
247 @type w_hat: vector
248 @param w_hat: w hat
249 @type w: vector
250 @param w: model vector
251 @type steps: number
252 @param steps: steps
253 """
254 if steps == 0:
255 for i in range(len(w_hat)):
256 w_hat[i] = w[i]
257 else:
258 steps = float(steps)
259 for i in range(len(w_hat)):
260 w_hat[i] = w[i] * 1.0/steps + steps/(steps + 1) * w_hat[i]
261