Machine Learning/Deep Learning Toolkit Installation on Windows 10

NeilZ
5 min readJun 11, 2019

--

Python 3.7/Keras 2.2.4/Tensorflow-GPU 1.13.1/PyTorch 0.3.1.post2/CUDA Toolkit 10.0/cuDNN 7.6.1/Jupyter Notebook/xgboost (June 2019)

Install Anaconda 2019.03 python 3.7 version

  • Download Anaconda3–2019.03-Windows-x86_64.exe
  • Install Anaconda3 following the GUI instructions
  • I installed under the default location using the default options. The ProgramData folder is hidden, so you’ll only see it if you show hidden files in File Explorer.
C:\ProgramData\Anaconda3

Install CUDA Toolkit 10.0

Install cuDNN 7.6.0 for CUDA 10.0

C:\cudnn-10.0-windows10-x64-v7.6.0.64

Add cuDNN 7.6.0 into Windows 10 Environment PATH

  • Type path in windows 10 search box
  • Open settings: Edit the System environment variables from control panel
  • In the Advanced tab, click Environment Variables…
  • Select Path under User variables for your-user-name and click Edit...
  • In the new window Edit environment variable , click New and paste following path into the text box
C:\cudnn-10.0-windows10-x64-v7.6.0.64\cuda\bin
  • Click OK to close the window, and OK again to close the Environment Variables window and System Properties window
  • Open a Anaconda Prompt and type echo %PATH% , you should be able to see the newly added path in the output.

Setup Environment for Deep Learning on GPU

Create virtual environment

  • Open an Anaconda Prompt
  • Type following command to create an environment called tf

conda create -n tf python=3.7 numpy scipy matplotlib spyder pandas

Install Tensorflow

  • Open an Anaconda Prompt
  • Activate your environment first
activate tf
  • Install tensorflow-gpu 1.13.1
pip install --ignore-installed --upgrade tensorflow-gpu==1.13.1
  • You may see an error
ERROR: astroid 2.2.5 requires typed-ast>=1.3.0; implementation_name == "cpython", which is not installed.
  • Try install astroid independently first
pip install astroid
  • Try install tensorflow-gpu 1.13.1 again
pip install --ignore-installed --upgrade tensorflow-gpu==1.13.1
  • It should work now

Test tensorflow

  • Open a new Anaconda prompt window
#activate tf environment first
> activate tf
#run python
> python
# Type 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

Test if Keras is using TensorFlow as backend

# activate 'tf' environment
(base) C:\Users\your-user-name>activate tf
(tf) C:\Users\your-user-name>python -c "import keras"
Using TensorFlow backend.

keras should be already using TensorFlow as backend.

Make Sure TensorFlow is running on GPU

Run

#Activate 'tf' environment
#Run python
>>>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))

Output on my computer

MatMul: (MatMul): /job:localhost/replica:0/task:0/device:GPU:0
2019-06-10 21:15:07.721041: I tensorflow/core/common_runtime/placer.cc:1059] MatMul: (MatMul)/job:localhost/replica:0/task:0/device:GPU:0
a: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2019-06-10 21:15:07.726428: I tensorflow/core/common_runtime/placer.cc:1059] a: (Const)/job:localhost/replica:0/task:0/device:GPU:0
b: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2019-06-10 21:15:07.731839: I tensorflow/core/common_runtime/placer.cc:1059] b: (Const)/job:localhost/replica:0/task:0/device:GPU:0
[[22. 28.]
[49. 64.]]

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 tf (as setup above)

Run following command with and without environment to verify:

python -c "import tensorflow as tf"

Running Jupyter Notebook

jupyter notebook --notebook-dir=D:\Downloads

If you see error as below, it means the jupyter-notebook.exe script is not in system PATH

Error executing Jupyter command 'notebook': [Errno 'jupyter-notebook' not found] 2

Add the jupyter-notebook.exe script to PATH, you can find it in directory:

C:\ProgramData\Anaconda3\Scripts

Open a new Anaconda prompt and re-try, jupyter notebook should start.

Setup Jupyter Notebook to start at a specific directory

  • First, generate the default configuration file
jupyter notebook --generate-config

This should create a default configuration file, e.g.

C:\Users\your-user-name\.jupyter\jupyter_notebook_config.py
  • Edit the configuration file, jupyter_notebook_config.py , set the start up directory you want, e.g.
c.NotebookApp.notebook_dir = 'D:\Kaggle'

Don’t forget the un-comment the # at the beginning of the line.

  • Run jupyter notebook again, you should be at the specified directory.

Import Python libraries from Jupyter Notebook

  • When you import a module in jupyter notebook, you might encounter following error
import holidays
ModuleNotFoundError
: No module named 'holidays'
  • To fix this, you need add the path to the library into sys.path.
  • If you are working within a virtual environment, e.g. tf . You can add following paths into the sys.path such as below
import sysextra_paths = ['', 
'C:\\Users\\your-user-name\\.conda\\envs\\tf\\python37.zip', 'C:\\Users\\your-user-name\\.conda\\envs\\tf\\DLLs', 'C:\\Users\\your-user-name\\.conda\\envs\\tf\\lib', 'C:\\Users\\your-user-name\\.conda\\envs\\tf',
'C:\\Users\\your-user-name\\.conda\\envs\\tf\\lib\\site-packages', 'C:\\Users\\your-user-name\\.conda\\envs\\tf\\lib\\site-packages\\win32',
'C:\\Users\\your-user-name\\.conda\\envs\\tf\\lib\\site-packages\\win32\\lib',
'C:\\Users\\your-user-name\\.conda\\envs\\tf\\lib\\site-packages\\Pythonwin']
sys.path = sys.path + extra_paths
print(sys.path)
import holidays #Should work now
  • The xgboost might be an exception. When you import xgboost , you may still get an error as below
XGBoostLibraryNotFound: Cannot find XGBoost Library in the candidate path, did you install compilers and run build.sh in root path?
List of candidates:
C:\Users\your-user-name\.conda\envs\tf\lib\site-packages\xgboost\xgboost.dll
C:\Users\your-user-name\.conda\envs\tf\lib\site-packages\xgboost\../../lib/xgboost.dll
C:\Users\your-user-name\.conda\envs\tf\lib\site-packages\xgboost\./lib/xgboost.dll
C:\ProgramData\Anaconda3\xgboost\xgboost.dll
C:\Users\your-user-name\.conda\envs\tf\lib\site-packages\xgboost\../../windows/x64/Release/xgboost.dll
C:\Users\your-user-name\.conda\envs\tf\lib\site-packages\xgboost\./windows/x64/Release/xgboost.dll

To fix the issue, just search for xgboost.dll file under your C:\Users\your-user-name directory. If you have installed xgboost in your virtual environment, you should find a copy of the dll file somewhere.

To install xgboost within Anaconda virtual environment, just type

pip install xgboost

Once find the xgboost.dll file, copy it to one of the paths mentioned in the error message, e.g. C:\ProgramData\Anaconda3\xgboost . Import should work now.

Update conda

conda update -n base -c defaults conda

Install PyTorch 1.1.0

conda install pytorch torchvision cudatoolkit=10.0 -c pytorch

Test PyTorch

Type python and run following commands:

>>> import torch
>>> torch.cuda.is_available()
True
>>> from __future__ import print_function
>>> import torch
>>> x = torch.rand(5, 3)
>>> print(x)
tensor([[0.8621, 0.8517, 0.9397],
[0.7043, 0.5053, 0.7151],
[0.6190, 0.0841, 0.3810],
[0.8150, 0.0539, 0.9147],
[0.2369, 0.9813, 0.9763]])
>>>

Link to the old version of instructions for CUDA 9 and Tensorflow 1.6

--

--