First Steps — Deep Learning using Python and Keras
By: Prateek Karkare
Tags: Keras, Artificial Intelligence, Python, TensorFlow, Machine Learning

For quite sometime now you have been reading about Machine Learning articles and concepts here on X8 and hopefully all over the internet. You read about introduction to various algorithms from Linear regression to Neural Networks to Decision Trees and Random Forests. We saw how Neural Networks specifically CNNs work and are good at image classification tasks. It will be fun to implement a small CNN and see things in action!

But before actually going ahead and implementing a neural network we will have to equip our computer with the right set of tools to enable us to implement a neural network quickly. There are various tools and languages which can help us do that but this and the next article will just talk about a Python Anaconda environment, Jupyter notebooks and relevant python packages. We will be using a machine learning framework called Keras which runs on top of TensorFlow library for our mini-implementation. We will not talk about GPU acceleration in this article as this is targeted towards a first timer foraying into Deep learning. All the setup in this article assumes a windows 10 system but you can easily find the setup steps of those tools for other systems as well.

First, let us look at what those fancy reptilious words in the above paragraph are about.

Python

Photo by Sharon McCutcheon on Unsplash

Python is one of the most used programming languages for machine learning and data science. It is a high level programming language which supports multiple programming paradigms and has a large and comprehensive standard library and an awesome community. What this means is that you easily get small pieces of code to accomplish a lot of tasks and you just have to take care of the high level functionality of your program. You also get a lot of support from the forums and online community if you run into any issues or are looking for optimal implementations of various tasks.

Anaconda

Python equips us with lot of functionality with its libraries a.k.a. packages. Those packages are maintained by the Python community and are updated regularly. Which means there are multiple version for every package and a huge number of packages. Keeping track of all those packages is a big pain and moreover sometimes certain versions of packages are incompatible with each other. Moreover you might want to use a Python package A with version 1.0 with another package B having version 2.3 for one project. For another project you might want to use version 1.2 for package A and 2.5 for package B. Anaconda takes care of this for us, you can create a virtual environment in Anaconda with specific packages and versions. Anaconda highly simplifies package management and deployment.

Jupyter Notebook

Random Trivia: The “Great Red Spot” on Jupiter is 1.3 times the diameter of the Earth

Jupyter Notebook is an incredible tool for working with Juila, Python and R languages and it is the go-to code writing tool for a data scientists. Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more. I would highly recommend you to get familiar with Jupyter Notebook with a basic tutorial.

Keras

Keras is a Deep Learning library written in Python language. It can utilize TensorFlow library and makes life so much easier when it comes to fast experimentation with implementing Neural Networks. It provides the building blocks of a Neural Network so you don’t have to worry about the lower level implementations. It is very modular and user friendly and allows you to just define your Neural Network architecture and connect layers with each other and voila, much like a Lego kit you can assemble your neural network!

About TensorFlow

TensorFlow is an open source software library for high performance numerical computation. Originally developed by researchers and engineers from the Google Brain team within Google’s AI organization, it comes with strong support for machine learning and deep learning and the flexible numerical computation core is used across many other scientific domains. TensorFlow simplifies a lot of tasks for us while implementing various machine learning algorithms. Imagine writing code for backpropagation with all the derivatives and matrix multiplications. Using TensorFlow it is just a few lines of code and the lower level details are already implemented for you.

Lets see how you can turn your boring windows 10 machine into your own deep learning laboratory.

TL;DR — Installing Anaconda

We will start with installing Anaconda first. The other packages will be installed in an Anaconda virtual environment. Head to the Anaconda download site

Hit download on the Python 3.x version. You can also download the 2.x version if you wish but it is recommended to get the 3.x version. The implementations we will do later in this guide will use 3.7 version.

Click next for the first few screens. Accept the license agreement and on the select installation type screen select “Just me” you can select the other option as well but that will require you to have admin privileges to your system.

When you reach the above screen just select the Add Anaconda to my PATH environment variable. You will need admin rights to your system for this option. You can use the Anaconda prompt post installation if you don’t select this option while installation. PATH variable just lets your computer know where Anaconda is installed so that when you run the command to launch Anaconda from the cmd terminal it immediately picks up the executable file from the PATH variable.

Click Install, you can also choose to install Visual Studio Code which is an Integrated Development Environment (IDE). It is not necessary but it is a very nice coding environment which provides lot of functionality to ease your development process. You can also use PyCharm which is a Python IDE. Meanwhile Anaconda installation is in progress, check your sitting posture, get up from your chair and stretch a bit!

Open Anaconda Navigator after the installation. As mentioned earlier Anaconda can create different virtual environments depending on your project requirements. We will just create a basic environment for our first CNN implementation.

Creating Anaconda Virtual Environment

Anaconda provides a GUI navigator to help you create an environment. You can also create a new environment from the windows command shell using the following command. This is where the PATH variable will help. If that is not set the below command will give you an error.

conda create --name my-new-environment python=3.7

You can choose any name for your environment and the version of python you want to use.

If you want to create the environment using Anaconda Navigator follow the steps shown in the image below

Click on Environments in the left panel

Click on create and enter the name of the environment and the python version you want to use and wait for the process to finish. It might take a couple of minutes.

After the environment creation is done click Home on the left panel of Anaconda Navigator and click on Launch on the Jupyter Notebook card. If it says install then hit install and wait for the Navigator to install Jupyter.

I would suggest to move to the windows cmd since it is faster and you won’t have to wait for the GUI to open each time. On the windows command prompt run the following commands after creating the environment

activate my-new-environment

Creating environment is just a one time task. After this you will just have to activate and deactivate the environments. To deactivate the current environment just type deactivate.

The activate command will activate your newly created environment. After this you can run the following command from the windows command prompt to install Jupyter —

conda install jupyter

After installing Jupyter notebook we will now go ahead with installing TensorFlow, Keras and other required libraries.

Installing required packages

Now that you have spent so much effort and disk space on installing Anaconda it is time to reap its benefits. As I mentioned earlier the power of Python lies in its ease of use and huge number of community maintained libraries and Anaconda makes the management very simple. You will be using lot of math and matrix operations while implementing Deep Learning codes. Go ahead and install keras in your virtual environment. The command for that is —

conda install keras

This will install the required dependencies like TensorFlow and numpy. If you want to install any other package for Python you can use this command after you have activated your environment.

That is all! You are all set with your laboratory for Deep Learning projects. In the next article we will implement a small Neural Network using Keras and will build a website of our own which will somewhat look like the below image —

Since implementing a neural network and building a website is a slightly longer process we will do that in the next article. Till then you can familiarize yourself with Jupyter notebooks, Python and Anaconda environments. Also read and watch videos about TensorFlow and Keras. You will be able to write digits on screen using your mouse and the neural network will recognize them. How cool is that!