Instagram
youtube
Facebook
Twitter

Types of Tensors

In TensorFlow, tensors are the fundamental data structure used for computations. A tensor is a multi-dimensional array with a uniform data type. In this tutorial, we’ll learn about the types of tensors in Tensorflow that are commonly used to train models.

Some common types of tensors in tensorflow are:

Constant Tensors: These are tensors with fixed values that cannot be changed during runtime. We can define constants using the tf.constant() method.

Example:

import tensorflow as tf
a = tf.constant([2,3,4])
b = tf.constant([9,4,5])
c = tf.add(a,b)
print(c)

Output:

tf.Tensor([11  7  9], shape=(3,), dtype=int32)

Variable Tensors: These tensors can be changed during execution. They are often used for model weights or other trainable parameters. They allow us to add new trainable parameters to graph. It can be defined using the tf.Variable() method.

Example:

import tensorflow as tf
a = tf.Variable([2,3,4])
b = tf.Variable([9,4,5])
b.assign(tf.constant([2,3,3]))
c = tf.add(a,b)
print(c)

Here, we reassigned the value of b.

Output:

tf.Tensor([4 6 7], shape=(3,), dtype=int32)

Placeholder tensors: These are tensors that act as placeholders for input data. They are typically used for feeding input data into a TensorFlow model.  In TensorFlow 2.0, placeholders have been deprecated in favor of the tf.data API.

Sparse tensors: These are tensors that contain mostly zeros. They are used to represent large, sparse datasets efficiently. It is defined using tf.sparse() method.

Example:

import tensorflow as tf
dense_tensor = tf.constant([[0, 0, 0, 2],
                            [1, 0, 3, 0],
                            [0, 4, 0, 5]])
sparse_tensor = tf.sparse.from_dense(dense_tensor)
print(sparse_tensor)

Output:

SparseTensor(indices=tf.Tensor(
[[0 3]
 [1 0]
 [1 2]
 [2 1]
 [2 3]], shape=(5, 2), dtype=int64), values=tf.Tensor([2 1 3 4 5], shape=(5,), dtype=int32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))