[box type=”note” align=”” class=”” width=””]This article is a book excerpt written by Rodolfo Bonnin, titled Building Machine Learning Projects with TensorFlow. In this book, you will learn to build powerful machine learning projects to tackle complex data for gaining valuable insights. [/box]

Today, you will learn everything about Tensors, their properties and how they are used to represent data.

## What are tensors?

TensorFlow bases its data management on tensors. Tensors are concepts from the field of mathematics, and are developed as a generalization of the linear algebra terms of vectors and matrices.

Talking specifically about TensorFlow, a tensor is just a typed, multidimensional array, with additional operations, modeled in the tensor object.

### Tensor properties – ranks, shapes, and types

TensorFlow uses tensor data structure to represent all data. Any tensor has a static type and dynamic dimensions, so you can change a tensor’s internal organization in real-time.

Another property of tensors, is that only objects of the tensor type can be passed between nodes in the computation graph.

Let’s now see what the properties of tensors are (from now on, every time we use the word tensor, we’ll be referring to TensorFlow’s tensor objects).

### Tensor rank

Tensor ranks represent the dimensional aspect of a tensor, but is not the same as a matrix rank. It represents the quantity of dimensions in which the tensor lives, and is not a precise measure of the extension of the tensor in rows/columns or spatial equivalents.

A rank one tensor is the equivalent of a vector, and a rank one tensor is a matrix. For a rank two tensor you can access any element with the syntax t[i, j]. For a rank three tensor you would need to address an element with t[i, j, k], and so on.

In the following example, we will create a tensor, and access one of its components:

``````import tensorflow as tf

sess = tf.Session()

tens1 = tf.constant([[[1,2],[2,3]],[[3,4],[5,6]]])

print sess.run(tens1)[1,1,0]``````

Output:

5

This is a tensor of rank three, because in each element of the containing matrix, there is a vector element:

 Rank Math entity Code definition example 0 Scalar scalar = 1000 1 Vector Vector vector = [2, 8, 3] 2 Matrix matrix = [[4, 2, 1], [5, 3, 2], [5, 5, 6]] 3 3-tensor tensor = [[, , ], [, , ], [, , ]] n n-tensor …

### Tensor shape

The TensorFlow documentation uses three notational conventions to describe tensor dimensionality: rank, shape, and dimension number. The following table shows how these relate to one another:

 Rank Shape Dimension number Example 0 [] 0 4 1 [D0] 1  2 [D0, D1] 2 [6, 2] 3 [D0, D1, D2] 3 [7, 3, 2] n [D0, D1, … Dn-1] n-D A tensor with shape [D0, D1, … Dn-1]

In the following example, we create a sample rank three tensor, and print the shape of it: ### Tensor data types

In addition to dimensionality, tensors have a fixed data type. You can assign any one of the following data types to a tensor:

 Data type Python type Description DT_FLOAT tf.float32 32 bits floating point. DT_DOUBLE tf.float64 64 bits floating point. DT_INT8 tf.int8 8 bits signed integer. DT_INT16 tf.int16 16 bits signed integer. DT_INT32 tf.int32 32 bits signed integer. DT_INT64 tf.int64 64 bits signed integer. DT_UINT8 tf.uint8 8 bits unsigned integer. DT_STRING tf.string Variable length byte arrays. Each element of a tensor is a byte array. DT_BOOL tf.bool Boolean.

## Creating new tensors

We can either create our own tensors, or derivate them from the well-known numpy library. In the following example, we create some numpy arrays, and do some basic math with them:

``````import tensorflow as tf

import numpy as np

x = tf.constant(np.random.rand(32).astype(np.float32))

y= tf.constant ([1,2,3])

x

Y``````

Output:

``<tf.Tensor 'Const_2:0' shape=(3,) dtype=int32>``

### From numpy to tensors and vice versa

TensorFlow is interoperable with numpy, and normally the `eval()` function calls will return a numpy object, ready to be worked with the standard numerical tools.

We must note that the tensor object is a symbolic handle for the result of an operation, so it doesn’t hold the resulting values of the structures it contains. For this reason, we must run the `eval()`
method to get the actual values, which is the equivalent to `Session.run(tensor_to_eval)`.

In this example, we build two numpy arrays, and convert them to tensors:

``````import tensorflow as tf #we import tensorflow

import numpy as np #we import numpy

sess = tf.Session() #start a new Session Object

x_data = np.array([[1.,2.,3.],[3.,2.,6.]]) # 2x3 matrix

x = tf.convert_to_tensor(x_data, dtype=tf.float32)

print (x)``````

Output:

``Tensor("Const_3:0", shape=(2, 3), dtype=float32)``

Useful method:
`tf.convert_to_tensor`: This function converts Python objects of various types to tensor objects. It accepts tensorobjects, numpy arrays, Python lists, and Python scalars.

## Getting things done – interacting with TensorFlow

As with the majority of Python’s modules, TensorFlow allows the use of Python’s interactive console: In the previous figure, we call the Python interpreter (by simply calling Python) and create a tensor of constant type. Then we invoke it again, and the Python interpreter shows the shape and type of the tensor.

We can also use the IPython interpreter, which will allow us to employ a format more compatible with notebook-style tools, such as Jupyter: When talking about running `TensorFlow` Sessions in an interactive manner, it’s better to employ the `InteractiveSession` object. Unlike the normal `tf.Session` class, the `tf.InteractiveSession` class installs itself as the default session on construction. So when you try to eval a tensor, or run an operation, it will not be necessary to pass a Session object to indicate which session it refers to.

To summarize, we have learned about tensors, the key data structure in TensorFlow and simple operations we can apply to the data.

To know more about different machine learning techniques and algorithms that can be used to build efficient and powerful projects, you can refer to the book Building Machine Learning Projects with TensorFlow.  Data Science fanatic. Cricket fan. Series Binge watcher. You can find me hooked to my PC updating myself constantly if I am not cracking lame jokes with my team.