Created
November 2, 2014 01:33
-
-
Save JonathanRaiman/a579c9841161d98ea2b9 to your computer and use it in GitHub Desktop.
A Gaussian Kernel in Theano
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import theano, theano.tensor as T, numpy as np | |
def _outer_substract(x, y): | |
x = x.dimshuffle(0, 1, 'x') | |
x = T.addbroadcast(x, 2) | |
return (x - y.T).T | |
def _gaussian_kernel(x, y, beta = 0.1): | |
K = _outer_substract(x,y) | |
return T.exp( -beta * K.norm(L=2,axis=1)) | |
x = T.matrix() | |
z = T.matrix() | |
gaussian_kernel = theano.function([x, z], _gaussian_kernel(x,z), allow_input_downcast=True) | |
N = 100 | |
M = 200 | |
DIMS = 10 | |
a = np.random.randn(N, DIMS) | |
b = np.random.randn(M, DIMS) | |
%timeit gaussian_kernel(a, b) | |
# 1000 loops, best of 3: 1.04 ms per loop | |
def np_outer_substract(x, y): | |
return (x[:,:, np.newaxis] - y.T).T | |
def np_gaussian_kernel(x,y, beta=0.1): | |
K = np_outer_substract(x,y) | |
return np.exp( -beta * np.linalg.norm(K, axis=1)) | |
%timeit np_gaussian_kernel(a,b) | |
# 1000 loops, best of 3: 1.14 ms per loop | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment