Q-Learning

Machine Learning
Introduced by
Christopher Watkins & Peter Dayan
Year
1992
Type
Model-free reinforcement-learning control algorithm
Family
Reinforcement learning

What does it do?

Q-learning, introduced by Christopher Watkins and Peter Dayan in 1992, is a model-free reinforcement-learning algorithm that learns the value of taking each action in each state (the Q-function) from sampled experience, without a model of the environment.

It is off-policy: it learns action-values for the optimal behaviour while the agent follows an exploratory policy. In the finite tabular case, under standard conditions (bounded rewards, every state-action pair visited infinitely often, and step sizes satisfying the Robbins–Monro conditions), the Q-values converge with probability 1 to the optimal action-values, so the greedy policy derived from them is optimal. Beyond the tabular case, this guarantee does not carry over directly.

The intuition behind the method

An agent learning by trial and error needs to know, for each situation, how good each available action is in the long run. Q-learning maintains this as a table, or a function, of Q-values estimating the total discounted future reward of taking an action in a state and acting optimally afterwards. Each time the agent acts, observes a reward, and reaches a new state, it moves the Q-value for the action it took toward the observed reward plus the best value it currently assigns to the next state. The update uses the best next action rather than the action the agent actually takes next. This lets the agent explore freely while the learned values still describe the optimal strategy.

History

Q-learning was introduced by Watkins and Dayan (1992) as a method for learning optimal action-values from experience. It extends temporal-difference learning to control and is treated in Sutton and Barto's textbook. Deep Q-Networks later replaced the Q-table with a deep neural network so that the same update could be used with high-dimensional inputs.

When to use it

Use Q-learning for sequential decision problems that can be framed as a Markov decision process, when you can sample transitions and rewards but do not have a model of the dynamics, or have only an unreliable one. Tabular Q-learning fits problems with a small, discrete set of states and actions, where a table of Q-values can be stored and inspected, and it requires sufficient exploration and a suitably decaying step size for its guarantee. When the state or action space is large or continuous, the table is replaced by a function approximator, as in Deep Q-Networks; the tabular convergence guarantee then no longer applies directly, and training may become unstable. Do not choose it when the environment is clearly non-stationary or partially observable, since the method assumes the Markov property and a stationary environment. Where a stochastic or continuous-action policy is needed, policy-gradient methods are the alternative.

Limitations and considerations

  • The tabular form stores one value per state-action pair, so it does not scale to large or continuous state or action spaces without a function approximator.
  • Learning is sample-inefficient: many transitions may be needed before the Q-values settle.
  • The max operator in the target biases estimates upward (maximization bias); Double Q-learning is the usual remedy.
  • The Markov and stationarity assumptions do not hold under partial observability or a drifting environment, where the learned values can become misleading.
  • With function approximation, off-policy bootstrapping can diverge (the 'deadly triad' of function approximation, bootstrapping and off-policy updates). The stabilising measures used in DQN reduce this risk but do not guarantee convergence.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Model-free: it needs only sampled transitions and rewards, not a model of the transition or reward dynamics.
  • Off-policy: it learns the action-values of the optimal policy while the agent explores with a different policy.
  • In the tabular case, convergence to the optimal action-values is guaranteed under standard conditions, notably sufficient exploration and an appropriately decaying step size.
  • Each update touches a single observed transition, so the rule is cheap per step and can be applied online. The same temporal-difference target forms the core of Deep Q-Networks.

Applications

  • Discrete grid-navigation and maze tasks with a small, enumerable state space.
  • Small tabular control problems where the Q-table can be stored and inspected directly.
  • Discrete-action game playing with function approximation, in the style of Deep Q-Networks.
  • A value-based baseline against which other reinforcement-learning methods are compared.

How it works

How it works

1

Q(s, a), as written in the formula, is the expected discounted sum of future rewards from taking action a in state s, with later actions drawn from the policy that generates the data. The discount factor γ weights nearer rewards more heavily than distant ones. The optimal action-value Q*(s, a) is the same quantity when all later actions are chosen optimally. If Q* is known for every state-action pair, acting optimally is a matter of choosing the highest-valued action in each state, which is why learning Q* is a route to solving the control problem.

Formula
Q(s,a) \approx \mathbb{E}\!\left[\textstyle\sum_{k\ge0}\gamma^{k} r_{t+k+1}\mid s_t=s,a_t=a\right]
2

After each observed transition (state s, action a, reward r, next state s'), Q(s, a) is moved by a step size α toward the temporal-difference target r + γ·max over a' of Q(s', a'). The max term is what makes the method off-policy: the target refers to the best next action, not to the action the agent actually takes next.

Formula
Q(s,a) \leftarrow Q(s,a) + \alpha\big[r + \gamma \max_{a'} Q(s',a') - Q(s,a)\big]
3

To gather experience, the agent acts with an exploration policy, commonly ε-greedy: it usually picks the action with the highest current Q-value and with probability ε picks a random action. The convergence guarantee requires that every state-action pair keeps being tried, so exploration cannot be switched off too early. ε is therefore commonly reduced gradually over time; a gradual reduction lets both Sarsa and Q-learning converge asymptotically to the optimal policy.

Formula
a = \begin{cases}\arg\max_a Q(s,a) & \text{prob } 1-\epsilon\\ \text{random} & \text{prob } \epsilon\end{cases}
4

When Q has converged to the optimal action-values Q*, the optimal policy takes, in each state, the action with the highest Q* value. In practice the greedy policy with respect to the current Q is extracted at the end of learning or used during deployment.

Formula
\pi^*(s) = \arg\max_a Q^*(s,a)

Common pitfalls

  • Exploration that is too weak, or decays too fast, leaves some state-action pairs untried, so the optimum may never be learned.
  • A poorly chosen learning-rate schedule can cause the values to oscillate or learn very slowly.
  • Applying the tabular form to a problem with a very large or continuous state space, where the table cannot be filled.
  • Ignoring maximization bias in stochastic problems, where the upward bias in Q-values can lead to a poor greedy policy and Double Q-learning is worth considering.

Frequently asked questions

What does 'off-policy' mean in Q-learning?

It means Q-learning learns the value of the optimal policy while the agent follows a different, exploratory behaviour policy. The update target is r + γ·max Q(s', a'), which refers to the best next action rather than the action actually taken, so exploration does not bias the learned optimum.

How does Q-learning relate to Deep Q-Networks (DQN)?

DQN keeps the Q-learning update but replaces the Q-table with a deep neural network that approximates Q(s, a), so the method can handle large or high-dimensional state spaces such as raw sensory input. It adds stabilising measures, including experience replay and a target network, while the core learning rule remains the temporal-difference update. These measures improve stability but do not restore the tabular convergence guarantee.

Why does Q-learning overestimate values?

The max operator in the update selects the largest of several noisy estimates, which biases Q-values upward, especially when rewards are noisy. Double Q-learning reduces this bias by using two estimators, one to select the action and the other to evaluate it.

Related methods

Sources

  1. Watkins, C. J. C. H., & Dayan, P. (1992). Q-learning. Machine Learning, 8(3–4), 279–292.

    Open DOI reference
  2. Sutton, R. S., & Barto, A. G. (2018). Reinforcement Learning: An Introduction (2nd ed.). MIT Press.

    ISBN: 978-0-262-03924-6