CHAPTER 06
Intermediate
PyTorch Tensors and Tensor Operations
Updated: May 16, 2026
6 min read
# CHAPTER 6
PyTorch Tensors and Tensor Operations
1. Introduction
The fundamental building block of PyTorch is the Tensor. A tensor is a multi-dimensional array of numbers. If you read Chapter 4, you might be thinking: *"Wait, that is exactly what a NumPy ndarray is!"* You are correct. Tensors and NumPy arrays are almost identical, but Tensors have two mathematical superpowers: they can run on GPUs, and they can automatically calculate calculus gradients. In this chapter, we master Tensor manipulation.2. Learning Objectives
By the end of this chapter, you will be able to:- Create PyTorch Tensors from scratch or from NumPy arrays.
- Understand Tensor dimensions (Ranks) and shapes.
- Perform basic tensor operations (addition, multiplication).
- Move tensors between the CPU and the GPU.
- Understand the basics of the Autograd engine.
3. Creating Tensors
Let's importtorch and create some basic tensors.
python
4. Tensor Operations
PyTorch includes heavily optimized functions for all standard mathematical operations.
python
5. GPU Tensors
A massive advantage of PyTorch over NumPy is GPU acceleration. By default, tensors are created on the CPU. We must explicitly move them to the GPU.
python
6. The Magic of Autograd
The second superpower of Tensors is Autograd. If you tell a Tensorrequires_grad=True, PyTorch will quietly record every single mathematical operation done to that Tensor. When you call .backward(), PyTorch will instantly calculate the calculus derivative (gradient) for Backpropagation.
python
7. Mini Project: Tensor Operations Playground
Let's generate some random tensors and inspect them, a very common task when initializing neural network weights.
python
*Notice .item()? If you have a Tensor that contains exactly ONE number (like maxval), you call .item() to extract the raw Python float out of it.*
8. Common Mistakes
-
Shape Mismatch in Matrix Multiplication: You cannot use
torch.matmulon two tensors unless the inner dimensions match. E.g., A(3, 2)matrix can multiply with a(2, 5)matrix, but NOT a(3, 5)matrix. This is a strict rule of linear algebra!
-
Mixing Devices: You cannot add a CPU Tensor to a GPU Tensor. PyTorch will crash. Both tensors must be on the same
devicebefore performing math.
9. Best Practices
-
Use
torch.float32: Deep learning relies on decimals. Ensure your input tensors are usuallyfloat32. Usingfloat64takes up twice as much RAM on your GPU and rarely improves accuracy.
10. Exercises
-
1.
Create a PyTorch 2D matrix filled entirely with the number 1 (Hint: research
torch.ones).
-
2.
Write code to move a tensor named
mytensorto the GPU using the.to()method.
11. MCQ Quiz with Answers
Question 1
What are the two primary advantages a PyTorch Tensor has over a standard NumPy array?
Question 2
If you try to add Tensor A (which is on the CPU) to Tensor B (which is on the GPU), what happens?
12. Interview Questions
-
Q: Explain what the
requiresgrad=Trueparameter does when initializing a PyTorch Tensor.
- Q: Explain what "Rank" means in the context of a Tensor and give an example of a Rank-2 Tensor.
13. FAQs
Q: Do I need to manually track gradients in production (e.g., when the model is making predictions for a user)? A: No! During production/inference, you don't need to do Backpropagation, so tracking gradients is a waste of memory. You should wrap your prediction code in awith torch.nograd(): block.