Skip to main content
LearnMLAcademy
Intermediate10 min read

Reinforcement Learning (Advanced)

Implement Deep Q-Networks and policy gradient algorithms for game agents, with Python code, architecture diagrams, worked examples, and interview quiz.

What you will master in this lesson
Intuitive core mechanisms & practical real-world intuition
Step-by-step mathematical breakdown & visual diagrams
Runnable Python implementation & practical trade-offs

Reinforcement Learning

Start with a question

What if nobody can tell the model the correct answer?

Imagine a robot inside a maze. We do not give it a labelled dataset saying “at this square, move right”. Instead, the robot tries an action, sees what happens, receives feedback, and gradually learns which decisions lead to the goal.

Observe
Where am I?
Act
Which move should I try?
Receive reward
Was that useful?
Learn
What should I do next time?

State → Action → Reward → New State → Repeat

Machine Learning systems are designed to learn patterns from data and make intelligent decisions. Traditional supervised learning usually learns from historical labelled examples. Reinforcement Learning is different: the learner improves by interacting with an environment and experiencing the consequences of its actions.

  • A spam detection model learns from labeled spam emails.
  • A house price prediction model learns from past housing data.
  • A medical diagnosis model learns from historical patient records.

These approaches work well when correct answers already exist. However, many real-world problems are completely different. In many situations, there is no labeled dataset, the environment changes continuously, decisions must be made step-by-step, and actions affect future outcomes.

Self-Driving Cars
Game-Playing AI
Robotics
Stock Trading

In such environments, the system cannot simply memorize examples. Instead, it must learn through trial and error. This leads to one of the most powerful areas of Artificial Intelligence: Reinforcement Learning (RL).


What is Reinforcement Learning?

Reinforcement Learning is a branch of Machine Learning in which an intelligent agent learns how to make decisions by interacting with an environment and receiving rewards or penalties based on its actions.

The Main Goal: Maximize cumulative rewards over time.

Simple Human Analogy

Imagine teaching a child to ride a bicycle. Initially, the child falls frequently, makes mistakes, and loses balance. But gradually, they learn from mistakes, understand balance, improve movement, and gain confidence. Eventually, the child masters bicycle riding without a labeled dataset. Learning happened through trial and error.

Learning Curve

Notice how the agent starts with highly negative rewards (falling over) but through trial and error, exploits good actions that yield maximum rewards.

The RL Loop — One Decision at a Time

The easiest way to understand RL is to follow one interaction. Suppose our maze robot is standing at square A. It can move left or right. Moving right brings it closer to the exit.

1
State
Robot is at A
2
Action
Move right
3
Reward
+2
4
Next state
Robot reaches B
5
Learning
Right becomes more attractive at A

The key difference: supervised learning asks “what is the correct answer for this example?” RL asks “which action should I take now so that my total future reward becomes as large as possible?”

Components of Reinforcement Learning

Every Reinforcement Learning system contains several important components orchestrating the simulation:

1Agent

The learner or decision-maker. It interacts with the environment and tries to maximize rewards. (e.g., A Self-driving Car AI).

2Environment

The world in which the agent operates. The environment responds to the agent’s actions. (e.g., Roads and traffic).

3State & Action

A State represents the current situation of the environment. An Action is a decision taken by the agent (e.g., State: Obstacle ahead, Action: Brake).

4Reward Signal

Feedback received from the environment telling the agent whether its action was good or bad. (e.g., Safe driving yields +5, Collision yields -100).

Policy, Return and the Exploration Problem

Policy (π)

The agent's strategy: given the current state, which action should it choose?

Return

The accumulated reward over time. RL usually cares about long-term reward, not only the next reward.

Explore vs Exploit

Try unfamiliar actions to learn more, or choose the action that currently looks best.

Why immediate reward can be misleading

A move giving +5 now may lead to a dead end, while a move giving 0 now may lead to +100 later. RL therefore needs a way to value future rewards. This is why the discount factor γ appears in Q-learning.


Mathematical Understanding: Q-Learning

Reinforcement Learning is mathematically modeled using a Markov Decision Process (MDP), assuming the future depends only on the current state and action, not the full history.Q-Learning is one of the most popular algorithms to solve this natively.

Q-Learning Update Formula:

Q(s,a) = Q(s,a) + α [r + γ max Q(s',a') - Q(s,a)]
  • Q(s,a) = Current Q-value (Quality of the action in the state)
  • α (Alpha) = Learning rate
  • r = Immediate reward
  • γ (Gamma) = Discount factor (Importance of future rewards)
  • max Q(s',a') = The maximum predicted Q-value for the next state

Solved Q-Learning Update — Step by Step

Suppose the agent just moved from state s using action a. Its current Q-value is 10. It receives reward 5, and the best Q-value available in the next state is 20. Let α = 0.1 and γ = 0.9.

Qnew = 10 + 0.1 × [5 + 0.9 × 20 − 10]

Qnew = 10 + 0.1 × [5 + 18 − 10]

Qnew = 10 + 0.1 × 13

Qnew = 11.3

The value rises from 10 to 11.3. In plain English, the experience made this state-action pair look better because it produced a useful immediate reward and also led to a promising next state.

Python Implementation (Q-Learning)

Let's implement a simple Q-Learning Maze agent using Numpy to understand the trial and error process logic.

Implementing Q-Learning Logic

import numpy as np# 1. Define Q-Table (5x5 Maze, 4 possible actions)# Actions: 0=Left, 1=Right, 2=Up, 3=DownQ = np.zeros((5, 5, 4))# 2. Define Parametersalpha = 0.1   # Learning rategamma = 0.9   # Discount factorepsilon = 0.2 # Exploration probabilityepisodes = 1000# 3. Training Loopfor episode in range(episodes): state = start_state done = Falsewhile not done:# Epsilon-Greedy Action Selection (Exploration vs Exploitation)if np.random.uniform(0, 1) < epsilon: action = np.random.choice(4) # Exploreelse: action = np.argmax(Q[state[0], state[1]]) # Exploit# Environment gives the next state and reward based on actionnext_state, reward, done = env.step(state, action)# 4. Q-Learning Update Formulacurrent_q = Q[state[0], state[1], action] max_future_q = np.max(Q[next_state[0], next_state[1]]) Q[state[0], state[1], action] = current_q + alpha * (reward + gamma * max_future_q - current_q) state = next_state
Note: After 1000 episodes, the Q-table will contain optimal values indicating the correct "policy" (directions) from every tile of the maze directly to the goal state.

Advantages & Disadvantages

Advantages

  • ✓ Learns autonomously without needing a labeled dataset.
  • ✓ Can discover effective strategies through experience, including strategies that may be difficult to hand-code.
  • ✓ Is designed for sequential decision problems where actions influence future outcomes.
  • ✓ Can be useful in uncertain environments when safe exploration, feedback, and training resources are available.

Disadvantages

  • × Can be computationally expensive and sample-inefficient, especially in large or complex environments.
  • × Difficult "Reward Design": poorly designed rewards cause disastrous edge-case exploits.
  • × Navigates early "Exploration Risks" which can be hazardous in physical robotics.
  • × Training instability is notoriously difficult to debug compared to supervised learning.

Common Mistakes

“Reward means the same thing as the final goal.”

Not always. Reward is the feedback signal; the agent aims to maximize cumulative return over time.

“The agent should always choose the action with the highest known Q-value.”

Doing only that can stop exploration too early and miss better actions.

“A high immediate reward is always the best choice.”

Future consequences matter. The discount factor helps balance immediate and future reward.

“Q-learning needs a labelled training dataset.”

No. It learns Q-values from state, action, reward and next-state experiences.

“Reinforcement learning and online learning are the same.”

No. Online learning incrementally updates a predictive model; RL learns sequential decisions from rewards.

Quick Knowledge Check

1. What does a policy tell an RL agent?

Which action to choose in a given state.

2. Why do we need exploration?

Because the action that currently looks best may not actually be optimal; trying alternatives can reveal better choices.

3. What does γ control?

How strongly future rewards influence the current Q-value update.

4. In Q-learning, what does Q(s,a) represent?

The estimated long-term value of taking action a in state s and then behaving well afterward.

Final Summary

Reinforcement Learning is a Machine Learning paradigm where an intelligent agent learns by interacting with an environment and receiving rewards or penalties. The goal is to maximize long-term cumulative rewards step-by-step through pure experience.

RL is used in research and selected production systems for areas such as robotics, games, recommendation, control, and sequential optimization. Deep Reinforcement Learning combines RL with neural networks so agents can work with large or high-dimensional state spaces.

Most Important Insight to Remember:

Reinforcement Learning learns from consequences rather than a fixed table of correct answers. The agent repeatedly observes, acts, receives feedback, and updates its strategy so that long-term cumulative reward improves.

Apply this concept in a real hands-on project

You have studied the idea. Now use it in working Python code, see the calculation and output, and explain what changed. Each project includes step-by-step instructions, full source and tests.

Practice challenge: predict an outcome first, run the code, and explain the difference.

Track Your Progress0% curriculum done

Finished "Reinforcement Learning (Advanced)"?

Mark this lesson as completed to update your progress bar and track your milestones across the full curriculum.

Overall Progress0 / 167 lessons

Was this tutorial helpful?

Your feedback helps us continuously refine and improve our open-access tutorials.

Summary / Key Takeaways

  • A policy selects actions to maximize expected cumulative return.
  • Value functions estimate states; Q-values estimate state-action choices.
  • Bellman relationships connect current reward and discounted future value.
  • Q-learning updates toward reward plus the best next-state value.
  • Exploration remains necessary under uncertainty.
  • Delayed rewards, safety, and sample cost complicate evaluation.

Knowledge Check

Progress: 0/4

Test your active recall. Choose one answer for each question, then check it to reveal the step-by-step reasoning.

Question 1 of 4
What is the Q-function and what does Q(s,a) represent?
Question 2 of 4
What is epsilon-greedy and how does it address exploration-exploitation?
Question 3 of 4
What problem does a replay buffer solve in Deep Q-Networks (DQN)?
Question 4 of 4
What is the policy gradient approach and how does it differ from Q-Learning?

Knowledge Check Review & Explanations

Review the questions, verified correct answers, and conceptual explanations for this lesson.

1What is the Q-function and what does Q(s,a) represent?▼

Correct Answer: Q(s,a) is the expected total future reward from taking action a in state s and then following the optimal policy. Q-Learning updates Q values using the Bellman equation, converging to true Q-star given enough exploration

The distinction between immediate reward and expected cumulative reward is critical. An action with small immediate reward but high-reward future state has a high Q value.

2What is epsilon-greedy and how does it address exploration-exploitation?▼

Correct Answer: Randomly explores with probability epsilon and exploits (takes the known best action) with probability 1-epsilon. Typically epsilon decays over training — high exploration early, more exploitation later as Q values stabilise

Early in training epsilon is high — the agent doesn't know enough to exploit. As Q values stabilise, epsilon decreases — more exploitation. The decay schedule itself is an important hyperparameter.

3What problem does a replay buffer solve in Deep Q-Networks (DQN)?▼

Correct Answer: Neural networks assume IID training examples. In RL, consecutive experiences are highly correlated. A replay buffer stores past experiences and samples random mini-batches — breaking temporal correlation and stabilising training

Without replay buffer: training on correlated states causes catastrophic overfitting to the current trajectory. With it: random sampling breaks correlation, making training stable.

4What is the policy gradient approach and how does it differ from Q-Learning?▼

Correct Answer: Q-Learning learns a value function Q(s,a) and derives actions by maximising. Policy gradients directly parameterise and optimise the policy pi(a|s) — the probability distribution over actions. Policy gradients work in continuous action spaces where maximising over actions is intractable

Q-Learning cannot handle 'turn the wheel 37.4 degrees' — you cannot maximise over a continuous action. Policy gradient directly learns P(action|state), enabling continuous control in robotics and autonomous vehicles.

Keep moving through the path

Continue Learning

Free Download: ML Interview Cheatsheet

100 essential Machine Learning interview questions and answers covering foundations, algorithms, evaluation, deep learning, Python, system design and interview strategy. Enter your email to download it instantly.

Optional Learning Resources

These resources may complement the lesson. They are optional and are not required to use ML Academy.

As an Amazon Associate I earn from qualifying purchases. Other links are identified separately when they are not affiliate links.

Next