kernel smoothing

Attention is a reinvention of kernel smoothing

Gaussian kernel

Data Points & Prediction

Normalized weights αi

by xi

Attention as Kernel Smoothing

The attention mechanism in Transformers is mathematically equivalent to Nadaraya-Watson kernel regression. Both compute a weighted average where weights come from a similarity function.

Kernel Smoothing

K(xq,xi)K(x_q, x_i) = similarity-to-weight mapping

αi=K(xq,xi)jK(xq,xj)\alpha_i = \frac{K(x_q, x_i)}{\sum_j K(x_q, x_j)}

y^(xq)=iαiyi\hat{y}(x_q) = \sum_i \alpha_i y_i

Attention (scaled dot-product)

scorei=qkidk\text{score}_i = \frac{q \cdot k_i}{\sqrt{d_k}}

αi=softmax(score)i\alpha_i = \text{softmax}(\text{score})_i

out=iαivi\text{out} = \sum_i \alpha_i v_i

Component Mapping

Transformer AttentionKernel Regression
query qqevaluation point xqx_q
keys {ki}\{k_i\}sample locations {xi}\{x_i\}
values {vi}\{v_i\}sample responses {yi}\{y_i\}
score qki/dkq \cdot k_i / \sqrt{d_k}similarity / negative distance
softmax weights αi\alpha_inormalized kernel weights
output iαivi\sum_i \alpha_i v_ilocally weighted average

Key Insights

  • Learned kernel: In Transformers, the projections q=xWQq = xW_Q, k=xWKk = xW_K, v=xWVv = xW_V are learned, so the model learns the similarity geometry.
  • Multiple heads: Multi-head attention uses multiple learned kernels in parallel.
  • Bandwidth vs temperature: The Gaussian bandwidth hh controls locality; the softmax temperature τ\tau (or dk\sqrt{d_k}) controls how peaky the weights become.
  • Efficient attention: Because softmax attention is a kernel operation, methods like Performer approximate it with random feature maps for linear complexity.

What to Try

  • Set hh small (e.g., 0.2) and move xqx_q: weights become very local.
  • Set τ\tau small in Softmax-dot: weights become peaky (attention focuses on few keys).
  • Change xix_i positions to cluster points: observe how α\alpha redistributes.
  • Compare Gaussian vs Softmax-dot: note how softmax-dot prefers points with same sign as xqx_q.
claude opus 4.8January 2026·first cut. builds the kernel-smoothing sandbox: a Gaussian similarity kernel and a softmax-dot kernel, normalized weights, and the Nadaraya-Watson prediction shown live across a kernel-shape plot, a data-and-prediction scatter, and a weight bar chart. adds exact calibration of the kernel axioms (density peak, normalization, symmetry, constant reproduction), six assumptions covering the bias-variance tradeoff, and the attention-as-kernel analogy kept explicitly contested.
v1.0January 2026
  • kernel model: Gaussian similarity weight K(xq, xi) = exp(-(xq - xi)^2 / 2h^2) with bandwidth h, and a softmax-dot weight exp(xq xi / tau) with temperature tau.
  • Nadaraya-Watson estimator: normalized weights alpha_i = K / sum K and prediction yHat = sum alpha_i y_i, all derived live from the editable points and query.
  • added proper probability-kernel forms for calibration: the unit-bandwidth Gaussian density (peak 1 / sqrt(2 pi), unit mass) and the Epanechnikov kernel on compact support.
  • calibration: density peak K(0), Gaussian and Epanechnikov normalization to mass 1, even-function symmetry K(-u) = K(u), and exact reproduction of a constant signal; every predicted value computed from the logic functions.
  • six assumptions spanning the bias-variance bandwidth tradeoff, second-order kernel choice, boundary bias, and the attention-as-kernel correspondence marked contested.