How to Run TensorFlow in a Jupyter Notebook?

If you are a machine learning practitioner you might have come across the TensorFlow library. TensorFlow is a popular machine learning library and finds its use in a lot of AI and machine learning applications. In this tutorial you will learn

  1. How to install TensorFlow in a virtual environment
  2. How to activate your environment in Jupyter Notebook
  3. How to use TensorFlow in a Jupyter Notebook

How to install TensorFlow in a virtual environment

In order to use TensorFlow in a Juypter notebook, we need to create an independent environment to manage our dependencies. We will begin by creating an anaconda environment. We first begin by creating a directory with an environments.yml file and a notebooks directory. We will use the notebooks directory to create our notebook for TensorFlow experiments. The environments.yml file is used to manage our dependencies

As a next step, you can open a text-editor of your choice and add the following line in your environments.yml file

name: tensorflow-development
channels:
- anaconda
- conda-forge
- defaults
 
dependencies:
 - python=3.7
 - numpy
 - matplotlib
 - pandas
 - tensorflow
 - notebook
 - nb_conda_kernels
 - jupyter_contrib_nbextensions

We will now create a new environment called tensorflow-development using the following command in your terminal:

conda env create -f environment.yml

How to activate your environment in Jupyter Notebook

Once you have created your environment let us now see how we can activate our environment

conda activate tensorflow-development

How to use TensorFlow in a Jupyter Notebook

We will now execute the following command to start the Jupyter notebook

jupyter notebook

We can now choose the environment which we created and start the Jupyter notebook

We can now navigate to notebooks/ and create our notebook. We will test to see if TensorFlow was installed successfully. We will import the TensorFlow library and print the version number of the library.

Summary

In this blog post, we learned how to install the TensorFlow library in a managed python environment. We then had to look at how to use Tensorflow in a Jupyter notebook environment.