TensorFlow
Jupyter Notebook
Machine Learning
Python
Data Science

Running Tensorflow in Jupyter Notebook

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Jupyter Notebook is a natural environment for TensorFlow because it lets you run code interactively, inspect tensors, and iterate on models without restarting a full application. The most common problems are not in TensorFlow itself but in environment setup: the wrong Python interpreter, the wrong notebook kernel, or a package mismatch between what the terminal sees and what Jupyter sees.

A clean setup solves most of that. The reliable pattern is: create a dedicated environment, install TensorFlow and ipykernel into that environment, register the kernel, and verify the import from inside the notebook.

Create An Isolated Environment

Using a virtual environment avoids package conflicts with system Python or unrelated projects:

bash
1python3 -m venv .venv
2source .venv/bin/activate
3pip install --upgrade pip
4pip install tensorflow jupyter ipykernel

Then register the environment as a notebook kernel:

bash
python -m ipykernel install --user --name tf-notebook --display-name "Python (tf-notebook)"

Now start Jupyter:

bash
jupyter notebook

When the notebook opens, choose the Python (tf-notebook) kernel. That step matters because installing TensorFlow in one environment does not automatically make it available to every Jupyter kernel on the machine.

Verify TensorFlow Inside The Notebook

The first notebook cell should confirm that Jupyter is using the interpreter you expect:

python
1import sys
2import tensorflow as tf
3
4print(sys.executable)
5print(tf.__version__)
6print(tf.reduce_sum(tf.constant([1.0, 2.0, 3.0])))

If that cell runs successfully, the core installation is working. The printed interpreter path is especially useful because it tells you whether the notebook is actually using your intended environment.

A Small Keras Example

Once TensorFlow imports correctly, run a minimal model to confirm that the higher-level APIs are available too:

python
1import numpy as np
2from tensorflow import keras
3from tensorflow.keras import layers
4
5x = np.random.rand(100, 4).astype("float32")
6y = (x.sum(axis=1) > 2).astype("float32")
7
8model = keras.Sequential([
9    layers.Input(shape=(4,)),
10    layers.Dense(8, activation="relu"),
11    layers.Dense(1, activation="sigmoid"),
12])
13
14model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
15model.fit(x, y, epochs=2, verbose=1)

This is not a meaningful model, but it is a good environment check because it exercises TensorFlow tensors, Keras layers, compilation, and training.

Notebook-Specific Considerations

Jupyter makes experimentation easy, but notebooks also hide state. If you redefine a model cell several times, old objects may still exist in memory. If GPU memory usage grows unexpectedly or imports behave strangely after changing packages, restart the kernel and rerun cells from the top.

It is also worth separating setup cells from training cells. A notebook becomes much easier to debug when imports, dataset preparation, and model training are clearly divided.

Kernel Selection Matters

A terminal import and a notebook import are not the same check. You can install TensorFlow successfully in one environment and still have ModuleNotFoundError in Jupyter because the notebook kernel points somewhere else. When setup looks correct but the notebook still fails, inspect sys.executable from inside the notebook before changing packages again.

Common Pitfalls

  • Installing TensorFlow in one environment while the notebook uses a different kernel.
  • Assuming pip install tensorflow in a terminal automatically fixes an already-running notebook kernel.
  • Forgetting to install ipykernel into the TensorFlow environment.
  • Debugging stale notebook state instead of restarting the kernel.
  • Mixing CPU and GPU assumptions without first confirming what TensorFlow actually detects.

Summary

  • Use a dedicated environment for TensorFlow notebooks.
  • Install both TensorFlow and ipykernel in that environment.
  • Register the environment as a Jupyter kernel and select it explicitly.
  • Verify the interpreter path and TensorFlow version from inside the notebook.
  • Restart the kernel when notebook state becomes confusing or stale.

Course illustration
Course illustration

All Rights Reserved.