Command Palette

Search for a command to run...

Назад к скиллам
скилл № 68978

statistical-mechanics

Use this skill when the user says 'diffusion model math', 'score matching', 'DDPM', 'Langevin dynamics', 'reverse SDE', 'probability flow ODE', 'Boltzmann…

БесплатноБез исполняемых скриптов
j4flmaoот j4flmao★ 47 звёзд на GitHub

Установить statistical-mechanics в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Об этом скилле

Statistical Mechanics & Diffusion Models

Purpose

Diffusion and score-based models borrow directly from statistical physics: data is gradually corrupted by Gaussian noise (a forward diffusion process), and a network learns the score ∇ₓ log pₜ(x) needed to run the process backwards. Softmax temperature, energy-based models, and Langevin sampling share the same Boltzmann foundations. This skill gives the exact equations, how they map to code, and the practical knobs (schedules, parameterization, guidance, samplers).

Agent Protocol

Trigger

"diffusion math", "DDPM", "DDIM", "score matching", "score function", "Langevin", "reverse SDE", "probability flow ODE", "noise schedule", "classifier-free guidance", "flow matching", "energy-based model", "Boltzmann".

Input Context

  • Goal: understanding, implementing from scratch, or debugging training/sampling.
  • Data type and resolution; compute budget.
  • Existing code (if debugging): parameterization (ε, x₀, v), schedule, sampler.

Output Artifact

Derivation summary with the governing equations and a minimal correct implementation or a diagnosis.

Response Format

Forward: q(x_t|x_0) = N(√ᾱ_t x_0, (1−ᾱ_t) I)  schedule={linear|cosine}, T={n}
Parameterization: {ε|x0|v}-prediction  Loss: {MSE on target}
Sampler: {ancestral DDPM|DDIM|DPM-Solver|ODE}  steps={n}
Guidance: CFG w={value} (p_uncond={x} in training)
Diagnosis: {issue -> fix}

Completion Criteria

  • Forward process and parameterization consistent between training and sampling.
  • Schedule/ᾱ indexing verified (off-by-one is the classic bug).
  • Sampler matches the trained parameterization.

Boltzmann Foundations

  • Boltzmann distribution: p(x) = exp(−E(x)/kT) / Z. High-energy states are exponentially unlikely; temperature T flattens or sharpens the distribution.
  • Softmax with temperature is exactly a Boltzmann distribution over logits with E = −logit.
  • Score: ∇ₓ log p(x) = −∇ₓ E(x)/kT; it does not depend on the intractable normalizer Z, which is why score-based learning is practical.
  • Langevin dynamics samples p using only the score: xₖ₊₁ = xₖ + (ε/2) ∇ₓ log p(xₖ) + √ε · zₖ, zₖ ~ N(0, I). As ε → 0 and steps → ∞ (with Metropolis correction or annealing for finite steps) it converges to p.

Forward and Reverse Processes

Discrete (DDPM, Ho et al. 2020): with variance schedule βₜ, αₜ = 1 − βₜ, ᾱₜ = ∏ₛ≤ₜ αₛ,

q(x_t | x_0) = N( √ᾱ_t · x_0 , (1 − ᾱ_t) I )      ⇒  x_t = √ᾱ_t x_0 + √(1−ᾱ_t) ε

Continuous (Song et al. 2021): forward SDE dx = f(x,t) dt + g(t) dw. Its time reversal (Anderson 1982) is

dx = [ f(x,t) − g(t)² ∇ₓ log p_t(x) ] dt + g(t) dw̄

and the probability flow ODE dx = [ f − ½ g² ∇ₓ log p_t(x) ] dt has the same marginals, enabling deterministic sampling (DDIM is a discretization of this view) and likelihood computation.

Denoising score matching: predicting the added noise ε is equivalent to learning the score, since ∇ log q(xₜ|x₀) = −ε / √(1−ᾱₜ).

Minimal DDPM (PyTorch)

from __future__ import annotations

import math

import torch
import torch.nn.functional as F


def cosine_alpha_bar(T: int, s: float = 0.008) -> torch.Tensor:
    t = torch.linspace(0, T, T + 1, dtype=torch.float64) / T
    f = torch.cos((t + s) / (1 + s) * math.pi / 2) ** 2
    alpha_bar = (f / f[0])[1:]                       # ᾱ_1..ᾱ_T
    return alpha_bar.clamp(1e-5, 1.0).float()


class DDPM:
    def __init__(self, model: torch.nn.Module, T: int = 1000) -> None:
        self.model, self.T = model, T
        self.alpha_bar = cosine_alpha_bar(T)
        prev = torch.cat([torch.ones(1), self.alpha_bar[:-1]])
        self.alpha = self.alpha_bar / prev
        self.beta = (1 - self.alpha).clamp(max=0.999)

    def loss(self, x0: torch.Tensor) -> torch.Tensor:
        b = x0.shape[0]
        t = torch.randint(0, self.T, (b,), device=x0.device)
        ab = self.alpha_bar.to(x0.device)[t].view(b, *([1] * (x0.dim() - 1)))
        eps = torch.randn_like(x0)
        xt = ab.sqrt() * x0 + (1 - ab).sqrt() * eps
        return F.mse_loss(self.model(xt, t), eps)    # ε-prediction

    @torch.inference_mode()
    def sample(self, shape: tuple[int, ...], device: torch.device) -> torch.Tensor:
        x = torch.randn(shape, device=device)
        for i in reversed(range(self.T)):
            t = torch.full((shape[0],), i, device=device, dtype=torch.long)
            a, ab, beta = (v.to(device)[i] for v in (self.alpha, self.alpha_bar, self.beta))
            eps = self.model(x, t)
            mean = (x - beta / (1 - ab).sqrt() * eps) / a.sqrt()
            x = mean + beta.sqrt() * torch.randn_like(x) if i > 0 else mean
        return x

model(x_t, t) is typically a U-Net or transformer with timestep embedding. For speed, use DDIM or DPM-Solver samplers (20-50 steps) on the same trained model.

Practical Knobs

Knob Options Notes
Schedule Linear (DDPM), cosine (Nichol & Dhariwal 2021) Cosine avoids destroying informati
Полный SKILL.md на GitHub

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Без ограничений — скилл может использовать любой инструмент.

FAQ

Что делает скилл statistical-mechanics?

Use this skill when the user says 'diffusion model math', 'score matching', 'DDPM', 'Langevin dynamics', 'reverse SDE', 'probability flow ODE', 'Boltzmann distribution', 'energy-based model', 'classifier-free guidance', 'flow matching', 'noise schedule'. Explains the statistical-mechanics view of generative modeling (Boltzmann distributions, Langevin dynamics, forward/reverse diffusion SDEs, score functions) and implements a correct DDPM training and sampling loop. Do NOT use for: using pretrained image generators (ai/genai-vision), information-theoretic losses (fundamental-truths/information-theory), or physics simulation in games.

Как установить скилл statistical-mechanics?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл statistical-mechanics запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить statistical-mechanics с