Skip to content

Instantly share code, notes, and snippets.

@matpalm
Created February 24, 2015 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matpalm/7b519c93ca6fb732dd17 to your computer and use it in GitHub Desktop.
Save matpalm/7b519c93ca6fb732dd17 to your computer and use it in GitHub Desktop.
import numpy as np
import time
np.random.seed(123)
# square matrices will do for a demo
m = np.random.randn(1000, 1000).astype('float32')
x = np.random.randn(1000, 1000).astype('float32')
b = np.random.randn(1000, 1000).astype('float32')
start = time.time()
for i in range(500):
y = np.add(np.dot(m, x), b)
print "numpy", time.time()-start, "sec"
import numpy as np
import time
import theano
import theano.tensor as T
# define data
np.random.seed(123)
m = np.random.randn(1000, 1000).astype('float32')
x = np.random.randn(1000, 1000).astype('float32')
b = np.random.randn(1000, 1000).astype('float32')
# define a symbolic expression of the equations in theano
tm = T.matrix("m")
tx = T.matrix("x")
tb = T.matrix("b")
ty = T.add(T.dot(tm, tx), tb)
# and compile it
line = theano.function(inputs=[tx, tm, tb], outputs=[ty])
print theano.printing.debugprint(line)
print theano.printing.pydotprint(line)
# then run same loop as before
start = time.time()
for i in range(500):
y, = line(m, x, b)
print "theano", time.time()-start, "sec"
import numpy as np
import time
import theano
import theano.tensor as T
# define data
np.random.seed(123)
m = np.random.randn(1000, 1000).astype('float32')
x = np.random.randn(1000, 1000).astype('float32')
b = np.random.randn(1000, 1000).astype('float32')
# define a symbolic expression of the equations in theano
tm = theano.shared(m, borrow=True)
tx = T.matrix("x")
tb = theano.shared(b, borrow=True)
ty = T.add(T.dot(tm, tx), tb)
# and compile it
line = theano.function(inputs=[tx], outputs=[ty])
print theano.printing.debugprint(line)
print theano.printing.pydotprint(line)
# then run same loop as before
start = time.time()
for i in range(500):
y_, = line(x)
print tm.get_value().shape
print "theano", time.time()-start, "sec"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment