Skip to content

Instantly share code, notes, and snippets.

@JonathanRaiman
Created November 2, 2014 01:33
Show Gist options
  • Save JonathanRaiman/a579c9841161d98ea2b9 to your computer and use it in GitHub Desktop.
Save JonathanRaiman/a579c9841161d98ea2b9 to your computer and use it in GitHub Desktop.
A Gaussian Kernel in Theano
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