Article complete

Hands-On Guide to Understand and Implement Q - Learning

A person stands on cracked ice, with visible patterns and textures beneath their feet, wearing brown boots and jeans.

Key takeaways

  • Q-Learning is a model-free approach for training reinforcement learning agents, introduced in 1992.
  • The Q-Table stores Q values for state and action pairs, helping agents select optimal actions for maximum reward.
  • Q-Learning can struggle with the curse-of-dimensionality, making it challenging to store all mappings in large environments.
  • The Q-function can be optimised iteratively using Bellman Equations to achieve optimal Q-values.

Takeaways drafted with AI from the article text.

Q-Learning is a traditional model-free approach to train Reinforcement Learning agents. It is also viewed as a method of asynchronous dynamic programming. It was introduced by Watkins&Dayan in 1992.

Q-Learning Overview

In Q-Learning we build a Q-Table to store Q values for all possible combinations of state and action pairs. It is called Q-Learning because it represents the quality of a certain action an agent can take in a provided space.
The agents use a Q-table to choose the best action which gives maximum reward to the agent. So, basically the Q-Table acts as a cheat sheet to the agent as it has all the possible combinations for the environment. It is also called model-free because the Q-value is not approximated using any function, it is simply stored inside a table, with rows as states and actions as columns.

However, Q-learning suffers from curse-of-dimensionality as sometimes due to a large number of state and action pairs it’s not possible to store all the mappings.

Q - Learning Algorithm

Let’s Implement the Q-Learning algorithm using Numpy and see how it works.

The Q-function can be iteratively optimized to reach an optimal Q-value using the Bellman Equations.

This is how a Q-table schema looks like,

Q - Learning Implementation

Let’s implement a Q-Learning algorithm from scratch to play Frozen Lake provided by OpenAI Gym. We will use NumPy to implement the entire algorithm.

Environment Details

SFFF       (S: starting point, safe)

FHFH       (F: frozen surface, safe)

FFFH       (H: hole, fall to your doom)

HFFG       (G: goal, where the frisbee is located)

The episode ends when you reach the goal or fall in a hole. You receive a reward of 1 if you reach the goal, and zero otherwise.

Code Walkthrough

Let’s understand the NumPy code step by step.

  • Let’s declare a two-dimensional array with rows equal to state size and columns equal to action size.
  • Let’s see how the Q-table looks like, we can see that it has 16 possible states with 4 different actions,
    • Possible 16 States in Frozen-lake environment are as follows.
      1. SFFF       (S: starting point, safe)
      2. FHFH       (F: frozen surface, safe)
      3. FFFH       (H: hole, fall to your doom)
      4. HFFG       (G: goal, where the frisbee is located)
  • Possible 4 Actions in Frozen-lake environment are as follows.
    • Top, Bottom, Right, Left
  • Finally, Q-Table has respective 16 states and 4 actions.
  • Let’s define some hyperparameters needed to learn the Q-values.
  • Let’s go ahead and Implement the Q-Learning algorithm now.
    • Based on the hyperparameters defined above, let’s iterate through the total number of episodes, for every episode the agent is allowed to take a maximum of 99 steps as max_steps.
    • We keep the trade-off between exploration vs. exploitation using a random number generator, here exp_tradeoff.
    • We take a random step if epsilon is lesser than exp_tradeoff.
    • We record the rewards for every step and update the Q-table using Bellman Equations.

Let’s have a look at the Q-Learning Algorithm Code snippet,

NoteBook

Results

The above figure shows the number of steps it took the Q-learning based agent to reach the goal. We basically tested our agent on 5 episodes and in every episode, the agent was able to reach the Goal(G).

This is how we can train an end to end Q-learning agent using NumPy.

Published

Spotted an error? Tell our editors. Read our editorial standards and corrections.

Selling to AI teams? Put your brand in front of the 5M+ people who read AIM every month. Plan a campaign

More in this story

Reinforcement Learning · 5 stories
  1. You are here: Hands-On Guide to Understand and Implement Q - Learning
  2. Guide To VGG-SOUND Datasets For Visual-Audio Recognition
  3. Ad Click-Through-Rate (CTR) Prediction using Reinforcement Learning
  4. Guide To Google’s AudioSet Datasets With Implementation in PyTorch
  5. An Introductory Guide to Meta Reinforcement Learning (Meta-RL)
About the author

Contributor

Experienced Data Scientist with a demonstrated history of working in Industrial IOT (IIOT), Industry 4.0, Power Systems and Manufacturing domain. I have experience in designing robust solutions for various clients using Machine Learning, Artificial Intelligence, and Deep Learning. I have been instrumental in developing end to end solutions from scratch and deploying them independently at scale.

All stories
Belamy · Daily

The day’s AI news, before your first meeting.