-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeano_kernels.py
212 lines (148 loc) · 5.75 KB
/
theano_kernels.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# GPU/Theano implementations of common kernel functions
#
# Contains:
# theano_rbf (Radial Basis Function)
# theano_chi2 (Chi-Squared)
# theano_poly (Polynomial)
#
# Requires:
# Theano
# Numpy
import theano
import theano.tensor as T
import numpy as np
def theano_rbf(X, Y=None, gamma=.5):
'''GPU implementation of radial basis function (RBF) kernel:
K(x,y) = e ^ ( -gamma .* ||x-y||^2 )
Optimised by expanding the ||x-y||^2 term and calculating dot
products on GPU.
----------------------------------------------------------------------
usage: K = theano_rbf(X, [Y=None, gamma=.5])
input: X - numpy array 2D
MxN array containing M observations of N features
Y - numpy array 2D
PxN array (note must have same number of columns as X)
gamma - scalar
spread parameter for rbf kernel
output: K - numpy array 2D
MxP array of pairwise RBF similarities between
observations in X and Y
'''
#set Y - if None -> X
if Y is None:
Y = X
#check dimensions
assert X.shape[1] == Y.shape[1], 'X and Y must be of same dimension'
#define the symbolic vars
x = T.matrix('x')
y = T.matrix('y')
x_dot_y = T.dot(x,y.T)
#compile the theano function
f = theano.function(inputs=[x, y], outputs=x_dot_y)
#calc the dot products on GPU
X = X.astype('Float32')
Y = Y.astype('Float32')
x_dot_y = f(X,Y).astype('Float64') #for ease with sklearn
#x^2 and y^2 terms in the expansion
#these ops generally better on CPU due to memory
x2 = np.tile(np.sum(X**2, axis=1)[:,None], [1, Y.shape[0]])
y2 = np.tile(np.sum(Y**2, axis=1)[None,:], [X.shape[0], 1])
#add together terms, multiply by gamma, and take exponential
K = np.exp(-gamma * (x2 + y2 - 2*x_dot_y))
return K
def theano_chi2(X, Y=None, gamma=1):
'''GPU implementation of exponential chi-squared kernel - often used in
kernel classification and regression with bag-of-features representations:
K(x,y) = e ^ ( -gamma .* Sum[ (xi-yi)^2 / (xi+yi) ] )
i
----------------------------------------------------------------------
usage: K = theano_chi2(X, [Y=None, gamma=1])
input: X - numpy array 2D
MxN array containing M observations of N features
Y - numpy array 2D
PxN array (note must have same number of columns as X)
gamma - scalar
spread parameter for chi-squared kernel
output: K - numpy array 2D
MxP array of pairwise chi-squared distances between
observations in X and Y
'''
#set Y - if None -> X
if Y is None:
Y = X
#check dimensions
assert X.shape[1] == Y.shape[1], 'X and Y must be of same dimension'
#add epsilon to avoid division by 0
X = X + 1e-15
Y = Y + 1e-15
#ensure float32 type
X = X.astype('Float32')
Y = Y.astype('Float32')
#declare constant for tiling (within K_row_comp())
TILING_REPS_CONST = Y.shape[0]
#constant for spread param
GAMMA_CONST = gamma
def K_row_comp(x_vec, y_mat):
'''calculates a row of the chi-squared gram matrix'''
#add singleton dimension to vector for tiling
x_vec_2d = x_vec[:,None]
#tile the vector
x_vec_tiled = T.tile(x_vec_2d, [1, TILING_REPS_CONST])
#subtract y and square
x_min_y = x_vec_tiled - y_mat.T
x_min_y2 = x_min_y ** 2
#x + y term
x_plus_y = x_vec_tiled + y_mat.T
#divide and sum
raw_row = T.sum( x_min_y2 / x_plus_y , axis=0)
#take exponential
K_row = T.exp(-GAMMA_CONST * raw_row)
return K_row
#define the symbolic vars
x = T.matrix('x')
y = T.matrix('y')
#scan over the rows of X
result, updates = theano.scan(K_row_comp,
outputs_info = None,
sequences = x,
non_sequences = y)
#compile the theano function
f = theano.function(inputs=[x, y], outputs=result)
#return
return f(X,Y).astype('Float64') #for ease with sklearn
def theano_poly(X, Y=None, degree=3, coef0=1, gamma=1):
'''GPU implementation of polynomial kernel:
K(x,y) = Sum[ (gamma*xi*yi + coef0)^degree ]
i
----------------------------------------------------------------------
usage: K = theano_poly(X, [Y=None, degree=3, coef0=1, gamma=1])
input: X - numpy array 2D
MxN array containing M observations of N features
Y - numpy array 2D
PxN array (note must have same number of columns as X)
degree- scalar
dimension of polynomial (2 -> quadratic etc.)
coef0 - scalar
gamma - scalar
output: K - numpy array 2D
MxP array of pairwise polynomial distances between
observations in X and Y
'''
#set Y - if None -> X
if Y is None:
Y = X
#check dimensions
assert X.shape[1] == Y.shape[1], 'X and Y must be of same dimension'
#define the symbolic vars
x = T.matrix('x')
y = T.matrix('y')
x_dot_y = T.dot(x,y.T)
#compile the theano function
f = theano.function(inputs=[x, y], outputs=x_dot_y)
#calc the dot products on GPU
X = X.astype('Float32')
Y = Y.astype('Float32')
x_dot_y = f(X,Y).astype('Float64') #for ease with sklearn
#add together terms, multiply by gamma
K = (gamma*x_dot_y + coef0)**degree
return K