tensorflow
colab
downgrade
tutorial
version-control

How to downgrade tensorflow version in colab?

Master System Design with Codemia

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

Introduction

Downgrading TensorFlow in Google Colab is common when notebooks rely on older APIs, model checkpoints, or third-party packages that are not ready for newer releases. The install command is easy, but stable results depend on version checks, runtime restart, and dependency pinning. In Colab, the harder problem is compatibility with whatever Python and system image the runtime currently provides.

Check the Runtime Before Installing

Colab runtimes change over time, and not every TensorFlow release supports the active Python version. Always inspect your environment first.

python
1import sys
2import platform
3import tensorflow as tf
4
5print("Python:", sys.version)
6print("Platform:", platform.platform())
7print("Current TensorFlow:", tf.__version__)

If you skip this step, you can spend time chasing wheel compatibility problems that were predictable from version metadata.

Install a Specific TensorFlow Version

Use %pip inside the notebook, not shell pip, so the install targets the active kernel environment rather than some unrelated interpreter path.

python
%pip install -q tensorflow==2.12.0

After installation, restart runtime and confirm the active version.

python
import tensorflow as tf
print(tf.__version__)

Restart matters because previously imported modules stay in memory until the kernel restarts.

Pin Companion Packages for Stability

TensorFlow versions often need matching package versions for Keras, NumPy, and related tooling. Pinning avoids resolver drift.

python
1%pip install -q \
2  tensorflow==2.12.0 \
3  keras==2.12.0 \
4  numpy==1.23.5

Keep these pins in one setup cell at the top of the notebook so all users share the same environment contract.

Validate with a Minimal Training Run

Printing version is not enough. Run a tiny model to verify that imports, execution, and gradients actually work.

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

This quick check catches backend and dependency issues early.

Confirm GPU Availability After Downgrade

If your notebook depends on GPU acceleration, verify devices after the install and restart.

python
import tensorflow as tf
print(tf.config.list_physical_devices("GPU"))

If GPU list is empty, your target TensorFlow version may not match the runtime CUDA stack. In that case, either move to a nearby supported TensorFlow version or run in a custom environment.

Build a Reproducible Notebook Bootstrap Cell

A reliable team notebook starts with one environment bootstrap cell:

  • Install pinned packages.
  • Print versions.
  • Fail fast if version mismatch is unacceptable.
python
1EXPECTED_TF = "2.12.0"
2
3import tensorflow as tf
4if tf.__version__ != EXPECTED_TF:
5    raise RuntimeError(f"Expected TensorFlow {EXPECTED_TF}, got {tf.__version__}")

Failing early is better than debugging silent behavior differences later in training.

What to Do When Target Version Is Unsupported

Some old TensorFlow versions do not provide wheels for current Colab Python. If installation fails repeatedly:

  • Choose the nearest supported TensorFlow version and adapt code.
  • Use compatibility APIs such as tf.compat.v1 for legacy flows.
  • Run legacy notebooks in a Docker or VM environment with controlled Python and CUDA versions.

Trying to force unsupported wheels in Colab usually leads to unstable sessions.

Common Pitfalls

  • Installing TensorFlow and forgetting to restart runtime.
  • Pinning TensorFlow only, while leaving dependent packages unpinned.
  • Running install commands in many scattered cells instead of one setup cell.
  • Assuming old notebook instructions still match current Colab runtime defaults.
  • Validating with print(tf.__version__) only and skipping a real training sanity check.

Summary

  • Inspect Python and TensorFlow versions before downgrade attempts.
  • Install with %pip and restart runtime immediately after version changes.
  • Pin related packages to keep resolver behavior stable.
  • Validate with a minimal training step and GPU device check.
  • Use a top-of-notebook bootstrap cell for reproducible team workflows.

Course illustration
Course illustration

All Rights Reserved.