TensorFlow ranks and tensors

The TensorFlow library lets users define operations and functions over tensors as computational graphs. Tensors are a generalizable mathematical notation for multidimensional arrays holding data values, where the dimensionality of a tensor is typically referred to as its rank.

We've worked mostly, so far, with tensors of rank zero to two. For instance, a scalar, a single number such as an integer or float, is a tensor of rank 0. A vector is a tensor of rank 1, and a matrix is a tensor of rank 2. But, it doesn't stop here. The tensor notation can be generalized to higher dimensions—as we'll see in the next chapter, when we work with an input of rank 3 and weight tensors of rank 4 to support images with multiple color channels.

To make the concept of a tensor more intuitive, consider the following figure, which represents tensors of ranks 0 and 1 in the first row, and tensors of ranks 2 and 3 in the second row:

TensorFlow ranks and tensors

How to get the rank and shape of a tensor

We can use the tf.rank function to get the rank of a tensor. It is important to note that tf.rank will return a tensor as output, and in order to get the actual value, we will need to evaluate that tensor.

In addition to the tensor rank, we can also get the shape of a TensorFlow tensor (similar to the shape of a NumPy array). For example, if X is a tensor, we can get its shape using X.get_shape(), which will return an object of a special class called TensorShape.

We can print the shape and use it directly for the shape argument when creating other tensors. However, we cannot index or slice this object directly. If we want to index or slice different elements of this object, then we can convert it into a Python list, using the as_list method of the tensor class.

See the following examples on how to use the tf.rank function and the get_shape method of a tensor. The following code example illustrates how to retrieve the rank and shape of the tensor objects in a TensorFlow session:

>>> import tensorflow as tf
>>> import numpy as np
>>>
>>> g = tf.Graph()
>>>
>>> ## define the computation graph
>>> with g.as_default():
...     ## define tensors t1, t2, t3
...     t1 = tf.constant(np.pi)
...     t2 = tf.constant([1, 2, 3, 4])
...     t3 = tf.constant([[1, 2], [3, 4]])
...
...     ## get their ranks
...     r1 = tf.rank(t1)
...     r2 = tf.rank(t2)
...     r3 = tf.rank(t3)
...
...     ## get their shapes
...     s1 = t1.get_shape()
...     s2 = t2.get_shape()
...     s3 = t3.get_shape()
...     print('Shapes:', s1, s2, s3)
Shapes: [] (4,) (2, 2)
>>> with tf.Session(graph=g) as sess:
...     print('Ranks:',
...           r1.eval(),
...           r2.eval(),
...           r3.eval())

Ranks: 0 1 2

As we can see, the rank of the t1 tensor is 0 since it is just a scalar (corresponding to the [] shape). The rank of the t2 vector is 1, and since it has four elements, its shape is the one-element tuple (4, ). Lastly, the shape of the 2 × 2 matrix t3 is 2; thus, its corresponding shape is given by the (2, 2) tuple.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset