Welcome! This is the first entry in Artintellica’s Reinforcement Learning series—a practical, code-driven open-source course where we build mathematical foundations and RL agents from vectors to deep Q-networks, all with PyTorch. Whether you’ve found us via GitHub, search, or your love of math and code, thank you. This blog is intended for ambitious beginners who want more than a “black box” approach.
What sets this course apart?
This first post serves as both an orientation and your first hands-on steps: installing PyTorch, running “Hello Tensors”, and manipulating your first data structures.
This post will help you:
uv
package managertorch.Tensor
objectsEach concept comes with code and hands-on exercises. By the end, you’ll be ready for the math and code engine that powers everything in modern deep learning (and, soon, RL!).
PyTorch is a numeric computation library like NumPy, but with several important differences that make it essential for modern ML and RL:
Throughout this course, you’ll see how PyTorch lets us build RL agents—from table-based to neural-based—with minimal friction.
At its core, PyTorch’s Tensor object generalizes the matrix and vector
concepts from linear algebra. In this blog, everything you learned about scalars
(), vectors (), and matrices () will correspond directly to
PyTorch Tensor
objects—uniquely enabling you to:
We’ll often use math notation (, , etc.) side-by-side with its
exact code representation (torch.Tensor
). As you encounter concepts like dot
product, matrix multiplication, or vector norms, we’ll show both the formula and
its PyTorch incarnation.
uv
)ℹ️ For this blog, we’ll assume you use Python 3.9 or later on Linux, macOS, or Windows. Use VS Code, Jupyter, or any Python IDE you like.
We recommend uv for fast, reproducible, modern package management.
uv
(if not already installed)pip install --upgrade uv
mkdir rl-with-pytorch && cd rl-with-pytorch
uv venv
source .venv/bin/activate # or .venv\Scripts\activate on Windows
Find your install command at: https://pytorch.org/get-started/locally/.
For most CPU-only systems:
uv pip install torch torchvision torchaudio matplotlib
For CUDA-enabled GPUs, use the correct torch
version:
uv pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
uv pip install matplotlib
Open Python:
import torch
print("Torch version:", torch.__version__)
print(torch.rand(2, 2))
If this prints the version and a tensor of random numbers, you’re ready!
Let’s walk through key tensor operations.
import torch
# Create a 1D tensor of floats, from 0 to 4
x: torch.Tensor = torch.arange(5, dtype=torch.float32)
print("x:", x) # Tensor data
print("Shape:", x.shape)
print("Dtype:", x.dtype)
# Detect GPUs or MPS device (Apple Silicon)
if torch.cuda.is_available():
device: torch.device = torch.device("cuda")
elif torch.backends.mps.is_available():
device = torch.device("mps")
else:
device = torch.device("cpu")
print(f"Using device: {device}")
x_gpu: torch.Tensor = x.to(device)
print("x is on device:", x_gpu.device)
Elementwise addition is just like with math or NumPy:
y: torch.Tensor = torch.ones(5, dtype=torch.float32, device=device)
z: torch.Tensor = x_gpu + y # elementwise add
print("z:", z)
Try these yourself! (Start from a new Python file, or use a Jupyter notebook.)
uv
as above. Write Python code to import torch
and print its
version.(4, 3)
(4 rows, 3 cols) filled with random numbers.shape
, and dtype
..device
at each stage.(6,)
(one with all zeros, one with all ones).Sample Starter Code for Exercises:
import torch
# EXERCISE 1
print("PyTorch version:", torch.__version__)
# EXERCISE 2
a: torch.Tensor = torch.randn(4, 3)
print("Tensor a:", a)
print("Shape:", a.shape)
print("Dtype:", a.dtype)
# EXERCISE 3
device: torch.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
b: torch.Tensor = torch.arange(6)
print("Before:", b.device)
b = b.to(device)
print("After:", b.device)
# EXERCISE 4
zero: torch.Tensor = torch.zeros(6)
one: torch.Tensor = torch.ones(6)
sum_: torch.Tensor = zero + one
print("Sum:", sum_)
In this post, you’ve:
uv
Next: We’ll dig into the fundamentals of vectors and scalars in PyTorch, learning to manipulate, index, and visualize them—the real foundation for RL algorithms.