Setup a Python Environment for Machine Learning and Deep Learning | by Hussnain Fareed | Towards Data Science

Interest in Machine Learning and Deep Learning has exploded over the past decade. You see machine learning in computer science programs, industry conferences, and in many applications in daily life.
I am assuming you already know about Machine Learning, therefore I will not be explaining What and Why.
So, I find many beginners facing problems while installing libraries and setting up the environment. As I have faced the first time when I was trying. So this guide is totally for beginners.
In this story, I will tell you how you can easily set up a python environment on your system. I am using Windows but this guide is also suitable for Ubuntu & Linux users.
After completing this tutorial, you will have a working Python environment to begin learning and developing machine learning and deep learning software.
First of all to perform machine learning and deep learning on any dataset, the software/program requires a computer system powerful enough to handle the computing power necessary. So the following is required:
Central Processing Unit (CPU) — Intel Core i5 6th Generation processor or higher. An AMD equivalent processor will also be optimal.RAM — 8 GB minimum, 16 GB or higher is recommended.Graphics Processing Unit (GPU) — NVIDIA GeForce GTX 960 or higher. AMD GPUs are not able to perform deep learning regardless. For more information on NVIDIA GPUs for deep learning please visit https://developer.nvidia.com/cuda-gpus.Operating System — Ubuntu or Microsoft Windows 10. I recommend updating Windows 10 to the latest version before proceeding forward.
Central Processing Unit (CPU) — Intel Core i5 6th Generation processor or higher. An AMD equivalent processor will also be optimal.
RAM — 8 GB minimum, 16 GB or higher is recommended.
Graphics Processing Unit (GPU) — NVIDIA GeForce GTX 960 or higher. AMD GPUs are not able to perform deep learning regardless. For more information on NVIDIA GPUs for deep learning please visit https://developer.nvidia.com/cuda-gpus.
Operating System — Ubuntu or Microsoft Windows 10. I recommend updating Windows 10 to the latest version before proceeding forward.
Note: In the case of laptops, the ideal option would be to purchase a gaming laptop from any vendor deemed suitable such as Alienware, ASUS, Lenovo Legion, Acer Predator, etc.
Let’s just get straight to the installation process. we are gonna hit the rock 😉
This tool is designed to update your NVIDIA GPU drivers and it is far easier to do it like this and it is highly recommended to be installed if you have an NVIDIA GPU.
Download NVIDIA GeForce Experience
In this tutorial, we will cover the following steps:
Download AnacondaInstall Anaconda & PythonStart and Update AnacondaInstall CUDA Toolkit & cuDNNCreate an Anaconda EnvironmentInstall Deep Learning API’s (TensorFlow & Keras)
Download Anaconda
Install Anaconda & Python
Start and Update Anaconda
Install CUDA Toolkit & cuDNN
Create an Anaconda Environment
Install Deep Learning API’s (TensorFlow & Keras)
In this step, we will download the Anaconda Python package for your platform.
Anaconda is a free and easy-to-use environment for scientific Python.
1.Install Anaconda (Python 3.6 version) Download
I am using Windows you can choose according to your OS.
In this step, we will install the Anaconda Python software on your system.
Installation is very easy and quick once you download the setup. Open the setup and follow the wizard instructions.
#Note: It will automatically install Python and some basic libraries with it.
It might take 5 to 10 minutes or some more time according to your system.
Open Anaconda Prompt to type the following command(s). Don’t worry Anaconda Prompt works the same as cmd.
conda update condaconda update --all
Install CUDA Toolkit 9.0 or 8.0 Download
Install CUDA Toolkit 9.0 or 8.0 Download
Choose your version depending on your Operating System and GPU.
#Version Support: Here is a guide to check that if your version supports your Nvidia Graphic Card
For downloading other versions you can follow this link: https://developer.nvidia.com/cuda-toolkit-archive
#Note: CUDA 9.0 is recommended as TensorFlow is NOT compatible with CUDA Toolkit 9.1 and 9.2 versions. Kindly choose the CUDA version according to your Nvidia GPU version to avoid errors.
#Note: People with version 9.0 Download can also install the given patch in any case of error while proceeding.
2. Download cuDNN Download
Download the latest version of cuDNN. Choose your version depending on your Operating System and CUDA. Membership registration is required. Don’t worry you can easily create an account using your email.
Put your unzipped folder in C drive as follows:
C:\cudnn-9.0-windows10-x64-v7
Open Run dialogue using (Win + R) and run the command sysdm.cplIn Window-10 System Properties, please select the Tab Advanced.Select Environment VariablesAdd the following path to your Environment.
Open Run dialogue using (Win + R) and run the command sysdm.cpl
In Window-10 System Properties, please select the Tab Advanced.
Select Environment Variables
Add the following path to your Environment.
C:\cudnn-9.0-windows10-x64-v7\cuda\bin
Here we will create a new anaconda environment for our specific usage so that it will not affect the root of Anaconda. Amazing!! isn’t it? 😛
Open Anaconda Prompt to type the following commands.
Create a conda environment named “tensorflow” (you can change the name) by invoking the following command:
Create a conda environment named “tensorflow” (you can change the name) by invoking the following command:
conda create -n tensorflow pip python=3.6
2. Activate the conda environment by issuing the following command:
activate tensorflow (tensorflow)C:>  # Your prompt should change
In this step, we will install Python libraries used for deep learning, specifically: TensorFlow, and Keras.
TensorFlow
TensorFlow
TensorFlow is a tool for machine learning. While it contains a wide range of functionality, TensorFlow is mainly designed for deep neural network models.
=> For installing TensorFlow, Open Anaconda Prompt to type the following commands.
To install the GPU version of TensorFlow:
C:\> pip install tensorflow-gpu
To install the CPU-only version of TensorFlow:
C:\> pip install tensorflow
If your machine or system is the only CPU supported you can install CPU version for basic learning and practice.
=> You can test the installation by running this program on shell:
>>> import tensorflow as tf>>> hello = tf.constant('Hello, TensorFlow!')>>> sess = tf.Session()>>> print(sess.run(hello))
For getting started and documentation you can visit TensorFlow website.
2. Keras
Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano.
=> For installing Keras Open Anaconda Prompt to type the following commands.
pip install keras
=> Let’s try running Mnist_Mlp.Py in your prompt. you can use other examples as well.
Open Anaconda Prompt to type the following commands.
activate tensorflowpython mnist_mlp.py
For getting started and documentation you can visit Keras website.
Here is an implementation of Keras Standard Fully Connected Neural Network using Python for Digit Recognition I have done.
There are some other famous libraries like Pytorch, Theano, and Caffe2 you can use as per your choice and use.
Congratulations! 😉 You have successfully created the environment using TensorFlow, Keras (with Tensorflow backend) over GPU on Windows!