What is Tensor Dimension?
Tensor dimension refers to the length along a particular axis of a tensor. In simpler terms, it is the size or extent of a tensor along a specific direction. Each axis of a tensor corresponds to a dimension, and the number of dimensions in a tensor is its rank.
Python3
import tensorflow as tf # Create a 2D tensor (matrix) tensor = tf.constant([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]]) # Get the shape of the tensor shape = tensor.shape # Get the length of the first dimension dim1_length = shape[ 0 ] # Get the length of the second dimension dim2_length = shape[ 1 ] print ( "Tensor shape:" , shape) print ( "Length of first dimension:" , dim1_length) print ( "Length of second dimension:" , dim2_length) |
Output:
Tensor shape: (2, 3)
Length of first dimension: 2
Length of second dimension: 3
What is Tensor and Tensor Shapes?
Tensors are multidimensional arrays, fundamental to TensorFlow’s operations and computations. Understanding key concepts like tensor shape, size, rank, and dimension is crucial for effectively using TensorFlow in machine learning projects. In this article, we are going to understand tensor and its properties.