TensorFlow set up with Anaconda/Jupyter Notebooks

Aditya Rao
Geek Culture
Published in
4 min readMay 25, 2021
Photo by Hitesh Choudhary on Unsplash

TensorFlow has become one of the most used frameworks in the deep learning space providing ability to train models in python providing encapsulation of the boilerplate code and aiding in deployment of applications on cloud. Setting up TensorFlow with Jupyter Notebooks has been one of the issues which I faced when trying to get it to work so I’m putting together a series of steps just as FYI ( in case my memory goes bust someday ).

Anaconda Prompt

There are two ways in which a user can go about adding libraries and performing other operations in anaconda. The choice is totally up to the user depending on their comforts with the command line or with the GUI . So the user can do this
• Using the User Interface — Anaconda Navigator.
• Using Anaconda Prompt — the command line tool.

Both of the approaches have their own pros and cons. I pursued using anaconda prompt ( or anaconda CLI) given my recent obsession with working on black and white windows.

Virtual Environments

To provide a brief overview, a virtual environment simply provides a separate environment where you can do application development using specified version of libraries. It prevents issues/errors in code execution related to deprecation of commands or release of newer versions.

To create a virtual environment the command is as below:

(base) C:\Users\Aditya> conda create -n myEnvironment 

The environment can be activated using the following command : conda activate. Post the command, user can see the base changing to <environment name> indicating change of environment.

(base) C:\Users\Aditya> conda activate myEnvironment
(myEnvironment) C:\Users\Aditya>

You can exit the environment using the command conda deactivate

(myEnvironment) C:\Users\Aditya> conda deactivate 

To delete the environment using:

(base) C:\Users\Aditya> conda env remove -n myEnvironment

Setting up TensorFlow Environment using Anaconda

Now that we’ve done CRUD with the virtual environments, for tensorflow user can create a virtual environment using following set of commands below.

# Creating tensorflow virtual environment 
(base) C:\Users\Aditya\> conda create -n tf tensorflow
#Activate the environment
(base) C:\Users\Aditya\> conda activate tf

It installs the required set of libraries to start with Tensorflow. To test whether the libraries are installed or not we can run the following commands as a basic sanity check:

(base) C:\Users\Aditya> conda activate tf (tf) C:\Users\Aditya>python
Python 3.7.9 (default, Aug 31 2020, 17:10:11) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf;
>>> tf.__version__ # Checking the version of the library.
'2.1.0'

Final Flourish: Setting up Jupyter Notebooks to use Tensorflow virtual environment

Now that we have installation of libraries out of the way, we need to make sure that we’re able to use the virtual environment with Jupyter Notebook.

Install nb_conda for having the ability to change kernels when using Jupyter Notebooks. This will help user to change the kernel option on landing page of the jupyter notebooks.

conda install -c anaconda nb_conda

Now, even after installing this you might not be seeing tf as a kernel option in Jupyter Notebooks. The error is due to virtual environment not having a IPython kernel. So, we need to set up an IPython kernel for the virtual environment. Use the following commands to set up IPython kernel in your virtual environment in anaconda prompt.

conda install -c anaconda ipykernel

Now when you run Jupyter Notebooks you should be able to see tensorflow virtual environment option in your anaconda environments:

Jupyter Notebook

And you should be able to import TensorFlow into your Jupyter Notebook.

Conclusion

Jupyter notebooks by itself is an amazing application for implementing data analysis and building machine learning models. Ability to use Tensorflow and other ML frameworks takes it a step further when it comes to running quick analysis on data.

This is my first attempt at writing a technical article in quite some time. Any comments, feedback and constructive criticism is always welcome. I can be reached on twitter @Aditya1791 & LinkedIn.

--

--

Aditya Rao
Geek Culture

In between an analyst, scientist and programmer. Love stats, finance, ML, and perspectives. Currently interested in WebServices