Skip to content

Instantly share code, notes, and snippets.

@danielpcox
Created August 25, 2018 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielpcox/36b5f234de24b57c65bac86108d3b076 to your computer and use it in GitHub Desktop.
Save danielpcox/36b5f234de24b57c65bac86108d3b076 to your computer and use it in GitHub Desktop.
TensorFlow with GPU
Display the source blob
Display the rendered blob
Raw
{
"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"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"[View in Colaboratory](https://colab.research.google.com/gist/danielpcox/36b5f234de24b57c65bac86108d3b076/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": "0940e8b1-5283-4b2d-e3e6-8853f3deb804"
},
"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": 0,
"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": "c5ee74e3-f88b-4660-dcba-354d09fa5659"
},
"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": 0,
"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",
"8.350230318000058\n",
"GPU (s):\n",
"0.1842791589999706\n",
"GPU speedup over CPU: 45x\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment