Home Topics Reinforcement Learning

Reinforcement Learning

Learning through trial, error, and rewards

8 hours 10 examples Advanced
Beginner

What is Reinforcement Learning?

Reinforcement Learning (RL) is a type of ML where an agent learns to make decisions by taking actions in an environment to maximize cumulative reward. It's inspired by how humans and animals learn through trial and error.

The RL Loop

Agent → Takes Action → Environment responds → Agent receives Reward → Agent learns → Repeat

Agent

Action

Environment

Reward

Beginner

Key Concepts

State

Current situation of the agent in the environment.

Action

What the agent can do in a given state.

Reward

Feedback signal indicating how good an action was.

Policy

Strategy that defines which action to take in each state.

Intermediate

Q-Learning

Q-Learning is a model-free RL algorithm that learns the value of actions in states. The "Q" stands for quality - the quality of an action in a state.

q_learning_simple.py
import numpy as np

# Simple Q-Learning example
# States: 0, 1, 2 (goal)
# Actions: 0 (left), 1 (right)

Q = np.zeros((3, 2))  # Q-table: states x actions
alpha = 0.1  # Learning rate
gamma = 0.9  # Discount factor

def update_q(state, action, reward, next_state):
    # Q-Learning formula
    best_next = np.max(Q[next_state])
    Q[state, action] += alpha * (
        reward + gamma * best_next - Q[state, action]
    )

# Example update: from state 1, go right, get reward 10
update_q(state=1, action=1, reward=10, next_state=2)
print("Q-table after update:")
print(Q)
Intermediate

Exploration vs Exploitation

The fundamental dilemma in RL: should the agent explore new actions or exploit known good actions?

Exploration

Try new actions to discover better strategies.

Risk: May waste time on bad actions

Exploitation

Use the best known action to maximize reward.

Risk: May miss better strategies

ε-Greedy Strategy

With probability ε, explore randomly. Otherwise, exploit the best action. Start with high ε, decrease over time.

Advanced

Deep Reinforcement Learning

Combine deep neural networks with RL to handle complex environments with high-dimensional state spaces (like images).

DQN (Deep Q-Network)

Uses neural networks to approximate Q-values. Famously used to play Atari games.

Policy Gradient Methods

Directly learn the policy instead of Q-values. Examples: REINFORCE, PPO.

Actor-Critic

Combines value-based and policy-based methods. Best of both worlds.

Advanced

Real-World Applications

Game AI

AlphaGo, OpenAI Five, game NPCs

Robotics

Robot locomotion, manipulation

Autonomous Vehicles

Decision making for self-driving

Personalization

Ad placement, recommendations

Ready for Deep Learning?

Explore neural networks and deep learning architectures.