Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Tensors | PyTorch Basics
PyTorch Essentials
course content

Course Content

PyTorch Essentials

PyTorch Essentials

1. PyTorch Basics
2. Preparing for Neural Networks
3. Neural Networks

book
Tensors

Now that you've been introduced to PyTorch, it's time to explore one of its core components: tensors. Understanding tensors is crucial, as they form the foundation of how data is represented and manipulated in PyTorch.

What is a Tensor?

You are already familiar with some special cases of tensors:

  • Scalar (0D tensor): a single number, like 5 or 3.14;
  • Vector (1D tensor): a list of numbers, such as [1, 2, 3];
  • Matrix (2D tensor): a 2D grid of numbers, like a table with rows and columns.

Higher-dimensional tensors (3D, 4D, etc.) extend the concept of matrices into additional dimensions. For example, a 3D tensor can represent an image with height, width, and color channels.

While the terminology may seem complex at first, the key idea is that tensors are simply containers for numerical data, much like NumPy arrays.

Tensors in PyTorch vs. NumPy Arrays

PyTorch tensors behave similarly to NumPy arrays in many ways. Additionally, indexing and slicing in tensors work the same way as in NumPy arrays, so we won't cover these topics in this course.

However, PyTorch tensors offer additional advantages, such as:

  • Native support for GPU acceleration;
  • Integration with PyTorch's deep learning modules;
  • Compatibility with autograd, PyTorch's automatic differentiation tool for backpropagation.

Creating Tensors

PyTorch provides several ways to create tensors. One of the most basic approaches is to create a tensor from a list or a NumPy array. The recommended way to do this is by passing the data to the torch.tensor() function:

1234
import torch data = [[1, 2], [3, 4]] tensor = torch.tensor(data) print(tensor)
copy
Task
test

Swipe to begin your solution

Create a 3D tensor directly from a 3D list without storing the list in a separate variable. The tensor can have any dimensions and contain arbitrary elements.

Solution

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 2
toggle bottom row

book
Tensors

Now that you've been introduced to PyTorch, it's time to explore one of its core components: tensors. Understanding tensors is crucial, as they form the foundation of how data is represented and manipulated in PyTorch.

What is a Tensor?

You are already familiar with some special cases of tensors:

  • Scalar (0D tensor): a single number, like 5 or 3.14;
  • Vector (1D tensor): a list of numbers, such as [1, 2, 3];
  • Matrix (2D tensor): a 2D grid of numbers, like a table with rows and columns.

Higher-dimensional tensors (3D, 4D, etc.) extend the concept of matrices into additional dimensions. For example, a 3D tensor can represent an image with height, width, and color channels.

While the terminology may seem complex at first, the key idea is that tensors are simply containers for numerical data, much like NumPy arrays.

Tensors in PyTorch vs. NumPy Arrays

PyTorch tensors behave similarly to NumPy arrays in many ways. Additionally, indexing and slicing in tensors work the same way as in NumPy arrays, so we won't cover these topics in this course.

However, PyTorch tensors offer additional advantages, such as:

  • Native support for GPU acceleration;
  • Integration with PyTorch's deep learning modules;
  • Compatibility with autograd, PyTorch's automatic differentiation tool for backpropagation.

Creating Tensors

PyTorch provides several ways to create tensors. One of the most basic approaches is to create a tensor from a list or a NumPy array. The recommended way to do this is by passing the data to the torch.tensor() function:

1234
import torch data = [[1, 2], [3, 4]] tensor = torch.tensor(data) print(tensor)
copy
Task
test

Swipe to begin your solution

Create a 3D tensor directly from a 3D list without storing the list in a separate variable. The tensor can have any dimensions and contain arbitrary elements.

Solution

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 2
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