Skip to content
ml-recall

Topic notes

RL Fundamentals

The reinforcement-learning setting — agent, environment, states, actions, rewards, MDPs, discounted return, policies and value functions — and the main families of RL methods.

4-min read AI-checked

Practise 37 questions Take a test

Key ideas

Formulas

Gt=Rt+1+γRt+2+γ2Rt+3+⋯=∑k≥0γkRt+k+1=Rt+1+γ Gt+1G_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \dots = \sum_{k \ge 0} \gamma^k R_{t+k+1} = R_{t+1} + \gamma\, G_{t+1} Vπ(s)=Eπ[Gt∣St=s],Qπ(s,a)=Eπ[Gt∣St=s,At=a]V^\pi(s) = \mathbb{E}_\pi[G_t \mid S_t = s], \qquad Q^\pi(s, a) = \mathbb{E}_\pi[G_t \mid S_t = s, A_t = a]

Worked example

An episode gives R1,R2,R3,R4=1,0,0,10R_1, R_2, R_3, R_4 = 1, 0, 0, 10 and ends. With γ=0.9\gamma = 0.9:

With γ=0.5\gamma = 0.5 the same episode is worth only 1+0.125⋅10=2.251 + 0.125 \cdot 10 = 2.25, and with γ=0\gamma = 0 just the first reward, 1: a small γ\gamma makes the agent short-sighted.

In code

import gymnasium as gym
env = gym.make("CartPole-v1")
print(env.observation_space.shape, env.action_space)    # (4,) Discrete(2)
obs, info = env.reset(seed=0); env.action_space.seed(0)
G, gamma, t, done = 0.0, 0.99, 0, False
while not done:                                          # one episode
    a = env.action_space.sample()                        # random policy
    obs, r, terminated, truncated, info = env.step(a)    # reward +1 per step
    G += gamma**t * r                                    # discounted return G_0
    t += 1
    done = terminated or truncated                       # pole fell / time limit
print(t, round(G, 2), round((1 - gamma**t) / (1 - gamma), 2))   # 18 16.55 16.55

CartPole's state is continuous (cart position and velocity, pole angle and angular velocity), while its action space is discrete: push left or right.

Common pitfalls

References

  1. Hugging Face Deep Reinforcement Learning Course — Unit 1, The Reinforcement Learning Framework: The RL Process; Markov Property; Observations/States Space; Action Space; Rewards and the discounting
  2. Hugging Face Deep Reinforcement Learning Course — Unit 1, Type of tasks
  3. Hugging Face Deep Reinforcement Learning Course — Unit 1, The Exploration/Exploitation trade-off
  4. Hugging Face Deep Reinforcement Learning Course — Unit 1, Two main approaches for solving RL problems
  5. Dive into Deep Learning — Sec. 17.1 Markov Decision Process (17.1.1–17.1.3)
  6. OpenAI Spinning Up, Part 2: Kinds of RL Algorithms — Model-Free vs Model-Based RL; What to Learn