Contenido del Curso
Neural Networks with PyTorch
Neural Networks with PyTorch
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
or3.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:
import torch data = [[1, 2], [3, 4]] tensor = torch.tensor(data) print(tensor)
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.
Solución
¡Gracias por tus comentarios!
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
or3.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:
import torch data = [[1, 2], [3, 4]] tensor = torch.tensor(data) print(tensor)
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.
Solución
¡Gracias por tus comentarios!