-
-
Save mpizosdim/1f2bd9a5cfe5d9ab37f94864720595aa to your computer and use it in GitHub Desktop.
TensorFlow: GPU vs CPU in CNN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tensorflow.examples.tutorials.mnist import input_data | |
import tensorflow as tf | |
import time | |
def inference(images, keep_prob): | |
images_re = tf.reshape(images, [-1, 28, 28, 1]) | |
with tf.name_scope("Conv1"): | |
W_conv1 = tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1, dtype=tf.float32), name='W_conv1') | |
b_conv1 = tf.Variable(tf.constant(0.1, shape=[32])) | |
h_conv1 = tf.nn.relu(tf.nn.conv2d(images_re, W_conv1, strides=[1, 1, 1, 1], padding='SAME') + b_conv1) | |
with tf.name_scope("Pool1"): | |
pool1_op = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1], | |
strides=[1, 2, 2, 1], padding='SAME') | |
with tf.name_scope("Conv2"): | |
W_conv2 = tf.Variable(tf.truncated_normal([5, 5, 32, 64], stddev=0.1, dtype=tf.float32), name='W_conv2') | |
b_conv2 = tf.Variable(tf.constant(0.1, shape=[64])) | |
h_conv2 = tf.nn.relu(tf.nn.conv2d(pool1_op, W_conv2, strides=[1, 1, 1, 1], padding='SAME') + b_conv2) | |
with tf.name_scope("Pool2"): | |
pool2_op = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1], | |
strides=[1, 2, 2, 1], padding='SAME') | |
with tf.name_scope("Densely1"): | |
W_Dens1 = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.1, dtype=tf.float32), name='W_dens1') | |
b_Dens1 = tf.Variable(tf.constant(0.1, shape=[1024])) | |
h_pool2_flat = tf.reshape(pool2_op, [-1, 7 * 7 * 64]) | |
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_Dens1) + b_Dens1) | |
with tf.name_scope("Droupout"): | |
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) | |
with tf.name_scope("Readout"): | |
W_fc2 = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1, dtype=tf.float32), name='W_dens1') | |
b_fc2 = tf.Variable(tf.constant(0.1, shape=[10])) | |
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 | |
return y_conv | |
def main(): | |
n_classes = 10 | |
n_inputs = 784 | |
x = tf.placeholder(tf.float32, [None, n_inputs]) | |
y_ = tf.placeholder(tf.float32, [None, n_classes]) | |
keep_prob = tf.placeholder(tf.float32) | |
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) | |
y_conv = inference(x, keep_prob) | |
cross_entropy = tf.reduce_mean( | |
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv) | |
) | |
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) | |
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) | |
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) | |
start_time = time.time() | |
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) # , device_count = {'GPU': 0} | |
writer = tf.summary.FileWriter("./graphs", sess.graph) | |
sess.run(tf.initialize_all_variables()) | |
for i in range(20000): | |
batch = mnist.train.next_batch(50) | |
if i % 100 == 0: | |
train_accuracy = sess.run(accuracy, feed_dict={ | |
x: batch[0], y_: batch[1], keep_prob: 0.75}) | |
print("step %d, training accuracy %g" % (i, train_accuracy)) | |
sess.run(train_step, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) | |
test_accuracy = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}) | |
print("test accuracy %g" % test_accuracy) | |
end_time = time.time() | |
print("time to train and output the test prediction was: %d " % (end_time - start_time)) | |
writer.close() | |
if __name__ == "__main__": | |
main() | |
# gpu 2 layer: 284 sec | |
# cpu 2 layer: 2080 sec | |
# gpu 4 layers: 953 sec | |
# cpu 4 layers: 7991 sec |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What about time to score the models?