Introduction to Tensors in PyTorch
PyTorch is organized into three broad components: a tensor library, an automatic differentiation engine, and a deep learning library.
Why Start With Three Components
PyTorch is a large library with many functions, classes, and modules, so a newcomer can easily feel lost on the first day. Trying to memorize every part is a poor starting strategy. A better question is: what broad jobs does the library perform? The source material answers this by grouping PyTorch into three broad components: a tensor library, an automatic differentiation engine, and a deep learning library. Each component solves a different problem, and the three are designed to work together.
This article follows one realistic task through all three components. Each component is then tied to a concrete stage of work rather than being left as an abstract label.
A Training Step to Follow
Imagine you are training a small neural network to recognize handwritten digits. Before the network can learn anything, your images must be held in memory as numbers. The network multiplies those numbers by a set of adjustable weights, compares its output with the correct answer, and then nudges the weights so the next guess is a little better. Repeat this thousands of times and the network gradually improves.
What do you think happens?
In the digit-recognition loop, which component works out how much each weight should change?
Reveal answer
The automatic differentiation engine (autograd)
Working out how much each weight should change requires gradients. Gradients are computed by the automatic differentiation engine, which is why autograd is the component responsible for this step. The tensor library holds the numbers and performs the multiplications, and the deep learning library organizes the layers, error measure, and update rule.
Notice that the question has a clear answer only once you know what each component is for. The rest of this article builds that knowledge one component at a time, then returns to the same loop to assign each stage to its owner.
The Tensor Library
A tensor is a multidimensional array of numbers. PyTorch's tensor library is the first of the three components, and it provides the tensors that the other two components operate on.
PyTorch's tensor library is built as an extension of NumPy, which is an array-oriented programming library. Someone who already knows how to work with NumPy arrays will find many familiar ideas here. The important difference is that PyTorch adds a feature NumPy alone does not provide: it can accelerate computation on GPUs.
Because of this GPU support, PyTorch offers a seamless switch between CPUs and GPUs. In practical terms, the same tensor-based computation can run on either kind of processor, and the library handles the difference rather than forcing the programmer to rewrite the computation for each device. This is the base layer that the other two components are built on.
The Automatic Differentiation Engine
The automatic differentiation engine, also called autograd, computes gradients for tensor operations. A gradient describes how much a result changes when one of its inputs changes slightly.
Deep learning training depends on gradients, because they tell the model which direction to move its weights. Without gradients, the network in the digit-recognition task would have no principled way to nudge its weights toward better guesses.
Computing these gradients by hand becomes impractical once a model contains many operations. Autograd removes that burden. Because the engine handles gradient computation for tensor operations, the process of backpropagation is simplified. Backpropagation passes error information backward through the model to update its weights. The same mechanism also supports model optimization, since optimization steps rely on the gradients that autograd supplies.
The Deep Learning Library
The third component is a deep learning library. It offers modular, flexible, and efficient building blocks for designing and training deep learning models. The word modular matters here. Instead of writing every part of a network from scratch, a developer assembles components that are already defined and can be combined in different ways.
- Pretrained models: models that have already been trained, giving a starting point for new work.
- Loss functions: measures of how far a model's output is from the correct answer.
- Optimizers: tools that use the gradients to adjust the weights.
The source notes that these pieces serve both researchers and developers who design and train a wide range of deep learning models. In the digit-recognition task, the deep learning library is the component that organizes the network layers, the error measure, and the update rule into reusable parts.
How the Three Components Depend on One Another
The three components are not three unrelated toolboxes. The source describes a layered relationship. The deep learning utilities make use of the tensor library and the automatic differentiation engine. The deep learning library therefore sits on top, while the tensor library and autograd form the foundation it relies on.
The diagram makes one dependency easy to miss. Autograd does not compute gradients in isolation. It operates on the tensors and tensor operations provided by the tensor library. This is why the source lists the tensor library first and describes autograd as depending on it. Learning the three components in that order keeps each new idea anchored to the one before it.
Tracing the Training Step Stage by Stage
Return to the digit-recognition loop with the three roles in mind. Data enters as tensors, the model performs tensor operations on it, autograd produces gradients from those operations, and the gradients then drive backpropagation and the optimizer's weight updates. The diagram below traces that path in order.
Assigning each stage of one training step
A network is learning to recognize handwritten digits. For each stage of one training step, identify which PyTorch component handles it.
- 1Stage 1: hold the images as numbers
Images must be stored as numbers before the network can use them. Storing and manipulating numbers in array form is the job of the tensor library.
- 2Stage 2: multiply the numbers by the weights
The network performs tensor operations on the data. These multiplications are computed with tensors, so the tensor library handles them.
- 3Stage 3: compare the output with the correct answer
The error measure is a loss function, which is one of the reusable building blocks in the deep learning library.
- 4Stage 4: work out how each weight should change
Gradients describe how much each result changes when an input changes. The automatic differentiation engine computes these gradients for the tensor operations, and backpropagation uses them to pass error information backward.
- 5Stage 5: update the weights
The optimizer uses the gradients to adjust the weights. The optimizer is a building block from the deep learning library, and it consumes the gradients that autograd supplies.
Stages 1 and 2 belong to the tensor library, stage 3 and the optimizer in stage 5 belong to the deep learning library, and stage 4 belongs to the automatic differentiation engine. The step touches all three components in sequence, and the programmer supplies only the data and model structure.
Read the flow from left to right and notice what stays out of your hands. You supply the data and the model structure. The autograd engine supplies the gradients, and the optimization step consumes them. This is the handoff the source describes when it says autograd simplifies backpropagation and model optimization, with no manual gradient calculation in the middle.
Mistakes When Mapping Components to Jobs
Common mistake Assuming the deep learning library computes gradients
What it looks likeA learner sees the optimizer updating weights and concludes that the optimizer must also calculate how each weight should change.
Why it failsThe optimizer uses the gradients to adjust the weights, but the gradients themselves come from the automatic differentiation engine.
Do this insteadAssign gradient computation to autograd and weight adjustment to the optimizer in the deep learning library.
Common mistake Treating the tensor library and autograd as unrelated
What it looks likeA learner thinks gradients can be computed separately from the tensor operations they describe.
Why it failsAutograd operates on the tensors and tensor operations provided by the tensor library, so it depends on that foundation.
Do this insteadLearn the tensor library first, then autograd, then the deep learning library, following the layered order.
Common mistake Thinking GPU support requires rewriting the computation
What it looks likeA learner expects to write a separate version of the same tensor computation for each processor type.
Why it failsThe tensor library offers a seamless switch between CPUs and GPUs, handling the device difference for the programmer.
Do this insteadWrite the tensor computation once and let the library manage whether it runs on a CPU or a GPU.
Common mistake Believing the deep learning library must be written from scratch for every project
Practice Tracing a Step
Suppose a new model is trained to classify images of clothing. Without writing any code, list the stages of one training step in order and name the PyTorch component responsible for each stage. Mark which stage depends on gradients.
Hints
- Start with where the images are stored and ask which component holds numbers and performs multiplications.
- Ask which component supplies gradients, and which component's optimizer uses them to adjust weights.
- The gradient stage is the one that comes from autograd, and it sits between the error comparison and the weight update.
Key Takeaways
- PyTorch is organized into three broad components: a tensor library, an automatic differentiation engine (autograd), and a deep learning library.
- The tensor library extends NumPy's array-oriented approach with GPU acceleration and a seamless switch between CPUs and GPUs, and it is the base layer for the other two components.
- Autograd computes gradients for tensor operations, which simplifies backpropagation and supplies the gradients that model optimization relies on.
- The deep learning library provides modular building blocks such as pretrained models, loss functions, and optimizers, and it sits on top of the tensor library and autograd.
- A single training step passes through all three components in sequence: tensors hold the data, autograd produces gradients, and the deep learning library's optimizer updates the weights.