Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.