Created
August 25, 2016 17:15
-
-
Save arvidfm/4cff3e8d215e8d0c5629d968e355f0d9 to your computer and use it in GitHub Desktop.
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 timeit | |
import numpy as np | |
import theano | |
import theano.tensor as T | |
x = theano.shared(np.random.rand(500, 100).astype(theano.config.floatX)) | |
W = theano.shared(np.random.rand(10, 100, 50).astype(theano.config.floatX)) | |
y = theano.shared(np.random.randint(0, 10, 500).astype(theano.config.floatX)) | |
y = T.cast(y, 'int32') | |
def batched_dot1(): | |
return T.batched_dot(x, W[y]) | |
def batched_dot2(): | |
return (x[...,np.newaxis] * W[y]).sum(axis=1) | |
def batched_dot3(): | |
output, _ = theano.scan( | |
fn=lambda x, y, W: T.dot(x, W[y]), | |
sequences=[x, y], | |
non_sequences=W) | |
return output | |
for fn in (batched_dot1, batched_dot2, batched_dot3): | |
print("Running", fn.__name__) | |
f = theano.function([], fn()) | |
print("Took {} seconds".format( | |
timeit.timeit('f()', setup='from __main__ import f', number=5000))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment