Introduction
TensorFlow is one of the most prominent machine learning packages. Knowing which version is on the system is vital as different builds have different options. There are multiple ways to check the TensorFlow version depending on the installation method.
This article shows how to check the TensorFlow version in six different ways.
Prerequisites
- Installed Python 2 or Python 3
- Installed TensorFlow (Try our guides: How to install TensorFlow on CentOS, How to install TensorFlow GPU on Ubuntu).
- Access to CLI or IDE
Note: The recommended version is Python 3. Follow one of our guides on installing Python 3: How to install Python 3 on CentOS 7, How to install Python 3 on CentOS 8, How to install Python 3 on Ubuntu, How to install Python on Windows.
Check TensorFlow Version in Python
The simplest way to check the TensorFlow version is through a Python IDE or code editor. The library has built-in methods for displaying basic information.
To print the TensorFlow version in Python, enter:
import tensorflow as tf
print(tf.__version__)
TensorFlow Newer Versions
The TensorFlow 2.x versions provide a method for printing the TensorFlow version.
To check which one is on your system, use:
import tensorflow as tf
print(tf.version.VERSION)
TensorFlow Older Versions
TensorFlow 1.x has a slightly different method for checking the version of the library. Print the version for older TensorFlow builds in Python by running:
import tensorflow as tf
print(tf.VERSION)
Check TensorFlow Version in CLI
Display the TensorFlow version through Python invocation in the CLI with the python
command. Using the -c
option executes code.
If your machine has multiple instances of Python installed, use the python<version>
command.
Check TensorFlow Version in Linux Terminal
Print the TensorFlow version in the terminal by running:
python -c 'import tensorflow as tf; print(tf.__version__)'
If there are multiple instances of Python on the system, use:
python<version> -c 'import tensorflow as tf; print(tf.__version__)'
For example:
Check TensorFlow Version in Windows Command Line
Show the TensorFlow version in the command line by running:
python -c "import tensorflow as tf; print(tf.__version__)"
Check with a specific version of Python by adding the version number to the python
command:
python<version> -c "import tensorflow as tf; print(tf.__version__)"
Check TensorFlow Version in Pip
The most common way to install Python libraries is using the pip package manager. There are two ways to print the version with pip.
Note: If you want to install the pip package manager, try our guides: How to Install Pip on CentOS7, How to Install Pip on CentOS8, How to Install Pip on Debian, How to Install Pip on Ubuntu, How to Install Pip on Windows.
Method 1: Using pip show
The pip show
command prints information for any installed package.
To show the TensorFlow data, run this command:
pip show tensorflow
Method 2: Using pip list
The pip list
command shows all the packages installed using pip install
. In Linux, use the grep command to filter out the results:
pip list | grep tensorflow
For Windows, use findstr
to filter the pip list
results:
pip list | findstr "tensorflow"
Check TensorFlow Version in Virtual Environment
The TensorFlow documentation recommends installing the platform through a virtual environment. Activate the virtual environment before checking the version.
Step 1: Activate Virtual Environment
To activate the virtual environment, use the appropriate command for your OS:
For Linux, run:
virtualenv <environment name>
For Windows, use:
<environment name>/Scripts/activate
The environment shows up in the CLI as active:
Step 2: Check Version
Check the version inside the environment using the python -c
or pip show
command.
For example:
pip show tensorflow
Check TensorFlow Version in Anaconda
Anaconda uses the conda
package manager for installation. conda list
shows all the libraries installed using conda install
.
For Linux, filter the results with the grep
command:
conda list | grep tensorflow
For Windows, combine the conda list
and findstr
commands to print the TensorFlow version:
conda list | findstr "tensorflow"
Note: The conda package manager comes with all Anaconda and Miniconda versions. To install Anaconda, follow our guides: How to Install Anaconda on CentOS7, How to Install Anaconda on CentOS8, How to Install Anaconda on Ubuntu.
Check TensorFlow Version in Jupyter Notebook
The Jupyter Notebook runs commands and Python code directly in the environment. There are two ways to check the TensorFlow version in Jupyter Notebooks.
Method 1: Using Import
Import the TensorFlow library and print the version by running the following code:
import tensorflow as tf
print(tf.__version__)
Method 2: Using Pip
Show the TensorFlow version using the pip
command with an exclamation point:
!pip show tensorflow
Note: Learn how to upgrade or downgrade TensorFlow.
Conclusion
This tutorial explains how to check the TensorFlow version for different cases in different environments. For additional TensorFlow material, check out our comparison of PyTorch vs TensorFlow.
原创文章,作者:306829225,如若转载,请注明出处:https://blog.ytso.com/223935.html