---
title: "An Introduction to Q-Learning Part 2/2"
description: "Q-Learning is an off-policy, value-based reinforcement learning algorithm that uses temporal-difference updates to train a Q-table of state-action values, from which the optimal policy follows directly."
url: "https://swe.quest/articles/an-introduction-to-q-learning-part-2-2-2282a7e5e80bee59"
canonical: "https://huggingface.co/blog/deep-rl-q-part2"
source: "Hugging Face"
published: "2022-05-20"
difficulty: "medium"
kind: "How-to"
reading_minutes: 9
tags: ["machine-learning","model-training","algorithms"]
---

# An Introduction to Q-Learning Part 2/2

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2022-05-20, 9 min read, How-to. Original: [https://huggingface.co/blog/deep-rl-q-part2](https://huggingface.co/blog/deep-rl-q-part2)

## Summary

Q-Learning is an off-policy, value-based reinforcement learning algorithm that uses temporal-difference updates to train a Q-table of state-action values, from which the optimal policy follows directly. The walkthrough covers the four steps of the algorithm: initializing the Q-table, choosing actions with a decaying epsilon-greedy strategy, observing reward and next state, and updating Q(S,A) toward a TD target built from the reward plus the discounted best next-state value. It explains why acting epsilon-greedily while updating greedily makes Q-Learning off-policy, contrasting it with on-policy Sarsa. It then traces two hand-computed training steps in a mouse-and-cheese maze (learning rate 0.1, gamma 0.99) before pointing to a Colab notebook that trains agents on FrozenLake-v1 and Taxi.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)
- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [Algorithms](https://swe.quest/tags/algorithms.md) (Architecture & design)

## Prerequisites

- reinforcement learning basics ([easy posts on Model training](https://swe.quest/tags/model-training.md?difficulty=easy))
- temporal difference learning
- value functions

## Keywords

Q-Learning, Sarsa, epsilon-greedy, FrozenLake-v1, Taxi, Hugging Face Deep RL Course

## Related posts

- [An Introduction to Q-Learning Part 1](https://swe.quest/articles/an-introduction-to-q-learning-part-1-02c8621463858d6d.md): Hugging Face · 2022-05-18 · Medium · 13 min read · Machine learning, Model training, Algorithms
  Value-based reinforcement learning methods learn a state-value or action-value function and derive the policy from it, typically a greedy or epsilon-greedy policy, rather than training the policy directly.
- [An Introduction to Deep Reinforcement Learning](https://swe.quest/articles/an-introduction-to-deep-reinforcement-learning-d17541fa32bf28e1.md): Hugging Face · 2022-05-04 · Easy · 16 min read · Machine learning, Model training
  Deep reinforcement learning trains an agent to act in an environment through trial and error, using rewards as its only feedback, with the goal of maximizing expected cumulative (discounted) return.
- [Deep Q-Learning with Space Invaders](https://swe.quest/articles/deep-q-learning-with-space-invaders-f474bfcf35b2c89d.md): Hugging Face · 2022-06-07 · Medium · 10 min read · Machine learning, Model training, Games
  Deep Q-Learning replaces the tabular Q-table with a neural network that maps a state to Q-values for every action, making RL feasible for huge state spaces like Atari frames (256^100800 possible observations).
- [Policy Gradient with PyTorch](https://swe.quest/articles/policy-gradient-with-pytorch-8cc397d15e17e5f8.md): Hugging Face · 2022-06-30 · Easy · 7 min read · Machine learning, Model training, Python
  Policy-gradient methods optimize a stochastic policy directly via gradient ascent on expected return, instead of deriving a policy from learned Q-values as in Deep Q-Learning.
- [Advantage Actor Critic (A2C)](https://swe.quest/articles/advantage-actor-critic-a2c-5b4f17254e3ab304.md): Hugging Face · 2022-07-22 · Medium · 7 min read · Machine learning, Model training, Algorithms
  Actor-Critic methods reduce the high variance of Monte-Carlo policy-gradient estimates in Reinforce by pairing a policy network (Actor) with a value network (Critic) that scores the actions taken.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
