From 64e80194d7b82c9e5eb6deeec317684d4a079b5f Mon Sep 17 00:00:00 2001 From: James Bergstra Date: Tue, 9 Jul 2013 18:00:48 -0400 Subject: [PATCH 1/3] pairwise_theano --- pairwise/pairwise_theano.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 pairwise/pairwise_theano.py diff --git a/pairwise/pairwise_theano.py b/pairwise/pairwise_theano.py new file mode 100644 index 0000000..9b61e18 --- /dev/null +++ b/pairwise/pairwise_theano.py @@ -0,0 +1,18 @@ + +import theano +import theano.tensor as TT + +def pairwise_theano_prepare(dtype): + X = TT.matrix(dtype=str(dtype)) + X_norm_2 = (X ** 2).sum(axis=1) + dists = TT.sqrt(2 * X_norm_2 - TT.dot(X, X.T)) + rval = theano.function([X], dists, allow_input_downcast=True) + rval.__name__ = 'pairwise_theano_' + dtype + return rval + +benchmarks = ( + pairwise_theano_prepare('float32'), + pairwise_theano_prepare('float64'), +) + + From ab1d1bb38520a95fa3ac54daf9a3d086143d3193 Mon Sep 17 00:00:00 2001 From: James Bergstra Date: Tue, 9 Jul 2013 20:36:05 -0400 Subject: [PATCH 2/3] added Python2.6 support for OrderedDict --- run_benchmarks.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/run_benchmarks.py b/run_benchmarks.py index e72596e..c06a5af 100644 --- a/run_benchmarks.py +++ b/run_benchmarks.py @@ -2,7 +2,10 @@ # License: MIT from __future__ import print_function -from collections import OrderedDict +try: + from collections import OrderedDict +except: + from ordereddict import OrderedDict import json import os import traceback From 270554602715ef6245e1358f3994ca5ee0a6c0c5 Mon Sep 17 00:00:00 2001 From: James Bergstra Date: Tue, 9 Jul 2013 20:36:41 -0400 Subject: [PATCH 3/3] added non-cheating pairwise_theano impl --- pairwise/pairwise_theano.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/pairwise/pairwise_theano.py b/pairwise/pairwise_theano.py index 9b61e18..f47aede 100644 --- a/pairwise/pairwise_theano.py +++ b/pairwise/pairwise_theano.py @@ -2,17 +2,36 @@ import theano import theano.tensor as TT -def pairwise_theano_prepare(dtype): +from pairwise_python import pairwise_python_inner_broadcasting + +def pairwise_theano_tensor_prepare(dtype): + X = TT.matrix(dtype=str(dtype)) + dists = TT.sqrt( + TT.sum( + TT.sqr(X[:, None, :] - X), + axis=2)) + rval = theano.function([X], + theano.Out(dists, borrow=True), + allow_input_downcast=True) + rval.__name__ = 'pairwise_theano_tensor_' + dtype + return rval + +def pairwise_theano_blas_prepare(dtype): X = TT.matrix(dtype=str(dtype)) X_norm_2 = (X ** 2).sum(axis=1) dists = TT.sqrt(2 * X_norm_2 - TT.dot(X, X.T)) - rval = theano.function([X], dists, allow_input_downcast=True) - rval.__name__ = 'pairwise_theano_' + dtype + rval = theano.function([X], + theano.Out(dists, borrow=True), + allow_input_downcast=True) + rval.__name__ = 'pairwise_theano_blas_' + dtype return rval + benchmarks = ( - pairwise_theano_prepare('float32'), - pairwise_theano_prepare('float64'), + pairwise_theano_tensor_prepare('float32'), + pairwise_theano_tensor_prepare('float64'), + pairwise_theano_blas_prepare('float32'), + pairwise_theano_blas_prepare('float64'), )