Skip to main content
Hey everyone, In this lesson, we’ll dive into PyTorch Tensors—one of the core components for deep learning and model training. Tensors help you transform data into a format that is optimal for model training and inference. Throughout this guide, you will learn how to create, initialize, and manipulate tensors using various techniques. Below is some helper code that imports PyTorch and matplotlib, and defines a function to visualize tensor layers. This visualization function displays each layer along with its corresponding index, which is particularly useful for understanding multi-dimensional tensor structures.

Creating and Initializing Tensors

In this section, we demonstrate how to create different types of tensors including simple one-dimensional, multi-dimensional, and random tensors.

Simple and Multi-Dimensional Tensors

Let’s start by creating a one-dimensional tensor from a list, then a two-dimensional tensor, followed by a tensor filled with random values.
For a more advanced example, here is how to create a three-dimensional tensor with dimensions 3 x 4 x 5 filled with random values:
If you’re unsure about the structure of your tensor, use the visualize_tensor() helper function to better understand its layers, rows, and columns.

Tensors Filled with Zeros and Ones

PyTorch provides convenient functions to generate tensors pre-filled with zeros or ones, which can be extremely useful when initializing models or setting up placeholders.

Tensor Indexing and Concatenation

This section covers how to access individual elements within a tensor using indexing, as well as how to concatenate tensors along specific dimensions.

Indexing

Consider the following 3x3 tensor and learn how to access its rows and individual elements.

Concatenation

You can join tensors along an axis using PyTorch’s concatenation function. Note that concatenating tensors requires matching dimensions in the other axes.
Ensure that when concatenating tensors, all dimensions except the one being concatenated must match. Otherwise, PyTorch will raise a RuntimeError.

Transforming Images into Tensors

In real-world applications, such as building an image classifier, transforming images into tensors is essential. The PyTorch torchvision library simplifies this process by providing image transforms. Below is an example that uses the Pillow library (PIL) to open an image file and convert it into a tensor using torchvision.transforms. Replace “path_to_image.jpg” with the actual path to your image file.
When you convert an image to a tensor, the first dimension typically represents the number of channels (for example, 3 channels for an RGB image), the second dimension represents the height, and the third dimension represents the width.

Working with GPUs in PyTorch

PyTorch supports GPU acceleration, enabling faster computation for deep learning tasks. This section demonstrates how to check for GPU availability and move tensors to a GPU if one is available.
On a machine without an NVIDIA GPU or proper drivers, attempting to move a tensor to ‘cuda’ will raise a runtime error. Always check for GPU availability before transferring data.

Conclusion

This lesson provided a comprehensive introduction to working with PyTorch Tensors. We covered:
  • Creating and initializing tensors of various dimensions and data types
  • Generating tensors with zeros and ones
  • Indexing and concatenating tensors
  • Transforming images into tensors for computer vision tasks
  • Leveraging GPU acceleration for tensor computations
By mastering these fundamental concepts, you will be well-equipped to build and train deep learning models. Happy coding!

Watch Video

Practice Lab