Install Tensorflow(GPU support) with Anaconda on Windows 10

This guide is an enhanced version of the official installation instruction. It includes more details and explanations. You can find the original version here.

About TensorFlow

TensorFlow™ is an open source software library for numerical computation using data flow graphs. Nodes in the graph represent mathematical operations, while the graph edges represent the multidimensional data arrays (tensors) communicated between them. The flexible architecture allows you to deploy computation to one or more CPUs or GPUs in a desktop, server, or mobile device with a single API. TensorFlow was originally developed by researchers and engineers working on the Google Brain Team within Google’s Machine Intelligence research organization for the purposes of conducting machine learning and deep neural networks research, but the system is general enough to be applicable in a wide variety of other domains as well.

Determine which Tensorflow to install

There are two version of TensorFlow:

  • TensorFlow with CPU support only. If you only want to play around with TensorFlow or your system does not have a NVIDIA GPU, I recommend you install this version. Note that this version of TensorFlow is much easier to install.
  • TensorFlow with GPU support. If your system has a valid NVIDIA GPU and you want to build some real neural networks or deep networks which definitely need massive computation, I recommend you install this version. TensorFlow programs typically run much faster on a GPU than on a CPU. Here is an article about benchmark CPU and GPU performance.

In following parts of this guide, I will explain how to install TensorFlow with GPU support.

Requirements to run TensorFlow with GPU support

Attention!!! You must download and install the specified versions. This is very important. For example, the cuDNN version must match exactly: TensorFlow will not load if it cannot find cuDNN64_6.dll.

Check the CUDA Compute Capability of your GPU card.

It should be 3.0 or higher. See NVIDIA documentation for a list of supported GPU cards. If your GPU card capability is lower than 3.0 or you can’t find your GPU card in the list, you can just install the only CPU supported version or spend some dollars to buy a powerful NVIDIA GPU card. BTW, I’m using the GTX1080Ti.

CUDA Toolkit 8.0

what is CUDA?

CUDA is a parallel computing platform and programming model that makes using a GPU for general purpose computing simple and elegant. The developer still programs in the familiar C, C++, Fortran, or an ever expanding list of supported languages, and incorporates extensions of these languages in the form of a few basic keywords. Check this out!

How to install it?

Note that the version should be 8.0, not the newest version of CUDA Toolkit which is already 9.0 now and TensorFlow still does not support the newest version.

We can just google “cuda toolkit 8.0”, then click the first one. Here is the link.

cuda-toolkit-8

Then we set the right options. I recommend you to use the local installer in case we may need to install it again in the future.

cuda-toolkit-8-download

Create a new folder inside C:\Program Files, name it “NIVIDIA CUDA Computing Toolkit”. That is where we are going to put our CUDA toolkit in.

cuda-toolkit-8-install

After we successfully install it, we will have this file structure.

cuda-toolkit-8-done

We come the most important part. Ensure that you append the relevant Cuda pathnames to the %PATH% environment variable as described in the NVIDIA documentation. Open your Control panel, select System and Security, click System. Then enter Advanced system settings.

control-panel

Inside the Environment Variables, we choose Edit Path, then add two new variables.

  • C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin
  • C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\libnvvp

add-envir-variables

Install NVIDIA drivers associated with CUDA Toolkit 8.0

Since I have used NIVIDIA GPU for a very long time, I already have its driver installed in my computer. If you are new users, you can find and download drivers here. You can manually setup search options or let it automatically search the right driver.

nvidia-drivers

Download cuDNN v6.1

Note that you should download cuDNN v6.1 not the newest version which is already cuDNN 7.0 nowadays.

Here is the link to download cuDNN. Click the “Download” button which is very easy to be found at the middle of this webpage. For this part, you may need to create a new account to join the NVIDIA developer program. It will not charge you any money. Just relax and get enrolled. After you done this, you will be able to see the download options. Remeber that we need to choose cuDNN v6.0 Library for CUDA 8.0 and Windows 10.

cudnn-download

After download, unzip it into C:\Program Files.

cudnn-folder

Ensure that you add the directory where you installed the cuDNN DLL to your %PATH%environment variable. Follow what we did above, but this time we add cuDNN DLL into PATH.

  • C:\Program Files\cuda\bin
  • cudnn-path

So far, we already get everything we need for the GPU supported version. We almost there!

Determine how to install TensorFlow

There are two ways to install TensorFlow:

  • “native” pip. Native pip installs TensorFlow directly on your system without going through a virtual environment. Since a native pip installation is not walled-off in a separate container, the pip installation might interfere with other Python-based installations on your system. However, if you understand pip and your Python environment, a native pip installation often entails only a single command! Furthermore, if you install with native pip, users can run TensorFlow programs from any directory on the system.
  • Anaconda. Anaconda is the most popular python data science platform, I recommend you to use this way to install TensorFlow. In Anaconda, you may use conda to create a virtual environment. However, within Anaconda, we recommend installing TensorFlow with the pip install command, not with the conda install command since the conda package is community supported, not officially supported. That is, the TensorFlow team neither tests nor maintains this conda package. Use that package at your own risk.

Installing with Anaconda

  1. Follow the instructions on the Anaconda download site to download and install the right version of Anaconda.
  2. Create a conda environment named tensorflow by invoking the following command:
    1
    C:> conda create -n tensorflow python=3.6
  3. Activate the conda environment by issuing the following command:
    1
    2
    C:> activate tensorflow
    (tensorflow)C:> # Your prompt should change
  4. Issue the appropriate command to install TensorFlow inside your conda environment. To install the GPU version of TensorFlow, enter the following command:
    1
    (tensorflow)C:> pip install --ignore-installed --upgrade tensorflow-gpu 
    In case someone want to use CPU-only version, here is the command:
    1
    pip install --ignore-installed --upgrade tensorflow 

Validate your installation

Let’s check if it is work or not. Open Anaconda Prompt, activate our Anaconda environment.

1
(C:\Users\dbld2\Anaconda3) C:\Users\dbld2>activate tensorflow

Invoke python from our shell as follows:

1
(tensorflow) C:\Users\dbld2>python

Enter the following short program inside the python interactive shell:

1
2
3
4
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

we can see this result in the Anaconda Promote:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Python 3.6.3 |Anaconda, Inc.| (default, Nov  8 2017, 15:10:56) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
2017-11-21 03:21:23.240922: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2017-11-21 03:21:23.492298: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:1030] Found device 0 with properties:
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:01:00.0
totalMemory: 11.00GiB freeMemory: 9.10GiB
2017-11-21 03:21:23.492638: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
# The above information implies that your TensorFlow is using your GPU to do the computation.
>>> print(sess.run(hello))
b'Hello, TensorFlow!'
>>>

Congratulations! We did it! You are all set!

If you are new to TensorFlow, see Getting Started with TensorFlow. Also, check this, MNIST for ML Beginners. MNIST has become the canonical dataset for trying out a new machine learning toolkit. This is the super famous “Hello, World!” in ML.


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!