RL Study Notes: SA and SGD
A review of Stochastic Approximation and the Robbins-Monro algorithm, detailing the evolution, convergence properties, and sampling differences of SGD.
Stochastic Approximation Theory and Stochastic Gradient Descent#
Mean Estimation#
First, consider the problem of estimating the sample mean:
For the mean at the previous step, we have:
Through algebraic transformation, the full batch computation can be converted into a recursive form:
That is:
- This is an iterative algorithm that does not require storing and recalculating all historical data.
- In the early stages of iteration, the estimate may be inaccurate due to insufficient sample size; however, as the sample size increases, the result gradually approaches the true mean.
From this, we can derive a more generalized iterative equation:
- When the step size sequence satisfies certain conditions, the estimate will still converge to the expectation .
- This form can be viewed as a special case of the Stochastic Approximation (SA) algorithm, and it is also the foundational form of the Stochastic Gradient Descent (SGD) algorithm.
Robbins-Monro (RM) Algorithm#
Stochastic Approximation (SA)#
- SA is a broad class of algorithms that rely on stochastic iteration to find the roots of equations or solve optimization problems.
- The core advantage of SA is its black-box solving capability: it does not require knowledge of the analytical expression or global properties of the target equation, relying only on noisy observational data to update parameters.
RM Algorithm Definition#
A necessary condition for finding the extremum of a function is that the gradient is zero, i.e., . Similarly, for the case of , it can be transformed into a root-finding problem by moving the terms. SGD is exactly a special case of the RM algorithm.
The RM algorithm solves through the following iterative format:
Where:
- is the estimate of the root at the -th iteration.
- is the -th observation with random noise .
- is a positive coefficient controlling the step size.
Convergence Theorem and Condition Analysis#
If the following conditions hold:
(a) for all ; (b) and ; (c) and ;
Where represents the history up to time , then the sequence will converge almost surely (with probability 1) to the root satisfying .
- Condition (a): Requires the derivative (or gradient) of the function to have strict upper and lower bounds. This ensures the function is sufficiently smooth and its slope will not approach zero or infinity, thereby ensuring the update direction points stably and continuously towards the target solution .
- Condition (b): The classic step size (learning rate) constraint. ensures the total step size is infinite, giving the algorithm the capability to cover any initial distance to reach the target point; ensures the step size decays fast enough so the algorithm can ultimately converge without oscillating perpetually around the root.
- Condition (c): Constraints on the properties of the random noise. A conditional expectation of zero given the historical sequence (forming a martingale difference sequence) implies the noise estimation is unbiased; a finite conditional variance limits the fluctuation amplitude during single-step exploration, preventing the algorithm from diverging.
Stochastic Gradient Descent (SGD)#
SGD is primarily used to solve expected risk minimization problems:
- is the parameter to be optimized.
- is a random variable, and the expectation is calculated with respect to the distribution of .
- The function outputs a scalar, and both the parameter and input can be scalars or vectors.
Gradient Descent Evolution#
1. Gradient Descent (GD)
2. Batch Gradient Descent (BGD)
Approximating the mathematical expectation through the empirical mean of a finite sample:
3. Stochastic Gradient Descent (SGD)
- Compared to GD: Replaces the true gradient with the single-sample stochastic gradient .
- Compared to BGD: Equivalent to setting the batch size to .
According to RM algorithm theory, if the Hessian matrix is positive definite and bounded (), the learning rate satisfies the Robbins-Monro sequence conditions, and the sample sequence is independent and identically distributed (i.i.d.), then SGD will also converge almost surely (with probability 1) to the optimal solution.
SGD Relative Error and Randomness#
Introduce the relative error to measure the degree to which the stochastic gradient deviates from the true gradient:
At the optimal solution , holds. Substituting this into the denominator and applying the Mean Value Theorem for Integrals:
Due to the strong convexity assumption , we can bound the denominator:
Thus, we obtain the upper bound of the relative error:
This inequality strictly reveals an important convergence pattern of SGD:
- The relative error is inversely proportional to the distance to the optimal solution .
- When is far from the optimal solution, the denominator is large, and is small. At this point, the update direction of SGD is very close to the true gradient, exhibiting a descent trajectory highly similar to GD.
- As gradually approaches the optimal solution , the denominator tends to zero, and the relative error increases significantly. This means that in the neighborhood of the optimal solution, noise interference becomes dominant, causing the algorithm to exhibit strong random oscillation in the late stages of convergence (which is also why SGD requires learning rate decay).
Sampling Comparison: BGD, MBGD, and SGD#
This random sampling strategy shares similarities with truncated methods.
Assume we want to minimize and have a set of random samples for . The iterative formulas for the three algorithms are compared as follows:
- BGD: Computes the full samples at each iteration. When is sufficiently large, the update direction closely approximates the true expected gradient.
- MBGD: Draws a subset of size from the global samples at each iteration. This set is obtained through independent and identically distributed (i.i.d.) samples.
- SGD: Randomly draws a single sample at time step during each iteration.
Core Difference Note: Even when , MBGD is not equivalent to BGD. This is because the random samples in MBGD are typically drawn with replacement (potentially drawing duplicate samples), whereas BGD strictly iterates over all unique global sample data.