An Introduction to Q-Learning Part 2/2
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.
Related posts
- An Introduction to Q-Learning Part 1
Hugging Face mediumAdeptValue-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. The Bellman equation makes value estimation recursive: a state's value is the immediate reward plus the discounted value of the next state. Monte Carlo methods update values only after a full episode using the actual return, while Temporal Difference learning (TD(0)) updates after each step by bootstrapping from the current estimate of the next state. Worked numeric examples with a mouse-and-cheese grid show both update rules, setting up Q-Learning in the next part.
- An Introduction to Deep Reinforcement Learning
Hugging Face easyNoviceDeep 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. The core concepts are the state/action/reward loop, the Markov property, observation vs. state, discrete vs. continuous action spaces, discounting with gamma (typically 0.95–0.99), episodic vs. continuing tasks, and the exploration/exploitation tradeoff. The two main ways to learn an optimal policy are policy-based methods, which map states directly to actions or action distributions, and value-based methods, which learn state values and act greedily toward them. It is the first unit of Hugging Face's free Deep RL course, which goes on to train a lunar lander agent with Stable Baselines3.
- Deep Q-Learning with Space Invaders
Hugging Face mediumAdeptDeep 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). The DQN preprocesses frames to 84x84 grayscale and stacks four of them to capture motion, then passes them through three convolutional layers and fully connected layers. Training is stabilized with experience replay (reusing and decorrelating samples), a fixed target network copied every C steps, and Double DQN, which decouples action selection from target evaluation to curb Q-value overestimation. The unit closes with a Colab tutorial that trains a Space Invaders agent using RL-Baselines3 Zoo.
- Policy Gradient with PyTorch
Hugging Face easyNovicePolicy-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. Their advantages are built-in exploration, robustness to perceptual aliasing, and handling of continuous or high-dimensional action spaces. Their drawbacks are convergence to local maxima, slow training and high variance. Reinforce (Monte Carlo Policy Gradient) collects a full episode, estimates the gradient from the log-probabilities of the actions weighted by the return, and updates the weights. A linked Colab notebook implements it in PyTorch on CartPole-v1, PixelCopter and Pong.
- Advantage Actor Critic (A2C)
Hugging Face mediumAdeptActor-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. A2C stabilizes training further by using the advantage function, which measures how much better an action is than the state's average value. The TD error serves as a practical estimator, so separate Q and V networks aren't needed. The unit closes with a Colab exercise training bipedal and spider walkers in PyBullet with Stable-Baselines3.