【发布时间】:2016-08-15 06:35:48
【问题描述】:
我一直在各种框架中试验简单的基本(入门教程级)神经网络,但对我在 TensorFlow 中看到的性能感到困惑。
例如,来自Michael Nielsen's tutorial 的简单网络(在具有 30 个隐藏节点的网络中使用 L2 随机梯度下降的 MNIST 数字识别)的性能比Nielsen's basic NumPy code 的版本略有调整(使用one of the tutorial exercises 中建议的小批量矢量化)。
在单个 CPU 上运行的 TensorFlow 是否总是表现不佳?是否有我应该调整的设置以提高性能?或者 TensorFlow 是否真的只在更复杂的网络或学习机制中表现出色,因此对于这种简单的玩具案例预计不会有很好的表现?
from __future__ import (absolute_import, print_function, division, unicode_literals)
import tensorflow as tf
from 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 and 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))
for ep in range(30):
for 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})
【问题讨论】:
-
next_batch花了多少时间?该功能比使用简单 Numpy 切片的 Michael Nielsen 的版本做更多的事情 -
@YaroslavBulatov:有没有办法禁用“更多的东西”,使其完全符合迈克尔尼尔森的版本,所以我有一个更直接的比较?
-
也许您可以将 TensorFlow 插入到 Michael Nielsen 的版本中?
-
IE,Michael Nielsen 使用切片来执行类似 next_batch = data[slice...] 的操作,因此您可以直接执行此操作,而根本不使用“mnist.train.next_batch”。用于学习目的的“tensorflow.examples”中的内容并非高效。在一种情况下,我在该目录中看到一个示例,与更高效的版本相比,它的速度降低了 200 倍。
标签: python performance machine-learning tensorflow