使用 Tensorflow 对 MNIST 进行逻辑回归的速度
Speed of Logistic Regression on MNIST with Tensorflow
我正在参加 CS 20SI:斯坦福深度学习研究的 Tensorflow。我对以下代码有疑问:
import time
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# Step 1: Read in data
# using TF Learn's built in function to load MNIST data to the folder data/mnist
MNIST = input_data.read_data_sets("/data/mnist", one_hot=True)
# Batched logistic regression
learning_rate = 0.01
batch_size = 128
n_epochs = 25
X = tf.placeholder(tf.float32, [batch_size, 784], name = 'image')
Y = tf.placeholder(tf.float32, [batch_size, 10], name = 'label')
#w = tf.Variable(tf.random_normal(shape = [int(shape[1]), int(Y.shape[1])], stddev = 0.01), name='weights')
#b = tf.Variable(tf.zeros(shape = [1, int(Y.shape[1])]), name='bias')
w = tf.Variable(tf.random_normal(shape=[784, 10], stddev=0.01), name="weights")
b = tf.Variable(tf.zeros([1, 10]), name="bias")
logits = tf.matmul(X,w) + b
entropy = tf.nn.softmax_cross_entropy_with_logits( logits=logits, labels=Y)
loss = tf.reduce_mean(entropy) #computes the mean over examples in the batch
optimizer = tf.train.GradientDescentOptimizer(learning_rate = learning_rate).minimize(loss)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
n_batches = int(MNIST.train.num_examples/batch_size)
for i in range(n_epochs):
start_time = time.time()
for _ in range(n_batches):
X_batch, Y_batch = MNIST.train.next_batch(batch_size)
opt, loss_ = sess.run([optimizer, loss], feed_dict = {X: X_batch, Y:Y_batch})
end_time = time.time()
print('Epoch %d took %f'%(i, end_time - start_time))
在此代码中,执行了 MNIST 数据集的逻辑回归。作者声明:
Running on my Mac, the batch version of the model with batch size 128
runs in 0.5 second
然而,当我 运行 它时,每个纪元大约需要 2 秒,总执行时间约为一分钟。这个例子花费那么多时间合理吗?目前我有一个不带 OC (3.0GHz) 的 Ryzen 1700 和一个不带 OC 的 GPU Gtx 1080。
我在 GTX Titan X (Maxwell) 上试过这段代码,每个 epoch 大约有 0.5 秒。我希望 GTX 1080 应该能够得到类似的结果。
尝试使用最新的 tensorflow 和 cuda/cudnn 版本。确保没有限制(哪些 GPU 可见,tensorflow 可以使用多少内存等)环境变量集。您可以尝试 运行 微基准测试,看看您是否可以达到卡的规定 FLOPS,例如
我正在参加 CS 20SI:斯坦福深度学习研究的 Tensorflow。我对以下代码有疑问:
import time
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# Step 1: Read in data
# using TF Learn's built in function to load MNIST data to the folder data/mnist
MNIST = input_data.read_data_sets("/data/mnist", one_hot=True)
# Batched logistic regression
learning_rate = 0.01
batch_size = 128
n_epochs = 25
X = tf.placeholder(tf.float32, [batch_size, 784], name = 'image')
Y = tf.placeholder(tf.float32, [batch_size, 10], name = 'label')
#w = tf.Variable(tf.random_normal(shape = [int(shape[1]), int(Y.shape[1])], stddev = 0.01), name='weights')
#b = tf.Variable(tf.zeros(shape = [1, int(Y.shape[1])]), name='bias')
w = tf.Variable(tf.random_normal(shape=[784, 10], stddev=0.01), name="weights")
b = tf.Variable(tf.zeros([1, 10]), name="bias")
logits = tf.matmul(X,w) + b
entropy = tf.nn.softmax_cross_entropy_with_logits( logits=logits, labels=Y)
loss = tf.reduce_mean(entropy) #computes the mean over examples in the batch
optimizer = tf.train.GradientDescentOptimizer(learning_rate = learning_rate).minimize(loss)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
n_batches = int(MNIST.train.num_examples/batch_size)
for i in range(n_epochs):
start_time = time.time()
for _ in range(n_batches):
X_batch, Y_batch = MNIST.train.next_batch(batch_size)
opt, loss_ = sess.run([optimizer, loss], feed_dict = {X: X_batch, Y:Y_batch})
end_time = time.time()
print('Epoch %d took %f'%(i, end_time - start_time))
在此代码中,执行了 MNIST 数据集的逻辑回归。作者声明:
Running on my Mac, the batch version of the model with batch size 128 runs in 0.5 second
然而,当我 运行 它时,每个纪元大约需要 2 秒,总执行时间约为一分钟。这个例子花费那么多时间合理吗?目前我有一个不带 OC (3.0GHz) 的 Ryzen 1700 和一个不带 OC 的 GPU Gtx 1080。
我在 GTX Titan X (Maxwell) 上试过这段代码,每个 epoch 大约有 0.5 秒。我希望 GTX 1080 应该能够得到类似的结果。
尝试使用最新的 tensorflow 和 cuda/cudnn 版本。确保没有限制(哪些 GPU 可见,tensorflow 可以使用多少内存等)环境变量集。您可以尝试 运行 微基准测试,看看您是否可以达到卡的规定 FLOPS,例如