PAJAMA / LEARN
← Course overview

Learn from observations / 08 OF 12

How a pixel tells a parameter to move

After this lessonDerive a complete scalar gradient and connect its chain rule to the real splat evaluator.

A loss is like a height on a landscape. The derivative tells you which nearby direction goes uphill. To reduce loss, take a small step the other way.

Build the idea, one step at a time

Need a math refresher?

Open only the tool you need; return here to use it in the next step.

Derivatives as local ratesThe chain rule

STEP 1 / 5

Turn a color difference into a scalar

For one scalar pixel, let prediction be Ĉ and target C*. Squaring their difference makes positive and negative errors both cost something. For RGB MSE, average the three squared errors; for a batch, average across pixels and samples as well. The averaging factors belong in the derivative too.

L=(C^−C∗)2,∂L∂C^=2(C^−C∗)L=(\widehat C-C^*)^2,\qquad\frac{\partial L}{\partial\widehat C}=2(\widehat C-C^*)

PUT NUMBERS IN

At the initial values, loss is 0.04. A step with η = 1 changes β from 0 to 0.08, α to about 0.51999, prediction to 0.41599 and loss to about 0.03386. No mysterious “AI instruction” is involved—just this chain of arithmetic.

FORMULA → RUNNING CODE

Find the same operation in the code

These excerpts are read from the current repository when the site is built. Expand one to inspect it; the full snapshot opens in a separate tab.

public/learn/models.mjsL15–20

The exact arithmetic behind this lesson’s controls and worked example.

export function scalarGradient(beta,rate=1){
 const c=.8,target=.6,alpha=sigmoid(beta),prediction=c*alpha,loss=(prediction-target)**2;
 const gradient=2*(prediction-target)*c*alpha*(1-alpha),next=beta-rate*gradient;
 const lossAt=x=>(c*sigmoid(x)-target)**2,epsilon=1e-4;
 return {alpha,prediction,loss,gradient,next,nextLoss:lossAt(next),finite:(lossAt(beta+epsilon)-lossAt(beta-epsilon))/(2*epsilon)};
}
Open the deployed source snapshot ↗
public/math/trace.mjsL110–141

This longer derivative includes the real projection and compositing paths.

export function gradient(records, parameter, time = defaults.time, pixel = defaults.pixel, options = {}) {
  // Differentiate record A, while holding the visibility/sort branches fixed.
  // Supported fields: base mean x/y/z or opacity logit. Scales/rotation remain fixed.
  if (![0, 1, 2, 20].includes(parameter)) throw new Error('Unsupported teaching parameter');
  const result = forward(records, time, pixel, options);
  const index = result.splats.findIndex(s => s.id === 0), s = result.splats[index];
  let tail = [...background];
  for (let j = result.splats.length - 1; j > index; j--) {
    const layer = result.splats[j];
    tail = plus(scaled(layer.color, layer.alpha), scaled(tail, 1-layer.alpha));
  }
  const colorSlope = scaled(s.color.map((v, i) => v - tail[i]), s.transmittance);
  const lossColorSlope = result.color.map((v, i) => 2 * (v-result.target[i]) / 3);
  const lossAlphaSlope = dot(lossColorSlope, colorSlope);
  let meanPath = 0, covariancePath = 0, opacityPath = 0;
  if (s.branch === '光滑区间') {
    if (parameter === 20) opacityPath = s.alpha * (1-s.baseOpacity);
    else {
      const [x, y, z] = s.mean, f = camera.focal;
      const dJ = parameter === 0 ? [[0, 0, -f/z**2], [0, 0, 0]] :
        parameter === 1 ? [[0, 0, 0], [0, 0, -f/z**2]] :
        [[-f/z**2, 0, 2*f*x/z**3], [0, -f/z**2, 2*f*y/z**3]];
      const left = multiply(multiply(dJ, s.sigma), transpose(s.J));
      const right = multiply(multiply(s.J, s.sigma), transpose(dJ));
      const dC = left.map((row, i) => plus(row, right[i]));
      meanPath = s.alpha * dot(s.qd, s.J.map(row => row[parameter]));
      covariancePath = s.alpha / 2 * dot(s.qd, matrixVector(dC, s.qd));
    }
  }
  const alphaSlope = meanPath + covariancePath + opacityPath;
  const analytic = lossAlphaSlope * alphaSlope;
  const finite = (options.finite === false ? [] : [1e-4, 1e-5, 1e-6]).map(h => {
Open the deployed source snapshot ↗

CHECK YOUR UNDERSTANDING

Can you predict the result?

The gradient is negative. With positive learning rate, βnew = β − ηg will…

This is your own review mark. You can remove it at any time.

Go deeper with primary sources

3D Gaussian Splatting · paper & implementation ↗3DGS · training implementation ↗Full mathematical derivation ↗