学术
模块更新于 2026-11-21· 浏览 0· 下载 0

PVC 后验速度校正

用预测端点的测量残差和算子伴随修正速度,以桥方差安排强度,并用谱范数限制过松弛幅度。复用已有速度,不增加速度网络前向,也不求网络雅可比。

计算机视觉图像重建流匹配逆问题图像修复生成模型

结构图

PVC 后验速度校正结构图
PVC 后验速度校正结构图

用预测端点的测量残差和算子伴随修正速度,以桥方差安排强度,并用谱范数限制过松弛幅度。复用已有速度,不增加速度网络前向,也不求网络雅可比。

论文信息

  • 论文:Better Trajectories, Not More: Paired Posterior Correction for Flow-Matching Inverse Problems

核心痛点

流匹配重建的近端校正临近终点时减弱,网络又将预测拉回先验。多条独立轨迹平均可以降方差,却无法消掉共同偏差。

模块亮点

  • 用预测端点的测量残差和算子伴随修正速度,以桥方差安排强度,并用谱范数限制过松弛幅度。复用已有速度,不增加速度网络前向,也不求网络雅可比。

适用场景

已知线性退化与高斯噪声下的去噪、去模糊、超分和修复。当前代码仅含单步校正,完整求解器还需要近端求解及噪声配对;其他任务需单独验证。

插入位置建议

  • 放在速度预测之后、近端数据一致性之前。核对状态与速度形状、测量空间、算子伴随和正噪声标准差,预先提供可靠谱范数,并使近端权重匹配桥方差。

适用任务

  • 已知线性退化与高斯噪声下的去噪
  • 去模糊
  • 超分和修复。当前代码仅含单步校正

完整代码

pvc.py
# -*- coding: utf-8 -*-
"""PPC-Flow:后验速度校正 PosteriorVelocityCorrection(PVC)。

论文:Better Trajectories, Not More: Paired Posterior Correction for Flow-Matching
Inverse Problems(ICLR 2027 投稿,匿名)。本文件只抽取其中的后验速度校正模块。

问题:流匹配逆问题求解器做完近端数据一致性步后,速度网络仍会把端点拉回先验,而近端权重
λ_t 在终端时刻趋于零,晚期偏差无法消除;多轨迹独立平均只能以 1/K 速率降方差。方法:用
前向算子 H 及其转置 H^T 计算预测端点 m_{θ,t} 的测量残差,按桥对齐调度
α_t = min{a, ā_t}·r_t 修正速度,使随后的近端步等价于放大系数 ω_t = 1+(1-t)α_t/r_t ≥ 1
的过松弛近端步;闭式上限 ā_t 保证 ω_t·w_max(t) ≤ 1.9,使每个被观测残差分量逐步收缩。
该模块作用:无需网络雅可比、不增加 NFE 的单步校正算子,输出修正后的端点估计 x̂_1,
供近端与重注入步直接使用。
"""

import sys
from typing import Callable, Optional, Tuple

import torch
import torch.nn as nn

__all__ = ["PosteriorVelocityCorrection"]


class PosteriorVelocityCorrection(nn.Module):
    """式 (11) + 式 (17):后验速度校正(PPC-Flow 的偏差修正算子)。

    x_t, v_t : (B, *d)  任意维(图像 (B,C,H,W) 或向量 (B,d))
    y        : (B, m)   线性测量 y = H x + η,η ~ N(0, σ_n² I)
    h_op     : x -> y   前向算子 H(矩阵自由,需支持 batch)
    ht_op    : y -> x   伴随算子 H^T
    t        : 标量或 (B,)  当前 flow 时间 ∈ [0, 1]
    """

    def __init__(
        self,
        sigma_n: float = 0.05,
        strength: float = 1.0,
        omega_cap: float = 1.9,
        sigma_max: Optional[float] = None,
        power_iters: int = 10,
        eps: float = 1e-6,
    ) -> None:
        super().__init__()
        if sigma_n <= 0:
            raise ValueError("sigma_n must be positive")
        self.sigma_n = float(sigma_n)
        self.strength = float(strength)          # a:校正强度,a=0 退化为普通近端步
        self.omega_cap = float(omega_cap)        # ω_t·w_max(t) 的上限,论文取 1.9(阈值 2)
        self.sigma_max = sigma_max               # H 的最大奇异值;给定则跳过幂迭代
        self.power_iters = int(power_iters)
        self.eps = float(eps)
        self.last_alpha: Optional[torch.Tensor] = None
        self.last_omega: Optional[torch.Tensor] = None

    # ---------------------------------------------------------------- helpers
    @staticmethod
    def _as_broadcast(t, ref: torch.Tensor) -> torch.Tensor:
        """把时间 t 变成可与 (B, *d) 广播的形状。"""
        tt = torch.as_tensor(t, dtype=ref.dtype, device=ref.device)
        if tt.dim() == 0:
            tt = tt.expand(ref.shape[0])
        return tt.reshape((tt.shape[0],) + (1,) * (ref.dim() - 1))

    def _spectral_norm(self, x: torch.Tensor, h_op, ht_op) -> torch.Tensor:
        """无 SVD 时用幂迭代估计 ||H||_2,形状 (B,1,...,1)。"""
        if self.sigma_max is not None:
            return torch.as_tensor(self.sigma_max, dtype=x.dtype, device=x.device)
        flat = lambda z: z.reshape(z.shape[0], -1)
        z = torch.randn_like(x)
        z = z / flat(z).norm(dim=1).clamp_min(self.eps).view(-1, *([1] * (x.dim() - 1)))
        for _ in range(self.power_iters):
            z = ht_op(h_op(z))
            z = z / flat(z).norm(dim=1).clamp_min(self.eps).view(-1, *([1] * (x.dim() - 1)))
        hz = flat(h_op(z)).norm(dim=1)           # z 已归一化,故 σ_max ≈ ||H z||
        # 注:Rayleigh 商略小于真值,故上限用 1.9 而非阈值 2,为估计误差留余量
        return hz.clamp_min(self.eps).view(-1, *([1] * (x.dim() - 1)))

    # ----------------------------------------------------------------- forward
    def forward(
        self,
        x_t: torch.Tensor,
        v_t: torch.Tensor,
        y: torch.Tensor,
        t,
        h_op: Callable[[torch.Tensor], torch.Tensor],
        ht_op: Callable[[torch.Tensor], torch.Tensor],
        sigma_max: Optional[float] = None,
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        """返回 (x1, omega):修正后的端点 x̂_1 (B,*d) 与过松弛系数 ω_t (B,1,...,1)。"""
        if x_t.shape != v_t.shape:
            raise ValueError("x_t and v_t must share the same shape")
        t_b = self._as_broadcast(t, x_t)
        one_m_t = 1.0 - t_b                       # (B,1,..) 1-t

        # 1) 速度网络给出的预测端点 m_{θ,t} = x_t + (1-t) v_θ(式 6)
        m = x_t + one_m_t * v_t                   # (B,*d)
        residual = y - h_op(m)                    # (B,m)

        # 2) 继承 Flower 的桥方差近端权重 λ_t = r_t = (1-t)²/(t²+(1-t)²)
        lam = one_m_t ** 2 / (t_b ** 2 + one_m_t ** 2).clamp_min(self.eps)

        # 3) 闭式上限:ω_t w_max(t) ≤ omega_cap,其中 w_max = λσ_max²/(λσ_max²+σ_n²)
        smax = (
            self._spectral_norm(x_t, h_op, ht_op)
            if sigma_max is None
            else torch.as_tensor(sigma_max, dtype=x_t.dtype, device=x_t.device)
        )
        gain = lam * smax ** 2 / (lam * smax ** 2 + self.sigma_n ** 2)
        a_bar = (self.omega_cap / gain.clamp_min(self.eps) - 1.0) / one_m_t.clamp_min(self.eps)
        a_eff = torch.minimum(torch.full_like(a_bar, self.strength), a_bar).clamp_min(0.0)

        # 4) v_corr = v_θ + α_t Hᵀ(y - H m_{θ,t})/σ_n²,α_t = min{a, ā_t}·r_t(式 11/17)
        alpha = a_eff * lam                       # (B,1,..)
        omega = 1.0 + one_m_t * a_eff             # (B,1,..)
        v_corr = v_t + alpha * ht_op(residual) / self.sigma_n ** 2
        x1 = x_t + one_m_t * v_corr               # (B,*d)

        self.last_alpha, self.last_omega = alpha.detach(), omega.detach()
        return x1, omega


if __name__ == "__main__":
    sys.stdout.reconfigure(encoding="utf-8")
    torch.manual_seed(0)
    B, C, H, W = 2, 1, 16, 16
    t = 0.8

    # 最小线性算子:随机掩码采样 H x = mask ⊙ x(σ_max = 1),伴随为掩码回填
    mask = (torch.rand(1, C, H, W) > 0.5).float()
    h_op = lambda z: (z * mask).flatten(1)
    ht_op = lambda r: (r.view(B, C, H, W) * mask)

    x_t = torch.randn(B, C, H, W)
    v_t = torch.randn(B, C, H, W)
    y = h_op(torch.randn(B, C, H, W))

    model = PosteriorVelocityCorrection(sigma_n=0.05, strength=1.0, sigma_max=1.0)
    x1, omega = model(x_t, v_t, y, t, h_op, ht_op)

    print("input  x_t:", tuple(x_t.shape), " v_t:", tuple(v_t.shape), " y:", tuple(y.shape))
    print("output x1 :", tuple(x1.shape), " omega:", tuple(omega.shape))
    print("omega range:", float(omega.min()), float(omega.max()), " alpha:", tuple(model.last_alpha.shape))

    # 逐 batch 时间 t 也应可用;强校正 (a=100) 时由闭式上限 cap 住 ω_t
    t_batch = torch.tensor([0.2, 0.9])
    x1b, omb = model(x_t, v_t, y, t_batch, h_op, ht_op)
    print("batch-t  x1:", tuple(x1b.shape), " omega:", omb.flatten().tolist())
    strong = PosteriorVelocityCorrection(sigma_n=0.05, strength=100.0, sigma_max=1.0)
    _, om_cap = strong(x_t, v_t, y, t_batch, h_op, ht_op)
    print("capped omega (a=100):", [round(v, 3) for v in om_cap.flatten().tolist()])
    assert torch.isfinite(x1).all() and torch.isfinite(x1b).all()
    print("shape check passed")

环境与依赖

Python 3.9+

继续浏览

下一篇QSHP 乘积博弈归因利用乘积结构把归因化为一维多项式积分,以高斯求积配合对数前后缀扫描共享计算。首页使用原论文完整算法框,不是网络架构图;理论误差证书只控制精确算术下的求积误差。

相关资源