Anaconda, Jupyter Notebook, TensorFlow and Keras for Deep Learning
[Update: you no longer need to install Keras separately since it is part of the core TensorFlow API. “import tensorflow as tf” then use tf.keras in your code. You will notice the strikethrough of any mention of Keras installation in this blog post]
So you want to get started to study deep learning? The first step is to set up the tools. In this post I will share with you how to set up Anaconda and Jupyter Notebook, and then install TensorFlow (including Keras). Check out My Notes on TensorFlow 2.0 if you want to try out the 2.0 Preview.
There are different ways of installing TensorFlow:
- “native” pip or install from source
- install in a virtual environment with Virtualenv, Anaconda, or Docker. This post will be using Anaconda.
While Jupyter Notebook is not a pre-requisite for using TensorFlow (or Keras), I find that using Jupyter Notebook very helpful for beginners who just started with machine learning or deep learning. I plan to use Jupyter Notebook for all the tutorials that I will be writing in order to share my deep learning knowledge.
There are other deep learning frameworks out there but my future tutorials will be mostly using TensorFlow and tf.keras.
Let’s get started -
Anaconda will enable you to create virtual environments and install packages needed for data science and deep learning. With virtual environments you can install specific package versions for a particular project or a tutorial without worrying about version conflicts.
Download Anaconda for your platform and choose the Python 3.6 version: https://www.anaconda.com/download
By downloading Anaconda, you get conda, Python, Jupyter Notebook and hundreds of other open source packages.
Conda is a package manager to manage virtual environment and install packages. Here are some helpful commands using conda:
# update conda in your default environment
$ conda upgrade conda
$ conda upgrade --all# create a new environment with conda
$ conda create -n [my-env-name]
$ conda create -n [my-env-name] python=[python-version]# activate the environment you created
$ source activate [my-env-name]# take a look at the environment you created
$ conda info
$ conda list# install a package with conda and verify it's installed
$ conda install numpy
$ conda list# take a look at the list of environments you currently have
$ conda info -e# remove an environment
$ conda env remove --name [my-env-name]
I highly recommend you download and print out the Anaconda cheatsheet here.
Conda vs Pip install
You can use either conda or pip for installation in an virtual environment created with conda. They are both open source package managers. Here are some differences:
conda install— installs any software package.
pip install— installs python packages only and it’s the defacto python package manager.
Note: pip and conda use different packaging formats so they do not operate interchangeably.
conda listto see the list of packages installed and whether they are installed with conda or pip.
Install TensorFlow (including Keras)
Next we will install TenslowFlow in the virtual environment you created with conda. Note: we will be using
pip install instead of
conda install per the TensorFlow official installation documentation.
You can either install one package at a time or install several packages at once from a requirements.txt file.
# install pip in the virtual environment
$ conda install pip# install Tensorflow CPU version
$ pip install --upgrade tensorflow # for python 2.7
$ pip3 install --upgrade tensorflow # for python 3.*# install Keras (Note: please install TensorFlow first)
$ pip install Keras#̵ ̵i̵n̵s̵t̵a̵l̵l̵ ̵K̵e̵r̵a̵s̵ ̵(̵N̵o̵t̵e̵:̵ ̵p̵l̵e̵a̵s̵e̵ ̵i̵n̵s̵t̵a̵l̵l̵ ̵T̵e̵n̵s̵o̵r̵F̵l̵o̵w̵ ̵f̵i̵r̵s̵t̵)̵
̵ $̵ ̵p̵i̵p̵ ̵i̵n̵s̵t̵a̵l̵l̵ ̵K̵e̵r̵a̵s̵# Alternatively, install multiple packages from a requirements file
$ pip install -r requirements.txt
After installing TensorFlow you can verify its installation with python in command line:
Invoke python from your shell as follows:
# invoke python from your shell
$ python# create a simple TensorFlow program inside the python interactive shell
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))# Exit the python shell
If you see
Hello, TensorFlow! printed then it confirms that TensorFlow is stalled correctly.
Launch Jupyter Notebook
Jupyter Notebook is a web application that contain both computer code such as Python and rich text elements such as paragraph, equations, figures, links, etc. It’s an essential tool for data analysis and visualization.
As I said earlier, you get Jupyter Notebook already by downloading Anaconda.
Start/Stop Jupyter Notebook
You will start / stop the Jupyter Notebook server from the command line:
# launch the Jupyter Notebook server
$ jupyter notebook# stop the Jupter Notebook server
Alternatively you can shut down individual notebooks from the Notebook server UI.
When opening your notebook, if you get a Kernel not found error, use the drop down (with virtual environments) to select and set a kernel.
Running Jupyter Notebook with GPU
When you just start out with deep learning with some Hello World examples, you can run your training on CPU. As you progress in your learning, most likely you will need to run your training on a GPU. Check out my post on how to set up Jupyter Notebook on Floydhub or Amazon AWS.
Here is an informative Jupyter Notebook blog including a cheat sheet -
Jupyter Notebook Cheat Sheet
You'll probably know the Jupyter notebooks pretty well - it's one of the most well-known parts of the Jupyter…
Verify Jupyter Notebook is working
Now that you have a conda virtual environment and installed the required packages, and launch Jupyter Notebook. Let’s verify everything is working.
Create a new Notebook and create a new cell with
import tensorflow as tf
print ("TensorFlow version: " + tf.__version__)# Note you no longer need to import keras, use tf.keras instead
E̵x̵e̵c̵u̵t̵e̵ ̵t̵h̵e̵ ̵c̵e̵l̵l̵(̵s̵)̵,̵ ̵a̵n̵d̵ ̵y̵o̵u̵ ̵s̵h̵o̵u̵l̵d̵ ̵s̵e̵e̵ ̵t̵h̵e̵ ̵v̵e̵r̵s̵i̵o̵n̵ ̵o̵f̵ ̵T̵e̵n̵s̵o̵r̵F̵l̵o̵w̵ ̵a̵n̵d̵ ̵t̵h̵a̵t̵ ̵K̵e̵r̵a̵s̵ ̵i̵s̵ ̵u̵s̵i̵n̵g̵ ̵T̵e̵n̵s̵o̵r̵F̵l̵o̵w̵ ̵b̵a̵c̵k̵e̵n̵d̵.̵ You will only see “Using TensorFlow backend” if you explicitly import keras. Please use tf.keras instead.
If you see an error in your notebook complaining about TensorFlow o̵r̵ ̵K̵e̵r̵a̵s̵
module not found, even though you already install it in your conda environment, make sure you are setting the right kernel.
Protip: install nb_conda so that you can easily switch kernel in Jupyter Notebook (
conda install nb_conda)
To summarize, here are the steps to take for setting up everything:
- Download Anaconda
- Create a virtual environment and activate it
- Install K̵e̵r̵a̵s̵ ̵a̵n̵d̵ TensorFlow etc.
- Launch Jupyter Notebook
- Start writing some code!
# create a new environment
$ conda create -n fashion-mnist-tutorial# activate the environment you just created
$ source activate fashion-mnist-tutorial# install pip
$ conda install pip# install latest version of Tensorflow
$ pip install --upgrade tensorflow # if you are prompted to upgrade pip
$ pip install --upgrade pip#̵ ̵i̵n̵s̵t̵a̵l̵l̵ ̵t̵h̵e̵ ̵l̵a̵t̵e̵s̵t̵ ̵v̵e̵r̵s̵i̵o̵n̵ ̵o̵f̵ ̵K̵e̵r̵a̵s̵
̵$̵ ̵p̵i̵p̵ ̵i̵n̵s̵t̵a̵l̵l̵ ̵-̵-̵u̵p̵g̵r̵a̵d̵e̵ ̵k̵e̵r̵a̵s̵# install nb_conda for easily managing/switching notebook kernel
$ conda install nb_conda# launch Jupyter Notebook
$ jupyter notebook