Preparing Python Environment for Deep Learning

Last week was very fascinating! I joined a summer school program namely Indonesian Summer School on Music Information Retrieval at Faculty of Computer Science, University of Indonesia. The event that was held from August 14, 2017 to August 18, 2017 was supported by several great lecturers and researchers from University of Indonesia, Vienna University of Technology and Johannes Kepler University. I was so excited because it was my first experience joining some classes where the lecturers came from European universities (dan tentu saja mengobati rasa kekecewaanku setelah gagal sekolah di Eropa). There were several topics discussed there, such as introduction to music information retrieval, content based and context based music information retrieval, introduction to deep learning and designing for users. The discussion of deep learning took me a lot of attention. In this post, I will explain several steps for preparing the environment for Deep Learning. I’ve not been confident enough to explain the theory of Deep Learning itself because I’m also still learning it haha. I wrote this post as a note for myself that sometimes forget some installation steps when I have to re-install it.

In this tutorial, I will set up Python environment (the version of Python used here is 2.7) for Deep Learning using Anaconda. For Deep Learning libraries, it will be used Keras and Theano. The detail steps are as follows: 

1. Download and Install Anaconda for Python 2.7
We can download Anaconda Installer from this page and choose the installer that suitable to our platform (Windows, Mac, Linux). After finishing the download, follow the installation wizard to complete the installation.

2. Setting up Path for Python and Anaconda
Anaconda provides a GUI called Anaconda Navigator for easier access, but I prefer to use the console, both Command Line Prompt and Anaconda Prompt. When we choose to use Command Line Prompt, we have to set environment path so we can access Python and Conda in any directories. The path for Python can be found in \Anaconda2 and for Conda path can be found in \Anaconda2\Scripts. Add these paths to environment variable setting at Control Panel.

After setting the path, we can use Comand Line Prompt/ Anaconda Prompt to confirm the Python and Conda are installed correctly by typing:
conda -V
python -V

Checking anaconda and Python installation

3. Install C Compiler
C compiler is needed for running the Deep Learning library. We can install C Compiler in Ananconda Prompt using this following command:
conda install mingw libpython

4. Confirm Machine Learning Libraries
There are several Machine Learning libraries that also needed for Deep Learning, such as SciPy, NumPy, Matplotlib, Pandas, and Scikit-learn. Ananconda provides all those libraries automatically so we don’t need to install it one by one. Just for confirmation, we can check it by testing in Python Command Line

Confirm several machine learning libraries

5. Install Theano and Keras
Theano and Keras are used as the backend for Deep Learning. Besides Theano, there is another library called TensorFlow that can be used for Deep Learning. However, there may be found some problems in installing TensorFlow on some Windows machines. That’s why we will use Theano here.

To install Keras and Theano, we can use the following command:

pip install Keras Theano

6. Configure Keras to Use Theano
After completing the installation of Keras and Theano, we need to configure the properties of Keras. As a default, Keras uses TensorFlow for its backend. Because we use Theano, we need to configure keras.json in order to change the backend properties. The detail steps are as follows.
Generate .keras folder at user’s Home directory
Keras.json can be found in user’s Home directory. In Windows, we can found it in C:\Users\.keras\. However, it can’t be found before we generate via Python. To generate the folder, use python command line as shown in the following picture.

Import keras for the first time will generate .keras folder at user’s home directory

There will be found some error messages but don’t worry, it’s because Keras still uses default configuration which uses TensorFlow. After we execute this command, we can find a folder named .keras at user’s Home directory.

– Change the properties of keras.json
Inside .keras folder, there is a keras.json file which needs to be configured. Open the json file in a text editor, then change the two lines in that file to the following:

"image_dim_ordering": "th",

“backend”: “theano”


– Confirm the Keras Configuration
To confirm the configuration of Keras is installed correctly, we can check it by Python Command Line.

Confirm Keras Configuration

As seen in the picture above, Keras has used Theano as its backend. It means that our Python environment has ready for Deep Learning.

7. Other libraries: Librosa
The summer school I have attended was focused on Music Information Retrieval. That’s why we also need a library for audio and music processing namely Librosa. To install librosa, we can use pip in command line.

pip install librosa

In addition, we also need to install audio-encoding tools such as ffmpeg or GStreamer to completing Librosa installation. Using Anaconda prompt, type the following command to install ffmpeg. 

conda install -c conda-forge ffmpeg

Well, that’s all the brief tutorial for preparing Deep Learning environment in Python and I hope this will be useful. See you in the next post!