Introduction to Tensors and its Types


TensorFlow is an Open Source library, specially designed to perform complex numerical computations, using data-flow graphs. Tensor is fundamental computational unit in TensorFlow. The Article will help you to understand basic concepts of Tensor and commonly used type of Tensors.

Tensors: Tensors are the basic computation unit in tensor flow, which is nothing but an array of Numbers. They can be

A tensor may consist of a single number, in which case it is referred to as a tensor of order zero, or simply a scalar. For reasons which will become apparent, a scalar may be thought of as an array of dimension zero (same as the order of the tensor).

The most complicated tensor is the tensor of order one, otherwise known as a vector. in an n-dimensional space, a vector (tensor of order one) has n components. A vector may be thought of as an array of dimension one. This is because the components of a vector can be visualized as being written in a column or along a line, which is one dimensional.

Each element in the Tensor has the same data type, and the data type is always known.

Rank: A tensor’s rank is its number of dimensions of the array needed to represent the Tensor.

import tensorflow as tf
import numpy as np

# scalar of rank 0
myScalar = tf.constant(10000)
# vector of rank 1
myVector = tf.constant([1, 2, 3, 4, 5])
#matrix of rank 2
myMatrix = tf.constant([[1, 2, 3], [4, 5, 6]])

print(tf.rank(myScalar))
print(tf.rank(myVector))
print(tf.rank(myMatrix))

Shape: The tensor’s shape is the number of rows and columns it has. TensorFlow has ability to automatically infer shapes during graph construction.

import tensorflow as tf
import numpy as np

# scalar of rank 0
myScalar = tf.constant(10000)
# vector of rank 1
myVector = tf.constant([1, 2, 3, 4, 5])
#matrix of rank 2
myMatrix = tf.constant([[1, 2, 3], [4, 5, 6]])
print(myScalar.get_shape())
print(myVector.get_shape())
print(myMatrix.get_shape())

Type: The data type assigned to tensor elements. The list of datatypes supported in tensorflow is given below:

Types of Tensors: The types of tensors are:

  1. tf.Variable:

It is used to maintain state in the graph across calls to run(). The initial value of variable, defines the type and shape of the variable. After construction, the type and shape of the variable are fixed.By using tf.assign, an initializer set initial variable value. The value can be changed using assign methods.

import tensorflow as tf
import numpy as np

myVar = tf.Variable(0)

init = tf.global_variables_initializer()

with tf.Session() as session:
session.run(init)
print (session.run(myVar))
#reassign new value to variable
myVar = myVar.assign(29)
print(session.run(myVar))

2. tf.Constant:

It has value and data type which can not be changed through out the program. The argument value can be a constant value, or a list of values of type dtype.

#General Syntax to declare Constant
#constant(value, dtype=None, shape=None, name='Const', verify_shape=False)
pi = tf.constant(3.14, dtype= tf.float32)
# Constant 1-D Tensor populated with value list.
listConstant = tf.constant([1, 2, 3, 4, 5, 6, 7])
init = tf.global_variables_initializer()

with tf.Session() as session:
session.run(init)
print (session.run(pi))
print(session.run(listConstant))

3. tf.placeholder:

There is a more basic structure, the ‘placeholder’. A placeholder is a variable that we can assign data to at a later date. It allows to create our operations and build our computation graph, without needing the data. Its value must be fed using the feed_dict optional argument to Session.run()

#General syntax for placeholders
#tf.placeholder(dtype,shape=None,name=None)
x = tf.placeholder("float", [None, 3])
y = x * 2

with tf.Session() as session:
x_data = [[1, 2, 3],
[4, 5, 6],]
result = session.run(y, feed_dict={x: x_data})
print(result)

4. tf.SparseTensor:

SparseTensor representation for data that is sparse in multiple dimensions. A sparse representation of the same tensor will focus only on the non-zero values. Indices method will return only indices of non-zero values in the represented dense tensor, while shape will return the shape of the dense tensor.

import tensorflow as tf
tens = tf.Variable([[4, 3, 1, 2, 5],
[2, 3, 4, 1, 0],
[1, 2, 3, 0, 0],
[5, 4, 0, 0, 0]], tf.int32)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
idx = tf.where(tf.not_equal(tens, 0))
sparse = tf.SparseTensor(idx, tf.gather_nd(tens, idx), tens.get_shape())
myST = sess.run(sparse)
print (myST.indices)
print (myST.values)
print (myST.dense_shape)

Understanding basic concept of Tensor is very important. If you like the article, please like and share the article. Stay tuned for more articles on TensorFlow.

Source: Deep Learning on Medium