Tensor Dimensions
Tensor Dimensions
A tensor's rank indicates the number of axes it contains, and the size of each dimension represents the length along that axis.
0-D Tensor (Scalar)
This is a single number or a lone value.
Example of a 0-D Tensor (Scalar)
import tensorflow as tf
scalar = tf.constant(3.14)
print(scalar)
1-D Tensor (Vector)
An array of numbers, similar to a list in Python.
Example of a 1-D Tensor (Vector)
vector = tf.constant([1, 2, 3])
print(vector)
2-D Tensor (Matrix)
A structured dataset made up of rows and columns.
Example of a 2-D Tensor (Matrix)
matrix = tf.constant([[1, 2], [3, 4], [5, 6]])
print(matrix)
3-D and Higher-Dimensional Tensors
Used to represent more complex structures, such as images and video data.
Example of a 3-D Tensor
tensor_3d = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(tensor_3d)
Properties of a Tensor
Tensors have several properties:
-
shape
: A tuple representing the dimensions of the tensor. -
dtype
: The data type stored in the tensor (e.g., float32, int32). -
device
: Information about the device (CPU/GPU) where the tensor is executed.
Checking Tensor Properties
example_tensor = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32)
print(f"Shape: {example_tensor.shape}")
# Shape: (2, 3)
print(f"Data Type: {example_tensor.dtype}")
# Data Type: <dtype: 'float32'>
By examining the properties of a tensor, you can easily determine its structure and data type.
In the next lesson, we'll go over a brief quiz to review what we have learned so far.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.