Using the GPU(the video card in your PC or laptop) with Tensorflow is a lot faster than the fastest CPU(processor). Even a laptop GPU will beat a 2 x AMD Opteron 6168 1.9 GHz Processor (2×12 cores total)¹. Luckily, my Windows 10 laptop has a NVIDIA GeForce GTX 1050 video card and decided to use it for machine learning while away.
Ingredients:
- Latest GPU driver NVIDIA GeForce GTX 1050
- CUDA 9.0 Toolkit
- CUDA 9.0 Tookit Patch 4
- NVIDIA CUDA Deep Neural Network library
- Anaconda3 v4.1.1
- Python 3.5
- Tensorflow with GPU 1.12
Instructions:
- Install the latest GPU driver. Reboot.
- Install CUDA 9.0 Toolkit
- Install the CUDA 9.0 Toolkit Patch 4
- Extract the CUDA Deep Neural Network Library to <cudnn folder> of your choice
- Install Anaconda
- Open a command prompt and set PATH for CUDA and cudnn
SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin;%PATH% SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\extras\CUPTI\libx64;%PATH% SET PATH=&amp;lt;cudnn folder&amp;gt;\cuda\bin;%PATH%
- Reboot.
- Let’s create a Python 3.5 Environment. It is a good practice to create an environment for our particular need because some dependencies are only compatible with Python 3.5. We don’t want to mess up our setup when we accidentally update the root environment of Python. Open a command prompt and run code below.
conda create -n tf-gpu python=3.5.2
The tf-gpu is the name of our environment. You can choose any name.
- Let’s activate the new environment
activate tf-gpu (tf-gpu) D:\>
- After activating the environment, let’s install Tensorflow version 1.12 using PIP – Python package manager.
(tf-gpu) D:\>pip install --upgrade tensorflow-gpu==1.12.0
- Let’s verify the install.
(tf-gpu) D:\>python -c "import tensorflow as tf; tf.enable_eager_execution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))"
Sample output:
(tf-gpu) D:\>python -c "import tensorflow as tf; tf.enable_eager_execution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))" 2019-01-16 09:07:42.324696: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 2019-01-16 09:07:43.288456: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties: name: GeForce GTX 1050 major: 6 minor: 1 memoryClockRate(GHz): 1.493 pciBusID: 0000:01:00.0 totalMemory: 4.00GiB freeMemory: 3.30GiB 2019-01-16 09:07:43.293825: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0 2019-01-16 09:07:45.237102: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix: 2019-01-16 09:07:45.241664: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988] 0 2019-01-16 09:07:45.243892: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0: N 2019-01-16 09:07:45.247982: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 3020 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1050, pci bus id: 0000:01:00.0, compute capability: 6.1) tf.Tensor(240.87163, shape=(), dtype=float32)
- Now, that Tensorflow is installed, we can now start our journey to machine learning –> deep neural network.
- If you don’t have a GPU that supports CUDA Deep Neural Network Library, skip 1, 2, 3, 4, 6 and replace 10 with
(tf-gpu) D:\&amp;gt;pip install --upgrade tensorflow==1.12.0
To avoid headaches in your setup, install the exact version specified in our ingredients. Been there, been that!