Introduction
TensorFlow is one of the best machine learning libraries for Python. There are various TensorFlow versions with different capabilities. Additionally, certain releases are only compatible with specific Python versions.
This article explains how to upgrade or downgrade TensorFlow.
Prerequisites
- Python 3.6-3.9 installed and configured (check the Python version before starting).
- TensorFlow 2 installed.
- The pip package manager version 19.0 or greater (check the pip version and upgrade if necessary).
- Access to the command line/terminal or notebook environment.
Note: Depending on the Python version, only specific TensorFlow releases are available:
- Python 3.9 works with TensorFlow 2.5 and later releases.
- Python 3.8 works with TensorFlow 2.2 and later releases.
How to Upgrade TensorFlow
To upgrade TensorFlow to a newer version:
1. Open the terminal (CTRL+ALT+T).
2. Check the currently installed TensorFlow version:
pip3 show tensorflow
The command shows information about the package, including the version.
3. Upgrade TensorFlow to a newer version with:
pip3 install --upgrade tensorflow==<version>
Make sure to select a version compatible with your Python release. If the release is incompatible, the version will not install. For the notebook environment, use the following command and restart the kernel after completion:
!pip install --upgrade tensorflow==<version>
The install automatically removes the old version along with the dependencies and installs the newer upgrade.
4. Lastly, check the upgraded version by running:
pip3 show tensorflow
How to Downgrade TensorFlow
The best practice for TensorFlow downgrade is to use the latest version of Python and TensorFlow. Older versions have vulnerability issues, so be cautious when downgrading.
1. Check the currently installed TensorFlow version:
pip3 show tensorflow
2. Downgrade TensorFlow to a lower version by running:
pip3 install --upgrade tensorflow==<version>
Set the version to a lower number than the currently installed release. When choosing, make sure the version is compatible with the Python release.
If you are using a Notebook environment, run the following command and restart the kernel when the installation completes:
!pip install --upgrade tensorflow==<version>
The upgrade automatically removes the existing TensorFlow and installs the stated version.
3. Finally, use the pip show
command to confirm the correct TensorFlow version installed:
pip3 show tensorflow
Note: Deploy TensorFlow on a Bare Metal Cloud server instance to ensure that your TensorFlow workloads always have enough resources for optimal performance.
Conclusion
The pip package manager offers a simple method to upgrade or downgrade TensorFlow, regardless of environment.