ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Introduction to Reinforcement Learning
Chapter 1: Foundations of Reinforcement Learning
What is Reinforcement Learning?
Agents and Environments
States, Actions, and Rewards
Policies: Mapping States to Actions
The RL Workflow: Interaction Loops
Types of RL Tasks: Episodic vs Continuing
Comparing RL with Other Learning Types
Setting up Your Python Environment for RL
Quiz for Chapter 1
Chapter 2: Markov Decision Processes (MDPs)
Modeling Sequential Decision Making
Formal Definition of an MDP
State Transition Probabilities
Reward Functions
Return: Cumulative Future Rewards
Discounting Future Rewards
Policies and Value Functions (Vπ, Qπ)
Finding Optimal Policies
Quiz for Chapter 2
Chapter 3: Estimating Value Functions
The Bellman Expectation Equation
The Bellman Optimality Equation
Solving Bellman Equations (Overview)
Dynamic Programming: Policy Iteration
Dynamic Programming: Value Iteration
Limitations of Dynamic Programming
Quiz for Chapter 3
Chapter 4: Monte Carlo Methods
Learning from Complete Episodes
Monte Carlo Prediction: Estimating Vπ
Monte Carlo Control: Estimating Qπ
On-Policy vs Off-Policy Learning
MC Control without Exploring Starts
On-Policy First-Visit MC Control Implementation
Off-Policy MC Prediction and Control Intro
Practice: Implementing MC Prediction
Quiz for Chapter 4
Chapter 5: Temporal-Difference Learning
Learning from Incomplete Episodes
TD(0) Prediction: Estimating Vπ
Advantages of TD Learning over MC
SARSA: On-Policy TD Control
Q-Learning: Off-Policy TD Control
Comparing SARSA and Q-Learning
Expected SARSA
Hands-on Practical: Implementing Q-Learning
Quiz for Chapter 5
Chapter 6: Function Approximation in RL
Handling Large State Spaces
Value Function Approximation (VFA)
Feature Vectors for State Representation
Linear Methods for VFA
Gradient Descent for Parameter Learning
Semi-gradient TD Methods
Using Neural Networks for VFA
Practice: Applying Linear VFA
Quiz for Chapter 6
Chapter 7: Introduction to Deep Q-Networks (DQN)
Combining Q-Learning with Deep Learning
Challenges with Neural Networks in RL
Experience Replay Mechanism
Fixed Q-Targets (Target Networks)
The DQN Algorithm Structure
Architectural Considerations for DQNs
Hands-on Practical: Building a Basic DQN
Quiz for Chapter 7
Chapter 8: Introduction to Policy Gradient Methods
Learning Policies Directly
Policy Gradient Theorem (Concept)
REINFORCE Algorithm
Baselines for Variance Reduction
Actor-Critic Methods Overview
Comparing Value-Based and Policy-Based Methods
Practice: Implementing REINFORCE
Quiz for Chapter 8

Semi-gradient TD Methods

Gradient descent is a method used to learn the parameters θ\thetaθ of a function approximator v^(s,θ)\hat{v}(s, \theta)v^(s,θ). This technique aims to minimize the squared error between the predicted value v^(St,θ)\hat{v}(S_t, \theta)v^(St​,θ) and a target value UtU_tUt​. For Monte Carlo methods, this target UtU_tUt​ is the actual return GtG_tGt​ from that episode, which does not depend on current value estimates. This allows us to perform true gradient descent.

Now, let's examine Temporal Difference (TD) learning. Recall from Chapter 5 that TD methods update the value estimate for a state StS_tSt​ based on the observed reward Rt+1R_{t+1}Rt+1​ and the estimated value of the next state St+1S_{t+1}St+1​. The TD target for the update at time step ttt is:

Yt=Rt+1+γv^(St+1,θt)Y_t = R_{t+1} + \gamma \hat{v}(S_{t+1}, \theta_t)Yt​=Rt+1​+γv^(St+1​,θt​)

Here, v^(St+1,θt)\hat{v}(S_{t+1}, \theta_t)v^(St+1​,θt​) is the current estimate of the value of the next state, using the current parameters θt\theta_tθt​. This is where things get interesting when combined with function approximation.

If we try to apply the same gradient descent approach as before, aiming to minimize the Mean Squared Error (MSE) between the prediction v^(St,θ)\hat{v}(S_t, \theta)v^(St​,θ) and the TD target YtY_tYt​, we run into a subtle issue. The loss for a single transition looks like:

L(θ)=12[Yt−v^(St,θ)]2=12[Rt+1+γv^(St+1,θ)−v^(St,θ)]2L(\theta) = \frac{1}{2} [ Y_t - \hat{v}(S_t, \theta) ]^2 = \frac{1}{2} [ R_{t+1} + \gamma \hat{v}(S_{t+1}, \theta) - \hat{v}(S_t, \theta) ]^2L(θ)=21​[Yt​−v^(St​,θ)]2=21​[Rt+1​+γv^(St+1​,θ)−v^(St​,θ)]2

Notice that the target YtY_tYt​ itself depends on the parameters θ\thetaθ because it includes the term v^(St+1,θ)\hat{v}(S_{t+1}, \theta)v^(St+1​,θ). A true gradient descent update would require taking the gradient of the entire expression with respect to θ\thetaθ. This involves calculating the gradient of the target value v^(St+1,θ)\hat{v}(S_{t+1}, \theta)v^(St+1​,θ), which can be complex and computationally expensive.

More significantly, the target YtY_tYt​ is based on an estimate which is inherently noisy and biased (since it depends on the current, possibly inaccurate, weights θ\thetaθ). Updating our parameters based on the gradient of this potentially flawed target can lead to instability or slow convergence.

The Semi-gradient Approach

To overcome this, TD methods with function approximation typically employ what's called a semi-gradient method. The core idea is simple but effective: when calculating the gradient for the update, we treat the TD target YtY_tYt​ as if it were a fixed, observed value, just like the return GtG_tGt​ in Monte Carlo methods. We ignore the fact that YtY_tYt​ depends on the current parameters θt\theta_tθt​.

Essentially, we compute the gradient only with respect to our prediction v^(St,θ)\hat{v}(S_t, \theta)v^(St​,θ), not the target part Rt+1+γv^(St+1,θt)R_{t+1} + \gamma \hat{v}(S_{t+1}, \theta_t)Rt+1​+γv^(St+1​,θt​).

The gradient of the simplified loss (treating YtY_tYt​ as constant) is:

∇θL(θ)≈∇θ12[(Rt+1+γv^(St+1,θt))−v^(St,θ)]2\nabla_{\theta} L(\theta) \approx \nabla_{\theta} \frac{1}{2} [ (R_{t+1} + \gamma \hat{v}(S_{t+1}, \theta_t)) - \hat{v}(S_t, \theta) ]^2∇θ​L(θ)≈∇θ​21​[(Rt+1​+γv^(St+1​,θt​))−v^(St​,θ)]2 ∇θL(θ)≈−[Rt+1+γv^(St+1,θt)−v^(St,θ)]∇θv^(St,θ)\nabla_{\theta} L(\theta) \approx - [ R_{t+1} + \gamma \hat{v}(S_{t+1}, \theta_t) - \hat{v}(S_t, \theta) ] \nabla_{\theta} \hat{v}(S_t, \theta)∇θ​L(θ)≈−[Rt+1​+γv^(St+1​,θt​)−v^(St​,θ)]∇θ​v^(St​,θ)

Remember that gradient descent updates parameters in the opposite direction of the gradient. So, the update rule for the weights θ\thetaθ becomes:

θt+1←θt−α∇θL(θ)\theta_{t+1} \leftarrow \theta_t - \alpha \nabla_{\theta} L(\theta)θt+1​←θt​−α∇θ​L(θ) θt+1←θt+α[Rt+1+γv^(St+1,θt)−v^(St,θt)]∇θv^(St,θt)\theta_{t+1} \leftarrow \theta_t + \alpha [ R_{t+1} + \gamma \hat{v}(S_{t+1}, \theta_t) - \hat{v}(S_t, \theta_t) ] \nabla_{\theta} \hat{v}(S_t, \theta_t)θt+1​←θt​+α[Rt+1​+γv^(St+1​,θt​)−v^(St​,θt​)]∇θ​v^(St​,θt​)

Let's break this down:

  1. TD Error (δt\delta_tδt​): The term in the square brackets, Rt+1+γv^(St+1,θt)−v^(St,θt)R_{t+1} + \gamma \hat{v}(S_{t+1}, \theta_t) - \hat{v}(S_t, \theta_t)Rt+1​+γv^(St+1​,θt​)−v^(St​,θt​), is the familiar TD error we encountered in tabular TD(0). It represents the difference between the estimated value at StS_tSt​ and the better estimate derived from the immediate reward and the value of the next state.
  2. Gradient of Value Function (∇θv^(St,θt)\nabla_{\theta} \hat{v}(S_t, \theta_t)∇θ​v^(St​,θt​)): This term tells us how changing each parameter in θ\thetaθ would affect the value estimate for the current state StS_tSt​. It directs the update towards the parameters most responsible for the current prediction.
  3. Learning Rate (α\alphaα): Controls the step size of the update.

This is called a "semi-gradient" method because we are only using part of the true gradient. We're taking the gradient of our prediction v^(St,θ)\hat{v}(S_t, \theta)v^(St​,θ) but ignoring the gradient of the target v^(St+1,θ)\hat{v}(S_{t+1}, \theta)v^(St+1​,θ).

Semi-gradient TD(0) with Linear Function Approximation

Let's make this concrete for the case of linear function approximation, where our value estimate is v^(s,θ)=θTx(s)\hat{v}(s, \theta) = \theta^T x(s)v^(s,θ)=θTx(s), and x(s)x(s)x(s) is the feature vector for state sss.

As we saw earlier, the gradient of the linear value function with respect to the parameters θ\thetaθ is simply the feature vector itself:

∇θv^(s,θ)=x(s)\nabla_{\theta} \hat{v}(s, \theta) = x(s)∇θ​v^(s,θ)=x(s)

Substituting this into the general semi-gradient TD update rule gives us the update rule for Linear Semi-gradient TD(0):

θt+1←θt+α[Rt+1+γθtTx(St+1)−θtTx(St)]x(St)\theta_{t+1} \leftarrow \theta_t + \alpha [ R_{t+1} + \gamma \theta_t^T x(S_{t+1}) - \theta_t^T x(S_t) ] x(S_t)θt+1​←θt​+α[Rt+1​+γθtT​x(St+1​)−θtT​x(St​)]x(St​)

This update rule is computationally efficient and often performs very well in practice. After observing a transition (St,At,Rt+1,St+1)(S_t, A_t, R_{t+1}, S_{t+1})(St​,At​,Rt+1​,St+1​), we:

  1. Get the feature vectors for the current state, x(St)x(S_t)x(St​), and the next state, x(St+1)x(S_{t+1})x(St+1​).
  2. Calculate the current value estimates: v^(St,θt)=θtTx(St)\hat{v}(S_t, \theta_t) = \theta_t^T x(S_t)v^(St​,θt​)=θtT​x(St​) and v^(St+1,θt)=θtTx(St+1)\hat{v}(S_{t+1}, \theta_t) = \theta_t^T x(S_{t+1})v^(St+1​,θt​)=θtT​x(St+1​).
  3. Compute the TD error: δt=Rt+1+γv^(St+1,θt)−v^(St,θt)\delta_t = R_{t+1} + \gamma \hat{v}(S_{t+1}, \theta_t) - \hat{v}(S_t, \theta_t)δt​=Rt+1​+γv^(St+1​,θt​)−v^(St​,θt​).
  4. Update the weight vector: θt+1←θt+αδtx(St)\theta_{t+1} \leftarrow \theta_t + \alpha \delta_t x(S_t)θt+1​←θt​+αδt​x(St​).

The diagram below illustrates the flow of information in one step of a semi-gradient TD update.

Flow diagram for a single update step in Semi-gradient TD(0) with function approximation.

Why "Semi"? Convergence Notes

While semi-gradient methods are widely used and often effective, it's important to understand that they are not true gradient descent methods on the Bellman error. Because we ignore the gradient dependency in the target, we lose the theoretical convergence guarantees associated with standard gradient descent on a fixed objective function.

In some cases, particularly when using non-linear function approximators or off-policy learning (like trying to learn Q-values for a target policy π\piπ while following a different behavior policy bbb), semi-gradient methods can become unstable and the parameters might diverge. This is often referred to as the "deadly triad": function approximation, bootstrapping (TD updates), and off-policy learning.

However, for on-policy TD learning with linear function approximation, semi-gradient methods are generally stable and converge reliably, not necessarily to the absolute best possible weights, but to a reasonable approximation near the optimal solution within the limits of the chosen features and function approximator. The convergence is typically to a fixed point minimizing the Projected Bellman Error, a detailed discussion of which is outside our current scope but indicates a well-defined objective.

Semi-gradient methods provide a pragmatic and computationally feasible way to combine the power of TD learning with the necessity of function approximation for large-scale problems. They form the foundation for many advanced algorithms, including the Deep Q-Networks we will touch upon later. Next, we'll briefly explore more powerful, non-linear function approximators like neural networks.

  • Reinforcement Learning: An Introduction, Richard S. Sutton and Andrew G. Barto, 2018 (MIT Press) - This textbook is the definitive resource for reinforcement learning. Chapters 9 and 10 provide an in-depth explanation of function approximation, semi-gradient TD methods, and their convergence properties.
  • Gradient Descent for Reinforcement Learning, Leemon C. Baird, 1994 Advances in Neural Information Processing Systems 7, Vol. 7 (MIT Press) DOI: 10.5555/2984950.2985068 - This foundational paper highlights the 'deadly triad' (function approximation, bootstrapping, and off-policy learning) and the potential for divergence in TD learning with function approximation, explaining the challenges semi-gradient methods address.
  • CS234: Reinforcement Learning (Winter 2024), Emma Brunskill, 2025 (Stanford University) - Stanford's comprehensive course on Reinforcement Learning offers lecture notes and videos that explain semi-gradient TD methods and function approximation, providing an alternative pedagogical perspective.

© 2026 ApX Machine Learning