Does TensorFlow use NumPy?

We all know that TensorFlow uses the NumPy API. But not only NumPy, it also uses many other APIs. We will learn them too, as we progress.

But how can we implement the subset of the NumPy API?

The answer is, we use the tf.experimental.numpy.

As a result, it allows us to run the NumPy accelerated by TensorFlow. In addition, we can use other APIs of the TensorFlow also.

Enough talking, let’s take a look at the code implementation.

That‘s important because that will explain everything in a lucid way.

Let’s import the libraries that we need to see the relationship between TensorFlow and NumPy.

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import tensorflow.experimental.numpy as tnp
import timeit

tf.__version__

# output:
'2.8.2'

The output shows that we’re using TensorFlow version 2.8.2.

That’s perfect. Now we should enable the “tensorflow.experimental.numpy”.

tnp.experimental_enable_numpy_behavior()

As a result, now we can use the “tensorflow.experimental.numpy” attributes and methods.

We’ve discussed what are attributes and methods, before. Right?

One of such attributes is “ones”. In short we can create a multidimensional array which is an instance of tf.experimental.numpy.ndarray.

As an outcome, we can also check its different attributes.

ones = tnp.ones([5, 3], dtype=tnp.int32)
ones

# output:
<tf.Tensor: shape=(5, 3), dtype=int32, numpy=
array([[1, 1, 1],
       [1, 1, 1],
       [1, 1, 1],
       [1, 1, 1],
       [1, 1, 1]], dtype=int32)>

What do we see?

It creates a Matrix. Two dimensional array where there are 5 rows and 3 columns.

But remember, “ones” is the attribute of the “tensorflow.experimental.numpy

” library.

We can give it any name and see the error.

test = tnp.test([5, 3], dtype=tnp.int32)
test

# output:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-1a98433a2474> in <module>()
----> 1 test = tnp.test([5, 3], dtype=tnp.int32)
      2 test

AttributeError: module 'tensorflow.experimental.numpy' has no attribute 'test'

Now we can check other attributes also.

print(ones.device)
print(ones.dtype)
print(ones.ndim)

# output:
/job:localhost/replica:0/task:0/device:CPU:0
<dtype: 'int32'>
2

While creating the array, we have declared the data type. We have also defined dimensions. 

Therefore we can expect this output.

Now we can create different types of tensors.

x = tnp.ones([2, 3])
y = tnp.ones([3])
z = tnp.ones([1, 2, 1])

x, y, z

# output:
(<tf.Tensor: shape=(2, 3), dtype=float64, numpy=
 array([[1., 1., 1.],
        [1., 1., 1.]])>,
 <tf.Tensor: shape=(3,), dtype=float64, numpy=array([1., 1., 1.])>,
 <tf.Tensor: shape=(1, 2, 1), dtype=float64, numpy=
 array([[[1.],
         [1.]]])>)

As a result, the first one “x” is a Matrix. After that, “y” is a Vector. And finally “z” is a three dimensional tensor.

Finally we will check whether this instance belongs to TensorFlow or not?

That will certainly prove that TensorFlow uses the subset of NumPy library.

isinstance(ones, tf.Tensor)

# output:
True

We have seen how TensorFlow uses NumPy.

But they have many differences too.

We will discuss that in the coming sections.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

TensorFlow, Machine Learning, AI and Data Science

Flutter, Dart and Algorithm

Twitter


Posted

in

, ,

by

Comments

Leave a Reply