diff --git a/TensorFlow_with_GPU.ipynb b/TensorFlow_with_GPU.ipynb new file mode 100644 index 000000000000..1ffb45ecf561 --- /dev/null +++ b/TensorFlow_with_GPU.ipynb @@ -0,0 +1,162 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "TensorFlow with GPU", + "version": "0.3.2", + "provenance": [], + "collapsed_sections": [], + "toc_visible": true, + "include_colab_link": true + }, + "kernelspec": { + "display_name": "Python 2", + "name": "python2" + }, + "accelerator": "GPU" + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/dwcar49us/tutorials/blob/master/TensorFlow_with_GPU.ipynb)" + ] + }, + { + "metadata": { + "id": "BlmQIFSLZDdc", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Confirm TensorFlow can see the GPU\n", + "\n", + "Simply select \"GPU\" in the Accelerator drop-down in Notebook Settings (either through the Edit menu or the command palette at cmd/ctrl-shift-P)." + ] + }, + { + "metadata": { + "id": "3IEVK-KFxi5Z", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "2aed0996-09b4-4758-9a19-15ed20af1153" + }, + "cell_type": "code", + "source": [ + "import tensorflow as tf\n", + "device_name = tf.test.gpu_device_name()\n", + "if device_name != '/device:GPU:0':\n", + " raise SystemError('GPU device not found')\n", + "print('Found GPU at: {}'.format(device_name))" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Found GPU at: /device:GPU:0\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "QXRh0DPiZRyG", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Observe TensorFlow speedup on GPU relative to CPU\n", + "\n", + "This example constructs a typical convolutional neural network layer over a\n", + "random image and manually places the resulting ops on either the CPU or the GPU\n", + "to compare execution speed." + ] + }, + { + "metadata": { + "id": "t9ALbbpmY9rm", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "d84c9f11-fa31-4656-8425-b7034cdf6227" + }, + "cell_type": "code", + "source": [ + "import tensorflow as tf\n", + "import timeit\n", + "\n", + "# See https://www.tensorflow.org/tutorials/using_gpu#allowing_gpu_memory_growth\n", + "config = tf.ConfigProto()\n", + "config.gpu_options.allow_growth = True\n", + "\n", + "with tf.device('/cpu:0'):\n", + " random_image_cpu = tf.random_normal((100, 100, 100, 3))\n", + " net_cpu = tf.layers.conv2d(random_image_cpu, 32, 7)\n", + " net_cpu = tf.reduce_sum(net_cpu)\n", + "\n", + "with tf.device('/gpu:0'):\n", + " random_image_gpu = tf.random_normal((100, 100, 100, 3))\n", + " net_gpu = tf.layers.conv2d(random_image_gpu, 32, 7)\n", + " net_gpu = tf.reduce_sum(net_gpu)\n", + "\n", + "sess = tf.Session(config=config)\n", + "\n", + "# Test execution once to detect errors early.\n", + "try:\n", + " sess.run(tf.global_variables_initializer())\n", + "except tf.errors.InvalidArgumentError:\n", + " print(\n", + " '\\n\\nThis error most likely means that this notebook is not '\n", + " 'configured to use a GPU. Change this in Notebook Settings via the '\n", + " 'command palette (cmd/ctrl-shift-P) or the Edit menu.\\n\\n')\n", + " raise\n", + "\n", + "def cpu():\n", + " sess.run(net_cpu)\n", + " \n", + "def gpu():\n", + " sess.run(net_gpu)\n", + " \n", + "# Runs the op several times.\n", + "print('Time (s) to convolve 32x7x7x3 filter over random 100x100x100x3 images '\n", + " '(batch x height x width x channel). Sum of ten runs.')\n", + "print('CPU (s):')\n", + "cpu_time = timeit.timeit('cpu()', number=10, setup=\"from __main__ import cpu\")\n", + "print(cpu_time)\n", + "print('GPU (s):')\n", + "gpu_time = timeit.timeit('gpu()', number=10, setup=\"from __main__ import gpu\")\n", + "print(gpu_time)\n", + "print('GPU speedup over CPU: {}x'.format(int(cpu_time/gpu_time)))\n", + "\n", + "sess.close()" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Time (s) to convolve 32x7x7x3 filter over random 100x100x100x3 images (batch x height x width x channel). Sum of ten runs.\n", + "CPU (s):\n", + "10.0379359722\n", + "GPU (s):\n", + "0.98605799675\n", + "GPU speedup over CPU: 10x\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file