Find version of TensorFlow

In this article, we will see various ways to check the version of TensorFlow (like v1.15.0 or v2.4.0). We will explore commands to be used on terminal and techniques to get the version using code.

In short, you can use the following command in the terminal:

python -c 'import tensorflow as tf; print(tf.__version__)'

The output will be like:

v2.4.0

TensorFlow version is of the form X.X.X.Y and has four parts:

  • TF_MAJOR_VERSION (example: 2)
  • TF_MINOR_VERSION (example: 4)
  • TF_PATCH_VERSION (example: 0)
  • TF_VERSION_SUFFIX (example: rc-1 but usually empty)
    This brings the version to 2.4.0.rc-1.

We will explore the following techniques:

  • Method 1: Using Python in terminal
  • Method 2: Using pip
  • Method 3: Get version using Python Code
  • Method 4: In TensorFlow source code

Method 1: Using Python in terminal

In this method, we will explore a single command which when typed in the terminal returns the version of TensorFlow in use. In this technique, we will load tensorflow in Python and get the version using __version__ attribute.

Use the following command and check:

python -c 'import tensorflow as tf; print(tf.__version__)'

The output will be like:

v2.4.0

If you are using Python3, you shall use the following command:

python3 -c 'import tensorflow as tf; print(tf.__version__)'

The output will be like:

v2.4.0

Method 2: Using pip

In this method, we will check the TensorFlow version using pip. You can use the following command in the terminal:

pip show tensorflow

If you use pip3, the command will be:

pip3 show tensorflow

The output will be like:

Name: tensorflow
Version: 2.4.0
Summary: TensorFlow helps the tensors flow

We can also, use the following command:

pip list | grep tensorflow

or

pip3 list | grep tensorflow

The output will be like:

tensorflow (2.4.0)

Method 3: Get version using Python Code

Save the following code in a file named "version.py":

import tensorflow as tf
print(tf.version.VERSION)

We can run the code as:

python version.py

The output will be like:

v2.4.0

In this Python code, we are using the tf.version module which has several pre-defined attributes containing the version of different components. tf.version module provides several information such as:

  • COMPILER_VERSION
  • GIT_VERSION
  • GRAPH_DEF_VERSION
  • GRAPH_DEF_VERSION_MIN_CONSUMER
  • GRAPH_DEF_VERSION_MIN_PRODUCER
  • VERSION

VERSION gives the version of TensorFlow and can be used as follows:

print(tf.version.VERSION)

The output will be like:

2.0.1

If you want to get the compiler version, then we need to use COMPILER_VERSION attribute as follows:

print(tf.version.COMPILER_VERSION)

The output will be like:

'7.3.1 20180303'

Similarly, if you want to get the git version, then we need to use GIT_VERSION attribute as follows:

print(tf.version.GIT_VERSION)

The output will be like:

v2.5.0-rc3-213-ga4dfb8d1a71

Method 4: In TensorFlow source code

We can check the version of TensorFlow in the source code as well. Clone the TensorFlow source code and open the file version.h at this location:

tensorflow/tensorflow/core/public/version.h

For example, go to this URL:

https://github.com/tensorflow/tensorflow/blob/v2.4.0/tensorflow/core/public/version.h

A part of the code is as follows:

#ifndef TENSORFLOW_CORE_PUBLIC_VERSION_H_
#define TENSORFLOW_CORE_PUBLIC_VERSION_H_

// TensorFlow uses semantic versioning, see http://semver.org/.

// Also update tensorflow/tensorflow.bzl and
// tensorflow/tools/pip_package/setup.py
#define TF_MAJOR_VERSION 2
#define TF_MINOR_VERSION 4
#define TF_PATCH_VERSION 0

// TF_VERSION_SUFFIX is non-empty for pre-releases (e.g. "-alpha", "-alpha.1",
// "-beta", "-rc", "-rc.1")
#define TF_VERSION_SUFFIX ""

#define TF_STR_HELPER(x) #x
#define TF_STR(x) TF_STR_HELPER(x)

// e.g. "0.5.0" or "0.6.0-alpha".
#define TF_VERSION_STRING                                            \
  (TF_STR(TF_MAJOR_VERSION) "." TF_STR(TF_MINOR_VERSION) "." TF_STR( \
      TF_PATCH_VERSION) TF_VERSION_SUFFIX)

From this, we see TensorFlow version is of the form X.X.X.Y and has four parts:

  • TF_MAJOR_VERSION (example: 2)
  • TF_MINOR_VERSION (example: 4)
  • TF_PATCH_VERSION (example: 0)
  • TF_VERSION_SUFFIX (example: rc-1 but usually empty)

This brings the version to 2.4.0.rc-1.

With this article at OpenGenus, you have the complete knowledge of how to get TensorFlow version.