python - TensorFlow vs. Theano Performance -
in context of neural network research i'm evaluating several approaches on how implement these or library use. i'm comparing tensorflow , theano , i'm struggling getting tenorflow perform well. here simple hello-gradient-benchmark, optimizes scalar multiplication 1 coefficient.
import time class timer: def __init__(self, what): self.what = def __enter__(self): self.t1 = time.time() return self def __exit__(self,t,v,tb): t2 = time.time() print("{0} runs {1:.4f} seconds".format(self.what, t2-self.t1)) def run_tensorflow(): import tensorflow tf x = tf.placeholder(tf.float32) y = tf.placeholder(tf.float32) = tf.variable([1.], tf.float32) sess = tf.session() sess.run(tf.global_variables_initializer()) loss = (y-a*x)**2 step = tf.train.gradientdescentoptimizer(0.01).minimize(loss) def one_step(): sess.run(step, {x:1.,y:0.}) timer('tensorflow') t: result = [ one_step() n in range(1000) ] def run_theano(): import theano th x = th.tensor.dscalar() y = th.tensor.dscalar() = th.tensor.dscalar() l = a*x loss = (y-l)**2 dloss = th.tensor.grad(loss, a) dloss_f = th.function([x,y,a], dloss) = [1.] def one_step(): a[0] -= 0.01 * dloss_f(1.,0.,a[0]) timer('theano') t: result = [ one_step() n in range(1000) ] run_tensorflow() run_theano()
i'm running program on cpu packages installed via pip
. running times 0.36 , 0.043 seconds tensorflow , theano, respectively. see similar performance differences real networks matrix-multiplication overhead should dominate, still tensorflow slower.
i want know if i'm using tensorflow wrongly i'm trying do. should not call run()
method within loop?
tf , theano designed handling large objects, on order of 1m elements. benchmarking handling of scalars not particularly relevant.
this apples-to-oranges comparison: tf, timing both compilation , run time, while in theano, timing run time! because when call
theano.function
, compilation then. otoh in tf, of work shifted when first callsess.run
.
that said, there realistic scenarios when tf slower theano.
Comments
Post a Comment