Tensors, GPUs and Your First Colab Notebook
PyTorch is the most widely used library for building and training neural networks. Research labs use it, and so do many companies that ship AI products. In this course you will use it to train real models on real data, starting from zero.
This first lesson sets up your free workspace in Google Colab and teaches you the one object everything in PyTorch is built on: the tensor.
What You'll Learn
- Why this course runs in Google Colab and how to get a free GPU there
- What a tensor is and how it relates to a numpy array
- How to read a tensor's shape, data type and device
- The tensor operations you will use in every lesson
- Why GPUs make deep learning fast, and how to move a tensor to one
Before You Start
You need basic Python: variables, lists, loops and functions. If you have used numpy, even better. If not, the Python for AI & Data Science course covers what you need.
You do not need to install anything on your computer.
Where the Code Runs: Google Colab
Most lessons on FreeAcademy.ai have a Python playground that runs in your browser. That playground can run numpy, but it cannot run PyTorch. PyTorch is a large library, and training needs far more compute than a browser tab has.
So for every PyTorch code block in this course, you will copy the code into Google Colab. Colab is a free notebook from Google that runs Python on Google's computers. As of November 2026, PyTorch and torchvision come preinstalled in Colab, so there is nothing to set up.
To get started:
- Go to colab.research.google.com and sign in with a Google account.
- Click New notebook.
- Open the Runtime menu, choose Change runtime type, pick a GPU (usually the T4 GPU on the free plan), and save.
- Paste code into a cell and press Shift + Enter to run it.
A few things to know about the free plan:
- GPUs are free but not guaranteed. Google says Colab resources are not unlimited and usage limits change over time. If no GPU is available, every example in this course still runs on CPU, only slower.
- Your session resets after a while if you leave it idle. Files you create in Colab are deleted when it resets. Lesson 6 shows how to save your model to Google Drive.
- When a lesson says "keep your notebook open", it means the later code uses variables from earlier cells. If your session reset, run the earlier cells again from the top.
Check that everything works by running this in your first cell:
import torch
print(torch.__version__)
print("Accelerator available:", torch.accelerator.is_available())
You should see a version number starting with 2 and, if you picked a GPU, True. As of November 2026 the latest PyTorch release is 2.15, and Colab usually runs one version behind. Everything in this course works on both.
What Is a Tensor?
A tensor is a grid of numbers. That is all. The number of directions the grid goes in is called its number of dimensions.
| Dimensions | Common name | Example |
|---|---|---|
| 0 | Scalar | A single loss value: 0.37 |
| 1 | Vector | One row of features: [5.1, 3.5, 1.4] |
| 2 | Matrix | A grayscale image: 28 rows by 28 columns |
| 3 | 3D tensor | A color image: 3 color channels, height, width |
| 4 | 4D tensor | A batch of 64 color images |
If you know numpy, a tensor is almost the same as a numpy array. The two big differences are that a tensor can live on a GPU, and a tensor can track gradients for training (lesson 2).
Creating Tensors
Paste this into Colab:
import torch
a = torch.tensor([1.0, 2.0, 3.0]) # from a Python list
b = torch.zeros(2, 3) # 2 rows, 3 columns of zeros
c = torch.ones(2, 3)
d = torch.rand(2, 3) # random numbers between 0 and 1
e = torch.arange(0, 10) # 0, 1, 2, ... 9
print(a)
print(d)
print(e)
The Three Things to Check on Every Tensor
Most PyTorch errors come from one of three things not matching. Get in the habit of printing them:
x = torch.rand(64, 1, 28, 28)
print(x.shape) # torch.Size([64, 1, 28, 28])
print(x.dtype) # torch.float32
print(x.device) # cpu
- shape: the size along each dimension. Here: 64 images, 1 color channel, 28 by 28 pixels.
- dtype: the number type. Neural networks almost always use
torch.float32. Class labels usetorch.int64. - device: where the tensor lives,
cpuor a GPU such ascuda:0.
Operations You Will Use All the Time
x = torch.tensor([[1., 2.], [3., 4.]])
w = torch.tensor([[0.5], [0.25]])
print(x + 10) # add to every number
print(x * 2) # multiply every number
print(x @ w) # matrix multiplication, the core of a neural layer
print(x.sum(), x.mean())
print(x.max(dim=1)) # largest value in each row, and where it is
The @ sign does matrix multiplication. A neural network layer is mostly one matrix multiplication plus an addition. When you train on a GPU, these are the operations that run in parallel.
Reshaping
Layers expect inputs in a certain shape, so you will reshape tensors often:
img = torch.rand(28, 28)
flat = img.reshape(784) # 28 * 28 = 784 numbers in one row
print(flat.shape) # torch.Size([784])
batch = torch.rand(64, 28, 28)
flat_batch = batch.reshape(64, -1) # -1 means "work it out for me"
print(flat_batch.shape) # torch.Size([64, 784])
Moving Between numpy and PyTorch
import numpy as np
arr = np.array([1.0, 2.0, 3.0])
t = torch.from_numpy(arr) # numpy to tensor
back = t.numpy() # tensor to numpy (CPU tensors only)
print(t, back)
Why GPUs Matter
A CPU has a small number of strong cores. It is good at doing one complicated thing after another. A GPU has thousands of small cores. It is good at doing the same simple math on many numbers at once.
Training a neural network is mostly multiplying big grids of numbers. Each multiplication can be split into many small pieces that run at the same time. That is why a GPU often trains a model many times faster than a CPU.
Try it yourself. This compares one large matrix multiplication on CPU and on GPU:
import time
import torch
device = torch.accelerator.current_accelerator().type if torch.accelerator.is_available() else "cpu"
print("Using", device)
a = torch.rand(4000, 4000)
b = torch.rand(4000, 4000)
start = time.time()
a @ b
print(f"CPU: {time.time() - start:.3f} seconds")
if device != "cpu":
a_gpu, b_gpu = a.to(device), b.to(device)
a_gpu @ b_gpu # warm-up run
torch.accelerator.synchronize()
start = time.time()
a_gpu @ b_gpu
torch.accelerator.synchronize() # wait for the GPU to finish
print(f"GPU: {time.time() - start:.3f} seconds")
The exact numbers depend on the GPU you get, but the GPU is usually far faster.
The Device Line You Will Reuse
The first line in the code above is the one the official PyTorch tutorials use as of November 2026. It picks the GPU if one is available and falls back to the CPU if not:
device = torch.accelerator.current_accelerator().type if torch.accelerator.is_available() else "cpu"
On Colab with a GPU this gives "cuda". On a Mac with Apple silicon it gives "mps". You will put this line at the top of every notebook in this course.
One rule to remember: tensors must be on the same device to work together. If your model is on the GPU and your data is on the CPU, you get an error like "Expected all tensors to be on the same device". The fix is .to(device) on both.
x = torch.rand(3).to(device)
y = torch.rand(3).to(device)
print(x + y)
Key Takeaways
- PyTorch does not run in the browser playground. Paste the code into Google Colab, which has PyTorch preinstalled and a free GPU when one is available.
- A tensor is a grid of numbers, like a numpy array that can live on a GPU and track gradients.
- Always check a tensor's shape, dtype and device when something breaks.
- GPUs are fast at deep learning because they do the same math on many numbers at once.
- Use the
deviceline at the top of every notebook and move models and data with.to(device).
Quiz
Up next · Lesson 2 of 6
Autograd and Gradient Descent by Hand
5 min read
1 of 6 lessons done

Ask Byte about this lesson
A private chat about this lesson. Byte has read the whole lesson, answers in seconds, and shows the section each answer comes from. Ask follow-ups in any language.
Questions & Answers
Public questions about this lesson. The AI Tutor answers right away in your language, the FreeAcademy team checks answers, and every learner can read them.

