2017-12-30 44 views
0

私はスタンフォード大学のディープラーニング研究のためのCS 20SI:Tensorflowを採用しています。このコードではTensorflowを使用したMNISTでのロジスティック回帰のスピード

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データセットとロジスティック回帰が行われた:私は、次のコードについて質問があります。著者の状態:バッチサイズで私のMac、モデルのバッチバージョンに0.5秒

で128回の ランを実行

私はそれを実行したときただし、各エポックは約2秒かかり、約1分の実行時間を与えます。この例では時間がかかりますか?現在、私はOCなしのRyzen 1700(3.0GHz)とOCなしのGPU Gtx 1080を持っています。

答えて

1

私はGTX Titan X(Maxwell)でこのコードを試して、1エポックあたり約0.5秒を得ました。私はGTX 1080も同様の結果を得ることができるはずです。

最新のテンソルフローとcuda/cudnnバージョンをお試しください。環境変数が設定されていないことを確認します(どのGPUが表示されているか、どの程度のテンソルフローが使用できるかなど)。マイクロベンチマークを実行すると、指定したカードのFLOPSを達成できることがわかります。 Testing GPU with tensorflow matrix multiplication

関連する問題