Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Tensor Properties | Tensors
Introduction to TensorFlow
course content

Course Content

Introduction to TensorFlow

Introduction to TensorFlow

1. Tensors
2. Basics of TensorFlow

book
Tensor Properties

Tensor Properties

Tensors come with distinct properties that dictate their structure and how they process and store data.

  • Rank: It tells you the number of dimensions present in the tensor. For instance, a matrix has a rank of 2. You can get the rank of the tensor using the .ndim attribute:
1234567891011121314151617
import tensorflow as tf # Create tensors tensor_1D = tf.constant([1, 2, 3]) tensor_2D = tf.constant([ [1, 2], [3, 4] ]) tensor_3D = tf.constant([ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]) # Get ranks print(f'Rank of 1D tensor: {tensor_1D.ndim}') print(f'Rank of 2D tensor: {tensor_2D.ndim}') print(f'Rank of 3D tensor: {tensor_3D.ndim}')
copy

Note

We've structured the definition of Python lists over multiple lines for clearer readability. You can condense it into a single line to see that it functions the same way.

  • Shape: This describes how many values exist in each dimension. A 2x3 matrix has a shape of (2, 3). The length of the shape parameter matches the tensor's rank (its number of dimensions). You can get the the shape of the tensor by the .shape attribute:
123456789101112131415161718
import tensorflow as tf # Create tensors tensor_1D = tf.constant([1, 2, 3, 4]) tensor_2D = tf.constant([ [1, 2, 3], [4, 5, 6] ]) tensor_3D = tf.constant([ [[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]] ]) # Get shapes print(f'Shape of 1D tensor: {tensor_1D.shape}') print(f'Shape of 2D tensor: {tensor_2D.shape}') print(f'Shape of 3D tensor: {tensor_3D.shape}')
copy

Note

Getting tensor shapes and ranks correct is crucial in deep learning. Dimension mismatches are common pitfalls, especially when building complex models in TensorFlow.

  • Types: Tensors come in various data types. While there are many, some common ones include float32, int32, and string. We'll delve deeper into tensor data types in upcoming chapters. You can get the the data type of the tensor by the .dtype attribute:
1234567891011
import tensorflow as tf # Create tensors tensor_int = tf.constant([1, 2, 3, 4]) tensor_float = tf.constant([1., 2., 3., 4.]) tensor_string = tf.constant(['a', 'b', 'c', 'd']) # Get data type print(f'Data type of 1D tensor: {tensor_int.dtype}') print(f'Data type of 2D tensor: {tensor_float.dtype}') print(f'Data type of 3D tensor: {tensor_string.dtype}')
copy

Note

Data type of a tensor is determined by the content it contains. It's essential that all elements within the tensor are of the same type.

  • Axes: Axes help us navigate through dimensions of tensors. By specifying an axis, you can pinpoint a specific layer or direction in the tensor, making it easier to process and understand the data. Axes correspond directly to shape dimensions. Each axis corresponds to a specific shape value, with the 0th axis aligning with the first shape value, the 1st axis with the second, and so on.
Task
test

Swipe to show code editor

In this task, you are provided with two tensors. The first tensor is already created for you; your task is to display its properties using the relevant tensor attributes. For the second tensor, you'll need to construct it yourself with the following specifications:

  • Rank: 3.
  • Shape: (2, 4, 3).
  • Data type: float.

So, your steps are:

  1. Retrieve the properties of the first tensor.
  2. Construct a tensor that meets the specified criteria.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 3
toggle bottom row

book
Tensor Properties

Tensor Properties

Tensors come with distinct properties that dictate their structure and how they process and store data.

  • Rank: It tells you the number of dimensions present in the tensor. For instance, a matrix has a rank of 2. You can get the rank of the tensor using the .ndim attribute:
1234567891011121314151617
import tensorflow as tf # Create tensors tensor_1D = tf.constant([1, 2, 3]) tensor_2D = tf.constant([ [1, 2], [3, 4] ]) tensor_3D = tf.constant([ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]) # Get ranks print(f'Rank of 1D tensor: {tensor_1D.ndim}') print(f'Rank of 2D tensor: {tensor_2D.ndim}') print(f'Rank of 3D tensor: {tensor_3D.ndim}')
copy

Note

We've structured the definition of Python lists over multiple lines for clearer readability. You can condense it into a single line to see that it functions the same way.

  • Shape: This describes how many values exist in each dimension. A 2x3 matrix has a shape of (2, 3). The length of the shape parameter matches the tensor's rank (its number of dimensions). You can get the the shape of the tensor by the .shape attribute:
123456789101112131415161718
import tensorflow as tf # Create tensors tensor_1D = tf.constant([1, 2, 3, 4]) tensor_2D = tf.constant([ [1, 2, 3], [4, 5, 6] ]) tensor_3D = tf.constant([ [[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]] ]) # Get shapes print(f'Shape of 1D tensor: {tensor_1D.shape}') print(f'Shape of 2D tensor: {tensor_2D.shape}') print(f'Shape of 3D tensor: {tensor_3D.shape}')
copy

Note

Getting tensor shapes and ranks correct is crucial in deep learning. Dimension mismatches are common pitfalls, especially when building complex models in TensorFlow.

  • Types: Tensors come in various data types. While there are many, some common ones include float32, int32, and string. We'll delve deeper into tensor data types in upcoming chapters. You can get the the data type of the tensor by the .dtype attribute:
1234567891011
import tensorflow as tf # Create tensors tensor_int = tf.constant([1, 2, 3, 4]) tensor_float = tf.constant([1., 2., 3., 4.]) tensor_string = tf.constant(['a', 'b', 'c', 'd']) # Get data type print(f'Data type of 1D tensor: {tensor_int.dtype}') print(f'Data type of 2D tensor: {tensor_float.dtype}') print(f'Data type of 3D tensor: {tensor_string.dtype}')
copy

Note

Data type of a tensor is determined by the content it contains. It's essential that all elements within the tensor are of the same type.

  • Axes: Axes help us navigate through dimensions of tensors. By specifying an axis, you can pinpoint a specific layer or direction in the tensor, making it easier to process and understand the data. Axes correspond directly to shape dimensions. Each axis corresponds to a specific shape value, with the 0th axis aligning with the first shape value, the 1st axis with the second, and so on.
Task
test

Swipe to show code editor

In this task, you are provided with two tensors. The first tensor is already created for you; your task is to display its properties using the relevant tensor attributes. For the second tensor, you'll need to construct it yourself with the following specifications:

  • Rank: 3.
  • Shape: (2, 4, 3).
  • Data type: float.

So, your steps are:

  1. Retrieve the properties of the first tensor.
  2. Construct a tensor that meets the specified criteria.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 3
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt