python - Does TensorFlow perform poorly for simple networks? -
i've been experimenting simple basic (intro tutorial-level) neural networks in various frameworks, confused performance i'm seeing in tensorflow.
for example, simple network michael nielsen's tutorial (mnist digit recognition using l2 stochastic gradient descent in network 30 hidden nodes) performs worse (takes 8x long per epoch, same parameters) adapted (using vectorization mini-batch suggested in one of tutorial exercises) version of nielsen's basic numpy code.
does tensorflow, running on single cpu, perform badly? there settings should tweak improve performance? or tensorflow shine more complex networks or learning regimes, not expected such simple toy cases?
from __future__ import (absolute_import, print_function, division, unicode_literals) import tensorflow tf tensorflow.examples.tutorials.mnist import input_data import time def weight_variable(shape): return tf.variable(tf.truncated_normal(shape, stddev=0.1)) def bias_variable(shape): return tf.variable(tf.constant(0.1, shape=shape)) mnist = input_data.read_data_sets("./data/", one_hot=true) sess = tf.session() # inputs , outputs x = tf.placeholder(tf.float32, shape=[none, 784]) y_ = tf.placeholder(tf.float32, shape=[none, 10]) # model parameters w1 = weight_variable([784, 30]) b1 = bias_variable([30]) o1 = tf.nn.sigmoid(tf.matmul(x, w1) + b1, name='o1') w2 = weight_variable([30, 10]) b2 = bias_variable([10]) y = tf.nn.softmax(tf.matmul(o1, w2) + b2, name='y') sess.run(tf.initialize_all_variables()) loss = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) loss += 0.1/1000 * (tf.nn.l2_loss(w1) + tf.nn.l2_loss(w2)) train_step = tf.train.gradientdescentoptimizer(0.15).minimize(loss) accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)), tf.float32)) ep in range(30): mb in range(int(len(mnist.train.images)/40)): batch_xs, batch_ys = mnist.train.next_batch(40) sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
yes, expect hand coded specialized simple networks running on cpu run faster tensorflow ones. reason connected graph evaluation system tensorflow uses.
the benefit of using tensorflow when have more complex algorithms , want able test correctness first , able port use more machines , more processing units.
for example 1 thing can try run code on machine has gpu , see without changing in code speed up, maybe faster hand coded example linked. can see hand written code require considerable effort ported gpu.
Comments
Post a Comment