Skip to content

Instantly share code, notes, and snippets.

@viswanathgs
Created April 25, 2016 00:25
Show Gist options
  • Save viswanathgs/04bef980ba84848f631a05620311ad97 to your computer and use it in GitHub Desktop.
Save viswanathgs/04bef980ba84848f631a05620311ad97 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import numpy
import theano
import time
def timer(func):
def wrapper(*args, **kwargs):
print("Executing func %s" % func.__name__)
start = time.time()
func(*args, **kwargs)
end = time.time()
print("Elapsed time for func %s = %.2fs" % (func.__name__, end - start))
return wrapper
@timer
def theano_cube(a):
'''
Cube a matrix using theano
'''
x = theano.tensor.matrix()
y = pow(x, 3)
f = theano.function([x], y)
print(f(a))
@timer
def numpy_cube(a):
'''
Cube a matrix using theano
'''
print(numpy.power(a, 3))
@timer
def theano_eqn(a):
'''
Theano should canonicalize the equation y = x^3 / x^2 into y = x.
'''
x = theano.tensor.matrix()
y = pow(x, 3) / (x * x)
f = theano.function([x], y)
print(f(a))
@timer
def numpy_eqn(a):
'''
Numpy doesn't optimize the equation y = x^3 / x^2.
'''
print(numpy.power(a, 3) / (a * a))
if __name__ == '__main__':
a = numpy.random.rand(20000, 20000)
theano_cube(a)
numpy_cube(a)
theano_eqn(a)
numpy_eqn(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment