Install Python 3.6/Keras 2.1.5/Tensorflow GPU 1.6/PyTorch (0.3.1.post2) on Windows 10 (3/12/2018)

NeilZ
4 min readMar 13, 2018

--

This procedure mostly follow Keras-TensorFlow-GPU-Windows-Installation with some tweaks to make it work with latest tensorflow version 1.6 and CUDA toolkit 9.0.

An updated version for the latest versions of toolkits are avaiable here.

Install Anaconda 5.1.0 with python 3.6

Anaconda3–5.1.0-Windows-x86_64

Update Anaconda. Open Anaconda Prompt and type

conda update conda
conda update --all

The second command may cause error

LinkError: post-link script failed for package defaults::ipykernel-4.8.2-py36_0

In this case run following commands in sequence:

conda update ipykernel
conda clean --all
conda update --all

If you run following command in an environment, you may get Access denied error:

conda update -n base conda#Error Messages:
ERROR conda.core.link:_execute(502): An error occurred while uninstalling package 'conda-forge::conda-4.5.4-py36_0'.
PermissionError(13, 'Access is denied')
Attempting to roll back.
Rolling back transaction: donePermissionError(13, 'Access is denied')# Solution:
(1) "Run as administrator" for 'Anaconda Prompt', re-run the command outside of any environment.
(2) Close current command line window, open a new one, conda should have been updated.

Install CUDA Tookit 9.0

Need register in Nvidia Developer Program before you can download.

Download and install:

N.B. CUDA 9.1 won’t work with tensorflow version 1.6.0 and below.

cuda_9.0.176_win10_network.exe
cuda_9.0.176.1_windows.exe #patch 1
cuda_9.0.176.2_windows.exe #patch 2

Download Nvidia cuDNN, a GPU-accelerated library of primitives for deep neural networks

cudnn-9.1-windows10-x64-v7.zip

Put your unzipped folder in C drive as follows:

C:\cudnn-9.1-windows10-x64-v7

Add cuDNN into Environment PATH

Add the following path in your Environment. Subjected to changes in your installation path.

C:\cudnn-9.1-windows10-x64-v7\cuda\bin

Close all the prompts. Open a new Anaconda Prompt to type the following command(s)

echo %PATH%

You shall see that the new Environment PATH is there.

Create an Anaconda environment with Python=3.6

Open Anaconda Prompt to type the following command(s)

conda create -n tensorflow python=3.6 numpy scipy matplotlib spyder

Activate the environment

Open Anaconda Prompt to type the following command(s)

activate tensorflow

Install TensorFlow on Windows

Open Anaconda Prompt to type the following command(s)

#Only install tensorflow for GPU
pip install --ignore-installed --upgrade tensorflow-gpu

Test Tensorflow

#Open a new terminal if not done yet
#activate tensorflow environment first
> activate tensorflow
#run python
> python
# Try following commands in python command line tool
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))

If the system outputs the following, then you are ready to begin writing TensorFlow programs:

Hello, TensorFlow!

Install Keras

pip install keras

Change backend to tensorflow.

The default backend of keras is theano, let’s change it to tensorflow

set "KERAS_BACKEND=tensorflow"
python
>>> import keras
#This will tell you the backend keras is using now
or you can simple run:
python -c "import keras"

To change the backend permanently, you need change the config in /%USERPROFILE%/.keras/keras.json

First find out %USERPROFILE% on window

echo %USERPROFILE%or you can use python as well:python
>>> import os
>>> print(os.path.expanduser('~'))
C:\Users\your_win_user_name
>>>

Once find the %USERPROFILE%, open %USERPROFILE%/.keras/keras.json and change “backend” to “tensorflow” as below

{
"epsilon": 1e-07,
"floatx": "float32",
"image_data_format": "channels_last",
"backend": "tensorflow"
}

Sometimes, this won’t work even after you modified the keras.json file, that’s because there’s an environment variable KERAS_BACKEND which is automatically set to ‘theano’ at startup.

To fix this, find a file called ‘keras_activate.bat’, it’s under

c:\ProgramData\Anaconda3\etc\conda\activate.d

Just delete the file: keras_activate.bat and re-open a new Anaconda prompt window.

It should work now.

Make Sure TensorFlow is running on GPU

Run

#Run this with activate tensorflow
import tensorflow as tf
# Creates a graph.
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))

If you see

Device mapping:   no known devices

Then you are not running on GPU.

Make sure you have installed tensorflow-gpu instead of tensorflow.

Make sure you don’t have extra copy of tensorflow installed outside of environment ‘tensorflow’ (as setup above)

Run following command with and without environment to verify:

python -c "import tensorflow as tf"

Tools for Checking Hardware

1. c:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\extras\demo_suite\deviceQuery.exe2. c:\Program Files\NVIDIA Corporation\NVSMI>nvidia-smi.exe

Error while import tensorflow as tf

import tensorflow as tfImportError: Could not find 'cudart64_90.dll'. TensorFlow requires that this DLL be installed in a directory that is named in your %PATH% environment variable. Download and install CUDA 9.0 from this URL: https://developer.nvidia.com/cuda-toolkit

It seems that tensorflow 1.6.0 doesn’t work with cuda 9.1, I have to install cuda 9.0 instead.

Install PyTorch (version: 0.3.1.post2)

conda install -c peterjc123 pytorch

Install Pandas

activate tensorflow
pip install pandas

Run Jupyter Notebook

1. use --notebook-dir to specify starting directory
jupyter notebook --notebook-dir=C:\work_dir
2. If you don't see 'conda' tab and no environment choice available
conda install nb_conda
3. Check if tensorflow works in jupyter notebook
import tensorflow as tf

Other Packages you may need

activate tensorflow
pip install h5py
conda install -c conda-forge bcolz
pip install keras-tqdm
conda install eli5
conda install shap
conda update scikit-learn #update sklearn package under conda
conda install -c conda-forge lightgbm #install lightgbm
conda install -c conda-forge opencv #cv2

Install Python packages through GIT

# Example, this installs pdpbox
conda install git pip
pip install git+git://github.com/SauceCat/PDPbox.git

Errors you may see

1. Error while import keras module
from keras import metrics
--------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa
Solution: This may indicate that you need update your numpy version. Goto Anaconda terminal:
conda update numpy

--

--