PyTorch Introduction & Tensors
Install PyTorch, create and reshape tensors, move them to the GPU, and understand why PyTorch is the most popular deep learning framework.
What is PyTorch?
PyTorch is the most widely used framework for deep learning research and a leading choice in industry. It gives you:
- Tensors — fast multi-dimensional arrays that run on CPUs and GPUs.
- Autograd — automatic computation of gradients for training.
torch.nn— ready-made layers, loss functions and model building blocks.- A huge ecosystem — Hugging Face Transformers, torchvision, PyTorch Lightning and most open-source LLM tooling are built on it.
PyTorch code is plain Python: models are classes, training is a for loop, and you can debug with print() — which is why students find it easier to learn than older graph-based frameworks.
Installing PyTorch
pip install torch
For NVIDIA GPU support, use the install command for your CUDA version from the official PyTorch website. Check your setup:
import torch
print(torch.__version__)
print("GPU available:", torch.cuda.is_available())
No GPU? Everything in this tutorial runs on a CPU; free cloud notebooks provide a GPU when you need one.
Tensors: the core data structure
A tensor is an n-dimensional array, like a NumPy array that can also live on a GPU and track gradients.
import torch
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
print(x.shape, x.dtype)
torch.Size([2, 2]) torch.float32
| Rank | Example | Typical use |
|---|---|---|
| 0-D (scalar) | torch.tensor(3.14) |
A loss value |
| 1-D (vector) | torch.tensor([1, 2, 3]) |
A feature vector |
| 2-D (matrix) | Shape (batch, features) |
Tabular data in a batch |
| 3-D | Shape (batch, sequence, embedding) |
Text for a transformer |
| 4-D | Shape (batch, channels, height, width) |
A batch of images |
Creating tensors
torch.zeros(2, 3) # 2×3 of zeros
torch.ones(2, 3) # 2×3 of ones
torch.rand(2, 3) # uniform random values in [0, 1)
torch.randn(2, 3) # standard normal random values
torch.arange(0, 10, 2) # tensor([0, 2, 4, 6, 8])
torch.linspace(0, 1, 5) # 5 evenly spaced values from 0 to 1
Tensor operations
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
print(x @ x) # matrix multiplication
print(x * 10) # element-wise multiplication
print(x.reshape(4)) # change shape
print(x.T) # transpose
print(x.mean().item())
tensor([[ 7., 10.],
[15., 22.]])
tensor([[10., 20.],
[30., 40.]])
tensor([1., 2., 3., 4.])
tensor([[1., 3.],
[2., 4.]])
2.5
.item() converts a one-element tensor into a plain Python number.
Common shape tools: reshape/view (change shape), unsqueeze(dim) (add a dimension of size 1), squeeze() (remove size-1 dimensions), permute(...) (reorder dimensions) and torch.cat / torch.stack (combine tensors).
Moving tensors to the GPU
device = "cuda" if torch.cuda.is_available() else "cpu"
x = x.to(device)
print(x.device)
Rule: all tensors in one operation must be on the same device. Mixing a CPU tensor with a GPU tensor raises an error — one of the most common beginner bugs.
NumPy interoperability
import numpy as np
a = np.array([1, 2, 3])
t = torch.from_numpy(a) # shares memory with the NumPy array
back = t.numpy() # back to NumPy (CPU tensors only)
Use this to load data with NumPy or Pandas and train with PyTorch.
PyTorch vs TensorFlow
| PyTorch | TensorFlow / Keras | |
|---|---|---|
| Style | Pythonic, eager by default | High-level Keras API, graph compilation |
| Research use | Dominant | Smaller share |
| LLM ecosystem | Most open-source LLM tools | Less common |
| Mobile/edge | ExecuTorch | TensorFlow Lite |
Both are capable; for learning modern AI and working with open-source LLMs, PyTorch is the most practical first choice.
Common mistakes
- Mixing devices — move the model and every batch to the same device.
- Integer tensors for training data — models expect
float32;torch.tensor([1, 2])creates an integer tensor. - Shape mismatches — print
.shapeoften; most errors are a missing batch dimension. - Calling
.numpy()on a GPU tensor — move it with.cpu()first.
Key takeaways
- PyTorch = tensors + autograd +
torch.nn, all in plain Python. - Tensors are GPU-capable n-dimensional arrays with a shape, dtype and device.
- Keep data and models on the same device, and check shapes constantly.