Skip to content
QFT.org

Search QFT.org

Enter a word or phrase.

    You can also browse the main navigation and chapter contents.

    Stochastic Gradients and Optimization Noise

    A variational energy can be estimated accurately while the parameters used to define the state are still wandering. Conversely, an optimizer can stop at a plausible parameter whose final energy estimate is noisy. Which uncertainty belongs to the state, and which belongs to the measurement?

    We answer with a Gaussian trial state for a quartic oscillator. Its exact variational objective is known, so two sampled gradients, fixed learning-rate schedules, and independent final measurements can be checked separately. The calculation also gives a counterexample to the claim that differentiating through a sampling path always produces the lowest-variance gradient.

    Preparation: Expectations and importance sampling in Monte Carlo integration, and the distinction between a numerical stopping test and a physical result in constrained optimization. All oscillator quantities below are dimensionless after choosing units; the potential uses the site's quartic coefficient λ/4!\lambda/4!.

    An energy whose optimum is known

    Take

    H=−12d2dx2+m2x22+λx424,m=1,λ=4,H=-\frac12\frac{d^2}{dx^2}+\frac{m^2x^2}{2}+\frac{\lambda x^4}{24}, \qquad m=1,\quad\lambda=4,

    and the normalized trial family

    ψω(x)=(ωπ)1/4e−ωx2/2,ω>0.\psi_\omega(x)=\left(\frac\omega\pi\right)^{1/4} e^{-\omega x^2/2},\qquad \omega>0.

    The probability density is pω=∣ψω∣2p_\omega=|\psi_\omega|^2, with ⟨x2⟩=1/(2ω)\langle x^2\rangle=1/(2\omega) and ⟨x4⟩=3/(4ω2)\langle x^4\rangle=3/(4\omega^2). The kinetic expectation is ω/4\omega/4, hence

    E(ω)=ω4+m24ω+λ32ω2.E(\omega)=\frac\omega4+\frac{m^2}{4\omega}+\frac\lambda{32\omega^2}.

    Use θ=log⁡ω\theta=\log\omega to enforce positive width. Its exact gradient and curvature are

    g(θ)=dEdθ=ω4−m24ω−λ16ω2,d2Edθ2=ω4+m24ω+λ8ω2>0.\begin{aligned} g(\theta)&=\frac{dE}{d\theta} =\frac\omega4-\frac{m^2}{4\omega}-\frac\lambda{16\omega^2},\\ \frac{d^2E}{d\theta^2}&= \frac\omega4+\frac{m^2}{4\omega}+\frac\lambda{8\omega^2}>0. \end{aligned}

    The objective is strictly convex in θ\theta for m>0m>0, λ≥0\lambda\ge0. The stationary equation is 4ω3−4m2ω−λ=04\omega^3-4m^2\omega-\lambda=0. For the chosen parameters,

    ω∗3−ω∗−1=0,ω∗=1.3247179572,E∗=0.59112894225.\omega_*^3-\omega_*-1=0,\qquad \omega_*=1.3247179572,\qquad E_*=0.59112894225.

    These are the optimum and energy within this Gaussian family. Variational optimization does not establish that a Gaussian is the exact ground state of the quartic Hamiltonian. Optimizer error, sampling error, and trial-family error are different quantities.

    Specify what is sampled before differentiating

    The local energy is

    EL(x;ω)=Hψω(x)ψω(x)=ω2+m2−ω22x2+λ24x4.E_L(x;\omega)=\frac{H\psi_\omega(x)}{\psi_\omega(x)} =\frac\omega2+\frac{m^2-\omega^2}{2}x^2+\frac\lambda{24}x^4.

    Its expectation under pωp_\omega is E(ω)E(\omega). This standard variational Monte Carlo identity and its zero-variance limit are explained in Toulouse, Assaraf and Umrigar, §1.1–1.2.1, pp.3–4 (PDF). Here we can sample the Gaussian directly:

    ξ∼N(0,1),x=ξ2ω.\xi\sim N(0,1),\qquad x=\frac\xi{\sqrt{2\omega}}.

    There is no Markov chain, burn-in, or autocorrelation correction in this example. The training draws are independent conditional on the current parameter.

    Differentiate the path at fixed base noise

    Substitute the sampling map into the local energy:

    EL(ξ;θ)=ω2+(m24ω−ω4)ξ2+λ96ω2ξ4.E_L(\xi;\theta)=\frac\omega2+ \left(\frac{m^2}{4\omega}-\frac\omega4\right)\xi^2 +\frac\lambda{96\omega^2}\xi^4.

    Differentiation at fixed ξ\xi gives one unbiased gradient draw,

    Gpw=ω2−(m24ω+ω4)ξ2−λ48ω2ξ4,E[Gpw]=g(θ).G_{\rm pw}=\frac\omega2- \left(\frac{m^2}{4\omega}+\frac\omega4\right)\xi^2 -\frac\lambda{48\omega^2}\xi^4, \qquad \mathbb E[G_{\rm pw}]=g(\theta).

    Both the explicit parameter in ELE_L and the parameter in x(ξ,θ)x(\xi,\theta) were differentiated. Holding the sampled xx fixed and taking only the explicit derivative would omit a term. The interchange of integration and differentiation is valid here because, on any compact positive-width interval, the Gaussian supplies an integrable bound on the required polynomial factors. Mohamed et al., §§5.1–5.2, Eqs.(26)–(29) (PDF).

    Differentiate the probability measure instead

    The score at fixed xx is

    sθ(x)=∂θlog⁡pω(x)=12−ωx2=1−ξ22,E[sθ]=0.s_\theta(x)=\partial_\theta\log p_\omega(x) =\frac12-\omega x^2=\frac{1-\xi^2}{2}, \qquad \mathbb E[s_\theta]=0.

    In general, a parameter-dependent integrand requires

    dEdθ=E[∂θEL]+E[ELsθ].\frac{dE}{d\theta}=\mathbb E[\partial_\theta E_L] +\mathbb E[E_Ls_\theta].

    For this family, ∂θEL∣x=ω/2−ω2x2\partial_\theta E_L|_x=\omega/2-\omega^2x^2, whose expectation is zero. Thus

    g(θ)=Cov⁡(EL,sθ)=2Cov⁡(EL,∂θlog⁡ψω).g(\theta)=\operatorname{Cov}(E_L,s_\theta) =2\operatorname{Cov}(E_L,\partial_\theta\log\psi_\omega).

    The last expression is the usual variational-energy covariance gradient. The vanishing explicit-derivative expectation has been proved for this estimator; it is not a license to drop such derivatives in another model. The score identity and constant-baseline construction are given in Mohamed et al., §§4.1–4.3, Eqs.(11)–(14) (PDF).

    For a batch of B≥2B\ge2 draws, the companion uses

    g^sc=1B−1∑i=1B(EL,i−E‾L)(si−s‾).\widehat g_{\rm sc}=\frac1{B-1} \sum_{i=1}^B(E_{L,i}-\overline E_L)(s_i-\overline s).

    This sample covariance is unbiased. If the same-batch means are subtracted but the denominator is BB, the expectation is (B−1)g/B(B-1)g/B. A randomly fitted baseline cannot simply be treated as a fixed constant independent of its own sample.

    Estimator variance changes during optimization

    For the pathwise batch mean, the variance is Var⁡(Gpw)/B\operatorname{Var}(G_{\rm pw})/B. For the sample covariance, define centered moments

    μpq=E[(EL−E)p(s−Es)q].\mu_{pq}=\mathbb E[(E_L-E)^p(s-\mathbb E s)^q].

    Expanding the sample sums and grouping equal and unequal indices gives

    Var⁡(g^sc)=1B[μ22−B−2B−1μ112+μ20μ02B−1].\operatorname{Var}(\widehat g_{\rm sc})= \frac1B\left[ \mu_{22}-\frac{B-2}{B-1}\mu_{11}^2 +\frac{\mu_{20}\mu_{02}}{B-1}\right].

    All moments here are Gaussian polynomial integrals. The companion evaluates them using exact integer normal moments and independently with Gauss–Hermite quadrature. At B=16B=16 the resulting variances are:

    WidthPathwise varianceScore-covariance variance
    ω=0.5\omega=0.51.027994.83811
    ω=ω∗\omega=\omega_*0.08434920.0224371
    ω=2\omega=20.07096350.0612847

    The preferred estimator by this criterion changes with the parameter. An even sharper distinction occurs at the free minimum, λ=0\lambda=0 and ω=m\omega=m. The local energy is then the constant m/2m/2, so every sample-covariance gradient is zero. The pathwise gradient is instead m(1−ξ2)/2m(1-\xi^2)/2, with nonzero variance m2/2m^2/2 per draw. Equal expectations do not imply equal variances, even for derivatives of the same energy.

    There is also uncertainty in an empirical variance estimate. The fixed diagnostic uses 1024 independent batches at each width and batch size. At ω∗\omega_*, B=16B=16, its measured score-gradient variance is 0.009970.00997, while the independently evaluated population variance is 0.022440.02244. The score gradient contains high powers of a Gaussian, making rare large samples influential. We retain this discrepancy; a finite batch study is not a precise replacement for the available moment calculation. The two polynomial-moment paths agree within 1.3×10−131.3\times10^{-13}, and neither uses these random batches as its reference.

    Learning rates control a distribution of parameters

    Apply the update

    θk+1=Π[−log⁡4,log⁡4](θk−ηkg^k),\theta_{k+1}=\Pi_{[-\log4,\log4]} \bigl(\theta_k-\eta_k\widehat g_k\bigr),

    where Π\Pi projects onto the declared interval. The initial width is 0.50.5, and the true variational optimum is interior. Projection is an explicit algorithm choice, not unreported gradient clipping. Every boundary contact is counted.

    We compare a constant rate ηk=0.03\eta_k=0.03 with

    ηk=0.15(1+k/32)3/4,k=0,1,….\eta_k=\frac{0.15}{(1+k/32)^{3/4}},\qquad k=0,1,\ldots.

    The infinite decreasing sequence has ∑kηk=∞\sum_k\eta_k=\infty and ∑kηk2<∞\sum_k\eta_k^2<\infty. Such conditions help control noise under suitable regularity assumptions; they do not certify a finite run or automatically transfer an unprojected convergence theorem to this projected algorithm. Fixed steps can retain a stationary spread even when the gradient estimator is unbiased. Bottou, Curtis and Nocedal, §4.2, Eq.(4.19) and Theorems 4.6–4.7 (PDF).

    Near the optimum, write g^≈h(θ−θ∗)+ϵ\widehat g\approx h(\theta-\theta_*)+\epsilon, with h=E′′(θ∗)h=E''(\theta_*) and zero-mean gradient noise. A fixed step multiplies the old parameter error by 1−ηh1-\eta h and adds fresh noise. Exercise2 derives its limiting variance. This explains why averaging the last few parameters, using a decreasing rate, and increasing the batch size affect different parts of the calculation. Averaging θ\theta and averaging E(eθ)E(e^\theta) are also different operations.

    A frozen repeated-seed comparison

    Each of the eight estimator/schedule/batch combinations uses 32 independent runs. Every run consumes 16384 training normals: 1024 updates at B=16B=16 or 256 at B=64B=64. The seeds are SeedSequence([921025, 10, estimator, schedule, batch, replica]); the accompanying JSON declares the integer labels and runtime versions. Settings were fixed before production. Equal draw counts do not imply equal update counts or equal wall time.

    GradientScheduleBBMean final ω\omega ± SERMS error in θ\thetaMean E−E∗E-E_*
    PathwiseConstant161.32504±0.010001.32504\pm0.010000.042237.20×10−47.20\times10^{-4}
    PathwiseConstant641.33195±0.005121.33195\pm0.005120.022111.96×10−41.96\times10^{-4}
    PathwiseDecreasing161.32156±0.005881.32156\pm0.005880.024612.43×10−42.43\times10^{-4}
    PathwiseDecreasing641.32394±0.004401.32394\pm0.004400.018541.38×10−41.38\times10^{-4}
    Score covarianceConstant161.33405±0.005991.33405\pm0.005990.025512.60×10−42.60\times10^{-4}
    Score covarianceConstant641.32428±0.003031.32428\pm0.003030.012676.44×10−56.44\times10^{-5}
    Score covarianceDecreasing161.32610±0.003391.32610\pm0.003390.014138.00×10−58.00\times10^{-5}
    Score covarianceDecreasing641.32526±0.002301.32526\pm0.002300.009633.73×10−53.73\times10^{-5}

    The SE in this table describes the mean over 32 runs; it is not the spread of a single optimized width. The RMS error includes both finite-horizon bias and stochastic spread. All 256 runs finish without a projection contact. That observed result does not prove that the projection would remain inactive for every seed or coupling.

    The same recursions with exact gradients supply deterministic finite-horizon controls. The least accurate final exact-gradient energy in these settings exceeds E∗E_* by 6.67×10−76.67\times10^{-7}, for the shorter constant-rate run. The larger stochastic excesses therefore cannot be explained solely by that deterministic stopping horizon. All final iterates remain primary results; averages over the last quarter of updates are retained separately, without selecting whichever looks best after the calculation.

    All 32 final widths for each stochastic optimization setting, alongside exact gradient-estimator variances at three fixed widths.

    The final-width panel shows individual runs and their mean; the bars give one across-run standard deviation, whereas the table quotes the standard error of the mean. The dashed horizontal line is ω∗\omega_*. The variance panel uses the independent Gaussian-moment calculation at B=16B=16, not fitted variance estimates from the optimizer paths.

    Measure the final state with new draws

    After training, each run receives 8192 independent normals from a separate seed family beginning [921025, 12, ...]. At its final width ω^\widehat\omega, we estimate both ELE_L and x2x^2. Conditional on the trained parameter, their reference expectations are

    E(ω^),⟨x2⟩ω^=12ω^.E(\widehat\omega),\qquad \langle x^2\rangle_{\widehat\omega}=\frac1{2\widehat\omega}.

    The validation error bar is the sample standard deviation divided by 8192\sqrt{8192}. It measures uncertainty in an observable of that final trial state. It does not include its displacement from the variational optimum or the unknown Gaussian-family error relative to the true ground state.

    The mean reported energy SE ranges from 1.27×10−31.27\times10^{-3} to 1.41×10−31.41\times10^{-3} across the eight settings. Between 29 and 31 of the 32 energy estimates per setting lie within 1.96 reported SE of their own conditional expectation. These small ensembles are diagnostics, not precise coverage measurements. In particular, this validation noise can exceed the remaining mean optimization excess; quoting it as uncertainty about E∗E_* would mix two different targets.

    The law of total variance summarizes the separation. If O^\widehat O is an unbiased fresh estimate at a random trained width,

    Var⁡(O^)=E ⁣[Var⁡(O^∣ω^)]+Var⁡ ⁣(O(ω^)).\operatorname{Var}(\widehat O)= \mathbb E\!\left[\operatorname{Var}(\widehat O\mid\widehat\omega)\right] +\operatorname{Var}\!\left(O(\widehat\omega)\right).

    More validation draws reduce the first term, while further or improved optimization changes the second. Neither operation enlarges the variational family.

    Reproduction uses gaussian_variation.py, stochastic_gaussian.py, and stochastic-results.json in the chapter's example directory. The driver also writes all parameter histories to an ignored numerical archive. Training consumes 4194304 normal draws, validation 2097152, and the independent fixed-point gradient study 245760. The plotter reads retained summaries without rerunning any training.

    Summary

    An unbiased gradient is a statement about one conditional expectation, not a promise that a finite optimizer output is unbiased or optimal. The Gaussian example separates gradient variance, the distribution of trained parameters, and a fresh measurement of the final state. Its exact objective and moment calculations reveal finite sampling fluctuations without tuning them away, while the quartic model keeps variational-family error distinct from numerical uncertainty.

    Exercises

    1. Derive the optimum of the Gaussian family and show that the score-covariance gradient has zero variance at the free minimum, although the pathwise local-energy gradient does not.
    Solution

    The Gaussian moments give E=ω/4+m2/(4ω)+λ/(32ω2)E=\omega/4+m^2/(4\omega)+\lambda/(32\omega^2). Multiplication of dE/dω=0dE/d\omega=0 by 16ω316\omega^3 gives 4ω3−4m2ω−λ=04\omega^3-4m^2\omega-\lambda=0. Strict positivity of d2E/dθ2d^2E/d\theta^2 and divergence of EE at both width endpoints imply a unique minimum for m>0m>0, λ≥0\lambda\ge0.

    For λ=0\lambda=0, the solution is ω=m\omega=m. Substitution into ELE_L leaves the constant m/2m/2, so every centered local-energy sample is zero and g^sc=0\widehat g_{\rm sc}=0. In contrast,

    Gpw=m2(1−ξ2),E[Gpw]=0,Var⁡(Gpw)=m24(Eξ4−(Eξ2)2)=m22.G_{\rm pw}=\frac m2(1-\xi^2),\qquad \mathbb E[G_{\rm pw}]=0,\qquad \operatorname{Var}(G_{\rm pw})= \frac{m^2}{4}\bigl(\mathbb E\xi^4-(\mathbb E\xi^2)^2\bigr) =\frac{m^2}{2}.

    A batch mean divides that variance by BB. The derivative along the sampling path probes neighboring trial states, so constancy of the local energy at the single optimum does not force that derivative to be constant as a function of ξ\xi.

    1. Consider δk+1=(1−ηh)δk−ηϵk\delta_{k+1}=(1-\eta h)\delta_k-\eta\epsilon_k, where h>0h>0, the independent noises have mean zero and variance σ2\sigma^2, and δ0\delta_0 is fixed. Derive its mean and variance. What can an independent final observable measurement repair?
    Solution

    Write r=1−ηhr=1-\eta h. Iteration gives

    δk=rkδ0−η∑j=0k−1rk−1−jϵj.\delta_k=r^k\delta_0-\eta\sum_{j=0}^{k-1}r^{k-1-j}\epsilon_j.

    Consequently Eδk=rkδ0\mathbb E\delta_k=r^k\delta_0 and

    Var⁡(δk)=η2σ2∑j=0k−1r2j=η2σ2(1−r2k)1−r2(r2≠1).\operatorname{Var}(\delta_k)=\eta^2\sigma^2\sum_{j=0}^{k-1}r^{2j} =\frac{\eta^2\sigma^2(1-r^{2k})}{1-r^2}\qquad(r^2\ne1).

    When r2=1r^2=1, the finite sum instead gives Var⁡(δk)=kη2σ2\operatorname{Var}(\delta_k)=k\eta^2\sigma^2. For 0<ηh<20<\eta h<2, the mean tends to zero but the limiting variance is

    Var⁡(δ∞)=ησ2h(2−ηh).\operatorname{Var}(\delta_\infty)= \frac{\eta\sigma^2}{h(2-\eta h)}.

    For a quadratic objective E−E∗=hδ2/2E-E_*=h\delta^2/2, the expected excess energy is h[Var⁡(δk)+(Eδk)2]/2h[\operatorname{Var}(\delta_k)+(\mathbb E\delta_k)^2]/2. A zero mean parameter error therefore does not imply zero mean energy error. If an observable is subsequently estimated with fresh samples, its measurement noise can be reduced independently. Those samples do not move the already chosen parameter, so they cannot remove its bias or across-run spread. This linearized calculation assumes fixed noise variance and no active projection; it explains a mechanism rather than replacing the nonlinear experiment.

    References

    • Léon Bottou, Frank E. Curtis and Jorge Nocedal, “Optimization Methods for Large-Scale Machine Learning,” SIAM Review 60 (2018) 223–311, arXiv:1606.04838 (PDF), February 12, 2018 version, §§4.1–4.2, Eq.(4.19) and Theorems 4.6–4.7: gradient-moment assumptions and fixed/decreasing step sizes.
    • Shakir Mohamed, Mihaela Rosca, Michael Figurnov and Andriy Mnih, “Monte Carlo Gradient Estimation in Machine Learning,” Journal of Machine Learning Research 21 (132) (2020) 1–62, PDF, §§4.1–4.3 and 5.1–5.2: score and pathwise estimators, baselines and differentiation assumptions.
    • Julien Toulouse, Roland Assaraf and C. J. Umrigar, “Introduction to the variational and diffusion Monte Carlo methods,” arXiv:1508.02989v1 (PDF), §§1.1–1.2.1, Eq.(1) and pp.3–4: normalized local-energy expectation, iid uncertainty and the zero-variance property.

    Scrollable formula. Use Left and Right arrow keys to reveal hidden content.

    Scrollable table. Use Left and Right arrow keys to reveal hidden columns.

    Scrollable figure. Use Left and Right arrow keys to reveal hidden content.