Skip to main content

Attributes of Tensor

Summary

  • Shape
  • Rank
  • Axis
  • Data type

Content

Following are the main attributes of a tensor

  1. Shape - Length of the each dimension
tf.tensor.shape()
  1. Rank - Number of dimensions of the tensor
tf.tensor.ndim()
  1. Axis|Dimension - Particular dimension of a tensor
tensor[n]
  1. Size - Total number of items in the tensor
tf.size()
  1. DType - Data type of the stored values
tensor.dtype

Ex:-

rand = tf.random.Generator.from_seed(40)
tensor = rand.normal(shape=(2, 3, 5))

tensor.shape
# TensorShape([2, 3, 5])

tensor.ndim
# 3

tensor[0, 0]
"""
<tf.Tensor: shape=(5,), dtype=float32, numpy=
array([-1.2864609, 2.1502466, -0.6779422, 1.4553545, -0.8907286],
dtype=float32)>
"""

tf.size(tensor)
# <tf.Tensor: shape=(), dtype=int32, numpy=30>

tensor.dtype
# tf.float32