<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>https://wiki.swarma.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=%E9%BB%84%E4%B8%80%E5%87%A1</id>
	<title>集智百科 - 复杂系统|人工智能|复杂科学|复杂网络|自组织 - 用户贡献 [zh-cn]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.swarma.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=%E9%BB%84%E4%B8%80%E5%87%A1"/>
	<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php/%E7%89%B9%E6%AE%8A:%E7%94%A8%E6%88%B7%E8%B4%A1%E7%8C%AE/%E9%BB%84%E4%B8%80%E5%87%A1"/>
	<updated>2026-11-02T17:13:50Z</updated>
	<subtitle>用户贡献</subtitle>
	<generator>MediaWiki 1.35.0</generator>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50903</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50903"/>
		<updated>2026-09-07T11:12:46Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：修复部分图片显示问题&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''在标准化流的谱系中，RealNVP 属于[[耦合流]]（coupling flow）分支，与 NICE、Glow 同属一支，三者构成&amp;quot;加性耦合→仿射耦合→引入可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积&amp;quot;的演进序列。RealNVP 的贡献也体现在两个层面：作为完整模型，它是标准化耦合流这一派的代表作；而其标志性的'''[[仿射耦合层]]'''又常被单独取出，作为通用可逆模块应用于标准化流之外的场合（见&amp;quot;应用&amp;quot;一节）。&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的提出是沿着一条从数学工具到建模范式、再到具体架构的线索逐步成形的：先有密度估计的建模需求与变量替换公式这一数学工具，再有标准化流框架的正式提出，最后才落地为 RealNVP 等可训练的深度模型。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
深度生成建模的一个核心需求，是既能精确计算数据的概率密度，又能从模型中高效采样。满足这一需求的数学工具是概率论中的[[变量替换公式]]：若变换可逆，变换前后的密度便由雅可比行列式精确联系。用可逆变换把复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布的尝试由来已久——从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，都是这一思路的先驱。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
这一思路被正式提炼为建模范式是在 2010 年：Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;：通过一系列可微可逆映射的复合，让'''基分布'''（base distribution，变换起点的简单分布，通常取标准高斯；与变换要逼近的目标分布相对）像水流一样&amp;quot;流动&amp;quot;成目标分布，密度则由变量替换公式精确给出。2015 年，Rezende 与 Mohamed 将这一框架引入变分推断&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot; /&amp;gt;，&amp;quot;标准化流&amp;quot;一词由此流行。作为一种建模范式，标准化流只规定变换必须可逆、基分布必须指定、密度由变量替换公式计算，而不限定变换的具体结构——这为不同的实现方案留出了空间。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
最早把这一框架落到实处的是 2014 年 Dinh 等人提出的 '''NICE'''（Non-linear Independent Components Estimation）&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2015|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 在 NICE 的基础上将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的通用逼近器（universal approximator），而仿射耦合流在一定的良好条件下'''则是'''&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。除耦合流一支外，标准化流还有自回归流、连续流等其他实现路线（见&amp;quot;与相关框架的关系&amp;quot;一节）；RealNVP 是耦合流支线上的关键一环。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
值得一提的是，RealNVP 原论文通篇并未自称&amp;quot;标准化流&amp;quot;——该词在全文中仅出现于参考文献的标题里，作者当时将模型定位为基于[[变量替换公式]]、可精确计算[[似然]]的概率模型。将 RealNVP 正式归入标准化流大类下的耦合流分支，是由后来的综述文献确立的分类&amp;lt;ref name=&amp;quot;Kobyzev2021&amp;quot;&amp;gt;{{cite journal|author1=Kobyzev I.|author2=Prince S. J. D.|author3=Brubaker M. A.|title=Normalizing Flows: An Introduction and Review of Current Methods|journal=IEEE Transactions on Pattern Analysis and Machine Intelligence|year=2021|volume=43|issue=11|pages=3964-3979|url=https://arxiv.org/abs/1908.09257}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
= 模型架构 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类[[标准化流]]。标准化流是一类通过最大似然来估计分布的模型，其构造包含三个要素：一个可微的可逆映射、一个指定的基分布，以及由[[变量替换公式]]给出的精确密度。具体地，RealNVP 将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到基分布——通常取标准正态——&amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体[[对数似然]] &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''[[仿射耦合层]]'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''[[批量归一化]]'''（batch normalization）以稳定训练；'''[[压缩]]'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''[[变量分解]]'''（factor out）负责在每个尺度末尾抽取变量作为潜在变量。其中，仿射耦合层通过两种掩码实现输入的对半划分：'''空间[[棋盘掩码]]'''（checkerboard masking）像国际象棋棋盘一样按像素坐标的奇偶性把像素分成两组，'''[[通道级掩码]]'''（channel-wise masking）则按通道把变量分成前后两半；二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
RealNVP 的多尺度架构如下图所示。输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第 1 尺度。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（图中标为 3 × (Coupling (checkerboard) → BN)；凡是耦合层，输出后均接批量[[归一化]]（即块内的 BN），下文不再重复）、压缩块（图中 Squeeze）、再 3 个通道级掩码的耦合层（图中 3 × (Coupling (channel-wise) → BN)），最后进行变量分解（图中 Factor Out）；末尺度则只经过 4 个空间棋盘掩码耦合层（图中 4 × (Coupling (checkerboard) → BN)），随后把全部变量抽出作为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;，不再保留传递变量。非末尺度的变量分解把当前特征一分为二：一半作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接（图中 Concatenation）成完整潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;，其中末尺度因全部抽出而与其前一尺度等长），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|替代=|居中|缩略图|1098x1098像素|RealNVP 多尺度分层架构示意图。每个尺度（Scale）内数据自上而下流动，尺度间自左向右推进。图中 Coupling 即正文的仿射耦合层（括号内 checkerboard、channel-wise 分别为棋盘掩码与通道级掩码），块内&amp;quot;→ BN&amp;quot;表示每个耦合层输出后接批量归一化；Squeeze 为压缩，Factor Out 为变量分解，Concatenation 为拼接；h⁽ⁱ⁾、z⁽ⁱ⁾ 与正文符号一致。]]&lt;br /&gt;
&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
下图给出两个交替堆叠的仿射耦合层的前向流程。仿射耦合层是 RealNVP 的基本可逆变换单元；图中左半部分的符号与公式 (1) 一一对应。&lt;br /&gt;
[[文件:仿射耦合.png|替代=|缩略图|635x635像素|两个交替堆叠的仿射耦合层（正向，从左到右）：左半与公式 (1) 一一对应——&amp;lt;math&amp;gt;\mathbf{x}_1&amp;lt;/math&amp;gt; 直通（即 &amp;lt;math&amp;gt;\mathbf{y}_1=\mathbf{x}_1&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;\mathbf{x}_2&amp;lt;/math&amp;gt; 经 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 给出的缩放与平移得到 &amp;lt;math&amp;gt;\mathbf{y}_2&amp;lt;/math&amp;gt;；右半为下一耦合层（网络记为 &amp;lt;math&amp;gt;\mathbf{s}',\mathbf{t}'&amp;lt;/math&amp;gt;），两半角色互换，输出 &amp;lt;math&amp;gt;\mathbf{y}'&amp;lt;/math&amp;gt;。反向（从右到左）阅读即公式 (2) 的逆变换。]]&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_1 = \mathbf{x}_1 \\[6pt]&lt;br /&gt;
\mathbf{y}_2 = \mathbf{x}_2 \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_1)\bigr) + \mathbf{t}(\mathbf{x}_1)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构；图像任务中通常用[[卷积神经网络]]（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。其中 &amp;lt;math&amp;gt;\mathbf{x}_1&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{x}_2&amp;lt;/math&amp;gt; 并非第 1、2 个坐标，而是由掩码划分出的两个'''子向量'''：&amp;lt;math&amp;gt;\mathbf{x}_1&amp;lt;/math&amp;gt; 是保持不变的被动部分，&amp;lt;math&amp;gt;\mathbf{x}_2&amp;lt;/math&amp;gt; 是被变换的主动部分。通道级掩码下二者即前、后两半通道，棋盘掩码下则是按坐标奇偶性分出的两组像素；具体规则见下文&amp;quot;两种掩码分区模式&amp;quot;。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的[[雅可比矩阵]]是下三角块结构，行列式即为对角线元素的乘积——一般的 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维矩阵求行列式需要 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 的计算量，而这里只需把对角元相乘，[[计算复杂度]]降为 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_1 = \mathbf{y}_1 \\[6pt]&lt;br /&gt;
\mathbf{x}_2 = \bigl(\mathbf{y}_2 - \mathbf{t}(\mathbf{y}_1)\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_1)\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 两种掩码分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分：保持不变的一半称为'''被动'''部分，被变换的一半称为'''主动'''部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠使用不同掩码的耦合层，每个维度都能在不同层中分别充当被动与主动变量，信息得以充分混合。原论文的多尺度架构中，每个尺度内同一掩码的耦合层固定为 3 层（末尺度为 4 层）&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
== [[多尺度架构]]：分层打包[[潜在变量]] ==&lt;br /&gt;
​&lt;br /&gt;
如上图所示，多尺度架构依赖两个关键操作：[[压缩]]与[[变量分解]]。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在[[通道方向]]上重排，将 &amp;lt;math&amp;gt;h\times w\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;h/2 \times w/2 \times 4c&amp;lt;/math&amp;gt;。[[空间分辨率]]减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个非末尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度；末尺度则将全部变量提取为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''[[层次化表示]]'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''[[梯度分散]]'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
== [[批量归一化]]：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量[[归一化]]（Batch Normalization，BN）应用于每个[[耦合层]]的输出之后。其映射为&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;，&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\tilde{\boldsymbol{\mu}}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\tilde{\boldsymbol{\sigma}}&amp;lt;/math&amp;gt; 为均值与标准差——训练时按当前批次估计，推理时改用滑动平均（原因见下文）。由于 BN 对每个维度独立地缩放和平移，它本身也是一个可逆变换：其雅可比矩阵为对角阵，[[雅可比行列式]]即各维度缩放因子的乘积，可直接加入[[对数似然]]——这使 BN 可以像耦合层一样作为流中的一层。引入 BN 主要是为了稳定深层训练：它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。使用 BN 有一个容易忽略的细节：训练时均值与方差是从当前这一批样本中估计的，因此单个样本的输出会依赖同批次的其他样本——此时的 BN 并不是严格意义上逐样本的可逆变换，其行列式也与整个批量耦合。只有在推理阶段改用训练过程中滑动平均得到的固定统计量，BN 才成为逐样本的、严格可逆的仿射变换，其行列式才能精确计入对数似然。换言之，用训练好的模型评估密度时若忘了做这一切换，同一个输入会因同批样本不同而得到不同的密度值，结果既不可复现也不正确。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的数学原理可分为两个层次。其一是[[标准化流]]的'''通用框架'''：[[变量替换公式]]给出精确似然的表达式，[[最大似然估计]]提供直接的训练目标，无需近似下界或辅助网络——这些结论对一切流模型成立，并非 RealNVP 特有。其二是 RealNVP 的'''特有设计'''：通用框架要求变换可逆且雅可比行列式可算，[[仿射耦合层]]的三角雅可比结构正是为满足这一要求而构造的，它使行列式计算开销与维度成线性关系，从而保证可处理性。本节先介绍通用框架，再阐述 RealNVP 的特化实现，进而比较[[加性耦合]]与仿射耦合在体积保持上的差异，最后讨论训练目标中两项各自的含义与实际训练配置。&lt;br /&gt;
​&lt;br /&gt;
== [[标准化流]]与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数[[雅可比行列式]]）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于[[自回归模型]]（autoregressive model，如 PixelRNN）：这类模型按固定顺序逐维生成数据、每个维度以先前各维为条件，采样必须串行进行——这与流模型对所有维度同时变换形成鲜明对比。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
上一小节的结论适用于一切标准化流。RealNVP 要回答的问题是：如何构造一个既完全可逆、又满足行列式易算的变换？其答案就是仿射耦合层——它对被动半 &amp;lt;math&amp;gt;\mathbf{x}_1&amp;lt;/math&amp;gt; 进行恒等映射，对主动半 &amp;lt;math&amp;gt;\mathbf{x}_2&amp;lt;/math&amp;gt; 进行仿射映射。记被动半的维度为 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt;（主动半的维度为 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt;），则雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_2}{\partial \mathbf{x}_1^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_1)]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_1)\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度网络（如深层卷积网络、残差网络），而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
对公式 (1) 做简单的代数移项，即可得到公式 (2) 给出的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练（这也是标准化流的通用训练方式），等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的分布整体逼近标准高斯，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：增大体积膨胀会提高似然，因此该项处处偏好扩张，与先验项的收缩压力相平衡——净效果是数据密集处扩张、稀疏处收缩，这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布（即数据分布经 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 映射到潜在空间后得到的分布）与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：[[自适应矩估计]]（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt;（原论文取 &amp;lt;math&amp;gt;\alpha = 0.05&amp;lt;/math&amp;gt;）处理，以缓解像素取值集中在 0 和 255 两个边界带来的问题。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的应用对应着它的两个层面：作为完整的标准化流模型，它直接服务于[[密度估计]]、生成、[[异常检测]]与重建推断等任务；作为通用可逆部件，其[[仿射耦合层]]凭借解析可逆、雅可比行列式易算的性质，也被用于概率建模之外的场合。下文按此顺序介绍。&lt;br /&gt;
​&lt;br /&gt;
== 自然图像[[密度估计]]与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（见下图）。与[[变分自编码器]]（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等[[自回归模型]]相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png|替代=|居中]]​&lt;br /&gt;
== [[异常检测]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时将似然显著低于正常水平的样本判为异常。也可以把流与重建类模型结合起来，综合似然与[[重建误差]]构造异常分数——下文的 AE-FLOW 即属此类。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。AE-FLOW 将自编码器与以 RealNVP [[仿射耦合层]]为核心的[[标准化流]]瓶颈相结合，在胸部 X 光、光学相干断层扫描、皮肤镜与脑部核磁共振等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的[[负对数似然]]构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
下图展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。|替代=|360x360px|居中]]​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合[[图像修复]]、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（见下图）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png|替代=|居中]]&lt;br /&gt;
​&lt;br /&gt;
== 在 [[NIS]]/[[NIS+]] 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus）中，RealNVP 的'''基础仿射耦合模块'''被用于构建[[可逆神经网络]]（Invertible Neural Networks，INN）编码器——堆叠 3 层，&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 为小型前馈网络——实现微观状态到宏观状态的可逆[[粗粒化]]。NIS 原文的表述本身即体现了范畴与实例的关系：&amp;quot;There are several ways to realize an invertible neural network ... we select RealNVP module ... to concretely implement the invertible computation.&amp;quot;（实现可逆神经网络有多种方式，本文选择 RealNVP 模块来具体实现可逆计算。）&amp;lt;ref name=&amp;quot;NIS2023&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2023|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;需要指出，此处复用的是部件而非完整模型：不含多尺度架构与高斯基分布，也不以似然为训练目标；同类的部件级复用还有 RevNet 的[[可逆残差结构]]&amp;lt;ref name=&amp;quot;RevNet2017&amp;quot; /&amp;gt;与 HiNet 的图像隐藏网络&amp;lt;ref name=&amp;quot;HiNet2021&amp;quot;&amp;gt;{{cite conference|author1=Jing J.|author2=Deng X.|author3=Xu M.|author4=Wang J.|author5=Guan Z.|title=HiNet: Deep Image Hiding by Invertible Network|conference=International Conference on Computer Vision (ICCV)|year=2021}}&amp;lt;/ref&amp;gt;。NIS 中的粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的 INN &amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''——相较于使用独立编码器和解码器的方案，参数量减少一半，如下图所示。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器与解码器（图中记作 φ 与 φ†，分别对应正文中的 &amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt;），信息丢弃与 EI 最大化在宏观动力学空间完成。|替代=|812x812px|居中]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2023&amp;quot; /&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使[[有效信息]]（[[Effective Information]]，[[EI]]）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
RealNVP作为NIS+中可逆表示学习模块，使粗粒化映射与反粗粒化映射共享参数，降低了模型复杂度，同时为有效信息最大化提供了可分析的数学框架。此外，基于可逆粗粒化结构构建的encoder具有通用逼近性质，能够表达不同复杂系统中的粗粒化映射。&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''[[精确似然]]'''：RealNVP 能直接算出任意数据点的精确概率密度——不像 VAE 只能优化似然的一个下界，也不像[[生成对抗网络]]（Generative Adversarial Network，GAN）完全不给密度。因此[[异常检测]]、[[贝叶斯推断]]等需要显式概率值的任务可以直接使用它&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''并行采样与推断'''：[[自回归模型]]必须逐像素按顺序生成，而 RealNVP 编码和解码时所有维度同时计算，生成一张图像只需一次前向传播，采样速度快得多。&lt;br /&gt;
* '''语义[[潜在空间]]'''：由于潜在分布是简单的标准高斯、且映射光滑可逆，在潜在空间中做插值等简单操作，就对应图像上有语义的连续变化（如人脸姿态、表情的渐变），适合表示学习与属性操控&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''架构灵活'''：[[耦合层]]只要求 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 两个网络给出缩放和平移量，对它们的内部结构没有任何限制，可以是任意深度的[[卷积神经网络]]（Convolutional Neural Network，CNN）或[[残差网络]]（Residual Network，ResNet）。提升表达能力可以直接借用主流的深层网络设计，而无须改动可逆框架本身。&lt;br /&gt;
* '''综合多种模型特性'''：RealNVP 同时具备了此前难以兼得的能力——自回归模型的精确似然、VAE 的一步采样，以及不依赖固定重建损失的生成质量。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：RealNVP 对数据分布的拟合精度仍略逊于 PixelRNN 等自回归模型&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在以拟合精度为核心的任务（如[[密度估计]]、[[数据压缩]]）上，这是它的相对劣势——尽管它以并行采样换来了远快于对手的生成速度。&lt;br /&gt;
* '''维度保持约束'''：可逆变换要求输入与输出同维，因此 RealNVP 不能像自编码器那样把高维图像压缩成低维向量——潜在变量的总维度始终与输入相同。[[多尺度架构]]中的变量分解并不改变这一点，它只是让后续层处理更小的特征图，并把潜在变量分配到不同尺度。&lt;br /&gt;
* '''[[仿射耦合]]的表达力上限'''：仿射耦合每层只对一半变量做&amp;quot;缩放加平移&amp;quot;，函数形式较为简单，拟合复杂分布往往需要堆叠很多层。后续工作把变换换成更灵活的函数形式——如 Flow++ 的 Logistic 混合 CDF 耦合&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;、Neural Spline Flows 的[[有理二次样条耦合]]&amp;lt;ref name=&amp;quot;NSF2019&amp;quot;&amp;gt;{{cite journal|author1=Durkan C.|author2=Bekasov A.|author3=Murray I.|author4=Papamakarios G.|title=Neural Spline Flows|journal=Advances in Neural Information Processing Systems (NeurIPS)|year=2019|volume=32|url=https://arxiv.org/abs/1906.04032}}&amp;lt;/ref&amp;gt;——用更少的层数即可达到更强的拟合能力。&lt;br /&gt;
* '''[[均匀去量化]]的次优性'''：像素是 0–255 的离散整数，而 RealNVP 建模的是连续分布。RealNVP 的做法是给像素加上均匀噪声，把离散数据&amp;quot;抹平&amp;quot;成连续的——但这相当于让模型拟合一个被噪声模糊过的分布，与真实分布存在偏差。Flow++ 提出的[[变分去量化]]让噪声分布本身也由模型学习，缓解了这一问题&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''分区固定性'''：掩码决定哪一半变量被变换，而这一划分在训练前就已固定、训练中不再改变；若某层的划分不理想，只能靠后续层弥补。Glow（Generative Flow with Invertible 1×1 Convolutions）改用可学习的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积，让模型自己学会如何重排变量，划分更灵活，生成效果也更好&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''训练显存开销大'''：标准反向传播需要保存每个耦合层的激活值，层数一多显存占用显著上升；RevNet 的[[可逆残差结构]]正是为省去这部分存储而提出的&amp;lt;ref name=&amp;quot;RevNet2017&amp;quot;&amp;gt;{{cite journal|author1=Gomez A. N.|author2=Ren M.|author3=Urtasun R.|author4=Grosse R. B.|title=The Reversible Residual Network: Backpropagation Without Storing Activations|journal=NeurIPS|year=2017|url=https://arxiv.org/abs/1707.04585}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''对分布外样本过度自信'''：上文的异常检测应用依赖&amp;quot;异常样本的似然更低&amp;quot;这一假设，但后续研究发现这一假设未必成立：例如在 CIFAR-10 上训练的 RealNVP，会给截然不同的 SVHN 街景数字图像赋予'''更高'''的似然&amp;lt;ref name=&amp;quot;Nalisnick2019&amp;quot;&amp;gt;{{cite journal|author1=Nalisnick E.|author2=Matsukawa A.|author3=Teh Y. W.|author4=Gorur D.|author5=Lakshminarayanan B.|title=Do Deep Generative Models Know What They Don't Know?|journal=International Conference on Learning Representations (ICLR)|year=2019|url=https://arxiv.org/abs/1810.09136}}&amp;lt;/ref&amp;gt;。似然高低与&amp;quot;是否属于训练分布&amp;quot;并不直接对应，因此基于似然的异常检测需要额外的校准手段。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 与相关框架的关系 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置。下表列举前驱与后继的代表性工作：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=参考文献=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50902</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50902"/>
		<updated>2026-09-07T11:07:45Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：优化历史渊源部分语言、修复realnvp_multiscale.png 图片问题&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''在标准化流的谱系中，RealNVP 属于[[耦合流]]（coupling flow）分支，与 NICE、Glow 同属一支，三者构成&amp;quot;加性耦合→仿射耦合→引入可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积&amp;quot;的演进序列。RealNVP 的贡献也体现在两个层面：作为完整模型，它是标准化耦合流这一派的代表作；而其标志性的'''[[仿射耦合层]]'''又常被单独取出，作为通用可逆模块应用于标准化流之外的场合（见&amp;quot;应用&amp;quot;一节）。&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的提出是沿着一条从数学工具到建模范式、再到具体架构的线索逐步成形的：先有密度估计的建模需求与变量替换公式这一数学工具，再有标准化流框架的正式提出，最后才落地为 RealNVP 等可训练的深度模型。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
深度生成建模的一个核心需求，是既能精确计算数据的概率密度，又能从模型中高效采样。满足这一需求的数学工具是概率论中的[[变量替换公式]]：若变换可逆，变换前后的密度便由雅可比行列式精确联系。用可逆变换把复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布的尝试由来已久——从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，都是这一思路的先驱。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
这一思路被正式提炼为建模范式是在 2010 年：Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;：通过一系列可微可逆映射的复合，让'''基分布'''（base distribution，变换起点的简单分布，通常取标准高斯；与变换要逼近的目标分布相对）像水流一样&amp;quot;流动&amp;quot;成目标分布，密度则由变量替换公式精确给出。2015 年，Rezende 与 Mohamed 将这一框架引入变分推断&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot; /&amp;gt;，&amp;quot;标准化流&amp;quot;一词由此流行。作为一种建模范式，标准化流只规定变换必须可逆、基分布必须指定、密度由变量替换公式计算，而不限定变换的具体结构——这为不同的实现方案留出了空间。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
最早把这一框架落到实处的是 2014 年 Dinh 等人提出的 '''NICE'''（Non-linear Independent Components Estimation）&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2015|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 在 NICE 的基础上将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的通用逼近器（universal approximator），而仿射耦合流在一定的良好条件下'''则是'''&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。除耦合流一支外，标准化流还有自回归流、连续流等其他实现路线（见&amp;quot;与相关框架的关系&amp;quot;一节）；RealNVP 是耦合流支线上的关键一环。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
值得一提的是，RealNVP 原论文通篇并未自称&amp;quot;标准化流&amp;quot;——该词在全文中仅出现于参考文献的标题里，作者当时将模型定位为基于[[变量替换公式]]、可精确计算[[似然]]的概率模型。将 RealNVP 正式归入标准化流大类下的耦合流分支，是由后来的综述文献确立的分类&amp;lt;ref name=&amp;quot;Kobyzev2021&amp;quot;&amp;gt;{{cite journal|author1=Kobyzev I.|author2=Prince S. J. D.|author3=Brubaker M. A.|title=Normalizing Flows: An Introduction and Review of Current Methods|journal=IEEE Transactions on Pattern Analysis and Machine Intelligence|year=2021|volume=43|issue=11|pages=3964-3979|url=https://arxiv.org/abs/1908.09257}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
= 模型架构 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类[[标准化流]]。标准化流是一类通过最大似然来估计分布的模型，其构造包含三个要素：一个可微的可逆映射、一个指定的基分布，以及由[[变量替换公式]]给出的精确密度。具体地，RealNVP 将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到基分布——通常取标准正态——&amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体[[对数似然]] &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''[[仿射耦合层]]'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''[[批量归一化]]'''（batch normalization）以稳定训练；'''[[压缩]]'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''[[变量分解]]'''（factor out）负责在每个尺度末尾抽取变量作为潜在变量。其中，仿射耦合层通过两种掩码实现输入的对半划分：'''空间[[棋盘掩码]]'''（checkerboard masking）像国际象棋棋盘一样按像素坐标的奇偶性把像素分成两组，'''[[通道级掩码]]'''（channel-wise masking）则按通道把变量分成前后两半；二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
RealNVP 的多尺度架构如下图所示。输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第 1 尺度。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（图中标为 3 × (Coupling (checkerboard) → BN)；凡是耦合层，输出后均接批量[[归一化]]（即块内的 BN），下文不再重复）、压缩块（图中 Squeeze）、再 3 个通道级掩码的耦合层（图中 3 × (Coupling (channel-wise) → BN)），最后进行变量分解（图中 Factor Out）；末尺度则只经过 4 个空间棋盘掩码耦合层（图中 4 × (Coupling (checkerboard) → BN)），随后把全部变量抽出作为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;，不再保留传递变量。非末尺度的变量分解把当前特征一分为二：一半作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接（图中 Concatenation）成完整潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;，其中末尺度因全部抽出而与其前一尺度等长），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_multiscale.png|缩略图|RealNVP 多尺度分层架构示意图。每个尺度（Scale）内数据自上而下流动，尺度间自左向右推进。图中 Coupling 即正文的仿射耦合层（括号内 checkerboard、channel-wise 分别为棋盘掩码与通道级掩码），块内&amp;quot;→ BN&amp;quot;表示每个耦合层输出后接批量归一化；Squeeze 为压缩，Factor Out 为变量分解，Concatenation 为拼接；h⁽ⁱ⁾、z⁽ⁱ⁾ 与正文符号一致。|替代=|848x848px|居中]]&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
下图给出两个交替堆叠的仿射耦合层的前向流程。仿射耦合层是 RealNVP 的基本可逆变换单元；图中左半部分的符号与公式 (1) 一一对应。[[文件:仿射耦合_公式对齐.png|缩略图|两个交替堆叠的仿射耦合层（正向，从左到右）：左半与公式 (1) 一一对应——&amp;lt;math&amp;gt;\mathbf{x}_1&amp;lt;/math&amp;gt; 直通（即 &amp;lt;math&amp;gt;\mathbf{y}_1=\mathbf{x}_1&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;\mathbf{x}_2&amp;lt;/math&amp;gt; 经 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 给出的缩放与平移得到 &amp;lt;math&amp;gt;\mathbf{y}_2&amp;lt;/math&amp;gt;；右半为下一耦合层（网络记为 &amp;lt;math&amp;gt;\mathbf{s}',\mathbf{t}'&amp;lt;/math&amp;gt;），两半角色互换，输出 &amp;lt;math&amp;gt;\mathbf{y}'&amp;lt;/math&amp;gt;。反向（从右到左）阅读即公式 (2) 的逆变换。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_1 = \mathbf{x}_1 \\[6pt]&lt;br /&gt;
\mathbf{y}_2 = \mathbf{x}_2 \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_1)\bigr) + \mathbf{t}(\mathbf{x}_1)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构；图像任务中通常用[[卷积神经网络]]（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。其中 &amp;lt;math&amp;gt;\mathbf{x}_1&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{x}_2&amp;lt;/math&amp;gt; 并非第 1、2 个坐标，而是由掩码划分出的两个'''子向量'''：&amp;lt;math&amp;gt;\mathbf{x}_1&amp;lt;/math&amp;gt; 是保持不变的被动部分，&amp;lt;math&amp;gt;\mathbf{x}_2&amp;lt;/math&amp;gt; 是被变换的主动部分。通道级掩码下二者即前、后两半通道，棋盘掩码下则是按坐标奇偶性分出的两组像素；具体规则见下文&amp;quot;两种掩码分区模式&amp;quot;。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的[[雅可比矩阵]]是下三角块结构，行列式即为对角线元素的乘积——一般的 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维矩阵求行列式需要 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 的计算量，而这里只需把对角元相乘，[[计算复杂度]]降为 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_1 = \mathbf{y}_1 \\[6pt]&lt;br /&gt;
\mathbf{x}_2 = \bigl(\mathbf{y}_2 - \mathbf{t}(\mathbf{y}_1)\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_1)\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 两种掩码分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分：保持不变的一半称为'''被动'''部分，被变换的一半称为'''主动'''部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠使用不同掩码的耦合层，每个维度都能在不同层中分别充当被动与主动变量，信息得以充分混合。原论文的多尺度架构中，每个尺度内同一掩码的耦合层固定为 3 层（末尺度为 4 层）&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
== [[多尺度架构]]：分层打包[[潜在变量]] ==&lt;br /&gt;
​&lt;br /&gt;
如上图所示，多尺度架构依赖两个关键操作：[[压缩]]与[[变量分解]]。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在[[通道方向]]上重排，将 &amp;lt;math&amp;gt;h\times w\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;h/2 \times w/2 \times 4c&amp;lt;/math&amp;gt;。[[空间分辨率]]减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个非末尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度；末尺度则将全部变量提取为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''[[层次化表示]]'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''[[梯度分散]]'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
== [[批量归一化]]：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量[[归一化]]（Batch Normalization，BN）应用于每个[[耦合层]]的输出之后。其映射为&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;，&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\tilde{\boldsymbol{\mu}}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\tilde{\boldsymbol{\sigma}}&amp;lt;/math&amp;gt; 为均值与标准差——训练时按当前批次估计，推理时改用滑动平均（原因见下文）。由于 BN 对每个维度独立地缩放和平移，它本身也是一个可逆变换：其雅可比矩阵为对角阵，[[雅可比行列式]]即各维度缩放因子的乘积，可直接加入[[对数似然]]——这使 BN 可以像耦合层一样作为流中的一层。引入 BN 主要是为了稳定深层训练：它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。使用 BN 有一个容易忽略的细节：训练时均值与方差是从当前这一批样本中估计的，因此单个样本的输出会依赖同批次的其他样本——此时的 BN 并不是严格意义上逐样本的可逆变换，其行列式也与整个批量耦合。只有在推理阶段改用训练过程中滑动平均得到的固定统计量，BN 才成为逐样本的、严格可逆的仿射变换，其行列式才能精确计入对数似然。换言之，用训练好的模型评估密度时若忘了做这一切换，同一个输入会因同批样本不同而得到不同的密度值，结果既不可复现也不正确。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的数学原理可分为两个层次。其一是[[标准化流]]的'''通用框架'''：[[变量替换公式]]给出精确似然的表达式，[[最大似然估计]]提供直接的训练目标，无需近似下界或辅助网络——这些结论对一切流模型成立，并非 RealNVP 特有。其二是 RealNVP 的'''特有设计'''：通用框架要求变换可逆且雅可比行列式可算，[[仿射耦合层]]的三角雅可比结构正是为满足这一要求而构造的，它使行列式计算开销与维度成线性关系，从而保证可处理性。本节先介绍通用框架，再阐述 RealNVP 的特化实现，进而比较[[加性耦合]]与仿射耦合在体积保持上的差异，最后讨论训练目标中两项各自的含义与实际训练配置。&lt;br /&gt;
​&lt;br /&gt;
== [[标准化流]]与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数[[雅可比行列式]]）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于[[自回归模型]]（autoregressive model，如 PixelRNN）：这类模型按固定顺序逐维生成数据、每个维度以先前各维为条件，采样必须串行进行——这与流模型对所有维度同时变换形成鲜明对比。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
上一小节的结论适用于一切标准化流。RealNVP 要回答的问题是：如何构造一个既完全可逆、又满足行列式易算的变换？其答案就是仿射耦合层——它对被动半 &amp;lt;math&amp;gt;\mathbf{x}_1&amp;lt;/math&amp;gt; 进行恒等映射，对主动半 &amp;lt;math&amp;gt;\mathbf{x}_2&amp;lt;/math&amp;gt; 进行仿射映射。记被动半的维度为 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt;（主动半的维度为 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt;），则雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_2}{\partial \mathbf{x}_1^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_1)]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_1)\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度网络（如深层卷积网络、残差网络），而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
对公式 (1) 做简单的代数移项，即可得到公式 (2) 给出的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练（这也是标准化流的通用训练方式），等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的分布整体逼近标准高斯，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：增大体积膨胀会提高似然，因此该项处处偏好扩张，与先验项的收缩压力相平衡——净效果是数据密集处扩张、稀疏处收缩，这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布（即数据分布经 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 映射到潜在空间后得到的分布）与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：[[自适应矩估计]]（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt;（原论文取 &amp;lt;math&amp;gt;\alpha = 0.05&amp;lt;/math&amp;gt;）处理，以缓解像素取值集中在 0 和 255 两个边界带来的问题。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的应用对应着它的两个层面：作为完整的标准化流模型，它直接服务于[[密度估计]]、生成、[[异常检测]]与重建推断等任务；作为通用可逆部件，其[[仿射耦合层]]凭借解析可逆、雅可比行列式易算的性质，也被用于概率建模之外的场合。下文按此顺序介绍。&lt;br /&gt;
​&lt;br /&gt;
== 自然图像[[密度估计]]与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（见下图）。与[[变分自编码器]]（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等[[自回归模型]]相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png|替代=|居中]]​&lt;br /&gt;
== [[异常检测]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时将似然显著低于正常水平的样本判为异常。也可以把流与重建类模型结合起来，综合似然与[[重建误差]]构造异常分数——下文的 AE-FLOW 即属此类。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。AE-FLOW 将自编码器与以 RealNVP [[仿射耦合层]]为核心的[[标准化流]]瓶颈相结合，在胸部 X 光、光学相干断层扫描、皮肤镜与脑部核磁共振等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的[[负对数似然]]构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
下图展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。|替代=|360x360px|居中]]​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合[[图像修复]]、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（见下图）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png|替代=|居中]]&lt;br /&gt;
​&lt;br /&gt;
== 在 [[NIS]]/[[NIS+]] 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus）中，RealNVP 的'''基础仿射耦合模块'''被用于构建[[可逆神经网络]]（Invertible Neural Networks，INN）编码器——堆叠 3 层，&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 为小型前馈网络——实现微观状态到宏观状态的可逆[[粗粒化]]。NIS 原文的表述本身即体现了范畴与实例的关系：&amp;quot;There are several ways to realize an invertible neural network ... we select RealNVP module ... to concretely implement the invertible computation.&amp;quot;（实现可逆神经网络有多种方式，本文选择 RealNVP 模块来具体实现可逆计算。）&amp;lt;ref name=&amp;quot;NIS2023&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2023|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;需要指出，此处复用的是部件而非完整模型：不含多尺度架构与高斯基分布，也不以似然为训练目标；同类的部件级复用还有 RevNet 的[[可逆残差结构]]&amp;lt;ref name=&amp;quot;RevNet2017&amp;quot; /&amp;gt;与 HiNet 的图像隐藏网络&amp;lt;ref name=&amp;quot;HiNet2021&amp;quot;&amp;gt;{{cite conference|author1=Jing J.|author2=Deng X.|author3=Xu M.|author4=Wang J.|author5=Guan Z.|title=HiNet: Deep Image Hiding by Invertible Network|conference=International Conference on Computer Vision (ICCV)|year=2021}}&amp;lt;/ref&amp;gt;。NIS 中的粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的 INN &amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''——相较于使用独立编码器和解码器的方案，参数量减少一半，如下图所示。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器与解码器（图中记作 φ 与 φ†，分别对应正文中的 &amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt;），信息丢弃与 EI 最大化在宏观动力学空间完成。|替代=|812x812px|居中]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2023&amp;quot; /&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使[[有效信息]]（[[Effective Information]]，[[EI]]）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
RealNVP作为NIS+中可逆表示学习模块，使粗粒化映射与反粗粒化映射共享参数，降低了模型复杂度，同时为有效信息最大化提供了可分析的数学框架。此外，基于可逆粗粒化结构构建的encoder具有通用逼近性质，能够表达不同复杂系统中的粗粒化映射。&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''[[精确似然]]'''：RealNVP 能直接算出任意数据点的精确概率密度——不像 VAE 只能优化似然的一个下界，也不像[[生成对抗网络]]（Generative Adversarial Network，GAN）完全不给密度。因此[[异常检测]]、[[贝叶斯推断]]等需要显式概率值的任务可以直接使用它&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''并行采样与推断'''：[[自回归模型]]必须逐像素按顺序生成，而 RealNVP 编码和解码时所有维度同时计算，生成一张图像只需一次前向传播，采样速度快得多。&lt;br /&gt;
* '''语义[[潜在空间]]'''：由于潜在分布是简单的标准高斯、且映射光滑可逆，在潜在空间中做插值等简单操作，就对应图像上有语义的连续变化（如人脸姿态、表情的渐变），适合表示学习与属性操控&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''架构灵活'''：[[耦合层]]只要求 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 两个网络给出缩放和平移量，对它们的内部结构没有任何限制，可以是任意深度的[[卷积神经网络]]（Convolutional Neural Network，CNN）或[[残差网络]]（Residual Network，ResNet）。提升表达能力可以直接借用主流的深层网络设计，而无须改动可逆框架本身。&lt;br /&gt;
* '''综合多种模型特性'''：RealNVP 同时具备了此前难以兼得的能力——自回归模型的精确似然、VAE 的一步采样，以及不依赖固定重建损失的生成质量。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：RealNVP 对数据分布的拟合精度仍略逊于 PixelRNN 等自回归模型&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在以拟合精度为核心的任务（如[[密度估计]]、[[数据压缩]]）上，这是它的相对劣势——尽管它以并行采样换来了远快于对手的生成速度。&lt;br /&gt;
* '''维度保持约束'''：可逆变换要求输入与输出同维，因此 RealNVP 不能像自编码器那样把高维图像压缩成低维向量——潜在变量的总维度始终与输入相同。[[多尺度架构]]中的变量分解并不改变这一点，它只是让后续层处理更小的特征图，并把潜在变量分配到不同尺度。&lt;br /&gt;
* '''[[仿射耦合]]的表达力上限'''：仿射耦合每层只对一半变量做&amp;quot;缩放加平移&amp;quot;，函数形式较为简单，拟合复杂分布往往需要堆叠很多层。后续工作把变换换成更灵活的函数形式——如 Flow++ 的 Logistic 混合 CDF 耦合&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;、Neural Spline Flows 的[[有理二次样条耦合]]&amp;lt;ref name=&amp;quot;NSF2019&amp;quot;&amp;gt;{{cite journal|author1=Durkan C.|author2=Bekasov A.|author3=Murray I.|author4=Papamakarios G.|title=Neural Spline Flows|journal=Advances in Neural Information Processing Systems (NeurIPS)|year=2019|volume=32|url=https://arxiv.org/abs/1906.04032}}&amp;lt;/ref&amp;gt;——用更少的层数即可达到更强的拟合能力。&lt;br /&gt;
* '''[[均匀去量化]]的次优性'''：像素是 0–255 的离散整数，而 RealNVP 建模的是连续分布。RealNVP 的做法是给像素加上均匀噪声，把离散数据&amp;quot;抹平&amp;quot;成连续的——但这相当于让模型拟合一个被噪声模糊过的分布，与真实分布存在偏差。Flow++ 提出的[[变分去量化]]让噪声分布本身也由模型学习，缓解了这一问题&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''分区固定性'''：掩码决定哪一半变量被变换，而这一划分在训练前就已固定、训练中不再改变；若某层的划分不理想，只能靠后续层弥补。Glow（Generative Flow with Invertible 1×1 Convolutions）改用可学习的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积，让模型自己学会如何重排变量，划分更灵活，生成效果也更好&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''训练显存开销大'''：标准反向传播需要保存每个耦合层的激活值，层数一多显存占用显著上升；RevNet 的[[可逆残差结构]]正是为省去这部分存储而提出的&amp;lt;ref name=&amp;quot;RevNet2017&amp;quot;&amp;gt;{{cite journal|author1=Gomez A. N.|author2=Ren M.|author3=Urtasun R.|author4=Grosse R. B.|title=The Reversible Residual Network: Backpropagation Without Storing Activations|journal=NeurIPS|year=2017|url=https://arxiv.org/abs/1707.04585}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''对分布外样本过度自信'''：上文的异常检测应用依赖&amp;quot;异常样本的似然更低&amp;quot;这一假设，但后续研究发现这一假设未必成立：例如在 CIFAR-10 上训练的 RealNVP，会给截然不同的 SVHN 街景数字图像赋予'''更高'''的似然&amp;lt;ref name=&amp;quot;Nalisnick2019&amp;quot;&amp;gt;{{cite journal|author1=Nalisnick E.|author2=Matsukawa A.|author3=Teh Y. W.|author4=Gorur D.|author5=Lakshminarayanan B.|title=Do Deep Generative Models Know What They Don't Know?|journal=International Conference on Learning Representations (ICLR)|year=2019|url=https://arxiv.org/abs/1810.09136}}&amp;lt;/ref&amp;gt;。似然高低与&amp;quot;是否属于训练分布&amp;quot;并不直接对应，因此基于似然的异常检测需要额外的校准手段。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 与相关框架的关系 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置。下表列举前驱与后继的代表性工作：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=参考文献=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50901</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50901"/>
		<updated>2026-09-07T07:54:30Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：术语规范：保留&amp;quot;基分布&amp;quot;译名，首次出现处加注（base distribution，变换起点的简单分布，通常取标准高斯，与目标分布相对）——会议共识为不换词、加英文注释；&amp;quot;普适逼近器&amp;quot;改为&amp;quot;通用逼近器（universal approximator）&amp;quot;并补全原句谓语（&amp;quot;……而仿射耦合流在一定的良好条件下则是&amp;quot;）；棋盘掩码、通道级掩码在首次出现处就地给出一句直觉解释，落实&amp;quot;术语首次出现均需解释&amp;quot;的原则。  结构更名：原&amp;quot;= 算法框架 =&amp;quot;一节更名&amp;quot;= 模型架构 =&amp;quot;，避免&amp;quot;框架&amp;quot;一词暗示 RealNVP 与标准化流平级。  历史渊源重写：整节由&amp;quot;两条主线&amp;quot;改为&amp;quot;需求—框架—实现&amp;quot;三层叙事——先讲生成建模的需求与变量替换公式这一数学工具（ICA、高斯化为先驱），再讲 Tabak &amp;amp; Vanden-Eijnden 2010 正式提出标准化流框架、Rezende &amp;amp; Mohamed 2015 推广该术语，最后讲 NICE、RealNVP 等具体实现；节末新增术语史段落：RealNVP 原文未自称&amp;quot;标准化流&amp;quot;，归类由 Kobyzev et al.（TPAMI 2021）与 Papamakarios&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''在标准化流的谱系中，RealNVP 属于[[耦合流]]（coupling flow）分支，与 NICE、Glow 同属一支，三者构成&amp;quot;加性耦合→仿射耦合→引入可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积&amp;quot;的演进序列。RealNVP 的贡献也体现在两个层面：作为完整模型，它是标准化耦合流这一派的代表作；而其标志性的'''[[仿射耦合层]]'''又常被单独取出，作为通用可逆模块应用于标准化流之外的场合（见&amp;quot;应用&amp;quot;一节）。&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的历史可以按&amp;quot;需求—框架—实现&amp;quot;三层来梳理：先有密度估计的建模需求与变量替换公式这一数学工具，再有标准化流框架的正式提出，最后才有 NICE、RealNVP 等具体实现方案。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
'''需求与原理'''：深度生成建模的一个核心需求，是既能精确计算数据的概率密度，又能从模型中高效采样。满足这一需求的数学工具是概率论中的[[变量替换公式]]：若变换可逆，变换前后的密度便由雅可比行列式精确联系。用可逆变换把复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布的尝试由来已久——从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，都是这一思路的先驱。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
'''框架'''：2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;：通过一系列可微可逆映射的复合，让'''基分布'''（base distribution，变换起点的简单分布，通常取标准高斯；与变换要逼近的目标分布相对）像水流一样&amp;quot;流动&amp;quot;成目标分布，密度则由变量替换公式精确给出。2015 年，Rezende 与 Mohamed 将这一框架引入变分推断&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot; /&amp;gt;，&amp;quot;标准化流&amp;quot;一词由此流行。作为一种建模范式，标准化流只规定变换必须可逆、基分布必须指定、密度由变量替换公式计算，而不限定变换的具体结构——这为不同的实现方案留出了空间。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
'''实现'''：最早可训练的深度实现是 2014 年 Dinh 等人提出的 '''NICE'''（Non-linear Independent Components Estimation）&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2015|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的通用逼近器（universal approximator），而仿射耦合流在一定的良好条件下'''则是'''&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。除耦合流一支外，标准化流还有自回归流、连续流等其他实现路线（见&amp;quot;与相关框架的关系&amp;quot;一节）；RealNVP 是耦合流支线上的关键一环。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
'''术语史'''：RealNVP 原论文通篇并未自称&amp;quot;标准化流&amp;quot;——该词在全文中仅出现于参考文献的标题里，作者当时将模型定位为基于[[变量替换公式]]、可精确计算[[似然]]的概率模型。将 RealNVP 正式归入标准化流大类下的耦合流分支，是由后来的综述文献确立的分类&amp;lt;ref name=&amp;quot;Kobyzev2021&amp;quot;&amp;gt;{{cite journal|author1=Kobyzev I.|author2=Prince S. J. D.|author3=Brubaker M. A.|title=Normalizing Flows: An Introduction and Review of Current Methods|journal=IEEE Transactions on Pattern Analysis and Machine Intelligence|year=2021|volume=43|issue=11|pages=3964-3979|url=https://arxiv.org/abs/1908.09257}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
= 模型架构 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类[[标准化流]]。标准化流是一类通过最大似然来估计分布的模型，其构造包含三个要素：一个可微的可逆映射、一个指定的基分布，以及由[[变量替换公式]]给出的精确密度。具体地，RealNVP 将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到基分布——通常取标准正态——&amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体[[对数似然]] &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''[[仿射耦合层]]'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''[[批量归一化]]'''（batch normalization）以稳定训练；'''[[压缩]]'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''[[变量分解]]'''（factor out）负责在每个尺度末尾抽取变量作为潜在变量。其中，仿射耦合层通过两种掩码实现输入的对半划分：'''空间[[棋盘掩码]]'''（checkerboard masking）像国际象棋棋盘一样按像素坐标的奇偶性把像素分成两组，'''[[通道级掩码]]'''（channel-wise masking）则按通道把变量分成前后两半；二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
RealNVP 的多尺度架构如下图所示。输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第 1 尺度。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（图中标为 3 × (Coupling (checkerboard) → BN)；凡是耦合层，输出后均接批量[[归一化]]（即块内的 BN），下文不再重复）、压缩块（图中 Squeeze）、再 3 个通道级掩码的耦合层（图中 3 × (Coupling (channel-wise) → BN)），最后进行变量分解（图中 Factor Out）；末尺度则只经过 4 个空间棋盘掩码耦合层（图中 4 × (Coupling (checkerboard) → BN)），随后把全部变量抽出作为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;，不再保留传递变量。非末尺度的变量分解把当前特征一分为二：一半作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接（图中 Concatenation）成完整潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;，其中末尺度因全部抽出而与其前一尺度等长），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_multiscale_v2.png|缩略图|RealNVP 多尺度分层架构示意图。每个尺度（Scale）内数据自上而下流动，尺度间自左向右推进。图中 Coupling 即正文的仿射耦合层（括号内 checkerboard、channel-wise 分别为棋盘掩码与通道级掩码），块内&amp;quot;→ BN&amp;quot;表示每个耦合层输出后接批量归一化；Squeeze 为压缩，Factor Out 为变量分解，Concatenation 为拼接；h⁽ⁱ⁾、z⁽ⁱ⁾ 与正文符号一致。|替代=|848x848px|居中]]&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
下图给出两个交替堆叠的仿射耦合层的前向流程。仿射耦合层是 RealNVP 的基本可逆变换单元；图中左半部分的符号与公式 (1) 一一对应。[[文件:仿射耦合_公式对齐.png|缩略图|两个交替堆叠的仿射耦合层（正向，从左到右）：左半与公式 (1) 一一对应——&amp;lt;math&amp;gt;\mathbf{x}_1&amp;lt;/math&amp;gt; 直通（即 &amp;lt;math&amp;gt;\mathbf{y}_1=\mathbf{x}_1&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;\mathbf{x}_2&amp;lt;/math&amp;gt; 经 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 给出的缩放与平移得到 &amp;lt;math&amp;gt;\mathbf{y}_2&amp;lt;/math&amp;gt;；右半为下一耦合层（网络记为 &amp;lt;math&amp;gt;\mathbf{s}',\mathbf{t}'&amp;lt;/math&amp;gt;），两半角色互换，输出 &amp;lt;math&amp;gt;\mathbf{y}'&amp;lt;/math&amp;gt;。反向（从右到左）阅读即公式 (2) 的逆变换。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_1 = \mathbf{x}_1 \\[6pt]&lt;br /&gt;
\mathbf{y}_2 = \mathbf{x}_2 \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_1)\bigr) + \mathbf{t}(\mathbf{x}_1)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构；图像任务中通常用[[卷积神经网络]]（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。其中 &amp;lt;math&amp;gt;\mathbf{x}_1&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{x}_2&amp;lt;/math&amp;gt; 并非第 1、2 个坐标，而是由掩码划分出的两个'''子向量'''：&amp;lt;math&amp;gt;\mathbf{x}_1&amp;lt;/math&amp;gt; 是保持不变的被动部分，&amp;lt;math&amp;gt;\mathbf{x}_2&amp;lt;/math&amp;gt; 是被变换的主动部分。通道级掩码下二者即前、后两半通道，棋盘掩码下则是按坐标奇偶性分出的两组像素；具体规则见下文&amp;quot;两种掩码分区模式&amp;quot;。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的[[雅可比矩阵]]是下三角块结构，行列式即为对角线元素的乘积——一般的 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维矩阵求行列式需要 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 的计算量，而这里只需把对角元相乘，[[计算复杂度]]降为 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_1 = \mathbf{y}_1 \\[6pt]&lt;br /&gt;
\mathbf{x}_2 = \bigl(\mathbf{y}_2 - \mathbf{t}(\mathbf{y}_1)\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_1)\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 两种掩码分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分：保持不变的一半称为'''被动'''部分，被变换的一半称为'''主动'''部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠使用不同掩码的耦合层，每个维度都能在不同层中分别充当被动与主动变量，信息得以充分混合。原论文的多尺度架构中，每个尺度内同一掩码的耦合层固定为 3 层（末尺度为 4 层）&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
== [[多尺度架构]]：分层打包[[潜在变量]] ==&lt;br /&gt;
​&lt;br /&gt;
如上图所示，多尺度架构依赖两个关键操作：[[压缩]]与[[变量分解]]。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在[[通道方向]]上重排，将 &amp;lt;math&amp;gt;h\times w\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;h/2 \times w/2 \times 4c&amp;lt;/math&amp;gt;。[[空间分辨率]]减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个非末尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度；末尺度则将全部变量提取为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''[[层次化表示]]'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''[[梯度分散]]'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
== [[批量归一化]]：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量[[归一化]]（Batch Normalization，BN）应用于每个[[耦合层]]的输出之后。其映射为&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;，&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\tilde{\boldsymbol{\mu}}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\tilde{\boldsymbol{\sigma}}&amp;lt;/math&amp;gt; 为均值与标准差——训练时按当前批次估计，推理时改用滑动平均（原因见下文）。由于 BN 对每个维度独立地缩放和平移，它本身也是一个可逆变换：其雅可比矩阵为对角阵，[[雅可比行列式]]即各维度缩放因子的乘积，可直接加入[[对数似然]]——这使 BN 可以像耦合层一样作为流中的一层。引入 BN 主要是为了稳定深层训练：它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。使用 BN 有一个容易忽略的细节：训练时均值与方差是从当前这一批样本中估计的，因此单个样本的输出会依赖同批次的其他样本——此时的 BN 并不是严格意义上逐样本的可逆变换，其行列式也与整个批量耦合。只有在推理阶段改用训练过程中滑动平均得到的固定统计量，BN 才成为逐样本的、严格可逆的仿射变换，其行列式才能精确计入对数似然。换言之，用训练好的模型评估密度时若忘了做这一切换，同一个输入会因同批样本不同而得到不同的密度值，结果既不可复现也不正确。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的数学原理可分为两个层次。其一是[[标准化流]]的'''通用框架'''：[[变量替换公式]]给出精确似然的表达式，[[最大似然估计]]提供直接的训练目标，无需近似下界或辅助网络——这些结论对一切流模型成立，并非 RealNVP 特有。其二是 RealNVP 的'''特有设计'''：通用框架要求变换可逆且雅可比行列式可算，[[仿射耦合层]]的三角雅可比结构正是为满足这一要求而构造的，它使行列式计算开销与维度成线性关系，从而保证可处理性。本节先介绍通用框架，再阐述 RealNVP 的特化实现，进而比较[[加性耦合]]与仿射耦合在体积保持上的差异，最后讨论训练目标中两项各自的含义与实际训练配置。&lt;br /&gt;
​&lt;br /&gt;
== [[标准化流]]与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数[[雅可比行列式]]）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于[[自回归模型]]（autoregressive model，如 PixelRNN）：这类模型按固定顺序逐维生成数据、每个维度以先前各维为条件，采样必须串行进行——这与流模型对所有维度同时变换形成鲜明对比。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
上一小节的结论适用于一切标准化流。RealNVP 要回答的问题是：如何构造一个既完全可逆、又满足行列式易算的变换？其答案就是仿射耦合层——它对被动半 &amp;lt;math&amp;gt;\mathbf{x}_1&amp;lt;/math&amp;gt; 进行恒等映射，对主动半 &amp;lt;math&amp;gt;\mathbf{x}_2&amp;lt;/math&amp;gt; 进行仿射映射。记被动半的维度为 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt;（主动半的维度为 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt;），则雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_2}{\partial \mathbf{x}_1^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_1)]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_1)\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度网络（如深层卷积网络、残差网络），而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
对公式 (1) 做简单的代数移项，即可得到公式 (2) 给出的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练（这也是标准化流的通用训练方式），等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的分布整体逼近标准高斯，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：增大体积膨胀会提高似然，因此该项处处偏好扩张，与先验项的收缩压力相平衡——净效果是数据密集处扩张、稀疏处收缩，这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布（即数据分布经 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 映射到潜在空间后得到的分布）与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：[[自适应矩估计]]（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt;（原论文取 &amp;lt;math&amp;gt;\alpha = 0.05&amp;lt;/math&amp;gt;）处理，以缓解像素取值集中在 0 和 255 两个边界带来的问题。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的应用对应着它的两个层面：作为完整的标准化流模型，它直接服务于[[密度估计]]、生成、[[异常检测]]与重建推断等任务；作为通用可逆部件，其[[仿射耦合层]]凭借解析可逆、雅可比行列式易算的性质，也被用于概率建模之外的场合。下文按此顺序介绍。&lt;br /&gt;
​&lt;br /&gt;
== 自然图像[[密度估计]]与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（见下图）。与[[变分自编码器]]（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等[[自回归模型]]相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png|替代=|居中]]​&lt;br /&gt;
== [[异常检测]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时将似然显著低于正常水平的样本判为异常。也可以把流与重建类模型结合起来，综合似然与[[重建误差]]构造异常分数——下文的 AE-FLOW 即属此类。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。AE-FLOW 将自编码器与以 RealNVP [[仿射耦合层]]为核心的[[标准化流]]瓶颈相结合，在胸部 X 光、光学相干断层扫描、皮肤镜与脑部核磁共振等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的[[负对数似然]]构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
下图展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。|替代=|360x360px|居中]]​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合[[图像修复]]、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（见下图）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png|替代=|居中]]&lt;br /&gt;
​&lt;br /&gt;
== 在 [[NIS]]/[[NIS+]] 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus）中，RealNVP 的'''基础仿射耦合模块'''被用于构建[[可逆神经网络]]（Invertible Neural Networks，INN）编码器——堆叠 3 层，&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 为小型前馈网络——实现微观状态到宏观状态的可逆[[粗粒化]]。NIS 原文的表述本身即体现了范畴与实例的关系：&amp;quot;There are several ways to realize an invertible neural network ... we select RealNVP module ... to concretely implement the invertible computation.&amp;quot;（实现可逆神经网络有多种方式，本文选择 RealNVP 模块来具体实现可逆计算。）&amp;lt;ref name=&amp;quot;NIS2023&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2023|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;需要指出，此处复用的是部件而非完整模型：不含多尺度架构与高斯基分布，也不以似然为训练目标；同类的部件级复用还有 RevNet 的[[可逆残差结构]]&amp;lt;ref name=&amp;quot;RevNet2017&amp;quot; /&amp;gt;与 HiNet 的图像隐藏网络&amp;lt;ref name=&amp;quot;HiNet2021&amp;quot;&amp;gt;{{cite conference|author1=Jing J.|author2=Deng X.|author3=Xu M.|author4=Wang J.|author5=Guan Z.|title=HiNet: Deep Image Hiding by Invertible Network|conference=International Conference on Computer Vision (ICCV)|year=2021}}&amp;lt;/ref&amp;gt;。NIS 中的粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的 INN &amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''——相较于使用独立编码器和解码器的方案，参数量减少一半，如下图所示。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器与解码器（图中记作 φ 与 φ†，分别对应正文中的 &amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt;），信息丢弃与 EI 最大化在宏观动力学空间完成。|替代=|812x812px|居中]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2023&amp;quot; /&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使[[有效信息]]（[[Effective Information]]，[[EI]]）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
RealNVP作为NIS+中可逆表示学习模块，使粗粒化映射与反粗粒化映射共享参数，降低了模型复杂度，同时为有效信息最大化提供了可分析的数学框架。此外，基于可逆粗粒化结构构建的encoder具有通用逼近性质，能够表达不同复杂系统中的粗粒化映射。&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''[[精确似然]]'''：RealNVP 能直接算出任意数据点的精确概率密度——不像 VAE 只能优化似然的一个下界，也不像[[生成对抗网络]]（Generative Adversarial Network，GAN）完全不给密度。因此[[异常检测]]、[[贝叶斯推断]]等需要显式概率值的任务可以直接使用它&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''并行采样与推断'''：[[自回归模型]]必须逐像素按顺序生成，而 RealNVP 编码和解码时所有维度同时计算，生成一张图像只需一次前向传播，采样速度快得多。&lt;br /&gt;
* '''语义[[潜在空间]]'''：由于潜在分布是简单的标准高斯、且映射光滑可逆，在潜在空间中做插值等简单操作，就对应图像上有语义的连续变化（如人脸姿态、表情的渐变），适合表示学习与属性操控&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''架构灵活'''：[[耦合层]]只要求 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 两个网络给出缩放和平移量，对它们的内部结构没有任何限制，可以是任意深度的[[卷积神经网络]]（Convolutional Neural Network，CNN）或[[残差网络]]（Residual Network，ResNet）。提升表达能力可以直接借用主流的深层网络设计，而无须改动可逆框架本身。&lt;br /&gt;
* '''综合多种模型特性'''：RealNVP 同时具备了此前难以兼得的能力——自回归模型的精确似然、VAE 的一步采样，以及不依赖固定重建损失的生成质量。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：RealNVP 对数据分布的拟合精度仍略逊于 PixelRNN 等自回归模型&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在以拟合精度为核心的任务（如[[密度估计]]、[[数据压缩]]）上，这是它的相对劣势——尽管它以并行采样换来了远快于对手的生成速度。&lt;br /&gt;
* '''维度保持约束'''：可逆变换要求输入与输出同维，因此 RealNVP 不能像自编码器那样把高维图像压缩成低维向量——潜在变量的总维度始终与输入相同。[[多尺度架构]]中的变量分解并不改变这一点，它只是让后续层处理更小的特征图，并把潜在变量分配到不同尺度。&lt;br /&gt;
* '''[[仿射耦合]]的表达力上限'''：仿射耦合每层只对一半变量做&amp;quot;缩放加平移&amp;quot;，函数形式较为简单，拟合复杂分布往往需要堆叠很多层。后续工作把变换换成更灵活的函数形式——如 Flow++ 的 Logistic 混合 CDF 耦合&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;、Neural Spline Flows 的[[有理二次样条耦合]]&amp;lt;ref name=&amp;quot;NSF2019&amp;quot;&amp;gt;{{cite journal|author1=Durkan C.|author2=Bekasov A.|author3=Murray I.|author4=Papamakarios G.|title=Neural Spline Flows|journal=Advances in Neural Information Processing Systems (NeurIPS)|year=2019|volume=32|url=https://arxiv.org/abs/1906.04032}}&amp;lt;/ref&amp;gt;——用更少的层数即可达到更强的拟合能力。&lt;br /&gt;
* '''[[均匀去量化]]的次优性'''：像素是 0–255 的离散整数，而 RealNVP 建模的是连续分布。RealNVP 的做法是给像素加上均匀噪声，把离散数据&amp;quot;抹平&amp;quot;成连续的——但这相当于让模型拟合一个被噪声模糊过的分布，与真实分布存在偏差。Flow++ 提出的[[变分去量化]]让噪声分布本身也由模型学习，缓解了这一问题&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''分区固定性'''：掩码决定哪一半变量被变换，而这一划分在训练前就已固定、训练中不再改变；若某层的划分不理想，只能靠后续层弥补。Glow（Generative Flow with Invertible 1×1 Convolutions）改用可学习的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积，让模型自己学会如何重排变量，划分更灵活，生成效果也更好&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''训练显存开销大'''：标准反向传播需要保存每个耦合层的激活值，层数一多显存占用显著上升；RevNet 的[[可逆残差结构]]正是为省去这部分存储而提出的&amp;lt;ref name=&amp;quot;RevNet2017&amp;quot;&amp;gt;{{cite journal|author1=Gomez A. N.|author2=Ren M.|author3=Urtasun R.|author4=Grosse R. B.|title=The Reversible Residual Network: Backpropagation Without Storing Activations|journal=NeurIPS|year=2017|url=https://arxiv.org/abs/1707.04585}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''对分布外样本过度自信'''：上文的异常检测应用依赖&amp;quot;异常样本的似然更低&amp;quot;这一假设，但后续研究发现这一假设未必成立：例如在 CIFAR-10 上训练的 RealNVP，会给截然不同的 SVHN 街景数字图像赋予'''更高'''的似然&amp;lt;ref name=&amp;quot;Nalisnick2019&amp;quot;&amp;gt;{{cite journal|author1=Nalisnick E.|author2=Matsukawa A.|author3=Teh Y. W.|author4=Gorur D.|author5=Lakshminarayanan B.|title=Do Deep Generative Models Know What They Don't Know?|journal=International Conference on Learning Representations (ICLR)|year=2019|url=https://arxiv.org/abs/1810.09136}}&amp;lt;/ref&amp;gt;。似然高低与&amp;quot;是否属于训练分布&amp;quot;并不直接对应，因此基于似然的异常检测需要额外的校准手段。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 与相关框架的关系 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置。下表列举前驱与后继的代表性工作：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=参考文献=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50899</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50899"/>
		<updated>2026-09-06T12:42:37Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：术语规范：保留&amp;quot;基分布&amp;quot;译名，首次出现处加注（base distribution，变换起点的简单分布，通常取标准高斯，与目标分布相对）——会议共识为不换词、加英文注释；&amp;quot;普适逼近器&amp;quot;改为&amp;quot;通用逼近器（universal approximator）&amp;quot;并补全原句谓语（&amp;quot;……而仿射耦合流在一定的良好条件下则是&amp;quot;）；棋盘掩码、通道级掩码在首次出现处就地给出一句直觉解释，落实&amp;quot;术语首次出现均需解释&amp;quot;的原则。  结构更名：原&amp;quot;= 算法框架 =&amp;quot;一节更名&amp;quot;= 模型架构 =&amp;quot;，避免&amp;quot;框架&amp;quot;一词暗示 RealNVP 与标准化流平级。  历史渊源重写：整节由&amp;quot;两条主线&amp;quot;改为&amp;quot;需求—框架—实现&amp;quot;三层叙事——先讲生成建模的需求与变量替换公式这一数学工具（ICA、高斯化为先驱），再讲 Tabak &amp;amp; Vanden-Eijnden 2010 正式提出标准化流框架、Rezende &amp;amp; Mohamed 2015 推广该术语，最后讲 NICE、RealNVP 等具体实现；节末新增术语史段落：RealNVP 原文未自称&amp;quot;标准化流&amp;quot;，归类由 Kobyzev et al.（TPAMI 2021）与 Papamakarios&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2015|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类[[标准化流]]：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体[[对数似然]] &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''[[仿射耦合层]]'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''[[批量归一化]]'''（batch normalization）以稳定训练；'''[[压缩]]'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''[[变量分解]]'''（factor out）负责在每个尺度末尾抽取变量作为潜在变量。其中，仿射耦合层通过空间[[棋盘掩码]]（checkerboard masking）与[[通道级掩码]]（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
RealNVP 的多尺度架构如下图所示。输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第 1 尺度。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量[[归一化]]）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后进行变量分解；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化），随后把全部变量抽出作为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;，不再保留传递变量。非末尺度的变量分解把当前特征一分为二：一半作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;，其中末尺度因全部抽出而与其前一尺度等长），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|848x848px|居中]]&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
下图给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构；图像任务中通常用[[卷积神经网络]]（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的[[雅可比矩阵]]是下三角块结构，行列式即为对角线元素的乘积——一般的 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维矩阵求行列式需要 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 的计算量，而这里只需把对角元相乘，[[计算复杂度]]降为 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 两种掩码分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分：保持不变的一半称为'''被动'''部分，被变换的一半称为'''主动'''部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠使用不同掩码的耦合层，每个维度都能在不同层中分别充当被动与主动变量，信息得以充分混合。原论文的多尺度架构中，每个尺度内同一掩码的耦合层固定为 3 层（末尺度为 4 层）&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
== [[多尺度架构]]：分层打包[[潜在变量]] ==&lt;br /&gt;
​&lt;br /&gt;
如上图所示，多尺度架构依赖两个关键操作：[[压缩]]与[[变量分解]]。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在[[通道方向]]上重排，将 &amp;lt;math&amp;gt;h\times w\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;h/2 \times w/2 \times 4c&amp;lt;/math&amp;gt;。[[空间分辨率]]减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个非末尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度；末尺度则将全部变量提取为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''[[层次化表示]]'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''[[梯度分散]]'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
== [[批量归一化]]：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量[[归一化]]（Batch Normalization，BN）应用于每个[[耦合层]]的输出之后。其映射为&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;，&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\tilde{\boldsymbol{\mu}}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\tilde{\boldsymbol{\sigma}}&amp;lt;/math&amp;gt; 为均值与标准差——训练时按当前批次估计，推理时改用滑动平均（原因见下文）。由于 BN 对每个维度独立地缩放和平移，它本身也是一个可逆变换：其雅可比矩阵为对角阵，[[雅可比行列式]]即各维度缩放因子的乘积，可直接加入[[对数似然]]——这使 BN 可以像耦合层一样作为流中的一层。引入 BN 主要是为了稳定深层训练：它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。使用 BN 有一个容易忽略的细节：训练时均值与方差是从当前这一批样本中估计的，因此单个样本的输出会依赖同批次的其他样本——此时的 BN 并不是严格意义上逐样本的可逆变换，其行列式也与整个批量耦合。只有在推理阶段改用训练过程中滑动平均得到的固定统计量，BN 才成为逐样本的、严格可逆的仿射变换，其行列式才能精确计入对数似然。换言之，用训练好的模型评估密度时若忘了做这一切换，同一个输入会因同批样本不同而得到不同的密度值，结果既不可复现也不正确。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的数学原理可分为两个层次。其一是[[标准化流]]的'''通用框架'''：[[变量替换公式]]给出精确似然的表达式，[[最大似然估计]]提供直接的训练目标，无需近似下界或辅助网络——这些结论对一切流模型成立，并非 RealNVP 特有。其二是 RealNVP 的'''特有设计'''：通用框架要求变换可逆且雅可比行列式可算，[[仿射耦合层]]的三角雅可比结构正是为满足这一要求而构造的，它使行列式计算开销与维度成线性关系，从而保证可处理性。本节先介绍通用框架，再阐述 RealNVP 的特化实现，进而比较[[加性耦合]]与仿射耦合在体积保持上的差异，最后讨论训练目标中两项各自的含义与实际训练配置。&lt;br /&gt;
​&lt;br /&gt;
== [[标准化流]]与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数[[雅可比行列式]]）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于[[自回归模型]]（autoregressive model，如 PixelRNN）：这类模型按固定顺序逐维生成数据、每个维度以先前各维为条件，采样必须串行进行——这与流模型对所有维度同时变换形成鲜明对比。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
上一小节的结论适用于一切标准化流。RealNVP 要回答的问题是：如何构造一个既完全可逆、又满足行列式易算的变换？其答案就是仿射耦合层——它对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度网络（如深层卷积网络、残差网络），而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
对公式 (1) 做简单的代数移项，即可得到公式 (2) 给出的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据[[对数似然]]来训练（这也是[[标准化流]]的通用训练方式），等价于最小化数据分布与模型分布的前向 [[KL 散度]]（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用[[蒙特卡洛近似]]：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''[[先验项]]''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的分布整体逼近[[标准高斯]]，相当于对[[潜在空间]]的[[正则化]]。&lt;br /&gt;
* '''[[行列式项]]'''：增大[[体积膨胀]]会提高似然，因此该项处处偏好扩张，与先验项的收缩压力相平衡——净效果是数据密集处扩张、稀疏处收缩，这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化[[推前分布]]（即数据分布经 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 映射到潜在空间后得到的分布）与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：[[自适应矩估计]]（Adaptive Moment Estimation，Adam）优化器，[[权重衰减]] &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，[[批量大小]] 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt;（原论文取 &amp;lt;math&amp;gt;\alpha = 0.05&amp;lt;/math&amp;gt;）处理，以缓解像素取值集中在 0 和 255 两个边界带来的问题。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像[[密度估计]]与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（见下图）。与[[变分自编码器]]（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等[[自回归模型]]相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png|替代=|居中]]​&lt;br /&gt;
== [[异常检测]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时将似然显著低于正常水平的样本判为异常。也可以把流与重建类模型结合起来，综合似然与[[重建误差]]构造异常分数——下文的 AE-FLOW 即属此类。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。AE-FLOW 将自编码器与以 RealNVP [[仿射耦合层]]为核心的[[标准化流]]瓶颈相结合，在胸部 X 光、光学相干断层扫描、皮肤镜与脑部核磁共振等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的[[负对数似然]]构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
下图展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。|替代=|360x360px|居中]]​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合[[图像修复]]、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（见下图）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png|替代=|居中]]&lt;br /&gt;
​&lt;br /&gt;
== 在 [[NIS]]/[[NIS+]] 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus）中，RealNVP 以[[可逆神经网络]]（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆[[粗粒化]]。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的 INN &amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''——相较于使用独立编码器和解码器的方案，参数量减少一半，如下图所示。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器与解码器（图中记作 φ 与 φ†，分别对应正文中的 &amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt;），信息丢弃与 EI 最大化在宏观动力学空间完成。|替代=|812x812px|居中]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使[[有效信息]]（[[Effective Information]]，[[EI]]）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
RealNVP作为NIS+中可逆表示学习模块，使粗粒化映射与反粗粒化映射共享参数，降低了模型复杂度，同时为有效信息最大化提供了可分析的数学框架。此外，基于可逆粗粒化结构构建的encoder具有通用逼近性质，能够表达不同复杂系统中的粗粒化映射。&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''[[精确似然]]'''：RealNVP 能直接算出任意数据点的精确概率密度——不像 VAE 只能优化似然的一个下界，也不像[[生成对抗网络]]（Generative Adversarial Network，GAN）完全不给密度。因此[[异常检测]]、[[贝叶斯推断]]等需要显式概率值的任务可以直接使用它&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''并行采样与推断'''：[[自回归模型]]必须逐像素按顺序生成，而 RealNVP 编码和解码时所有维度同时计算，生成一张图像只需一次前向传播，采样速度快得多。&lt;br /&gt;
* '''语义[[潜在空间]]'''：由于潜在分布是简单的标准高斯、且映射光滑可逆，在潜在空间中做插值等简单操作，就对应图像上有语义的连续变化（如人脸姿态、表情的渐变），适合表示学习与属性操控&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''架构灵活'''：[[耦合层]]只要求 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 两个网络给出缩放和平移量，对它们的内部结构没有任何限制，可以是任意深度的[[卷积神经网络]]（Convolutional Neural Network，CNN）或[[残差网络]]（Residual Network，ResNet）。提升表达能力可以直接借用主流的深层网络设计，而无须改动可逆框架本身。&lt;br /&gt;
* '''综合多种模型特性'''：RealNVP 同时具备了此前难以兼得的能力——自回归模型的精确似然、VAE 的一步采样，以及不依赖固定重建损失的生成质量。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：RealNVP 对数据分布的拟合精度仍略逊于 PixelRNN 等自回归模型&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在以拟合精度为核心的任务（如[[密度估计]]、[[数据压缩]]）上，这是它的相对劣势——尽管它以并行采样换来了远快于对手的生成速度。&lt;br /&gt;
* '''维度保持约束'''：可逆变换要求输入与输出同维，因此 RealNVP 不能像自编码器那样把高维图像压缩成低维向量——潜在变量的总维度始终与输入相同。[[多尺度架构]]中的变量分解并不改变这一点，它只是让后续层处理更小的特征图，并把潜在变量分配到不同尺度。&lt;br /&gt;
* '''[[仿射耦合]]的表达力上限'''：仿射耦合每层只对一半变量做&amp;quot;缩放加平移&amp;quot;，函数形式较为简单，拟合复杂分布往往需要堆叠很多层。后续工作把变换换成更灵活的函数形式——如 Flow++ 的 Logistic 混合 CDF 耦合&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;、Neural Spline Flows 的[[有理二次样条耦合]]&amp;lt;ref name=&amp;quot;NSF2019&amp;quot;&amp;gt;{{cite journal|author1=Durkan C.|author2=Bekasov A.|author3=Murray I.|author4=Papamakarios G.|title=Neural Spline Flows|journal=Advances in Neural Information Processing Systems (NeurIPS)|year=2019|volume=32|url=https://arxiv.org/abs/1906.04032}}&amp;lt;/ref&amp;gt;——用更少的层数即可达到更强的拟合能力。&lt;br /&gt;
* '''[[均匀去量化]]的次优性'''：像素是 0–255 的离散整数，而 RealNVP 建模的是连续分布。RealNVP 的做法是给像素加上均匀噪声，把离散数据&amp;quot;抹平&amp;quot;成连续的——但这相当于让模型拟合一个被噪声模糊过的分布，与真实分布存在偏差。Flow++ 提出的[[变分去量化]]让噪声分布本身也由模型学习，缓解了这一问题&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''分区固定性'''：掩码决定哪一半变量被变换，而这一划分在训练前就已固定、训练中不再改变；若某层的划分不理想，只能靠后续层弥补。Glow（Generative Flow with Invertible 1×1 Convolutions）改用可学习的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积，让模型自己学会如何重排变量，划分更灵活，生成效果也更好&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''训练显存开销大'''：标准反向传播需要保存每个耦合层的激活值，层数一多显存占用显著上升；RevNet 的[[可逆残差结构]]正是为省去这部分存储而提出的&amp;lt;ref name=&amp;quot;RevNet2017&amp;quot;&amp;gt;{{cite journal|author1=Gomez A. N.|author2=Ren M.|author3=Urtasun R.|author4=Grosse R. B.|title=The Reversible Residual Network: Backpropagation Without Storing Activations|journal=NeurIPS|year=2017|url=https://arxiv.org/abs/1707.04585}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''对分布外样本过度自信'''：上文的异常检测应用依赖&amp;quot;异常样本的似然更低&amp;quot;这一假设，但后续研究发现这一假设未必成立：例如在 CIFAR-10 上训练的 RealNVP，会给截然不同的 SVHN 街景数字图像赋予'''更高'''的似然&amp;lt;ref name=&amp;quot;Nalisnick2019&amp;quot;&amp;gt;{{cite journal|author1=Nalisnick E.|author2=Matsukawa A.|author3=Teh Y. W.|author4=Gorur D.|author5=Lakshminarayanan B.|title=Do Deep Generative Models Know What They Don't Know?|journal=International Conference on Learning Representations (ICLR)|year=2019|url=https://arxiv.org/abs/1810.09136}}&amp;lt;/ref&amp;gt;。似然高低与&amp;quot;是否属于训练分布&amp;quot;并不直接对应，因此基于似然的异常检测需要额外的校准手段。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 与相关框架的关系 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置。下表列举前驱与后继的代表性工作：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=参考文献=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50898</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50898"/>
		<updated>2026-09-06T12:39:27Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：术语规范：保留&amp;quot;基分布&amp;quot;译名，首次出现处加注（base distribution，变换起点的简单分布，通常取标准高斯，与目标分布相对）——会议共识为不换词、加英文注释；&amp;quot;普适逼近器&amp;quot;改为&amp;quot;通用逼近器（universal approximator）&amp;quot;并补全原句谓语（&amp;quot;……而仿射耦合流在一定的良好条件下则是&amp;quot;）；棋盘掩码、通道级掩码在首次出现处就地给出一句直觉解释，落实&amp;quot;术语首次出现均需解释&amp;quot;的原则。  结构更名：原&amp;quot;= 算法框架 =&amp;quot;一节更名&amp;quot;= 模型架构 =&amp;quot;，避免&amp;quot;框架&amp;quot;一词暗示 RealNVP 与标准化流平级。  历史渊源重写：整节由&amp;quot;两条主线&amp;quot;改为&amp;quot;需求—框架—实现&amp;quot;三层叙事——先讲生成建模的需求与变量替换公式这一数学工具（ICA、高斯化为先驱），再讲 Tabak &amp;amp; Vanden-Eijnden 2010 正式提出标准化流框架、Rezende &amp;amp; Mohamed 2015 推广该术语，最后讲 NICE、RealNVP 等具体实现；节末新增术语史段落：RealNVP 原文未自称&amp;quot;标准化流&amp;quot;，归类由 Kobyzev et al.（TPAMI 2021）与 Papamakarios&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''在[[标准化流]]的谱系中，RealNVP 属于[[耦合流]]（coupling flow）分支，与 [[NICE]]、[[Glow]] 同属一支，三者构成&amp;quot;[[加性耦合]]→仿射耦合→引入可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积&amp;quot;的演进序列。RealNVP 的贡献也体现在两个层面：作为完整模型，它是标准化[[耦合流]]这一派的代表作；而其标志性的'''[[仿射耦合层]]'''又常被单独取出，作为通用可逆模块应用于标准化流之外的场合（见&amp;quot;应用&amp;quot;一节）。&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的历史可以按&amp;quot;需求—框架—实现&amp;quot;三层来梳理：先有[[密度估计]]的建模需求与[[变量替换公式]]这一数学工具，再有标准化流框架的正式提出，最后才有 NICE、RealNVP 等具体实现方案。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
'''需求与原理'''：深度[[生成建模]]的一个核心需求，是既能精确计算数据的概率密度，又能从模型中高效采样。满足这一需求的数学工具是概率论中的[[变量替换公式]]：若变换可逆，变换前后的密度便由[[雅可比行列式]]精确联系。用[[可逆变换]]把复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布的尝试由来已久——从 1990 年代的[[独立成分分析]]（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''[[高斯化]]'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，都是这一思路的先驱。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
'''框架'''：2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;：通过一系列可微可逆映射的复合，让'''[[基分布]]'''（base distribution，变换起点的简单分布，通常取标准高斯；与变换要逼近的目标分布相对）像水流一样&amp;quot;流动&amp;quot;成目标分布，密度则由变量替换公式精确给出。2015 年，Rezende 与 Mohamed 将这一框架引入[[变分推断]]&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot; /&amp;gt;，&amp;quot;标准化流&amp;quot;一词由此流行。作为一种建模范式，标准化流只规定变换必须可逆、基分布必须指定、密度由变量替换公式计算，而不限定变换的具体结构——这为不同的实现方案留出了空间。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
'''实现'''：最早可训练的深度实现是 2014 年 Dinh 等人提出的 '''NICE'''（Non-linear Independent Components Estimation）&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2015|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的[[通用逼近器]]（universal approximator），而仿射耦合流在一定的良好条件下'''则是'''&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。除耦合流一支外，标准化流还有自回归流、连续流等其他实现路线（见&amp;quot;与相关框架的关系&amp;quot;一节）；RealNVP 是耦合流支线上的关键一环。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
'''术语史'''：RealNVP 原论文通篇并未自称&amp;quot;标准化流&amp;quot;——该词在全文中仅出现于参考文献的标题里，作者当时将模型定位为基于[[变量替换公式]]、可精确计算[[似然]]的概率模型。将 RealNVP 正式归入标准化流大类下的耦合流分支，是由后来的综述文献确立的分类&amp;lt;ref name=&amp;quot;Kobyzev2021&amp;quot;&amp;gt;{{cite journal|author1=Kobyzev I.|author2=Prince S. J. D.|author3=Brubaker M. A.|title=Normalizing Flows: An Introduction and Review of Current Methods|journal=IEEE Transactions on Pattern Analysis and Machine Intelligence|year=2021|volume=43|issue=11|pages=3964-3979|url=https://arxiv.org/abs/1908.09257}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
= 模型架构 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类[[标准化流]]。标准化流是一类通过[[最大似然]]来估计分布的模型，其构造包含三个要素：一个可微的可逆映射、一个指定的基分布，以及由[[变量替换公式]]给出的精确密度。具体地，RealNVP 将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到基分布——通常取标准正态——&amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体[[对数似然]] &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''[[仿射耦合层]]'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''[[批量归一化]]'''（batch normalization）以稳定训练；'''[[压缩]]'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''[[变量分解]]'''（factor out）负责在每个尺度末尾抽取变量作为[[潜在变量]]。其中，[[仿射耦合层]]通过两种掩码实现输入的对半划分：'''空间[[棋盘掩码]]'''（checkerboard masking）像国际象棋棋盘一样按像素坐标的奇偶性把像素分成两组，'''[[通道级掩码]]'''（channel-wise masking）则按通道把变量分成前后两半；二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
RealNVP 的[[多尺度架构]]如下图所示。输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第 1 尺度。每个非末尺度内部依次经过 3 个空间[[棋盘掩码]]的耦合层（图中标为 3 × (Coupling (checkerboard) → BN)；凡是耦合层，输出后均接批量[[归一化]]（即块内的 BN），下文不再重复）、[[压缩]]块（图中 Squeeze）、再 3 个[[通道级掩码]]的耦合层（图中 3 × (Coupling (channel-wise) → BN)），最后进行[[变量分解]]（图中 Factor Out）；末尺度则只经过 4 个空间棋盘掩码耦合层（图中 4 × (Coupling (checkerboard) → BN)），随后把全部变量抽出作为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;，不再保留传递变量。非末尺度的变量分解把当前特征一分为二：一半作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接（图中 Concatenation）成完整潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;，其中末尺度因全部抽出而与其前一尺度等长），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_multiscale_v2.png|缩略图|RealNVP 多尺度分层架构示意图。每个尺度（Scale）内数据自上而下流动，尺度间自左向右推进。图中 Coupling 即正文的仿射耦合层（括号内 checkerboard、channel-wise 分别为棋盘掩码与通道级掩码），块内&amp;quot;→ BN&amp;quot;表示每个耦合层输出后接批量归一化；Squeeze 为压缩，Factor Out 为变量分解，Concatenation 为拼接；h⁽ⁱ⁾、z⁽ⁱ⁾ 与正文符号一致。|替代=|848x848px|居中]]&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
下图给出两个交替堆叠的仿射耦合层的前向流程。仿射耦合层是 RealNVP 的基本可逆变换单元；图中左半部分的符号与公式 (1) 一一对应。[[文件:仿射耦合_公式对齐.png|缩略图|两个交替堆叠的仿射耦合层（正向，从左到右）：左半与公式 (1) 一一对应——&amp;lt;math&amp;gt;\mathbf{x}_1&amp;lt;/math&amp;gt; 直通（即 &amp;lt;math&amp;gt;\mathbf{y}_1=\mathbf{x}_1&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;\mathbf{x}_2&amp;lt;/math&amp;gt; 经 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 给出的缩放与平移得到 &amp;lt;math&amp;gt;\mathbf{y}_2&amp;lt;/math&amp;gt;；右半为下一耦合层（网络记为 &amp;lt;math&amp;gt;\mathbf{s}',\mathbf{t}'&amp;lt;/math&amp;gt;），两半角色互换，输出 &amp;lt;math&amp;gt;\mathbf{y}'&amp;lt;/math&amp;gt;。反向（从右到左）阅读即公式 (2) 的逆变换。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_1 = \mathbf{x}_1 \\[6pt]&lt;br /&gt;
\mathbf{y}_2 = \mathbf{x}_2 \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_1)\bigr) + \mathbf{t}(\mathbf{x}_1)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构；图像任务中通常用[[卷积神经网络]]（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。其中 &amp;lt;math&amp;gt;\mathbf{x}_1&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{x}_2&amp;lt;/math&amp;gt; 并非第 1、2 个坐标，而是由掩码划分出的两个'''子向量'''：&amp;lt;math&amp;gt;\mathbf{x}_1&amp;lt;/math&amp;gt; 是保持不变的被动部分，&amp;lt;math&amp;gt;\mathbf{x}_2&amp;lt;/math&amp;gt; 是被变换的主动部分。通道级掩码下二者即前、后两半通道，棋盘掩码下则是按坐标奇偶性分出的两组像素；具体规则见下文&amp;quot;两种掩码分区模式&amp;quot;。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的[[雅可比矩阵]]是下三角块结构，行列式即为对角线元素的乘积——一般的 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维矩阵求行列式需要 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 的计算量，而这里只需把对角元相乘，[[计算复杂度]]降为 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_1 = \mathbf{y}_1 \\[6pt]&lt;br /&gt;
\mathbf{x}_2 = \bigl(\mathbf{y}_2 - \mathbf{t}(\mathbf{y}_1)\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_1)\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 两种掩码分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分：保持不变的一半称为'''被动'''部分，被变换的一半称为'''主动'''部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠使用不同掩码的耦合层，每个维度都能在不同层中分别充当被动与主动变量，信息得以充分混合。原论文的多尺度架构中，每个尺度内同一掩码的耦合层固定为 3 层（末尺度为 4 层）&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
== [[多尺度架构]]：分层打包[[潜在变量]] ==&lt;br /&gt;
​&lt;br /&gt;
如上图所示，多尺度架构依赖两个关键操作：[[压缩]]与[[变量分解]]。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在[[通道方向]]上重排，将 &amp;lt;math&amp;gt;h\times w\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;h/2 \times w/2 \times 4c&amp;lt;/math&amp;gt;。[[空间分辨率]]减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个非末尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度；末尺度则将全部变量提取为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''[[层次化表示]]'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''[[梯度分散]]'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
== [[批量归一化]]：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量[[归一化]]（Batch Normalization，BN）应用于每个[[耦合层]]的输出之后。其映射为&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;，&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\tilde{\boldsymbol{\mu}}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\tilde{\boldsymbol{\sigma}}&amp;lt;/math&amp;gt; 为均值与标准差——训练时按当前批次估计，推理时改用滑动平均（原因见下文）。由于 BN 对每个维度独立地缩放和平移，它本身也是一个可逆变换：其雅可比矩阵为对角阵，[[雅可比行列式]]即各维度缩放因子的乘积，可直接加入[[对数似然]]——这使 BN 可以像耦合层一样作为流中的一层。引入 BN 主要是为了稳定深层训练：它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。使用 BN 有一个容易忽略的细节：训练时均值与方差是从当前这一批样本中估计的，因此单个样本的输出会依赖同批次的其他样本——此时的 BN 并不是严格意义上逐样本的可逆变换，其行列式也与整个批量耦合。只有在推理阶段改用训练过程中滑动平均得到的固定统计量，BN 才成为逐样本的、严格可逆的仿射变换，其行列式才能精确计入[[对数似然]]。换言之，用训练好的模型评估密度时若忘了做这一切换，同一个输入会因同批样本不同而得到不同的密度值，结果既不可复现也不正确。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的数学原理可分为两个层次。其一是[[标准化流]]的'''通用框架'''：[[变量替换公式]]给出精确似然的表达式，[[最大似然估计]]提供直接的训练目标，无需近似下界或辅助网络——这些结论对一切流模型成立，并非 RealNVP 特有。其二是 RealNVP 的'''特有设计'''：通用框架要求变换可逆且雅可比行列式可算，[[仿射耦合层]]的[[三角雅可比]]结构正是为满足这一要求而构造的，它使行列式计算开销与维度成线性关系，从而保证可处理性。本节先介绍通用框架，再阐述 RealNVP 的特化实现，进而比较[[加性耦合]]与仿射耦合在[[体积保持]]上的差异，最后讨论训练目标中两项各自的含义与实际训练配置。&lt;br /&gt;
​&lt;br /&gt;
== [[标准化流]]与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数[[雅可比行列式]]）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于[[自回归模型]]（autoregressive model，如 PixelRNN）：这类模型按固定顺序逐维生成数据、每个维度以先前各维为条件，采样必须串行进行——这与流模型对所有维度同时变换形成鲜明对比。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
上一小节的结论适用于一切标准化流。RealNVP 要回答的问题是：如何构造一个既完全可逆、又满足行列式易算的变换？其答案就是仿射耦合层——它对被动半 &amp;lt;math&amp;gt;\mathbf{x}_1&amp;lt;/math&amp;gt; 进行恒等映射，对主动半 &amp;lt;math&amp;gt;\mathbf{x}_2&amp;lt;/math&amp;gt; 进行仿射映射。记被动半的维度为 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt;（主动半的维度为 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt;），则雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_2}{\partial \mathbf{x}_1^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_1)]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_1)\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度网络（如深层卷积网络、[[残差网络]]），而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
对公式 (1) 做简单的代数移项，即可得到公式 (2) 给出的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练（这也是标准化流的通用训练方式），等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的分布整体逼近标准高斯，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：增大体积膨胀会提高似然，因此该项处处偏好扩张，与先验项的收缩压力相平衡——净效果是数据密集处扩张、稀疏处收缩，这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布（即数据分布经 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 映射到潜在空间后得到的分布）与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：[[自适应矩估计]]（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt;（原论文取 &amp;lt;math&amp;gt;\alpha = 0.05&amp;lt;/math&amp;gt;）处理，以缓解像素取值集中在 0 和 255 两个边界带来的问题。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的应用对应着它的两个层面：作为完整的标准化流模型，它直接服务于[[密度估计]]、生成、[[异常检测]]与重建推断等任务；作为通用可逆部件，其[[仿射耦合层]]凭借解析可逆、雅可比行列式易算的性质，也被用于概率建模之外的场合。下文按此顺序介绍。&lt;br /&gt;
​&lt;br /&gt;
== 自然图像[[密度估计]]与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（见下图）。与[[变分自编码器]]（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如[[均方误差]]（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等[[自回归模型]]相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png|替代=|居中]]​&lt;br /&gt;
== [[异常检测]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督[[异常检测]]：在仅含正常样本的数据上训练流模型，测试时将似然显著低于正常水平的样本判为异常。也可以把流与重建类模型结合起来，综合似然与[[重建误差]]构造异常分数——下文的 [[AE-FLOW]] 即属此类。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。AE-FLOW 将自编码器与以 RealNVP [[仿射耦合层]]为核心的[[标准化流]]瓶颈相结合，在胸部 X 光、光学相干断层扫描、皮肤镜与脑部核磁共振等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的[[负对数似然]]构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
下图展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。|替代=|360x360px|居中]]​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合[[图像修复]]、去噪、着色等重建设置。以[[图像修复]]为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（见下图）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png|替代=|居中]]&lt;br /&gt;
​&lt;br /&gt;
== 在 [[NIS]]/[[NIS+]] 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在[[神经信息挤压器]]（Neural Information Squeezer，[[NIS]]）及其扩展 [[NIS+]]（Neural Information Squeezer Plus）中，RealNVP 的'''基础仿射耦合模块'''被用于构建[[可逆神经网络]]（Invertible Neural Networks，INN）编码器——堆叠 3 层，&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 为小型前馈网络——实现微观状态到宏观状态的可逆[[粗粒化]]。NIS 原文的表述本身即体现了范畴与实例的关系：&amp;quot;There are several ways to realize an invertible neural network ... we select RealNVP module ... to concretely implement the invertible computation.&amp;quot;（实现[[可逆神经网络]]有多种方式，本文选择 RealNVP 模块来具体实现可逆计算。）&amp;lt;ref name=&amp;quot;NIS2023&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2023|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;需要指出，此处复用的是部件而非完整模型：不含多尺度架构与高斯基分布，也不以似然为训练目标；同类的部件级复用还有 [[RevNet]] 的[[可逆残差结构]]&amp;lt;ref name=&amp;quot;RevNet2017&amp;quot; /&amp;gt;与 [[HiNet]] 的图像隐藏网络&amp;lt;ref name=&amp;quot;HiNet2021&amp;quot;&amp;gt;{{cite conference|author1=Jing J.|author2=Deng X.|author3=Xu M.|author4=Wang J.|author5=Guan Z.|title=HiNet: Deep Image Hiding by Invertible Network|conference=International Conference on Computer Vision (ICCV)|year=2021}}&amp;lt;/ref&amp;gt;。NIS 中的[[粗粒化]][[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的 INN &amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''——相较于使用独立编码器和解码器的方案，参数量减少一半，如下图所示。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器与解码器（图中记作 φ 与 φ†，分别对应正文中的 &amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt;），信息丢弃与 EI 最大化在宏观动力学空间完成。|替代=|812x812px|居中]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2023&amp;quot; /&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使[[有效信息]]（[[Effective Information]]，[[EI]]）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
RealNVP作为NIS+中可逆[[表示学习]]模块，使粗粒化映射与反粗粒化映射共享参数，降低了模型复杂度，同时为[[有效信息]]最大化提供了可分析的数学框架。此外，基于可逆粗粒化结构构建的encoder具有通用逼近性质，能够表达不同[[复杂系统]]中的粗粒化映射。&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''[[精确似然]]'''：RealNVP 能直接算出任意数据点的精确概率密度——不像 VAE 只能优化似然的一个下界，也不像[[生成对抗网络]]（Generative Adversarial Network，GAN）完全不给密度。因此[[异常检测]]、[[贝叶斯推断]]等需要显式概率值的任务可以直接使用它&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''并行采样与推断'''：[[自回归模型]]必须逐像素按顺序生成，而 RealNVP 编码和解码时所有维度同时计算，生成一张图像只需一次前向传播，采样速度快得多。&lt;br /&gt;
* '''语义[[潜在空间]]'''：由于潜在分布是简单的标准高斯、且映射光滑可逆，在潜在空间中做插值等简单操作，就对应图像上有语义的连续变化（如人脸姿态、表情的渐变），适合表示学习与属性操控&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''架构灵活'''：[[耦合层]]只要求 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 两个网络给出缩放和平移量，对它们的内部结构没有任何限制，可以是任意深度的[[卷积神经网络]]（Convolutional Neural Network，CNN）或[[残差网络]]（Residual Network，ResNet）。提升表达能力可以直接借用主流的深层网络设计，而无须改动可逆框架本身。&lt;br /&gt;
* '''综合多种模型特性'''：RealNVP 同时具备了此前难以兼得的能力——[[自回归模型]]的精确似然、VAE 的一步采样，以及不依赖固定重建损失的生成质量。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：RealNVP 对数据分布的拟合精度仍略逊于 PixelRNN 等自回归模型&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在以拟合精度为核心的任务（如[[密度估计]]、[[数据压缩]]）上，这是它的相对劣势——尽管它以并行采样换来了远快于对手的生成速度。&lt;br /&gt;
* '''维度保持约束'''：可逆变换要求输入与输出同维，因此 RealNVP 不能像自编码器那样把高维图像压缩成低维向量——潜在变量的总维度始终与输入相同。[[多尺度架构]]中的变量分解并不改变这一点，它只是让后续层处理更小的特征图，并把潜在变量分配到不同尺度。&lt;br /&gt;
* '''[[仿射耦合]]的表达力上限'''：仿射耦合每层只对一半变量做&amp;quot;缩放加平移&amp;quot;，函数形式较为简单，拟合复杂分布往往需要堆叠很多层。后续工作把变换换成更灵活的函数形式——如 [[Flow++]] 的 Logistic 混合 CDF 耦合&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;、Neural Spline Flows 的[[有理二次样条耦合]]&amp;lt;ref name=&amp;quot;NSF2019&amp;quot;&amp;gt;{{cite journal|author1=Durkan C.|author2=Bekasov A.|author3=Murray I.|author4=Papamakarios G.|title=Neural Spline Flows|journal=Advances in Neural Information Processing Systems (NeurIPS)|year=2019|volume=32|url=https://arxiv.org/abs/1906.04032}}&amp;lt;/ref&amp;gt;——用更少的层数即可达到更强的拟合能力。&lt;br /&gt;
* '''[[均匀去量化]]的次优性'''：像素是 0–255 的离散整数，而 RealNVP 建模的是连续分布。RealNVP 的做法是给像素加上均匀噪声，把离散数据&amp;quot;抹平&amp;quot;成连续的——但这相当于让模型拟合一个被噪声模糊过的分布，与真实分布存在偏差。Flow++ 提出的[[变分去量化]]让噪声分布本身也由模型学习，缓解了这一问题&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''分区固定性'''：掩码决定哪一半变量被变换，而这一划分在训练前就已固定、训练中不再改变；若某层的划分不理想，只能靠后续层弥补。Glow（Generative Flow with Invertible 1×1 Convolutions）改用可学习的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积，让模型自己学会如何重排变量，划分更灵活，生成效果也更好&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''训练显存开销大'''：标准反向传播需要保存每个耦合层的激活值，层数一多显存占用显著上升；RevNet 的[[可逆残差结构]]正是为省去这部分存储而提出的&amp;lt;ref name=&amp;quot;RevNet2017&amp;quot;&amp;gt;{{cite journal|author1=Gomez A. N.|author2=Ren M.|author3=Urtasun R.|author4=Grosse R. B.|title=The Reversible Residual Network: Backpropagation Without Storing Activations|journal=NeurIPS|year=2017|url=https://arxiv.org/abs/1707.04585}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''对分布外样本过度自信'''：上文的异常检测应用依赖&amp;quot;异常样本的似然更低&amp;quot;这一假设，但后续研究发现这一假设未必成立：例如在 CIFAR-10 上训练的 RealNVP，会给截然不同的 SVHN 街景数字图像赋予'''更高'''的似然&amp;lt;ref name=&amp;quot;Nalisnick2019&amp;quot;&amp;gt;{{cite journal|author1=Nalisnick E.|author2=Matsukawa A.|author3=Teh Y. W.|author4=Gorur D.|author5=Lakshminarayanan B.|title=Do Deep Generative Models Know What They Don't Know?|journal=International Conference on Learning Representations (ICLR)|year=2019|url=https://arxiv.org/abs/1810.09136}}&amp;lt;/ref&amp;gt;。似然高低与&amp;quot;是否属于训练分布&amp;quot;并不直接对应，因此基于似然的异常检测需要额外的校准手段。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 与相关框架的关系 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置。下表列举前驱与后继的代表性工作：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''[[MAF]]'''（Masked Autoregressive Flow）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''[[FFJORD]]'''（Free-form Jacobian of Reversible Dynamics）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + [[常微分方程]]（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || [[变分去量化]] + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、[[去量化]]和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || [[无偏行列式估计]] || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=参考文献=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:%E4%BB%BF%E5%B0%84%E8%80%A6%E5%90%88.png&amp;diff=50897</id>
		<title>文件:仿射耦合.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:%E4%BB%BF%E5%B0%84%E8%80%A6%E5%90%88.png&amp;diff=50897"/>
		<updated>2026-09-06T11:52:38Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：黄一凡上传文件:仿射耦合.png的新版本&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50804</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50804"/>
		<updated>2026-08-24T13:04:29Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：重新表述使 RealNVP 和一般流模型的关系厘清&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2015|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类[[标准化流]]：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体[[对数似然]] &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''[[仿射耦合层]]'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''[[批量归一化]]'''（batch normalization）以稳定训练；'''[[压缩]]'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''[[变量分解]]'''（factor out）负责在每个尺度末尾抽取变量作为潜在变量。其中，仿射耦合层通过空间[[棋盘掩码]]（checkerboard masking）与[[通道级掩码]]（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
RealNVP 的多尺度架构如下图所示。输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第 1 尺度。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量[[归一化]]）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后进行变量分解；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化），随后把全部变量抽出作为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;，不再保留传递变量。非末尺度的变量分解把当前特征一分为二：一半作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;，其中末尺度因全部抽出而与其前一尺度等长），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|848x848px|居中]]&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
下图给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构；图像任务中通常用[[卷积神经网络]]（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的[[雅可比矩阵]]是下三角块结构，行列式即为对角线元素的乘积——一般的 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维矩阵求行列式需要 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 的计算量，而这里只需把对角元相乘，[[计算复杂度]]降为 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 两种掩码分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分：保持不变的一半称为'''被动'''部分，被变换的一半称为'''主动'''部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠使用不同掩码的耦合层，每个维度都能在不同层中分别充当被动与主动变量，信息得以充分混合。原论文的多尺度架构中，每个尺度内同一掩码的耦合层固定为 3 层（末尺度为 4 层）&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
== [[多尺度架构]]：分层打包[[潜在变量]] ==&lt;br /&gt;
​&lt;br /&gt;
如上图所示，多尺度架构依赖两个关键操作：[[压缩]]与[[变量分解]]。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在[[通道方向]]上重排，将 &amp;lt;math&amp;gt;h\times w\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;h/2 \times w/2 \times 4c&amp;lt;/math&amp;gt;。[[空间分辨率]]减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个非末尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度；末尺度则将全部变量提取为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''[[层次化表示]]'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''[[梯度分散]]'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
== [[批量归一化]]：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量[[归一化]]（Batch Normalization，BN）应用于每个[[耦合层]]的输出之后。其映射为&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;，&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\tilde{\boldsymbol{\mu}}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\tilde{\boldsymbol{\sigma}}&amp;lt;/math&amp;gt; 为均值与标准差——训练时按当前批次估计，推理时改用滑动平均（原因见下文）。由于 BN 对每个维度独立地缩放和平移，它本身也是一个可逆变换：其雅可比矩阵为对角阵，[[雅可比行列式]]即各维度缩放因子的乘积，可直接加入[[对数似然]]——这使 BN 可以像耦合层一样作为流中的一层。引入 BN 主要是为了稳定深层训练：它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。使用 BN 有一个容易忽略的细节：训练时均值与方差是从当前这一批样本中估计的，因此单个样本的输出会依赖同批次的其他样本——此时的 BN 并不是严格意义上逐样本的可逆变换，其行列式也与整个批量耦合。只有在推理阶段改用训练过程中滑动平均得到的固定统计量，BN 才成为逐样本的、严格可逆的仿射变换，其行列式才能精确计入对数似然。换言之，用训练好的模型评估密度时若忘了做这一切换，同一个输入会因同批样本不同而得到不同的密度值，结果既不可复现也不正确。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的数学原理可分为两个层次。其一是[[标准化流]]的'''通用框架'''：[[变量替换公式]]给出精确似然的表达式，[[最大似然估计]]提供直接的训练目标，无需近似下界或辅助网络——这些结论对一切流模型成立，并非 RealNVP 特有。其二是 RealNVP 的'''特有设计'''：通用框架要求变换可逆且雅可比行列式可算，[[仿射耦合层]]的三角雅可比结构正是为满足这一要求而构造的，它使行列式计算开销与维度成线性关系，从而保证可处理性。本节先介绍通用框架，再阐述 RealNVP 的特化实现，进而比较[[加性耦合]]与仿射耦合在体积保持上的差异，最后讨论训练目标中两项各自的含义与实际训练配置。&lt;br /&gt;
​&lt;br /&gt;
== [[标准化流]]与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数[[雅可比行列式]]）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于[[自回归模型]]（autoregressive model，如 PixelRNN）：这类模型按固定顺序逐维生成数据、每个维度以先前各维为条件，采样必须串行进行——这与流模型对所有维度同时变换形成鲜明对比。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
上一小节的结论适用于一切标准化流。RealNVP 要回答的问题是：如何构造一个既完全可逆、又满足行列式易算的变换？其答案就是仿射耦合层——它对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度网络（如深层卷积网络、残差网络），而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
对公式 (1) 做简单的代数移项，即可得到公式 (2) 给出的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练（这也是标准化流的通用训练方式），等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的分布整体逼近标准高斯，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：增大体积膨胀会提高似然，因此该项处处偏好扩张，与先验项的收缩压力相平衡——净效果是数据密集处扩张、稀疏处收缩，这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布（即数据分布经 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 映射到潜在空间后得到的分布）与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：[[自适应矩估计]]（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt;（原论文取 &amp;lt;math&amp;gt;\alpha = 0.05&amp;lt;/math&amp;gt;）处理，以缓解像素取值集中在 0 和 255 两个边界带来的问题。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像[[密度估计]]与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（见下图）。与[[变分自编码器]]（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等[[自回归模型]]相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png|替代=|居中]]​&lt;br /&gt;
== [[异常检测]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时将似然显著低于正常水平的样本判为异常。也可以把流与重建类模型结合起来，综合似然与[[重建误差]]构造异常分数——下文的 AE-FLOW 即属此类。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。AE-FLOW 将自编码器与以 RealNVP [[仿射耦合层]]为核心的[[标准化流]]瓶颈相结合，在胸部 X 光、光学相干断层扫描、皮肤镜与脑部核磁共振等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的[[负对数似然]]构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
下图展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。|替代=|360x360px|居中]]​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合[[图像修复]]、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（见下图）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png|替代=|居中]]&lt;br /&gt;
​&lt;br /&gt;
== 在 [[NIS]]/[[NIS+]] 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus）中，RealNVP 以[[可逆神经网络]]（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆[[粗粒化]]。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的 INN &amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''——相较于使用独立编码器和解码器的方案，参数量减少一半，如下图所示。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器与解码器（图中记作 φ 与 φ†，分别对应正文中的 &amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt;），信息丢弃与 EI 最大化在宏观动力学空间完成。|替代=|812x812px|居中]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使[[有效信息]]（[[Effective Information]]，[[EI]]）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
RealNVP作为NIS+中可逆表示学习模块，使粗粒化映射与反粗粒化映射共享参数，降低了模型复杂度，同时为有效信息最大化提供了可分析的数学框架。此外，基于可逆粗粒化结构构建的encoder具有通用逼近性质，能够表达不同复杂系统中的粗粒化映射。&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''[[精确似然]]'''：RealNVP 能直接算出任意数据点的精确概率密度——不像 VAE 只能优化似然的一个下界，也不像[[生成对抗网络]]（Generative Adversarial Network，GAN）完全不给密度。因此[[异常检测]]、[[贝叶斯推断]]等需要显式概率值的任务可以直接使用它&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''并行采样与推断'''：[[自回归模型]]必须逐像素按顺序生成，而 RealNVP 编码和解码时所有维度同时计算，生成一张图像只需一次前向传播，采样速度快得多。&lt;br /&gt;
* '''语义[[潜在空间]]'''：由于潜在分布是简单的标准高斯、且映射光滑可逆，在潜在空间中做插值等简单操作，就对应图像上有语义的连续变化（如人脸姿态、表情的渐变），适合表示学习与属性操控&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''架构灵活'''：[[耦合层]]只要求 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 两个网络给出缩放和平移量，对它们的内部结构没有任何限制，可以是任意深度的[[卷积神经网络]]（Convolutional Neural Network，CNN）或[[残差网络]]（Residual Network，ResNet）。提升表达能力可以直接借用主流的深层网络设计，而无须改动可逆框架本身。&lt;br /&gt;
* '''综合多种模型特性'''：RealNVP 同时具备了此前难以兼得的能力——自回归模型的精确似然、VAE 的一步采样，以及不依赖固定重建损失的生成质量。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：RealNVP 对数据分布的拟合精度仍略逊于 PixelRNN 等自回归模型&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在以拟合精度为核心的任务（如[[密度估计]]、[[数据压缩]]）上，这是它的相对劣势——尽管它以并行采样换来了远快于对手的生成速度。&lt;br /&gt;
* '''维度保持约束'''：可逆变换要求输入与输出同维，因此 RealNVP 不能像自编码器那样把高维图像压缩成低维向量——潜在变量的总维度始终与输入相同。[[多尺度架构]]中的变量分解并不改变这一点，它只是让后续层处理更小的特征图，并把潜在变量分配到不同尺度。&lt;br /&gt;
* '''[[仿射耦合]]的表达力上限'''：仿射耦合每层只对一半变量做&amp;quot;缩放加平移&amp;quot;，函数形式较为简单，拟合复杂分布往往需要堆叠很多层。后续工作把变换换成更灵活的函数形式——如 Flow++ 的 Logistic 混合 CDF 耦合&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;、Neural Spline Flows 的[[有理二次样条耦合]]&amp;lt;ref name=&amp;quot;NSF2019&amp;quot;&amp;gt;{{cite journal|author1=Durkan C.|author2=Bekasov A.|author3=Murray I.|author4=Papamakarios G.|title=Neural Spline Flows|journal=Advances in Neural Information Processing Systems (NeurIPS)|year=2019|volume=32|url=https://arxiv.org/abs/1906.04032}}&amp;lt;/ref&amp;gt;——用更少的层数即可达到更强的拟合能力。&lt;br /&gt;
* '''[[均匀去量化]]的次优性'''：像素是 0–255 的离散整数，而 RealNVP 建模的是连续分布。RealNVP 的做法是给像素加上均匀噪声，把离散数据&amp;quot;抹平&amp;quot;成连续的——但这相当于让模型拟合一个被噪声模糊过的分布，与真实分布存在偏差。Flow++ 提出的[[变分去量化]]让噪声分布本身也由模型学习，缓解了这一问题&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''分区固定性'''：掩码决定哪一半变量被变换，而这一划分在训练前就已固定、训练中不再改变；若某层的划分不理想，只能靠后续层弥补。Glow（Generative Flow with Invertible 1×1 Convolutions）改用可学习的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积，让模型自己学会如何重排变量，划分更灵活，生成效果也更好&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''训练显存开销大'''：标准反向传播需要保存每个耦合层的激活值，层数一多显存占用显著上升；RevNet 的[[可逆残差结构]]正是为省去这部分存储而提出的&amp;lt;ref name=&amp;quot;RevNet2017&amp;quot;&amp;gt;{{cite journal|author1=Gomez A. N.|author2=Ren M.|author3=Urtasun R.|author4=Grosse R. B.|title=The Reversible Residual Network: Backpropagation Without Storing Activations|journal=NeurIPS|year=2017|url=https://arxiv.org/abs/1707.04585}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''对分布外样本过度自信'''：上文的异常检测应用依赖&amp;quot;异常样本的似然更低&amp;quot;这一假设，但后续研究发现这一假设未必成立：例如在 CIFAR-10 上训练的 RealNVP，会给截然不同的 SVHN 街景数字图像赋予'''更高'''的似然&amp;lt;ref name=&amp;quot;Nalisnick2019&amp;quot;&amp;gt;{{cite journal|author1=Nalisnick E.|author2=Matsukawa A.|author3=Teh Y. W.|author4=Gorur D.|author5=Lakshminarayanan B.|title=Do Deep Generative Models Know What They Don't Know?|journal=International Conference on Learning Representations (ICLR)|year=2019|url=https://arxiv.org/abs/1810.09136}}&amp;lt;/ref&amp;gt;。似然高低与&amp;quot;是否属于训练分布&amp;quot;并不直接对应，因此基于似然的异常检测需要额外的校准手段。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 与相关框架的关系 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置。下表列举前驱与后继的代表性工作：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=参考文献=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50803</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50803"/>
		<updated>2026-08-24T12:50:19Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：修复部分分段没成功的问题&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2015|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类[[标准化流]]：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体[[对数似然]] &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''[[仿射耦合层]]'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''[[批量归一化]]'''（batch normalization）以稳定训练；'''[[压缩]]'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''[[变量分解]]'''（factor out）负责在每个尺度末尾抽取变量作为潜在变量。其中，仿射耦合层通过空间[[棋盘掩码]]（checkerboard masking）与[[通道级掩码]]（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
RealNVP 的多尺度架构如下图所示。输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第 1 尺度。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量[[归一化]]）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后进行变量分解；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化），随后把全部变量抽出作为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;，不再保留传递变量。非末尺度的变量分解把当前特征一分为二：一半作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;，其中末尺度因全部抽出而与其前一尺度等长），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|848x848px|居中]]&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
下图给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构；图像任务中通常用[[卷积神经网络]]（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的[[雅可比矩阵]]是下三角块结构，行列式即为对角线元素的乘积——一般的 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维矩阵求行列式需要 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 的计算量，而这里只需把对角元相乘，[[计算复杂度]]降为 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 两种掩码分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分：保持不变的一半称为'''被动'''部分，被变换的一半称为'''主动'''部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠使用不同掩码的耦合层，每个维度都能在不同层中分别充当被动与主动变量，信息得以充分混合。原论文的多尺度架构中，每个尺度内同一掩码的耦合层固定为 3 层（末尺度为 4 层）&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
== [[多尺度架构]]：分层打包[[潜在变量]] ==&lt;br /&gt;
​&lt;br /&gt;
如上图所示，多尺度架构依赖两个关键操作：[[压缩]]与[[变量分解]]。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在[[通道方向]]上重排，将 &amp;lt;math&amp;gt;h\times w\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;h/2 \times w/2 \times 4c&amp;lt;/math&amp;gt;。[[空间分辨率]]减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个非末尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度；末尺度则将全部变量提取为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''[[层次化表示]]'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''[[梯度分散]]'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
== [[批量归一化]]：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量[[归一化]]（Batch Normalization，BN）应用于每个[[耦合层]]的输出之后。其映射为&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;，&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\tilde{\boldsymbol{\mu}}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\tilde{\boldsymbol{\sigma}}&amp;lt;/math&amp;gt; 为均值与标准差——训练时按当前批次估计，推理时改用滑动平均（原因见下文）。由于 BN 对每个维度独立地缩放和平移，它本身也是一个可逆变换：其雅可比矩阵为对角阵，[[雅可比行列式]]即各维度缩放因子的乘积，可直接加入[[对数似然]]——这使 BN 可以像耦合层一样作为流中的一层。引入 BN 主要是为了稳定深层训练：它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。使用 BN 有一个容易忽略的细节：训练时均值与方差是从当前这一批样本中估计的，因此单个样本的输出会依赖同批次的其他样本——此时的 BN 并不是严格意义上逐样本的可逆变换，其行列式也与整个批量耦合。只有在推理阶段改用训练过程中滑动平均得到的固定统计量，BN 才成为逐样本的、严格可逆的仿射变换，其行列式才能精确计入对数似然。换言之，用训练好的模型评估密度时若忘了做这一切换，同一个输入会因同批样本不同而得到不同的密度值，结果既不可复现也不正确。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的数学设计围绕一个核心目标展开：让概率密度的计算既精确又高效。这一目标由三个要素支撑——概率论中的[[变量替换公式]]给出精确似然的表达式；[[仿射耦合层]]的三角雅可比结构使行列式计算开销与维度成线性关系，从而保证可处理性；[[最大似然估计]]则提供直接的训练目标，无需近似下界或辅助网络。本节依次介绍这三个要素，进而比较[[加性耦合]]与仿射耦合在体积保持上的差异，最后讨论训练目标中两项各自的含义与实际训练配置。&lt;br /&gt;
​&lt;br /&gt;
== [[标准化流]]与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数[[雅可比行列式]]）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于[[自回归模型]]（autoregressive model，如 PixelRNN）：这类模型按固定顺序逐维生成数据、每个维度以先前各维为条件，采样必须串行进行——这与流模型对所有维度同时变换形成鲜明对比。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度网络（如深层卷积网络、残差网络），而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
对公式 (1) 做简单的代数移项，即可得到公式 (2) 给出的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的分布整体逼近标准高斯，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：增大体积膨胀会提高似然，因此该项处处偏好扩张，与先验项的收缩压力相平衡——净效果是数据密集处扩张、稀疏处收缩，这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布（即数据分布经 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 映射到潜在空间后得到的分布）与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：[[自适应矩估计]]（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt;（原论文取 &amp;lt;math&amp;gt;\alpha = 0.05&amp;lt;/math&amp;gt;）处理，以缓解像素取值集中在 0 和 255 两个边界带来的问题。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像[[密度估计]]与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（见下图）。与[[变分自编码器]]（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等[[自回归模型]]相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png|替代=|居中]]​&lt;br /&gt;
== [[异常检测]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时将似然显著低于正常水平的样本判为异常。也可以把流与重建类模型结合起来，综合似然与[[重建误差]]构造异常分数——下文的 AE-FLOW 即属此类。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。AE-FLOW 将自编码器与以 RealNVP [[仿射耦合层]]为核心的[[标准化流]]瓶颈相结合，在胸部 X 光、光学相干断层扫描、皮肤镜与脑部核磁共振等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的[[负对数似然]]构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
下图展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。|替代=|360x360px|居中]]​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合[[图像修复]]、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（见下图）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png|替代=|居中]]&lt;br /&gt;
​&lt;br /&gt;
== 在 [[NIS]]/[[NIS+]] 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus）中，RealNVP 以[[可逆神经网络]]（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆[[粗粒化]]。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的 INN &amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''——相较于使用独立编码器和解码器的方案，参数量减少一半，如下图所示。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器与解码器（图中记作 φ 与 φ†，分别对应正文中的 &amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt;），信息丢弃与 EI 最大化在宏观动力学空间完成。|替代=|812x812px|居中]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使[[有效信息]]（[[Effective Information]]，[[EI]]）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
RealNVP作为NIS+中可逆表示学习模块，使粗粒化映射与反粗粒化映射共享参数，降低了模型复杂度，同时为有效信息最大化提供了可分析的数学框架。此外，基于可逆粗粒化结构构建的encoder具有通用逼近性质，能够表达不同复杂系统中的粗粒化映射。&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''[[精确似然]]'''：RealNVP 能直接算出任意数据点的精确概率密度——不像 VAE 只能优化似然的一个下界，也不像[[生成对抗网络]]（Generative Adversarial Network，GAN）完全不给密度。因此[[异常检测]]、[[贝叶斯推断]]等需要显式概率值的任务可以直接使用它&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''并行采样与推断'''：[[自回归模型]]必须逐像素按顺序生成，而 RealNVP 编码和解码时所有维度同时计算，生成一张图像只需一次前向传播，采样速度快得多。&lt;br /&gt;
* '''语义[[潜在空间]]'''：由于潜在分布是简单的标准高斯、且映射光滑可逆，在潜在空间中做插值等简单操作，就对应图像上有语义的连续变化（如人脸姿态、表情的渐变），适合表示学习与属性操控&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''架构灵活'''：[[耦合层]]只要求 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 两个网络给出缩放和平移量，对它们的内部结构没有任何限制，可以是任意深度的[[卷积神经网络]]（Convolutional Neural Network，CNN）或[[残差网络]]（Residual Network，ResNet）。提升表达能力可以直接借用主流的深层网络设计，而无须改动可逆框架本身。&lt;br /&gt;
* '''综合多种模型特性'''：RealNVP 同时具备了此前难以兼得的能力——自回归模型的精确似然、VAE 的一步采样，以及不依赖固定重建损失的生成质量。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：RealNVP 对数据分布的拟合精度仍略逊于 PixelRNN 等自回归模型&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在以拟合精度为核心的任务（如[[密度估计]]、[[数据压缩]]）上，这是它的相对劣势——尽管它以并行采样换来了远快于对手的生成速度。&lt;br /&gt;
* '''维度保持约束'''：可逆变换要求输入与输出同维，因此 RealNVP 不能像自编码器那样把高维图像压缩成低维向量——潜在变量的总维度始终与输入相同。[[多尺度架构]]中的变量分解并不改变这一点，它只是让后续层处理更小的特征图，并把潜在变量分配到不同尺度。&lt;br /&gt;
* '''[[仿射耦合]]的表达力上限'''：仿射耦合每层只对一半变量做&amp;quot;缩放加平移&amp;quot;，函数形式较为简单，拟合复杂分布往往需要堆叠很多层。后续工作把变换换成更灵活的函数形式——如 Flow++ 的 Logistic 混合 CDF 耦合&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;、Neural Spline Flows 的[[有理二次样条耦合]]&amp;lt;ref name=&amp;quot;NSF2019&amp;quot;&amp;gt;{{cite journal|author1=Durkan C.|author2=Bekasov A.|author3=Murray I.|author4=Papamakarios G.|title=Neural Spline Flows|journal=Advances in Neural Information Processing Systems (NeurIPS)|year=2019|volume=32|url=https://arxiv.org/abs/1906.04032}}&amp;lt;/ref&amp;gt;——用更少的层数即可达到更强的拟合能力。&lt;br /&gt;
* '''[[均匀去量化]]的次优性'''：像素是 0–255 的离散整数，而 RealNVP 建模的是连续分布。RealNVP 的做法是给像素加上均匀噪声，把离散数据&amp;quot;抹平&amp;quot;成连续的——但这相当于让模型拟合一个被噪声模糊过的分布，与真实分布存在偏差。Flow++ 提出的[[变分去量化]]让噪声分布本身也由模型学习，缓解了这一问题&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''分区固定性'''：掩码决定哪一半变量被变换，而这一划分在训练前就已固定、训练中不再改变；若某层的划分不理想，只能靠后续层弥补。Glow（Generative Flow with Invertible 1×1 Convolutions）改用可学习的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积，让模型自己学会如何重排变量，划分更灵活，生成效果也更好&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''训练显存开销大'''：标准反向传播需要保存每个耦合层的激活值，层数一多显存占用显著上升；RevNet 的[[可逆残差结构]]正是为省去这部分存储而提出的&amp;lt;ref name=&amp;quot;RevNet2017&amp;quot;&amp;gt;{{cite journal|author1=Gomez A. N.|author2=Ren M.|author3=Urtasun R.|author4=Grosse R. B.|title=The Reversible Residual Network: Backpropagation Without Storing Activations|journal=NeurIPS|year=2017|url=https://arxiv.org/abs/1707.04585}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''对分布外样本过度自信'''：上文的异常检测应用依赖&amp;quot;异常样本的似然更低&amp;quot;这一假设，但后续研究发现这一假设未必成立：例如在 CIFAR-10 上训练的 RealNVP，会给截然不同的 SVHN 街景数字图像赋予'''更高'''的似然&amp;lt;ref name=&amp;quot;Nalisnick2019&amp;quot;&amp;gt;{{cite journal|author1=Nalisnick E.|author2=Matsukawa A.|author3=Teh Y. W.|author4=Gorur D.|author5=Lakshminarayanan B.|title=Do Deep Generative Models Know What They Don't Know?|journal=International Conference on Learning Representations (ICLR)|year=2019|url=https://arxiv.org/abs/1810.09136}}&amp;lt;/ref&amp;gt;。似然高低与&amp;quot;是否属于训练分布&amp;quot;并不直接对应，因此基于似然的异常检测需要额外的校准手段。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 与相关框架的关系 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置。下表列举前驱与后继的代表性工作：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=参考文献=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50794</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50794"/>
		<updated>2026-08-22T07:32:13Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：严谨化一些表述&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2015|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类[[标准化流]]：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体[[对数似然]] &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''[[仿射耦合层]]'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''[[批量归一化]]'''（batch normalization）以稳定训练；'''[[压缩]]'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''[[变量分解]]'''（factor out）负责在每个尺度末尾抽取变量作为潜在变量。其中，仿射耦合层通过空间[[棋盘掩码]]（checkerboard masking）与[[通道级掩码]]（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的多尺度架构如下图所示。输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第 1 尺度。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量[[归一化]]）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后进行变量分解；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化），随后把全部变量抽出作为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;，不再保留传递变量。非末尺度的变量分解把当前特征一分为二：一半作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;，其中末尺度因全部抽出而与其前一尺度等长），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|848x848px|居中]]&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
下图给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构；图像任务中通常用[[卷积神经网络]]（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的[[雅可比矩阵]]是下三角块结构，行列式即为对角线元素的乘积——一般的 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维矩阵求行列式需要 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 的计算量，而这里只需把对角元相乘，[[计算复杂度]]降为 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 两种掩码分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分：保持不变的一半称为'''被动'''部分，被变换的一半称为'''主动'''部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠使用不同掩码的耦合层，每个维度都能在不同层中分别充当被动与主动变量，信息得以充分混合。原论文的多尺度架构中，每个尺度内同一掩码的耦合层固定为 3 层（末尺度为 4 层）&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
== [[多尺度架构]]：分层打包[[潜在变量]] ==&lt;br /&gt;
​&lt;br /&gt;
如上图所示，多尺度架构依赖两个关键操作：[[压缩]]与[[变量分解]]。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在[[通道方向]]上重排，将 &amp;lt;math&amp;gt;h\times w\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;h/2 \times w/2 \times 4c&amp;lt;/math&amp;gt;。[[空间分辨率]]减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个非末尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度；末尺度则将全部变量提取为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''[[层次化表示]]'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''[[梯度分散]]'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
== [[批量归一化]]：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量[[归一化]]（Batch Normalization，BN）应用于每个[[耦合层]]的输出之后。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\tilde{\boldsymbol{\mu}}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\tilde{\boldsymbol{\sigma}}&amp;lt;/math&amp;gt; 为均值与标准差——训练时按当前批次估计，推理时改用滑动平均（原因见下文）。由于 BN 对每个维度独立地缩放和平移，它本身也是一个可逆变换：其雅可比矩阵为对角阵，[[雅可比行列式]]即各维度缩放因子的乘积，可直接加入[[对数似然]]——这使 BN 可以像耦合层一样作为流中的一层。引入 BN 主要是为了稳定深层训练：它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。使用 BN 有一个容易忽略的细节：训练时均值与方差是从当前这一批样本中估计的，因此单个样本的输出会依赖同批次的其他样本——此时的 BN 并不是严格意义上逐样本的可逆变换，其行列式也与整个批量耦合。只有在推理阶段改用训练过程中滑动平均得到的固定统计量，BN 才成为逐样本的、严格可逆的仿射变换，其行列式才能精确计入对数似然。换言之，用训练好的模型评估密度时若忘了做这一切换，同一个输入会因同批样本不同而得到不同的密度值，结果既不可复现也不正确。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的数学设计围绕一个核心目标展开：让概率密度的计算既精确又高效。这一目标由三个要素支撑——概率论中的变量替换公式给出精确似然的表达式；仿射耦合层的三角雅可比结构使行列式计算开销与维度成线性关系，从而保证可处理性；最大似然估计则提供直接的训练目标，无需近似下界或辅助网络。本节依次介绍这三个要素，进而比较加性耦合与仿射耦合在体积保持上的差异，最后讨论训练目标中两项各自的含义与实际训练配置。&lt;br /&gt;
​&lt;br /&gt;
== 标准化流与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于[[自回归模型]]（autoregressive model，如 PixelRNN）：这类模型按固定顺序逐维生成数据、每个维度以先前各维为条件，采样必须串行进行——这与流模型对所有维度同时变换形成鲜明对比。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度网络（如深层卷积网络、残差网络），而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
对公式 (1) 做简单的代数移项，即可得到公式 (2) 给出的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的分布整体逼近标准高斯，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：增大体积膨胀会提高似然，因此该项处处偏好扩张，与先验项的收缩压力相平衡——净效果是数据密集处扩张、稀疏处收缩，这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布（即数据分布经 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 映射到潜在空间后得到的分布）与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：自适应矩估计（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt;（原论文取 &amp;lt;math&amp;gt;\alpha = 0.05&amp;lt;/math&amp;gt;）处理，以缓解像素取值集中在 0 和 255 两个边界带来的问题。&lt;br /&gt;
​&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（见下图）。与变分自编码器（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等自回归模型相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png|替代=|居中]]​&lt;br /&gt;
== 异常检测 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时将似然显著低于正常水平的样本判为异常。也可以把流与重建类模型结合起来，综合似然与重建误差构造异常分数——下文的 AE-FLOW 即属此类。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。&lt;br /&gt;
​&lt;br /&gt;
AE-FLOW 将自编码器与以 RealNVP 仿射耦合层为核心的标准化流瓶颈相结合，在胸部 X 光、光学相干断层扫描、皮肤镜与脑部核磁共振等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的负对数似然构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
下图展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。|替代=|360x360px|居中]]​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合图像修复、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（见下图）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png|替代=|居中]]&lt;br /&gt;
​&lt;br /&gt;
== 在 NIS/NIS+ 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus）中，RealNVP 以可逆神经网络（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆粗粒化。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的 INN &amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''——相较于使用独立编码器和解码器的方案，参数量减少一半，如下图所示。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器与解码器（图中记作 φ 与 φ†，分别对应正文中的 &amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt;），信息丢弃与 EI 最大化在宏观动力学空间完成。|替代=|812x812px|居中]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（Effective Information，EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''精确似然'''：RealNVP 能直接算出任意数据点的精确概率密度——不像 VAE 只能优化似然的一个下界，也不像生成对抗网络（Generative Adversarial Network，GAN）完全不给密度。因此异常检测、贝叶斯推断等需要显式概率值的任务可以直接使用它&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''并行采样与推断'''：自回归模型必须逐像素按顺序生成，而 RealNVP 编码和解码时所有维度同时计算，生成一张图像只需一次前向传播，采样速度快得多。&lt;br /&gt;
* '''语义潜在空间'''：由于潜在分布是简单的标准高斯、且映射光滑可逆，在潜在空间中做插值等简单操作，就对应图像上有语义的连续变化（如人脸姿态、表情的渐变），适合表示学习与属性操控&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''架构灵活'''：耦合层只要求 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 两个网络给出缩放和平移量，对它们的内部结构没有任何限制，可以是任意深度的卷积神经网络（Convolutional Neural Network，CNN）或残差网络（Residual Network，ResNet）。提升表达能力可以直接借用主流的深层网络设计，而无须改动可逆框架本身。&lt;br /&gt;
* '''综合多种模型特性'''：RealNVP 同时具备了此前难以兼得的能力——自回归模型的精确似然、VAE 的一步采样，以及不依赖固定重建损失的生成质量。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：RealNVP 对数据分布的拟合精度仍略逊于 PixelRNN 等自回归模型&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在以拟合精度为核心的任务（如密度估计、数据压缩）上，这是它的相对劣势——尽管它以并行采样换来了远快于对手的生成速度。&lt;br /&gt;
* '''维度保持约束'''：可逆变换要求输入与输出同维，因此 RealNVP 不能像自编码器那样把高维图像压缩成低维向量——潜在变量的总维度始终与输入相同。多尺度架构中的变量分解并不改变这一点，它只是让后续层处理更小的特征图，并把潜在变量分配到不同尺度。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：仿射耦合每层只对一半变量做&amp;quot;缩放加平移&amp;quot;，函数形式较为简单，拟合复杂分布往往需要堆叠很多层。后续工作把变换换成更灵活的函数形式——如 Flow++ 的 Logistic 混合 CDF 耦合&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;、Neural Spline Flows 的有理二次样条耦合&amp;lt;ref name=&amp;quot;NSF2019&amp;quot;&amp;gt;{{cite journal|author1=Durkan C.|author2=Bekasov A.|author3=Murray I.|author4=Papamakarios G.|title=Neural Spline Flows|journal=Advances in Neural Information Processing Systems (NeurIPS)|year=2019|volume=32|url=https://arxiv.org/abs/1906.04032}}&amp;lt;/ref&amp;gt;——用更少的层数即可达到更强的拟合能力。&lt;br /&gt;
* '''均匀去量化的次优性'''：像素是 0–255 的离散整数，而 RealNVP 建模的是连续分布。RealNVP 的做法是给像素加上均匀噪声，把离散数据&amp;quot;抹平&amp;quot;成连续的——但这相当于让模型拟合一个被噪声模糊过的分布，与真实分布存在偏差。Flow++ 提出的变分去量化让噪声分布本身也由模型学习，缓解了这一问题&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''分区固定性'''：掩码决定哪一半变量被变换，而这一划分在训练前就已固定、训练中不再改变；若某层的划分不理想，只能靠后续层弥补。Glow（Generative Flow with Invertible 1×1 Convolutions）改用可学习的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积，让模型自己学会如何重排变量，划分更灵活，生成效果也更好&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''训练显存开销大'''：标准反向传播需要保存每个耦合层的激活值，层数一多显存占用显著上升；RevNet 的可逆残差结构正是为省去这部分存储而提出的&amp;lt;ref name=&amp;quot;RevNet2017&amp;quot;&amp;gt;{{cite journal|author1=Gomez A. N.|author2=Ren M.|author3=Urtasun R.|author4=Grosse R. B.|title=The Reversible Residual Network: Backpropagation Without Storing Activations|journal=NeurIPS|year=2017|url=https://arxiv.org/abs/1707.04585}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''对分布外样本过度自信'''：上文的异常检测应用依赖&amp;quot;异常样本的似然更低&amp;quot;这一假设，但后续研究发现这一假设未必成立：例如在 CIFAR-10 上训练的 RealNVP，会给截然不同的 SVHN 街景数字图像赋予'''更高'''的似然&amp;lt;ref name=&amp;quot;Nalisnick2019&amp;quot;&amp;gt;{{cite journal|author1=Nalisnick E.|author2=Matsukawa A.|author3=Teh Y. W.|author4=Gorur D.|author5=Lakshminarayanan B.|title=Do Deep Generative Models Know What They Don't Know?|journal=International Conference on Learning Representations (ICLR)|year=2019|url=https://arxiv.org/abs/1810.09136}}&amp;lt;/ref&amp;gt;。似然高低与&amp;quot;是否属于训练分布&amp;quot;并不直接对应，因此基于似然的异常检测需要额外的校准手段。&lt;br /&gt;
​&lt;br /&gt;
= 与相关框架的关系 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置。下表列举前驱与后继的代表性工作：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50786</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50786"/>
		<updated>2026-08-19T13:44:55Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：仿射耦合层图片居中&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation，NICE），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类[[标准化流]]：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体[[对数似然]] &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''[[仿射耦合层]]'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''[[批量归一化]]'''（batch normalization）以稳定训练；'''[[压缩]]'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''[[变量分解]]'''（factor out）负责在每个尺度末尾抽取变量作为潜在变量。其中，仿射耦合层通过空间[[棋盘掩码]]（checkerboard masking）与[[通道级掩码]]（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的多尺度架构如下图所示。输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第 1 尺度。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量[[归一化]]）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后进行变量分解；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化），随后把全部变量抽出作为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;，不再保留传递变量。非末尺度的变量分解把当前特征一分为二：一半作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|848x848px|居中]]&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
下图给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|626x626px|居中]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构，因为常用于图像相关的任务，所以人们通常用[[卷积神经网络]]（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的[[雅可比矩阵]]是下三角块结构，行列式即为对角线元素的乘积——一般的 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维矩阵求行列式需要 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 的计算量，而这里只需把对角元相乘，[[计算复杂度]]降为 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分：保持不变的一半称为'''被动'''部分，被变换的一半称为'''主动'''部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠使用不同掩码的耦合层，每个维度都能在不同层中分别充当被动与主动变量，信息得以充分混合。原论文的多尺度架构中，每个尺度内同一掩码的耦合层固定为 3 层（末尺度为 4 层）&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
== [[多尺度架构]]：分层打包[[潜在变量]] ==&lt;br /&gt;
​&lt;br /&gt;
如上图所示，多尺度架构依赖两个关键操作：[[压缩]]与[[变量分解]]。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在[[通道方向]]上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。[[空间分辨率]]减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个非末尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度；末尺度则将全部变量提取为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''[[层次化表示]]'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''[[梯度分散]]'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
== [[批量归一化]]：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量[[归一化]]（Batch Normalization，BN）应用于每个[[耦合层]]的输出之后。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于 BN 对每个维度独立地缩放和平移，它本身也是一个可逆变换：其雅可比矩阵为对角阵，[[雅可比行列式]]即各维度缩放因子的乘积，可直接加入[[对数似然]]——这使 BN 可以像耦合层一样作为流中的一层。引入 BN 主要是为了稳定深层训练：它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。使用 BN 有一个容易忽略的细节：训练时均值与方差是从当前这一批样本中估计的，因此单个样本的输出会依赖同批次的其他样本——此时的 BN 并不是严格意义上逐样本的可逆变换，其行列式也与整个批量耦合。只有在推理阶段改用训练过程中滑动平均得到的固定统计量，BN 才成为逐样本的、严格可逆的仿射变换，其行列式才能精确计入对数似然。换言之，用训练好的模型评估密度时若忘了做这一切换，同一个输入会因同批样本不同而得到不同的密度值，结果既不可复现也不正确。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的数学设计围绕一个核心目标展开：让概率密度的计算既精确又高效。这一目标由三个要素支撑——概率论中的变量替换公式给出精确似然的表达式；仿射耦合层的三角雅可比结构使行列式计算开销与维度成线性关系，从而保证可处理性；最大似然估计则提供直接的训练目标，无需近似下界或辅助网络。本节依次介绍这三个要素，并讨论训练目标中两项各自的含义与实际训练配置。&lt;br /&gt;
​&lt;br /&gt;
== 标准化流与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于[[自回归模型]]（autoregressive model，如 PixelRNN）：这类模型按固定顺序逐维生成数据、每个维度以先前各维为条件，采样必须串行进行——这与流模型对所有维度同时变换形成鲜明对比。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
对公式 (1) 做简单的代数移项，即可得到公式 (2) 给出的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的分布整体逼近标准高斯，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：自适应矩估计（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
​&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（见下图）。与变分自编码器（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等自回归模型相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png|替代=|居中]]​&lt;br /&gt;
== 异常检测 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时将似然显著低于正常水平的样本判为异常。也可以把流与重建类模型结合起来，综合似然与重建误差构造异常分数——下文的 AE-FLOW 即属此类。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。&lt;br /&gt;
​&lt;br /&gt;
AE-FLOW 将自编码器与以 RealNVP 仿射耦合层为核心的标准化流瓶颈相结合，在胸部 X 光、光学相干断层扫描、皮肤镜与脑部核磁共振等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的负对数似然构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
下图展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。|替代=|360x360px|居中]]​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合图像修复、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（见下图）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png|替代=|居中]]&lt;br /&gt;
​&lt;br /&gt;
== 在 NIS/NIS+ 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus，NIS+）中，RealNVP 以可逆神经网络（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆粗粒化。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的 INN &amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''——相较于使用独立编码器和解码器的方案，参数量减少一半，如下图所示。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器与解码器（图中记作 φ 与 φ†，分别对应正文中的 &amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt;），信息丢弃与 EI 最大化在宏观动力学空间完成。|替代=|812x812px|居中]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（Effective Information，EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''精确似然'''：RealNVP 能直接算出任意数据点的精确概率密度——不像 VAE 只能优化似然的一个下界，也不像生成对抗网络（Generative Adversarial Network，GAN）完全不给密度。因此异常检测、贝叶斯推断等需要显式概率值的任务可以直接使用它&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''并行采样与推断'''：自回归模型必须逐像素按顺序生成，而 RealNVP 编码和解码时所有维度同时计算，生成一张图像只需一次前向传播，采样速度快得多。&lt;br /&gt;
* '''语义潜在空间'''：由于潜在分布是简单的标准高斯、且映射光滑可逆，在潜在空间中做插值等简单操作，就对应图像上有语义的连续变化（如人脸姿态、表情的渐变），适合表示学习与属性操控&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''架构灵活'''：耦合层只要求 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 两个网络给出缩放和平移量，对它们的内部结构没有任何限制，可以是任意深度的卷积神经网络（Convolutional Neural Network，CNN）或残差网络（Residual Network，ResNet）。提升表达能力可以直接借用主流的深层网络设计，而无须改动可逆框架本身。&lt;br /&gt;
* '''综合多种模型特性'''：RealNVP 同时具备了此前难以兼得的能力——自回归模型的精确似然、VAE 的一步采样，以及不依赖固定重建损失的生成质量。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：RealNVP 对数据分布的拟合精度仍略逊于 PixelRNN 等自回归模型&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在以拟合精度为核心的任务（如密度估计、数据压缩）上，这是它的相对劣势——尽管它以并行采样换来了远快于对手的生成速度。&lt;br /&gt;
* '''维度保持约束'''：可逆变换要求输入与输出同维，因此 RealNVP 不能像自编码器那样把高维图像直接压缩成低维向量——潜在空间和数据空间一样大。所需的&amp;quot;降维&amp;quot;只能靠多尺度架构中的变量分解间接实现，即在各尺度逐步把一部分变量固定为潜在变量。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：仿射耦合每层只对一半变量做&amp;quot;缩放加平移&amp;quot;，函数形式较为简单，拟合复杂分布往往需要堆叠很多层。后续工作把变换换成更灵活的函数形式——如 Flow++ 的 Logistic 混合 CDF 耦合&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;、Neural Spline Flows 的有理二次样条耦合&amp;lt;ref name=&amp;quot;NSF2019&amp;quot;&amp;gt;{{cite journal|author1=Durkan C.|author2=Bekasov A.|author3=Murray I.|author4=Papamakarios G.|title=Neural Spline Flows|journal=Advances in Neural Information Processing Systems (NeurIPS)|year=2019|volume=32|url=https://arxiv.org/abs/1906.04032}}&amp;lt;/ref&amp;gt;——用更少的层数即可达到更强的拟合能力。&lt;br /&gt;
* '''均匀去量化的次优性'''：像素是 0–255 的离散整数，而 RealNVP 建模的是连续分布。RealNVP 的做法是给像素加上均匀噪声，把离散数据&amp;quot;抹平&amp;quot;成连续的——但这相当于让模型拟合一个被噪声模糊过的分布，与真实分布存在偏差。Flow++ 提出的变分去量化让噪声分布本身也由模型学习，缓解了这一问题&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''分区固定性'''：掩码决定哪一半变量被变换，而这一划分在训练前就已固定、训练中不再改变；若某层的划分不理想，只能靠后续层弥补。Glow（Generative Flow with Invertible 1×1 Convolutions，Glow）改用可学习的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积，让模型自己学会如何重排变量，划分更灵活，生成效果也更好&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''训练显存开销大'''：标准反向传播需要保存每个耦合层的激活值，层数一多显存占用显著上升；RevNet 的可逆残差结构正是为省去这部分存储而提出的&amp;lt;ref name=&amp;quot;RevNet2017&amp;quot;&amp;gt;{{cite journal|author1=Gomez A. N.|author2=Ren M.|author3=Urtasun R.|author4=Grosse R. B.|title=The Reversible Residual Network: Backpropagation Without Storing Activations|journal=NeurIPS|year=2017|url=https://arxiv.org/abs/1707.04585}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''对分布外样本过度自信'''：上文的异常检测应用依赖&amp;quot;异常样本的似然更低&amp;quot;这一假设，但后续研究发现这一假设未必成立：例如在 CIFAR-10 上训练的 RealNVP，会给截然不同的 SVHN 街景数字图像赋予'''更高'''的似然&amp;lt;ref name=&amp;quot;Nalisnick2019&amp;quot;&amp;gt;{{cite journal|author1=Nalisnick E.|author2=Matsukawa A.|author3=Teh Y. W.|author4=Gorur D.|author5=Lakshminarayanan B.|title=Do Deep Generative Models Know What They Don't Know?|journal=International Conference on Learning Representations (ICLR)|year=2019|url=https://arxiv.org/abs/1810.09136}}&amp;lt;/ref&amp;gt;。似然高低与&amp;quot;是否属于训练分布&amp;quot;并不直接对应，因此基于似然的异常检测需要额外的校准手段。&lt;br /&gt;
​&lt;br /&gt;
= 与相关框架的关系 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow，MAF）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics，FFJORD）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50785</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50785"/>
		<updated>2026-08-19T13:43:54Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：1. 表述严谨化：先验项改为&amp;quot;促使编码后的分布整体逼近标准高斯&amp;quot;。 2. 内部自洽：多尺度架构的四处描述统一为&amp;quot;非末尺度抽一半、末尺度全部抽出&amp;quot;；删去重复的&amp;quot;一分为二&amp;quot;；层数表述统一为&amp;quot;同一掩码 3 层、末尺度 4 层&amp;quot;。 3. 去重：删除应用小节中与后文重复的&amp;quot;采样快于自回归模型&amp;quot;一句。 4. 图注：NIS+ 图注注明图中 φ、φ† 与正文 ψ_α、ψ_α⁻¹ 的对应。 5. 异常检测：拆开纯似然判据与重建误差，后者归入 AE-FLOW 语境。&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation，NICE），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类[[标准化流]]：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体[[对数似然]] &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''[[仿射耦合层]]'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''[[批量归一化]]'''（batch normalization）以稳定训练；'''[[压缩]]'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''[[变量分解]]'''（factor out）负责在每个尺度末尾抽取变量作为潜在变量。其中，仿射耦合层通过空间[[棋盘掩码]]（checkerboard masking）与[[通道级掩码]]（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的多尺度架构如下图所示。输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第 1 尺度。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量[[归一化]]）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后进行变量分解；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化），随后把全部变量抽出作为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;，不再保留传递变量。非末尺度的变量分解把当前特征一分为二：一半作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|848x848px|居中]]&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
下图给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构，因为常用于图像相关的任务，所以人们通常用[[卷积神经网络]]（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的[[雅可比矩阵]]是下三角块结构，行列式即为对角线元素的乘积——一般的 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维矩阵求行列式需要 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 的计算量，而这里只需把对角元相乘，[[计算复杂度]]降为 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分：保持不变的一半称为'''被动'''部分，被变换的一半称为'''主动'''部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠使用不同掩码的耦合层，每个维度都能在不同层中分别充当被动与主动变量，信息得以充分混合。原论文的多尺度架构中，每个尺度内同一掩码的耦合层固定为 3 层（末尺度为 4 层）&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
== [[多尺度架构]]：分层打包[[潜在变量]] ==&lt;br /&gt;
​&lt;br /&gt;
如上图所示，多尺度架构依赖两个关键操作：[[压缩]]与[[变量分解]]。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在[[通道方向]]上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。[[空间分辨率]]减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个非末尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度；末尺度则将全部变量提取为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''[[层次化表示]]'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''[[梯度分散]]'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
== [[批量归一化]]：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量[[归一化]]（Batch Normalization，BN）应用于每个[[耦合层]]的输出之后。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于 BN 对每个维度独立地缩放和平移，它本身也是一个可逆变换：其雅可比矩阵为对角阵，[[雅可比行列式]]即各维度缩放因子的乘积，可直接加入[[对数似然]]——这使 BN 可以像耦合层一样作为流中的一层。引入 BN 主要是为了稳定深层训练：它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。使用 BN 有一个容易忽略的细节：训练时均值与方差是从当前这一批样本中估计的，因此单个样本的输出会依赖同批次的其他样本——此时的 BN 并不是严格意义上逐样本的可逆变换，其行列式也与整个批量耦合。只有在推理阶段改用训练过程中滑动平均得到的固定统计量，BN 才成为逐样本的、严格可逆的仿射变换，其行列式才能精确计入对数似然。换言之，用训练好的模型评估密度时若忘了做这一切换，同一个输入会因同批样本不同而得到不同的密度值，结果既不可复现也不正确。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的数学设计围绕一个核心目标展开：让概率密度的计算既精确又高效。这一目标由三个要素支撑——概率论中的变量替换公式给出精确似然的表达式；仿射耦合层的三角雅可比结构使行列式计算开销与维度成线性关系，从而保证可处理性；最大似然估计则提供直接的训练目标，无需近似下界或辅助网络。本节依次介绍这三个要素，并讨论训练目标中两项各自的含义与实际训练配置。&lt;br /&gt;
​&lt;br /&gt;
== 标准化流与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于[[自回归模型]]（autoregressive model，如 PixelRNN）：这类模型按固定顺序逐维生成数据、每个维度以先前各维为条件，采样必须串行进行——这与流模型对所有维度同时变换形成鲜明对比。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
对公式 (1) 做简单的代数移项，即可得到公式 (2) 给出的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的分布整体逼近标准高斯，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：自适应矩估计（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
​&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（见下图）。与变分自编码器（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等自回归模型相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png|替代=|居中]]​&lt;br /&gt;
== 异常检测 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时将似然显著低于正常水平的样本判为异常。也可以把流与重建类模型结合起来，综合似然与重建误差构造异常分数——下文的 AE-FLOW 即属此类。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。&lt;br /&gt;
​&lt;br /&gt;
AE-FLOW 将自编码器与以 RealNVP 仿射耦合层为核心的标准化流瓶颈相结合，在胸部 X 光、光学相干断层扫描、皮肤镜与脑部核磁共振等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的负对数似然构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
下图展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。|替代=|360x360px|居中]]​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合图像修复、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（见下图）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png|替代=|居中]]&lt;br /&gt;
​&lt;br /&gt;
== 在 NIS/NIS+ 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus，NIS+）中，RealNVP 以可逆神经网络（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆粗粒化。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的 INN &amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''——相较于使用独立编码器和解码器的方案，参数量减少一半，如下图所示。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器与解码器（图中记作 φ 与 φ†，分别对应正文中的 &amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt;），信息丢弃与 EI 最大化在宏观动力学空间完成。|替代=|812x812px|居中]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（Effective Information，EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''精确似然'''：RealNVP 能直接算出任意数据点的精确概率密度——不像 VAE 只能优化似然的一个下界，也不像生成对抗网络（Generative Adversarial Network，GAN）完全不给密度。因此异常检测、贝叶斯推断等需要显式概率值的任务可以直接使用它&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''并行采样与推断'''：自回归模型必须逐像素按顺序生成，而 RealNVP 编码和解码时所有维度同时计算，生成一张图像只需一次前向传播，采样速度快得多。&lt;br /&gt;
* '''语义潜在空间'''：由于潜在分布是简单的标准高斯、且映射光滑可逆，在潜在空间中做插值等简单操作，就对应图像上有语义的连续变化（如人脸姿态、表情的渐变），适合表示学习与属性操控&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''架构灵活'''：耦合层只要求 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 两个网络给出缩放和平移量，对它们的内部结构没有任何限制，可以是任意深度的卷积神经网络（Convolutional Neural Network，CNN）或残差网络（Residual Network，ResNet）。提升表达能力可以直接借用主流的深层网络设计，而无须改动可逆框架本身。&lt;br /&gt;
* '''综合多种模型特性'''：RealNVP 同时具备了此前难以兼得的能力——自回归模型的精确似然、VAE 的一步采样，以及不依赖固定重建损失的生成质量。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：RealNVP 对数据分布的拟合精度仍略逊于 PixelRNN 等自回归模型&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在以拟合精度为核心的任务（如密度估计、数据压缩）上，这是它的相对劣势——尽管它以并行采样换来了远快于对手的生成速度。&lt;br /&gt;
* '''维度保持约束'''：可逆变换要求输入与输出同维，因此 RealNVP 不能像自编码器那样把高维图像直接压缩成低维向量——潜在空间和数据空间一样大。所需的&amp;quot;降维&amp;quot;只能靠多尺度架构中的变量分解间接实现，即在各尺度逐步把一部分变量固定为潜在变量。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：仿射耦合每层只对一半变量做&amp;quot;缩放加平移&amp;quot;，函数形式较为简单，拟合复杂分布往往需要堆叠很多层。后续工作把变换换成更灵活的函数形式——如 Flow++ 的 Logistic 混合 CDF 耦合&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;、Neural Spline Flows 的有理二次样条耦合&amp;lt;ref name=&amp;quot;NSF2019&amp;quot;&amp;gt;{{cite journal|author1=Durkan C.|author2=Bekasov A.|author3=Murray I.|author4=Papamakarios G.|title=Neural Spline Flows|journal=Advances in Neural Information Processing Systems (NeurIPS)|year=2019|volume=32|url=https://arxiv.org/abs/1906.04032}}&amp;lt;/ref&amp;gt;——用更少的层数即可达到更强的拟合能力。&lt;br /&gt;
* '''均匀去量化的次优性'''：像素是 0–255 的离散整数，而 RealNVP 建模的是连续分布。RealNVP 的做法是给像素加上均匀噪声，把离散数据&amp;quot;抹平&amp;quot;成连续的——但这相当于让模型拟合一个被噪声模糊过的分布，与真实分布存在偏差。Flow++ 提出的变分去量化让噪声分布本身也由模型学习，缓解了这一问题&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''分区固定性'''：掩码决定哪一半变量被变换，而这一划分在训练前就已固定、训练中不再改变；若某层的划分不理想，只能靠后续层弥补。Glow（Generative Flow with Invertible 1×1 Convolutions，Glow）改用可学习的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积，让模型自己学会如何重排变量，划分更灵活，生成效果也更好&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''训练显存开销大'''：标准反向传播需要保存每个耦合层的激活值，层数一多显存占用显著上升；RevNet 的可逆残差结构正是为省去这部分存储而提出的&amp;lt;ref name=&amp;quot;RevNet2017&amp;quot;&amp;gt;{{cite journal|author1=Gomez A. N.|author2=Ren M.|author3=Urtasun R.|author4=Grosse R. B.|title=The Reversible Residual Network: Backpropagation Without Storing Activations|journal=NeurIPS|year=2017|url=https://arxiv.org/abs/1707.04585}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''对分布外样本过度自信'''：上文的异常检测应用依赖&amp;quot;异常样本的似然更低&amp;quot;这一假设，但后续研究发现这一假设未必成立：例如在 CIFAR-10 上训练的 RealNVP，会给截然不同的 SVHN 街景数字图像赋予'''更高'''的似然&amp;lt;ref name=&amp;quot;Nalisnick2019&amp;quot;&amp;gt;{{cite journal|author1=Nalisnick E.|author2=Matsukawa A.|author3=Teh Y. W.|author4=Gorur D.|author5=Lakshminarayanan B.|title=Do Deep Generative Models Know What They Don't Know?|journal=International Conference on Learning Representations (ICLR)|year=2019|url=https://arxiv.org/abs/1810.09136}}&amp;lt;/ref&amp;gt;。似然高低与&amp;quot;是否属于训练分布&amp;quot;并不直接对应，因此基于似然的异常检测需要额外的校准手段。&lt;br /&gt;
​&lt;br /&gt;
= 与相关框架的关系 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow，MAF）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics，FFJORD）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50784</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50784"/>
		<updated>2026-08-19T13:11:53Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：删去 ”理论分析“ 无用段落&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation，NICE），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类[[标准化流]]：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体[[对数似然]] &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''[[仿射耦合层]]'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''[[批量归一化]]'''（batch normalization）以稳定训练；'''[[压缩]]'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''[[变量分解]]'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在变量。其中，仿射耦合层通过空间[[棋盘掩码]]（checkerboard masking）与[[通道级掩码]]（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的多尺度架构如下图所示。输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第 1 尺度。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量[[归一化]]）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后通过变量分解把当前特征一分为二；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化），随后把全部变量抽出作为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;，不再保留传递变量。非末尺度的变量分解则把当前特征一分为二：一半作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|848x848px|居中]]&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
下图给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|626x626px|居中]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构，因为常用于图像相关的任务，所以人们通常用[[卷积神经网络]]（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的[[雅可比矩阵]]是下三角块结构，行列式即为对角线元素的乘积——一般的 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维矩阵求行列式需要 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 的计算量，而这里只需把对角元相乘，[[计算复杂度]]降为 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分：保持不变的一半称为'''被动'''部分，被变换的一半称为'''主动'''部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠使用不同掩码的耦合层，每个维度都能在不同层中分别充当被动与主动变量，信息得以充分混合。原论文的多尺度架构中，每个阶段固定使用 3 个耦合层&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
== [[多尺度架构]]：分层打包[[潜在变量]] ==&lt;br /&gt;
​&lt;br /&gt;
如上图所示，多尺度架构依赖两个关键操作：[[压缩]]与[[变量分解]]。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在[[通道方向]]上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。[[空间分辨率]]减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''[[层次化表示]]'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''[[梯度分散]]'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
== [[批量归一化]]：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量[[归一化]]（Batch Normalization，BN）应用于每个[[耦合层]]的输出之后。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于 BN 对每个维度独立地缩放和平移，它本身也是一个可逆变换：其雅可比矩阵为对角阵，[[雅可比行列式]]即各维度缩放因子的乘积，可直接加入[[对数似然]]——这使 BN 可以像耦合层一样作为流中的一层。引入 BN 主要是为了稳定深层训练：它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。使用 BN 有一个容易忽略的细节：训练时均值与方差是从当前这一批样本中估计的，因此单个样本的输出会依赖同批次的其他样本——此时的 BN 并不是严格意义上逐样本的可逆变换，其行列式也与整个批量耦合。只有在推理阶段改用训练过程中滑动平均得到的固定统计量，BN 才成为逐样本的、严格可逆的仿射变换，其行列式才能精确计入对数似然。换言之，用训练好的模型评估密度时若忘了做这一切换，同一个输入会因同批样本不同而得到不同的密度值，结果既不可复现也不正确。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的数学设计围绕一个核心目标展开：让概率密度的计算既精确又高效。这一目标由三个要素支撑——概率论中的变量替换公式给出精确似然的表达式；仿射耦合层的三角雅可比结构使行列式计算开销与维度成线性关系，从而保证可处理性；最大似然估计则提供直接的训练目标，无需近似下界或辅助网络。本节依次介绍这三个要素，并讨论训练目标中两项各自的含义与实际训练配置。&lt;br /&gt;
​&lt;br /&gt;
== 标准化流与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于[[自回归模型]]（autoregressive model，如 PixelRNN）：这类模型按固定顺序逐维生成数据、每个维度以先前各维为条件，采样必须串行进行——这与流模型对所有维度同时变换形成鲜明对比。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
对公式 (1) 做简单的代数移项，即可得到公式 (2) 给出的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：自适应矩估计（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
​&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。由于采样过程不依赖逐像素的顺序解码，生成速度显著高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（见下图）。与变分自编码器（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等自回归模型相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png|替代=|居中]]​&lt;br /&gt;
== 异常检测 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时似然较低或重建误差较大的样本即被视为异常。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。&lt;br /&gt;
​&lt;br /&gt;
AE-FLOW 将自编码器与以 RealNVP 仿射耦合层为核心的标准化流瓶颈相结合，在胸部 X 光、光学相干断层扫描、皮肤镜与脑部核磁共振等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的负对数似然构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
下图展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。|替代=|360x360px|居中]]​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合图像修复、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（见下图）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png|替代=|居中]]&lt;br /&gt;
​&lt;br /&gt;
== 在 NIS/NIS+ 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus，NIS+）中，RealNVP 以可逆神经网络（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆粗粒化。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的 INN &amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''——相较于使用独立编码器和解码器的方案，参数量减少一半，如下图所示。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器 φ 与解码器 φ†，信息丢弃与 EI 最大化在宏观动力学空间完成。|替代=|812x812px|居中]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（Effective Information，EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''精确似然'''：RealNVP 能直接算出任意数据点的精确概率密度——不像 VAE 只能优化似然的一个下界，也不像生成对抗网络（Generative Adversarial Network，GAN）完全不给密度。因此异常检测、贝叶斯推断等需要显式概率值的任务可以直接使用它&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''并行采样与推断'''：自回归模型必须逐像素按顺序生成，而 RealNVP 编码和解码时所有维度同时计算，生成一张图像只需一次前向传播，采样速度快得多。&lt;br /&gt;
* '''语义潜在空间'''：由于潜在分布是简单的标准高斯、且映射光滑可逆，在潜在空间中做插值等简单操作，就对应图像上有语义的连续变化（如人脸姿态、表情的渐变），适合表示学习与属性操控&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''架构灵活'''：耦合层只要求 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 两个网络给出缩放和平移量，对它们的内部结构没有任何限制，可以是任意深度的卷积神经网络（Convolutional Neural Network，CNN）或残差网络（Residual Network，ResNet）。提升表达能力可以直接借用主流的深层网络设计，而无须改动可逆框架本身。&lt;br /&gt;
* '''综合多种模型特性'''：RealNVP 同时具备了此前难以兼得的能力——自回归模型的精确似然、VAE 的一步采样，以及不依赖固定重建损失的生成质量。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：RealNVP 对数据分布的拟合精度仍略逊于 PixelRNN 等自回归模型&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在以拟合精度为核心的任务（如密度估计、数据压缩）上，这是它的相对劣势——尽管它以并行采样换来了远快于对手的生成速度。&lt;br /&gt;
* '''维度保持约束'''：可逆变换要求输入与输出同维，因此 RealNVP 不能像自编码器那样把高维图像直接压缩成低维向量——潜在空间和数据空间一样大。所需的&amp;quot;降维&amp;quot;只能靠多尺度架构中的变量分解间接实现，即在各尺度逐步把一部分变量固定为潜在变量。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：仿射耦合每层只对一半变量做&amp;quot;缩放加平移&amp;quot;，函数形式较为简单，拟合复杂分布往往需要堆叠很多层。后续工作把变换换成更灵活的函数形式——如 Flow++ 的 Logistic 混合 CDF 耦合&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;、Neural Spline Flows 的有理二次样条耦合&amp;lt;ref name=&amp;quot;NSF2019&amp;quot;&amp;gt;{{cite journal|author1=Durkan C.|author2=Bekasov A.|author3=Murray I.|author4=Papamakarios G.|title=Neural Spline Flows|journal=Advances in Neural Information Processing Systems (NeurIPS)|year=2019|volume=32|url=https://arxiv.org/abs/1906.04032}}&amp;lt;/ref&amp;gt;——用更少的层数即可达到更强的拟合能力。&lt;br /&gt;
* '''均匀去量化的次优性'''：像素是 0–255 的离散整数，而 RealNVP 建模的是连续分布。RealNVP 的做法是给像素加上均匀噪声，把离散数据&amp;quot;抹平&amp;quot;成连续的——但这相当于让模型拟合一个被噪声模糊过的分布，与真实分布存在偏差。Flow++ 提出的变分去量化让噪声分布本身也由模型学习，缓解了这一问题&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''分区固定性'''：掩码决定哪一半变量被变换，而这一划分在训练前就已固定、训练中不再改变；若某层的划分不理想，只能靠后续层弥补。Glow（Generative Flow with Invertible 1×1 Convolutions，Glow）改用可学习的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积，让模型自己学会如何重排变量，划分更灵活，生成效果也更好&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''训练显存开销大'''：标准反向传播需要保存每个耦合层的激活值，层数一多显存占用显著上升；RevNet 的可逆残差结构正是为省去这部分存储而提出的&amp;lt;ref name=&amp;quot;RevNet2017&amp;quot;&amp;gt;{{cite journal|author1=Gomez A. N.|author2=Ren M.|author3=Urtasun R.|author4=Grosse R. B.|title=The Reversible Residual Network: Backpropagation Without Storing Activations|journal=NeurIPS|year=2017|url=https://arxiv.org/abs/1707.04585}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''对分布外样本过度自信'''：上文的异常检测应用依赖&amp;quot;异常样本的似然更低&amp;quot;这一假设，但后续研究发现这一假设未必成立：例如在 CIFAR-10 上训练的 RealNVP，会给截然不同的 SVHN 街景数字图像赋予'''更高'''的似然&amp;lt;ref name=&amp;quot;Nalisnick2019&amp;quot;&amp;gt;{{cite journal|author1=Nalisnick E.|author2=Matsukawa A.|author3=Teh Y. W.|author4=Gorur D.|author5=Lakshminarayanan B.|title=Do Deep Generative Models Know What They Don't Know?|journal=International Conference on Learning Representations (ICLR)|year=2019|url=https://arxiv.org/abs/1810.09136}}&amp;lt;/ref&amp;gt;。似然高低与&amp;quot;是否属于训练分布&amp;quot;并不直接对应，因此基于似然的异常检测需要额外的校准手段。&lt;br /&gt;
​&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow，MAF）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics，FFJORD）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50783</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50783"/>
		<updated>2026-08-19T12:55:19Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：1. BN 小节：补充雅可比行列式的过渡与训练/推理统计量差异的说明，删去语义重复。 2. &amp;quot;数学原理&amp;quot;：标题下新增一段引言，点出精确似然、三角雅可比、最大似然三个要素的主线。 3. 优缺点：优势与局限全部改写为&amp;quot;相比谁、在什么场景&amp;quot;的具体表述。 4. 局限扩充：新增&amp;quot;训练显存开销大&amp;quot;（RevNet）与&amp;quot;对分布外样本过度自信&amp;quot;（Nalisnick et al.）两条。 5. 图片编号：正文&amp;quot;图 x&amp;quot;全部改为&amp;quot;下图/见下图&amp;quot;等方位指代，删除三处 HTML 图注释占位。 6. 术语统一：首次出现处定义&amp;quot;被动/主动&amp;quot;、&amp;quot;自回归模型&amp;quot;；&amp;quot;潜变量&amp;quot;统一为&amp;quot;潜在变量&amp;quot;；INN 去重定义；OCT、MRI 改为中文译名。 7. 事实性修正：末尺度改为&amp;quot;全部变量抽出&amp;quot;以与维度公式自洽；删去无出处的&amp;quot;至少三层&amp;quot;；O(D³) 补基准说明；参数&amp;quot;减半&amp;quot;点明比较对象。 8. 参考文献：新增 Neural Spline Flows、RevNet、Nalisnick et al. 三条，并为关键论断补引原文。&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation，NICE），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类[[标准化流]]：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体[[对数似然]] &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''[[仿射耦合层]]'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''[[批量归一化]]'''（batch normalization）以稳定训练；'''[[压缩]]'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''[[变量分解]]'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在变量。其中，仿射耦合层通过空间[[棋盘掩码]]（checkerboard masking）与[[通道级掩码]]（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的多尺度架构如下图所示。输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第 1 尺度。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量[[归一化]]）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后通过变量分解把当前特征一分为二；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化），随后把全部变量抽出作为 &amp;lt;math&amp;gt;\mathbf{z}^{(L)}&amp;lt;/math&amp;gt;，不再保留传递变量。非末尺度的变量分解则把当前特征一分为二：一半作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|848x848px|居中]]&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
下图给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构，因为常用于图像相关的任务，所以人们通常用[[卷积神经网络]]（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的[[雅可比矩阵]]是下三角块结构，行列式即为对角线元素的乘积——一般的 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维矩阵求行列式需要 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 的计算量，而这里只需把对角元相乘，[[计算复杂度]]降为 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分：保持不变的一半称为'''被动'''部分，被变换的一半称为'''主动'''部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠使用不同掩码的耦合层，每个维度都能在不同层中分别充当被动与主动变量，信息得以充分混合。原论文的多尺度架构中，每个阶段固定使用 3 个耦合层&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
== [[多尺度架构]]：分层打包[[潜在变量]] ==&lt;br /&gt;
​&lt;br /&gt;
如上图所示，多尺度架构依赖两个关键操作：[[压缩]]与[[变量分解]]。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在[[通道方向]]上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。[[空间分辨率]]减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''[[层次化表示]]'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''[[梯度分散]]'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
== [[批量归一化]]：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量[[归一化]]（Batch Normalization，BN）应用于每个[[耦合层]]的输出之后。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于 BN 对每个维度独立地缩放和平移，它本身也是一个可逆变换：其雅可比矩阵为对角阵，[[雅可比行列式]]即各维度缩放因子的乘积，可直接加入[[对数似然]]——这使 BN 可以像耦合层一样作为流中的一层。引入 BN 主要是为了稳定深层训练：它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。使用 BN 有一个容易忽略的细节：训练时均值与方差是从当前这一批样本中估计的，因此单个样本的输出会依赖同批次的其他样本——此时的 BN 并不是严格意义上逐样本的可逆变换，其行列式也与整个批量耦合。只有在推理阶段改用训练过程中滑动平均得到的固定统计量，BN 才成为逐样本的、严格可逆的仿射变换，其行列式才能精确计入对数似然。换言之，用训练好的模型评估密度时若忘了做这一切换，同一个输入会因同批样本不同而得到不同的密度值，结果既不可复现也不正确。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 的数学设计围绕一个核心目标展开：让概率密度的计算既精确又高效。这一目标由三个要素支撑——概率论中的变量替换公式给出精确似然的表达式；仿射耦合层的三角雅可比结构使行列式计算开销与维度成线性关系，从而保证可处理性；最大似然估计则提供直接的训练目标，无需近似下界或辅助网络。本节依次介绍这三个要素，并讨论训练目标中两项各自的含义与实际训练配置。&lt;br /&gt;
​&lt;br /&gt;
== 标准化流与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于[[自回归模型]]（autoregressive model，如 PixelRNN）：这类模型按固定顺序逐维生成数据、每个维度以先前各维为条件，采样必须串行进行——这与流模型对所有维度同时变换形成鲜明对比。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
对公式 (1) 做简单的代数移项，即可得到公式 (2) 给出的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：自适应矩估计（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
​&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。由于采样过程不依赖逐像素的顺序解码，生成速度显著高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（见下图）。与变分自编码器（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等自回归模型相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png|替代=|居中]]​&lt;br /&gt;
== 异常检测 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时似然较低或重建误差较大的样本即被视为异常。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。&lt;br /&gt;
​&lt;br /&gt;
AE-FLOW 将自编码器与以 RealNVP 仿射耦合层为核心的标准化流瓶颈相结合，在胸部 X 光、光学相干断层扫描、皮肤镜与脑部核磁共振等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的负对数似然构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
下图展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。|替代=|360x360px|居中]]​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合图像修复、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（见下图）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png|替代=|居中]]&lt;br /&gt;
​&lt;br /&gt;
== 在 NIS/NIS+ 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus，NIS+）中，RealNVP 以可逆神经网络（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆粗粒化。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的 INN &amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''——相较于使用独立编码器和解码器的方案，参数量减少一半，如下图所示。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器 φ 与解码器 φ†，信息丢弃与 EI 最大化在宏观动力学空间完成。|替代=|812x812px|居中]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（Effective Information，EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''精确似然'''：RealNVP 能直接算出任意数据点的精确概率密度——不像 VAE 只能优化似然的一个下界，也不像生成对抗网络（Generative Adversarial Network，GAN）完全不给密度。因此异常检测、贝叶斯推断等需要显式概率值的任务可以直接使用它&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''并行采样与推断'''：自回归模型必须逐像素按顺序生成，而 RealNVP 编码和解码时所有维度同时计算，生成一张图像只需一次前向传播，采样速度快得多。&lt;br /&gt;
* '''语义潜在空间'''：由于潜在分布是简单的标准高斯、且映射光滑可逆，在潜在空间中做插值等简单操作，就对应图像上有语义的连续变化（如人脸姿态、表情的渐变），适合表示学习与属性操控&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''架构灵活'''：耦合层只要求 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;、&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 两个网络给出缩放和平移量，对它们的内部结构没有任何限制，可以是任意深度的卷积神经网络（Convolutional Neural Network，CNN）或残差网络（Residual Network，ResNet）。提升表达能力可以直接借用主流的深层网络设计，而无须改动可逆框架本身。&lt;br /&gt;
* '''综合多种模型特性'''：RealNVP 同时具备了此前难以兼得的能力——自回归模型的精确似然、VAE 的一步采样，以及不依赖固定重建损失的生成质量。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：RealNVP 对数据分布的拟合精度仍略逊于 PixelRNN 等自回归模型&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在以拟合精度为核心的任务（如密度估计、数据压缩）上，这是它的相对劣势——尽管它以并行采样换来了远快于对手的生成速度。&lt;br /&gt;
* '''维度保持约束'''：可逆变换要求输入与输出同维，因此 RealNVP 不能像自编码器那样把高维图像直接压缩成低维向量——潜在空间和数据空间一样大。所需的&amp;quot;降维&amp;quot;只能靠多尺度架构中的变量分解间接实现，即在各尺度逐步把一部分变量固定为潜在变量。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：仿射耦合每层只对一半变量做&amp;quot;缩放加平移&amp;quot;，函数形式较为简单，拟合复杂分布往往需要堆叠很多层。后续工作把变换换成更灵活的函数形式——如 Flow++ 的 Logistic 混合 CDF 耦合&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;、Neural Spline Flows 的有理二次样条耦合&amp;lt;ref name=&amp;quot;NSF2019&amp;quot;&amp;gt;{{cite journal|author1=Durkan C.|author2=Bekasov A.|author3=Murray I.|author4=Papamakarios G.|title=Neural Spline Flows|journal=Advances in Neural Information Processing Systems (NeurIPS)|year=2019|volume=32|url=https://arxiv.org/abs/1906.04032}}&amp;lt;/ref&amp;gt;——用更少的层数即可达到更强的拟合能力。&lt;br /&gt;
* '''均匀去量化的次优性'''：像素是 0–255 的离散整数，而 RealNVP 建模的是连续分布。RealNVP 的做法是给像素加上均匀噪声，把离散数据&amp;quot;抹平&amp;quot;成连续的——但这相当于让模型拟合一个被噪声模糊过的分布，与真实分布存在偏差。Flow++ 提出的变分去量化让噪声分布本身也由模型学习，缓解了这一问题&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt;。&lt;br /&gt;
* '''分区固定性'''：掩码决定哪一半变量被变换，而这一划分在训练前就已固定、训练中不再改变；若某层的划分不理想，只能靠后续层弥补。Glow（Generative Flow with Invertible 1×1 Convolutions，Glow）改用可学习的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积，让模型自己学会如何重排变量，划分更灵活，生成效果也更好&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''训练显存开销大'''：标准反向传播需要保存每个耦合层的激活值，层数一多显存占用显著上升；RevNet 的可逆残差结构正是为省去这部分存储而提出的&amp;lt;ref name=&amp;quot;RevNet2017&amp;quot;&amp;gt;{{cite journal|author1=Gomez A. N.|author2=Ren M.|author3=Urtasun R.|author4=Grosse R. B.|title=The Reversible Residual Network: Backpropagation Without Storing Activations|journal=NeurIPS|year=2017|url=https://arxiv.org/abs/1707.04585}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''对分布外样本过度自信'''：上文的异常检测应用依赖&amp;quot;异常样本的似然更低&amp;quot;这一假设，但后续研究发现这一假设未必成立：例如在 CIFAR-10 上训练的 RealNVP，会给截然不同的 SVHN 街景数字图像赋予'''更高'''的似然&amp;lt;ref name=&amp;quot;Nalisnick2019&amp;quot;&amp;gt;{{cite journal|author1=Nalisnick E.|author2=Matsukawa A.|author3=Teh Y. W.|author4=Gorur D.|author5=Lakshminarayanan B.|title=Do Deep Generative Models Know What They Don't Know?|journal=International Conference on Learning Representations (ICLR)|year=2019|url=https://arxiv.org/abs/1810.09136}}&amp;lt;/ref&amp;gt;。似然高低与&amp;quot;是否属于训练分布&amp;quot;并不直接对应，因此基于似然的异常检测需要额外的校准手段。&lt;br /&gt;
​&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow，MAF）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics，FFJORD）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
​&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:%E4%BB%BF%E5%B0%84%E8%80%A6%E5%90%88.png&amp;diff=50782</id>
		<title>文件:仿射耦合.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:%E4%BB%BF%E5%B0%84%E8%80%A6%E5%90%88.png&amp;diff=50782"/>
		<updated>2026-08-18T13:12:57Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：黄一凡上传文件:仿射耦合.png的新版本&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50758</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50758"/>
		<updated>2026-08-07T05:27:52Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：调节图片排版&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;编辑“RealNVP”&lt;br /&gt;
跳到导航跳到搜索&lt;br /&gt;
未选择任何文件&lt;br /&gt;
高级&lt;br /&gt;
特殊字符&lt;br /&gt;
帮助&lt;br /&gt;
拖动文件至此&lt;br /&gt;
&lt;br /&gt;
AI&lt;br /&gt;
一键智能添加链接&lt;br /&gt;
⚙&lt;br /&gt;
'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
​&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation，NICE），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过空间棋盘掩码（checkerboard masking）与通道级掩码（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
图 4 给出了整体的多尺度架构。如图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量归一化）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后通过变量分解把当前特征一分为二；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化）就进行变量分解。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|848x848px|居中]]&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
图 2 给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构，因为常用于图像相关的任务，所以人们通常用卷积神经网络（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——[[计算复杂度]]从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
​&lt;br /&gt;
图 4 的多尺度架构依赖两个关键操作：压缩与变量分解。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量归一化（Batch Normalization，BN）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BN 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
​&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
== 标准化流与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
仿射耦合的逆变换为：对于公式 (1) 中的前向变换，经过代数变换即可得到公式 (2) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：自适应矩估计（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
​&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。由于采样过程不依赖逐像素的顺序解码，生成速度显著高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（图 5）。与变分自编码器（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等自回归模型相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png|替代=|居中]]​&lt;br /&gt;
== 异常检测 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时似然较低或重建误差较大的样本即被视为异常。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。&lt;br /&gt;
​&lt;br /&gt;
AE-FLOW 将自编码器与以 RealNVP 仿射耦合层为核心的标准化流瓶颈相结合，在胸部 X 光、OCT、皮肤镜与脑部 MRI 等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的负对数似然构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
图 6 展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。|替代=|360x360px|居中]]​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合图像修复、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（图 7）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png|替代=|居中]]&lt;br /&gt;
​&lt;br /&gt;
== 在 NIS/NIS+ 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus，NIS+）中，RealNVP 以可逆神经网络（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆粗粒化。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半，如下图所示。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器 φ 与解码器 φ†，信息丢弃与 EI 最大化在宏观动力学空间完成。|替代=|812x812px|居中]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（Effective Information，EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度卷积神经网络（Convolutional Neural Network，CNN）/残差网络（Residual Network，ResNet），表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow（Generative Flow with Invertible 1×1 Convolutions，Glow）的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
​&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow，MAF）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics，FFJORD）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
​&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图9：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50757</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50757"/>
		<updated>2026-08-07T05:25:08Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：调节图片排版&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;编辑“RealNVP”&lt;br /&gt;
跳到导航跳到搜索&lt;br /&gt;
未选择任何文件&lt;br /&gt;
高级&lt;br /&gt;
特殊字符&lt;br /&gt;
帮助&lt;br /&gt;
拖动文件至此&lt;br /&gt;
&lt;br /&gt;
AI&lt;br /&gt;
一键智能添加链接&lt;br /&gt;
⚙&lt;br /&gt;
'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
​&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation，NICE），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|848x848px]]&lt;br /&gt;
​&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过空间棋盘掩码（checkerboard masking）与通道级掩码（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
图 4 给出了整体的多尺度架构。如图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量归一化）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后通过变量分解把当前特征一分为二；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化）就进行变量分解。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
图 2 给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构，因为常用于图像相关的任务，所以人们通常用卷积神经网络（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——[[计算复杂度]]从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
​&lt;br /&gt;
图 4 的多尺度架构依赖两个关键操作：压缩与变量分解。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量归一化（Batch Normalization，BN）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BN 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
​&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
== 标准化流与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
仿射耦合的逆变换为：对于公式 (1) 中的前向变换，经过代数变换即可得到公式 (2) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：自适应矩估计（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
​&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。由于采样过程不依赖逐像素的顺序解码，生成速度显著高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（图 5）。与变分自编码器（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等自回归模型相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png|替代=|居中]]​&lt;br /&gt;
== 异常检测 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时似然较低或重建误差较大的样本即被视为异常。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。&lt;br /&gt;
​&lt;br /&gt;
AE-FLOW 将自编码器与以 RealNVP 仿射耦合层为核心的标准化流瓶颈相结合，在胸部 X 光、OCT、皮肤镜与脑部 MRI 等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的负对数似然构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
图 6 展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。|替代=|360x360px|居中]]​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合图像修复、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（图 7）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png|替代=|居中]]&lt;br /&gt;
​&lt;br /&gt;
== 在 NIS/NIS+ 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus，NIS+）中，RealNVP 以可逆神经网络（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆粗粒化。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半，如下图所示。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器 φ 与解码器 φ†，信息丢弃与 EI 最大化在宏观动力学空间完成。|替代=|812x812px|居中]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（Effective Information，EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度卷积神经网络（Convolutional Neural Network，CNN）/残差网络（Residual Network，ResNet），表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow（Generative Flow with Invertible 1×1 Convolutions，Glow）的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
​&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow，MAF）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics，FFJORD）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
​&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图9：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50756</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50756"/>
		<updated>2026-08-05T12:46:22Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：补全两个 ref&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;编辑“RealNVP”&lt;br /&gt;
跳到导航跳到搜索&lt;br /&gt;
未选择任何文件&lt;br /&gt;
高级&lt;br /&gt;
特殊字符&lt;br /&gt;
帮助&lt;br /&gt;
拖动文件至此&lt;br /&gt;
&lt;br /&gt;
AI&lt;br /&gt;
一键智能添加链接&lt;br /&gt;
⚙&lt;br /&gt;
'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
​&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation，NICE），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|848x848px]]&lt;br /&gt;
​&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过空间棋盘掩码（checkerboard masking）与通道级掩码（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
图 4 给出了整体的多尺度架构。如图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量归一化）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后通过变量分解把当前特征一分为二；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化）就进行变量分解。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
图 2 给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构，因为常用于图像相关的任务，所以人们通常用卷积神经网络（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——[[计算复杂度]]从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
​&lt;br /&gt;
图 4 的多尺度架构依赖两个关键操作：压缩与变量分解。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量归一化（Batch Normalization，BN）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BN 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
​&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
== 标准化流与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
仿射耦合的逆变换为：对于公式 (1) 中的前向变换，经过代数变换即可得到公式 (2) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：自适应矩估计（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
​&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png]]&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。由于采样过程不依赖逐像素的顺序解码，生成速度显著高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（图 5）。与变分自编码器（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等自回归模型相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。|替代=|360x360px]]&lt;br /&gt;
== 异常检测 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时似然较低或重建误差较大的样本即被视为异常。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。&lt;br /&gt;
​&lt;br /&gt;
AE-FLOW 将自编码器与以 RealNVP 仿射耦合层为核心的标准化流瓶颈相结合，在胸部 X 光、OCT、皮肤镜与脑部 MRI 等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的负对数似然构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
图 6 展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合图像修复、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot; /&amp;gt;。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（图 7）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。引自 Dinh et al., 2017。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png]]&lt;br /&gt;
​&lt;br /&gt;
== 在 NIS/NIS+ 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus，NIS+）中，RealNVP 以可逆神经网络（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆粗粒化。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半（图 8）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器 φ 与解码器 φ†，信息丢弃与 EI 最大化在宏观动力学空间完成。|替代=|400x400px]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（Effective Information，EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度卷积神经网络（Convolutional Neural Network，CNN）/残差网络（Residual Network，ResNet），表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow（Generative Flow with Invertible 1×1 Convolutions，Glow）的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
​&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow，MAF）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics，FFJORD）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
​&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图9：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50755</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50755"/>
		<updated>2026-08-05T12:38:59Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：题注引用&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;编辑“RealNVP”&lt;br /&gt;
跳到导航跳到搜索&lt;br /&gt;
未选择任何文件&lt;br /&gt;
高级&lt;br /&gt;
特殊字符&lt;br /&gt;
帮助&lt;br /&gt;
拖动文件至此&lt;br /&gt;
&lt;br /&gt;
AI&lt;br /&gt;
一键智能添加链接&lt;br /&gt;
⚙&lt;br /&gt;
'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
​&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation，NICE），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|848x848px]]&lt;br /&gt;
​&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过空间棋盘掩码（checkerboard masking）与通道级掩码（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
图 4 给出了整体的多尺度架构。如图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量归一化）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后通过变量分解把当前特征一分为二；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化）就进行变量分解。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
图 2 给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构，因为常用于图像相关的任务，所以人们通常用卷积神经网络（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——[[计算复杂度]]从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
​&lt;br /&gt;
图 4 的多尺度架构依赖两个关键操作：压缩与变量分解。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量归一化（Batch Normalization，BN）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BN 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
​&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
== 标准化流与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
仿射耦合的逆变换为：对于公式 (1) 中的前向变换，经过代数变换即可得到公式 (2) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：自适应矩估计（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
​&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png]]&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。由于采样过程不依赖逐像素的顺序解码，生成速度显著高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（图 5）。与变分自编码器（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等自回归模型相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。|替代=|360x360px]]&lt;br /&gt;
== 异常检测 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时似然较低或重建误差较大的样本即被视为异常。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。&lt;br /&gt;
​&lt;br /&gt;
AE-FLOW 将自编码器与以 RealNVP 仿射耦合层为核心的标准化流瓶颈相结合，在胸部 X 光、OCT、皮肤镜与脑部 MRI 等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的负对数似然构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
图 6 展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合图像修复、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（图 7）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。引自 Dinh et al., 2017。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png]]&lt;br /&gt;
​&lt;br /&gt;
== 在 NIS/NIS+ 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus，NIS+）中，RealNVP 以可逆神经网络（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆粗粒化。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半（图 8）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器 φ 与解码器 φ†，信息丢弃与 EI 最大化在宏观动力学空间完成。|替代=|400x400px]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（Effective Information，EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度卷积神经网络（Convolutional Neural Network，CNN）/残差网络（Residual Network，ResNet），表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow（Generative Flow with Invertible 1×1 Convolutions，Glow）的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
​&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow，MAF）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics，FFJORD）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
​&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图9：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50754</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50754"/>
		<updated>2026-08-05T12:25:19Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：/* 仿射耦合的雅可比行列式与可逆性 */ 公式位置调整&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;编辑“RealNVP”&lt;br /&gt;
跳到导航跳到搜索&lt;br /&gt;
未选择任何文件&lt;br /&gt;
高级&lt;br /&gt;
特殊字符&lt;br /&gt;
帮助&lt;br /&gt;
拖动文件至此&lt;br /&gt;
&lt;br /&gt;
AI&lt;br /&gt;
一键智能添加链接&lt;br /&gt;
⚙&lt;br /&gt;
'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
​&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation，NICE），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|848x848px]]&lt;br /&gt;
​&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过空间棋盘掩码（checkerboard masking）与通道级掩码（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
图 4 给出了整体的多尺度架构。如图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量归一化）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后通过变量分解把当前特征一分为二；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化）就进行变量分解。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
图 2 给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构，因为常用于图像相关的任务，所以人们通常用卷积神经网络（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——[[计算复杂度]]从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
​&lt;br /&gt;
图 4 的多尺度架构依赖两个关键操作：压缩与变量分解。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量归一化（Batch Normalization，BN）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BN 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
​&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
== 标准化流与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
仿射耦合的逆变换为：对于公式 (1) 中的前向变换，经过代数变换即可得到公式 (2) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：自适应矩估计（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
​&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。引自 Dinh et al., 2017。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png]]&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。由于采样过程不依赖逐像素的顺序解码，生成速度显著高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（图 5）。与变分自编码器（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等自回归模型相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。引自 Zhao et al., 2023。|替代=|360x360px]]&lt;br /&gt;
== 异常检测 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时似然较低或重建误差较大的样本即被视为异常。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。&lt;br /&gt;
​&lt;br /&gt;
AE-FLOW 将自编码器与以 RealNVP 仿射耦合层为核心的标准化流瓶颈相结合，在胸部 X 光、OCT、皮肤镜与脑部 MRI 等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的负对数似然构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
图 6 展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合图像修复、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（图 7）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。引自 Dinh et al., 2017。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png]]&lt;br /&gt;
​&lt;br /&gt;
== 在 NIS/NIS+ 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus，NIS+）中，RealNVP 以可逆神经网络（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆粗粒化。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半（图 8）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器 φ 与解码器 φ†（即逆映射 φ^{-1}），信息丢弃与 EI 最大化在宏观动力学空间完成。引自 Yang et al., 2025。|替代=|400x400px]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（Effective Information，EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度卷积神经网络（Convolutional Neural Network，CNN）/残差网络（Residual Network，ResNet），表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow（Generative Flow with Invertible 1×1 Convolutions，Glow）的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
​&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow，MAF）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics，FFJORD）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
​&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图9：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50753</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50753"/>
		<updated>2026-08-05T12:24:11Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：调节图片排版&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;编辑“RealNVP”&lt;br /&gt;
跳到导航跳到搜索&lt;br /&gt;
未选择任何文件&lt;br /&gt;
高级&lt;br /&gt;
特殊字符&lt;br /&gt;
帮助&lt;br /&gt;
拖动文件至此&lt;br /&gt;
&lt;br /&gt;
AI&lt;br /&gt;
一键智能添加链接&lt;br /&gt;
⚙&lt;br /&gt;
'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
​&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation，NICE），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|848x848px]]&lt;br /&gt;
​&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过空间棋盘掩码（checkerboard masking）与通道级掩码（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
图 4 给出了整体的多尺度架构。如图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量归一化）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后通过变量分解把当前特征一分为二；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化）就进行变量分解。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
图 2 给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构，因为常用于图像相关的任务，所以人们通常用卷积神经网络（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——[[计算复杂度]]从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
​&lt;br /&gt;
图 4 的多尺度架构依赖两个关键操作：压缩与变量分解。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量归一化（Batch Normalization，BN）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BN 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
​&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
== 标准化流与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
仿射耦合的逆变换为：对于公式 (1) 中的前向变换，经过代数变换即可得到公式 (2) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：自适应矩估计（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
​&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。引自 Dinh et al., 2017。|264x264px|链接=Special:FilePath/Realnvp_celeba_generation.png]]&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。由于采样过程不依赖逐像素的顺序解码，生成速度显著高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（图 5）。与变分自编码器（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等自回归模型相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。引自 Zhao et al., 2023。|替代=|360x360px]]&lt;br /&gt;
== 异常检测 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时似然较低或重建误差较大的样本即被视为异常。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。&lt;br /&gt;
​&lt;br /&gt;
AE-FLOW 将自编码器与以 RealNVP 仿射耦合层为核心的标准化流瓶颈相结合，在胸部 X 光、OCT、皮肤镜与脑部 MRI 等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的负对数似然构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
图 6 展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​&lt;br /&gt;
&lt;br /&gt;
​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合图像修复、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（图 7）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。引自 Dinh et al., 2017。|274x274px|链接=Special:FilePath/Realnvp_lsun_inpainting.png]]&lt;br /&gt;
​&lt;br /&gt;
== 在 NIS/NIS+ 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus，NIS+）中，RealNVP 以可逆神经网络（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆粗粒化。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半（图 8）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器 φ 与解码器 φ†（即逆映射 φ^{-1}），信息丢弃与 EI 最大化在宏观动力学空间完成。引自 Yang et al., 2025。|替代=|400x400px]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（Effective Information，EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度卷积神经网络（Convolutional Neural Network，CNN）/残差网络（Residual Network，ResNet），表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow（Generative Flow with Invertible 1×1 Convolutions，Glow）的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
​&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow，MAF）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics，FFJORD）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
​&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图9：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50752</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50752"/>
		<updated>2026-08-05T12:20:56Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：NIS 应用示例配图修改&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;编辑“RealNVP”&lt;br /&gt;
跳到导航跳到搜索&lt;br /&gt;
未选择任何文件&lt;br /&gt;
高级&lt;br /&gt;
特殊字符&lt;br /&gt;
帮助&lt;br /&gt;
拖动文件至此&lt;br /&gt;
&lt;br /&gt;
AI&lt;br /&gt;
一键智能添加链接&lt;br /&gt;
⚙&lt;br /&gt;
'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
​&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation，NICE），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|848x848px]]&lt;br /&gt;
​&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过空间棋盘掩码（checkerboard masking）与通道级掩码（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
图 4 给出了整体的多尺度架构。如图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量归一化）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后通过变量分解把当前特征一分为二；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化）就进行变量分解。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
图 2 给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构，因为常用于图像相关的任务，所以人们通常用卷积神经网络（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——[[计算复杂度]]从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
​&lt;br /&gt;
图 4 的多尺度架构依赖两个关键操作：压缩与变量分解。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量归一化（Batch Normalization，BN）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BN 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
​&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
== 标准化流与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
仿射耦合的逆变换为：对于公式 (1) 中的前向变换，经过代数变换即可得到公式 (2) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：自适应矩估计（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
​&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。由于采样过程不依赖逐像素的顺序解码，生成速度显著高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（图 5）。与变分自编码器（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等自回归模型相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。引自 Dinh et al., 2017。|400x400px|链接=Special:FilePath/Realnvp_celeba_generation.png]]&lt;br /&gt;
​&lt;br /&gt;
== 异常检测 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时似然较低或重建误差较大的样本即被视为异常。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。&lt;br /&gt;
​&lt;br /&gt;
AE-FLOW 将自编码器与以 RealNVP 仿射耦合层为核心的标准化流瓶颈相结合，在胸部 X 光、OCT、皮肤镜与脑部 MRI 等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的负对数似然构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
图 6 展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​&lt;br /&gt;
[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。引自 Zhao et al., 2023。|替代=|400x400px]]&lt;br /&gt;
​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合图像修复、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（图 7）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。引自 Dinh et al., 2017。|400x400px|链接=Special:FilePath/Realnvp_lsun_inpainting.png]]&lt;br /&gt;
​&lt;br /&gt;
== 在 NIS/NIS+ 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus，NIS+）中，RealNVP 以可逆神经网络（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆粗粒化。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半（图 8）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:NIS+odd.png|缩略图|NIS/NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器 φ 与解码器 φ†（即逆映射 φ^{-1}），信息丢弃与 EI 最大化在宏观动力学空间完成。引自 Yang et al., 2025。|替代=|400x400px]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（Effective Information，EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度卷积神经网络（Convolutional Neural Network，CNN）/残差网络（Residual Network，ResNet），表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow（Generative Flow with Invertible 1×1 Convolutions，Glow）的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
​&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow，MAF）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics，FFJORD）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
​&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图9：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:Aeflow_anomaly_detection.png&amp;diff=50751</id>
		<title>文件:Aeflow anomaly detection.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:Aeflow_anomaly_detection.png&amp;diff=50751"/>
		<updated>2026-08-04T06:49:57Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：AE-FLOW 将自编码器与以 RealNVP 仿射耦合层为核心的标准化流瓶颈相结合，在胸部 X 光、OCT、皮肤镜与脑部 MRI 等多种医学影像数据上检测异常&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== 文件说明 ==&lt;br /&gt;
AE-FLOW 将自编码器与以 RealNVP 仿射耦合层为核心的标准化流瓶颈相结合，在胸部 X 光、OCT、皮肤镜与脑部 MRI 等多种医学影像数据上检测异常&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50750</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50750"/>
		<updated>2026-08-04T06:48:33Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：应用章节第二个小节改为「异常检测」  删除了「潜在空间插值与语义操控」小节及其图片引用。&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;编辑“RealNVP”&lt;br /&gt;
跳到导航跳到搜索&lt;br /&gt;
未选择任何文件&lt;br /&gt;
高级&lt;br /&gt;
特殊字符&lt;br /&gt;
帮助&lt;br /&gt;
拖动文件至此&lt;br /&gt;
&lt;br /&gt;
AI&lt;br /&gt;
一键智能添加链接&lt;br /&gt;
⚙&lt;br /&gt;
'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
​&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation，NICE），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|848x848px]]&lt;br /&gt;
​&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过空间棋盘掩码（checkerboard masking）与通道级掩码（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
图 4 给出了整体的多尺度架构。如图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量归一化）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后通过变量分解把当前特征一分为二；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化）就进行变量分解。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
图 2 给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构，因为常用于图像相关的任务，所以人们通常用卷积神经网络（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——[[计算复杂度]]从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
​&lt;br /&gt;
图 4 的多尺度架构依赖两个关键操作：压缩与变量分解。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量归一化（Batch Normalization，BN）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BN 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
​&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
== 标准化流与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
仿射耦合的逆变换为：对于公式 (1) 中的前向变换，经过代数变换即可得到公式 (2) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：自适应矩估计（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
​&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。由于采样过程不依赖逐像素的顺序解码，生成速度显著高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（图 5）。与变分自编码器（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等自回归模型相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。引自 Dinh et al., 2017。|400x400px|链接=Special:FilePath/Realnvp_celeba_generation.png]]&lt;br /&gt;
​&lt;br /&gt;
== 异常检测 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 能够给出数据的精确似然，这使它天然适合无监督异常检测：在仅含正常样本的数据上训练流模型，测试时似然较低或重建误差较大的样本即被视为异常。医学影像领域尤其典型——正常扫描数量庞大，而病变样本稀少且难以标注。&lt;br /&gt;
​&lt;br /&gt;
AE-FLOW 将自编码器与以 RealNVP 仿射耦合层为核心的标准化流瓶颈相结合，在胸部 X 光、OCT、皮肤镜与脑部 MRI 等多种医学影像数据上检测异常。模型同时利用重建误差与流模型给出的负对数似然构造异常分数：正常样本能被准确重建并获得较高似然，而病变区域会产生高重建残差与低似然&amp;lt;ref name=&amp;quot;AEFLOW2023&amp;quot;&amp;gt;{{cite journal|author1=Zhao Y.|author2=Ding Q.|author3=Zhang X.|title=AE-FLOW: Autoencoders with Normalizing Flows for Medical Images Anomaly Detection|journal=International Conference on Learning Representations (ICLR)|year=2023|url=https://openreview.net/forum?id=9OmCr1q54Z}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
图 6 展示了 AE-FLOW 在胸部 X 光数据上的结果。左列为异常（肺炎）样本，右列为正常样本；每列自上而下依次为原图、重建图与残差图。正常样本的残差整体较暗，而异常样本的肺部区域出现明显亮斑，直观地反映了模型对异常模式的识别能力。&lt;br /&gt;
​&lt;br /&gt;
[[文件:aeflow_anomaly_detection.png|缩略图|AE-FLOW 在胸部 X 光上的异常检测结果。左列为异常（肺炎）样本，右列为正常样本；自上而下依次为原图、重建图与残差图。引自 Zhao et al., 2023。|替代=|400x400px]]&lt;br /&gt;
​&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合图像修复、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（图 7）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。引自 Dinh et al., 2017。|400x400px|链接=Special:FilePath/Realnvp_lsun_inpainting.png]]&lt;br /&gt;
​&lt;br /&gt;
== 在 NIS/NIS+ 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus，NIS+）中，RealNVP 以可逆神经网络（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆粗粒化。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
​&lt;br /&gt;
[[文件:nis_plus_architecture.png|缩略图|NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器与解码器，信息丢弃与 EI 最大化在宏观动力学空间完成。引自 Yang et al., 2025。|400x400px|链接=Special:FilePath/Nis_plus_architecture.png]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（Effective Information，EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度卷积神经网络（Convolutional Neural Network，CNN）/残差网络（Residual Network，ResNet），表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow（Generative Flow with Invertible 1×1 Convolutions，Glow）的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
​&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow，MAF）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics，FFJORD）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
​&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图9：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:Realnvp_lsun_inpainting.png&amp;diff=50749</id>
		<title>文件:Realnvp lsun inpainting.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:Realnvp_lsun_inpainting.png&amp;diff=50749"/>
		<updated>2026-08-04T06:11:21Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：RealNVP在 LSUN 卧室数据集上的图像修复结果示例&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== 文件说明 ==&lt;br /&gt;
RealNVP在 LSUN 卧室数据集上的图像修复结果示例&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:Realnvp_celeba_interpolation.png&amp;diff=50748</id>
		<title>文件:Realnvp celeba interpolation.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:Realnvp_celeba_interpolation.png&amp;diff=50748"/>
		<updated>2026-08-04T06:06:58Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：RealNVP 在 celebA 中插值表现示例&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== 文件说明 ==&lt;br /&gt;
RealNVP 在 celebA 中插值表现示例&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:Realnvp_celeba_generation.png&amp;diff=50747</id>
		<title>文件:Realnvp celeba generation.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:Realnvp_celeba_generation.png&amp;diff=50747"/>
		<updated>2026-08-04T06:01:23Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：黄一凡上传文件:Realnvp celeba generation.png的新版本&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== 文件说明 ==&lt;br /&gt;
RealNVP在 celebA人脸生成任务效果示例&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:Realnvp_celeba_generation.png&amp;diff=50746</id>
		<title>文件:Realnvp celeba generation.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:Realnvp_celeba_generation.png&amp;diff=50746"/>
		<updated>2026-08-04T05:52:07Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：RealNVP在 celebA人脸生成任务效果示例&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== 文件说明 ==&lt;br /&gt;
RealNVP在 celebA人脸生成任务效果示例&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50745</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50745"/>
		<updated>2026-08-04T05:50:10Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：更改图片大小&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;编辑“RealNVP”&lt;br /&gt;
跳到导航跳到搜索&lt;br /&gt;
未选择任何文件&lt;br /&gt;
高级&lt;br /&gt;
特殊字符&lt;br /&gt;
帮助&lt;br /&gt;
拖动文件至此&lt;br /&gt;
&lt;br /&gt;
AI&lt;br /&gt;
一键智能添加链接&lt;br /&gt;
⚙&lt;br /&gt;
'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
​&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation，NICE），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|848x848px]]&lt;br /&gt;
​&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过空间棋盘掩码（checkerboard masking）与通道级掩码（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
图 4 给出了整体的多尺度架构。如图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量归一化）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后通过变量分解把当前特征一分为二；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化）就进行变量分解。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
图 2 给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|626x626px]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构，因为常用于图像相关的任务，所以人们通常用卷积神经网络（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——[[计算复杂度]]从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
​&lt;br /&gt;
图 4 的多尺度架构依赖两个关键操作：压缩与变量分解。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量归一化（Batch Normalization，BN）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BN 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
​&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
== 标准化流与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
仿射耦合的逆变换为：对于公式 (1) 中的前向变换，经过代数变换即可得到公式 (2) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：自适应矩估计（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
​&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。由于采样过程不依赖逐像素的顺序解码，生成速度显著高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（图 5）。与变分自编码器（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等自回归模型相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。引自 Dinh et al., 2017。|400x400px|链接=Special:FilePath/Realnvp_celeba_generation.png]]&lt;br /&gt;
​&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 学到的潜在空间具有可解释的语义结构，支持对图像属性的连续操控。在 CelebA 上，模型可以在四个验证样本的潜在编码之间做球面插值：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合（图 6）。在类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，表明该空间具有类似词向量的线性语义结构。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_celeba_interpolation.png|缩略图|RealNVP 在 CelebA 潜在空间中做球面插值的结果：上半部分为四个角点的生成样本，下半部分为沿球面路径连续插值得到的人脸变化。引自 Dinh et al., 2017。|400x400px|链接=Special:FilePath/Realnvp_celeba_interpolation.png]]&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合图像修复、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（图 7）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。引自 Dinh et al., 2017。|400x400px|链接=Special:FilePath/Realnvp_lsun_inpainting.png]]&lt;br /&gt;
​&lt;br /&gt;
== 在 NIS/NIS+ 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus，NIS+）中，RealNVP 以可逆神经网络（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆粗粒化。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
​&lt;br /&gt;
[[文件:nis_plus_architecture.png|缩略图|NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器与解码器，信息丢弃与 EI 最大化在宏观动力学空间完成。引自 Yang et al., 2025。|400x400px|链接=Special:FilePath/Nis_plus_architecture.png]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（Effective Information，EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度卷积神经网络（Convolutional Neural Network，CNN）/残差网络（Residual Network，ResNet），表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow（Generative Flow with Invertible 1×1 Convolutions，Glow）的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
​&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow，MAF）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics，FFJORD）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
​&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图9：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50744</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50744"/>
		<updated>2026-08-04T05:46:27Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：缩写统一为“中文概念（英文全称，缩写）”。 NIS/NIS+ 内容仅保留在应用章节 增加交叉引用：引用“图 2”“图 4”以及公式 (1)(2) 等。应用章节移到优缺点之前。 应用章节重写：删除/dim 表格，聚焦任务场景与效果，四个应用子节每节一张对应论文图片。&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;编辑“RealNVP”&lt;br /&gt;
跳到导航跳到搜索&lt;br /&gt;
未选择任何文件&lt;br /&gt;
高级&lt;br /&gt;
特殊字符&lt;br /&gt;
帮助&lt;br /&gt;
拖动文件至此&lt;br /&gt;
&lt;br /&gt;
AI&lt;br /&gt;
一键智能添加链接&lt;br /&gt;
⚙&lt;br /&gt;
'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
​&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
​&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
​&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（Independent Component Analysis，ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation，NICE），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
​&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
​&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。批量归一化（Batch Normalization，BN）为图中归一化操作的简写。|替代=|552x552px]]&lt;br /&gt;
​&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（affine coupling layer）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过空间棋盘掩码（checkerboard masking）与通道级掩码（channel-wise masking）实现，二者交替使用。&lt;br /&gt;
​&lt;br /&gt;
图 4 给出了整体的多尺度架构。如图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个空间棋盘掩码的耦合层（每个耦合层输出后接批量归一化）、压缩块、再 3 个通道级掩码的耦合层（同样每个后接批量归一化），最后通过变量分解把当前特征一分为二；末尺度则只经过 4 个空间棋盘掩码耦合层（同样每个后接批量归一化）就进行变量分解。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
​&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
​&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
​&lt;br /&gt;
图 2 给出了仿射耦合层的前向流程。图中的耦合层块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|344x344像素]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构，因为常用于图像相关的任务，所以人们通常用卷积神经网络（Convolutional Neural Network，CNN））。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——[[计算复杂度]]从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
​&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按公式 (2) 进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
​&lt;br /&gt;
* '''空间棋盘掩码'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
​&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
​&lt;br /&gt;
图 4 的多尺度架构依赖两个关键操作：压缩与变量分解。&lt;br /&gt;
​&lt;br /&gt;
'''压缩'''：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
​&lt;br /&gt;
'''变量分解'''：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
​&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
​&lt;br /&gt;
批量归一化（Batch Normalization，BN）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BN 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
​&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
​&lt;br /&gt;
== 标准化流与[[变量替换公式]] ==&lt;br /&gt;
​&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
​&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
​&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
​&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
​&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
​&lt;br /&gt;
仿射耦合的逆变换为：对于公式 (1) 中的前向变换，经过代数变换即可得到公式 (2) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
​&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
​&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度（Kullback-Leibler divergence，KL）：&lt;br /&gt;
​&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
​&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
​&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
​&lt;br /&gt;
'''实际训练配置'''：自适应矩估计（Adaptive Moment Estimation，Adam）优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
​&lt;br /&gt;
= 应用 =&lt;br /&gt;
​&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 最直接的应用场景是连续高维数据的密度估计与无条件生成。在图像生成任务中，模型一次性学习整个图像的联合分布，然后直接从标准高斯分布采样并通过逆映射得到新图像。由于采样过程不依赖逐像素的顺序解码，生成速度显著高于自回归模型。&lt;br /&gt;
​&lt;br /&gt;
在 CelebA 人脸生成任务中，RealNVP 能够生成全局连贯、五官清晰的人脸样本（图 5）。与变分自编码器（Variational Autoencoder，VAE）相比，RealNVP 不依赖固定的重建损失（如均方误差（Mean Squared Error，MSE）），因此不会过度平滑高频细节；与 PixelRNN 等自回归模型相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_celeba_generation.png|缩略图|RealNVP 在 CelebA 上生成的人脸样本。引自 Dinh et al., 2017。|替代=|400x400px]]&lt;br /&gt;
​&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 学到的潜在空间具有可解释的语义结构，支持对图像属性的连续操控。在 CelebA 上，模型可以在四个验证样本的潜在编码之间做球面插值：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合（图 6）。在类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，表明该空间具有类似词向量的线性语义结构。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_celeba_interpolation.png|缩略图|RealNVP 在 CelebA 潜在空间中做球面插值的结果：上半部分为四个角点的生成样本，下半部分为沿球面路径连续插值得到的人脸变化。引自 Dinh et al., 2017。|替代=|400x400px]]&lt;br /&gt;
​&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
​&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合图像修复、去噪、着色等重建设置。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。在 LSUN 卧室数据集上，RealNVP 对缺失区域（红框）的补全结果在语义和视觉上均保持合理（图 7）。&lt;br /&gt;
​&lt;br /&gt;
[[文件:realnvp_lsun_inpainting.png|缩略图|RealNVP 在 LSUN 卧室数据集上的图像修复结果：红框标出被遮挡的原始区域，模型仅根据剩余像素和全分布密度完成补全。引自 Dinh et al., 2017。|替代=|400x400px]]&lt;br /&gt;
​&lt;br /&gt;
== 在 NIS/NIS+ 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
​&lt;br /&gt;
在神经信息挤压器（Neural Information Squeezer，NIS）及其扩展 NIS+（Neural Information Squeezer Plus，NIS+）中，RealNVP 以可逆神经网络（Invertible Neural Networks，INN）作为编码器，实现微观状态到宏观状态的可逆粗粒化。粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
​&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
​&lt;br /&gt;
[[文件:nis_plus_architecture.png|缩略图|NIS+ 框架架构：RealNVP 的可逆神经网络（INN）实现编码器与解码器，信息丢弃与 EI 最大化在宏观动力学空间完成。引自 Yang et al., 2025。|替代=|400x400px]]&lt;br /&gt;
​&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（Effective Information，EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
​&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
​&lt;br /&gt;
== 优势 ==&lt;br /&gt;
​&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度卷积神经网络（Convolutional Neural Network，CNN）/残差网络（Residual Network，ResNet），表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
​&lt;br /&gt;
== 局限 ==&lt;br /&gt;
​&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow（Generative Flow with Invertible 1×1 Convolutions，Glow）的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
​&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
​&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
​&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
​&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''（Masked Autoregressive Flow，MAF）&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''（Free-form Jacobian of Reversible Dynamics，FFJORD）&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + 常微分方程（Ordinary Differential Equation，ODE）描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
​&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
​&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;!-- 图9：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
​&lt;br /&gt;
= 署名 =&lt;br /&gt;
​&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
​&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
​&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50743</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50743"/>
		<updated>2026-08-03T12:06:38Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：去掉多余的 ref&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
在[[因果涌现]]的研究中，RealNVP 常被用作[[可逆编码器]]（INN）的实现载体。以 [[NIS]] 和 [[NIS+]] 为例，编码器被分解为两步：先用 RealNVP 的耦合层将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 无损地映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;（信息转换），再通过投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t&amp;lt;/math&amp;gt;（信息丢弃）。因此，RealNVP 的可逆性、参数共享和解析雅可比行列式，是 NIS 系列实现“可控维度、双向编码—解码”的技术基础；需要强调的是，宏观粗粒化本身是有损的，真正无损的仅是 INN 内部的信息转换阶段。更详细的 NIS/NIS+ 应用见后文。&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。BN 为批量归一化（Batch Normalization）操作的简写。|替代=|552x552px]]&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（coupling）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过 空间棋盘（checkerboard）与通道级（channel-wise）两种掩码实现，二者交替使用。&lt;br /&gt;
&lt;br /&gt;
如上图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个 checkerboard 掩码的耦合层Coupling（每个耦合层输出后接批量归一化BN）、[[压缩]]块Squeeze 、再 3 个 channel-wise 掩码的耦合层（同样每个后接批量归一化），最后通过[[变量分解]] Factor Out 把当前特征一分为二；末尺度则只经过 4 个 checkerboard 耦合层（同样每个后接批量归一化）就进行 Factor Out。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
&lt;br /&gt;
图中的 '''Coupling''' 块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|344x344像素]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（可以是任意结构，因为常用于图像相关的任务，所以人们通常用[[CNN]]）。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——[[计算复杂度]]从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
&lt;br /&gt;
* '''空间棋盘模式'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
&lt;br /&gt;
多尺度架构依赖两个关键操作：压缩（Squeeze）与变量分解（Factor Out）。&lt;br /&gt;
&lt;br /&gt;
'''压缩'''（squeezing）：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
&lt;br /&gt;
'''变量分解'''（factoring out）：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
&lt;br /&gt;
批量归一化（BatchNorm）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BatchNorm 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
&lt;br /&gt;
== 标准化流与[[变量替换公式]] ==&lt;br /&gt;
&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
&lt;br /&gt;
仿射耦合的逆变换为：对于公式 ({{EquationNote|1}}) 中的前向变换，经过代数变换即可得到公式 ({{EquationNote|2}}) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
&lt;br /&gt;
'''实际训练配置'''：Adam 优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
&lt;br /&gt;
== 优势 ==&lt;br /&gt;
&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度 CNN/ResNet，表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
&lt;br /&gt;
== 局限 ==&lt;br /&gt;
&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow 的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + ODE 描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图7：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 应用 =&lt;br /&gt;
&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在多个图像基准上验证了其密度估计与生成性能：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 数据集 !! 分辨率 !! 测试对数似然（bits/dim） !! 关键发现&lt;br /&gt;
|-&lt;br /&gt;
| CIFAR-10 || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 3.49 || 生成样本全局连贯，优于 VAE&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 4.28 || 有效建模大规模自然图像分布&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || 3.98 || 能捕捉前景/背景分割与光照一致性&lt;br /&gt;
|-&lt;br /&gt;
| LSUN（卧室/塔楼/教堂） || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 建模了室内外场景的语义结构&lt;br /&gt;
|-&lt;br /&gt;
| CelebA || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 潜在空间具有可解释的语义方向&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
与 VAE 相比，RealNVP 不依赖固定的重建损失（如 MSE），避免了过度平滑高频细节；与自回归模型（如 PixelRNN）相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图5：CelebA 生成样本与真实样本对比 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的潜在空间具有可解释的语义结构。给定四个样本的编码 &amp;lt;math&amp;gt;\mathbf{z}^{(1)}, \dots, \mathbf{z}^{(4)}&amp;lt;/math&amp;gt;，可在潜在空间中做球面插值：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合。在 CelebA 的类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，表明该空间具有类似词向量的线性语义结构。&lt;br /&gt;
&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 适合图像修复、去噪、着色等任务。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。&lt;br /&gt;
&lt;br /&gt;
== 在 NIS/NIS+ 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
&lt;br /&gt;
在 [[NIS]]（[[神经信息挤压器]]）及 [[NIS+]] 中，RealNVP 被用作可逆编码器（INN），粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的[[可逆神经网络]]（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图6：NIS 框架中 RealNVP 作为 INN 的位置示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使[[有效信息]]（[[EI]]）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50683</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50683"/>
		<updated>2026-07-23T08:35:05Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值[[非体积保持]]变换）是一类基于[[标准化流]]（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的[[雅可比行列式]]都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
在[[因果涌现]]的研究中，RealNVP 常被用作可逆编码器（INN）的实现载体。以 [[NIS]] 和 [[NIS+]] 为例，编码器被分解为两步：先用 RealNVP 的耦合层将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 无损地映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;（信息转换），再通过投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t&amp;lt;/math&amp;gt;（信息丢弃）。因此，RealNVP 的可逆性、参数共享和解析雅可比行列式，是 NIS 系列实现“可控维度、双向编码—解码”的技术基础；需要强调的是，宏观粗粒化本身是有损的，真正无损的仅是 INN 内部的信息转换阶段。更详细的 NIS/NIS+ 应用见后文。&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;[[归一化]]&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''[[加性耦合层]]'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：[[体积保持]]的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。BN 为批量归一化（Batch Normalization）操作的简写。|替代=|552x552px]]&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（coupling）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过 空间棋盘（checkerboard）与通道级（channel-wise）两种掩码实现，二者交替使用。&lt;br /&gt;
&lt;br /&gt;
如上图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个 checkerboard 掩码的耦合层Coupling（每个耦合层输出后接批量归一化BN）、[[压缩]]块Squeeze 、再 3 个 channel-wise 掩码的耦合层（同样每个后接批量归一化），最后通过[[变量分解]] Factor Out 把当前特征一分为二；末尺度则只经过 4 个 checkerboard 耦合层（同样每个后接批量归一化）就进行 Factor Out。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
&lt;br /&gt;
== [[仿射耦合层]]：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
&lt;br /&gt;
图中的 '''Coupling''' 块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|344x344像素]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（通常是 CNN）。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——[[计算复杂度]]从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
&lt;br /&gt;
* '''空间棋盘模式'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''[[通道级掩码]]'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
&lt;br /&gt;
多尺度架构依赖两个关键操作：压缩（Squeeze）与变量分解（Factor Out）。&lt;br /&gt;
&lt;br /&gt;
'''压缩'''（squeezing）：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
&lt;br /&gt;
'''变量分解'''（factoring out）：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
&lt;br /&gt;
批量归一化（BatchNorm）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BatchNorm 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
&lt;br /&gt;
== 标准化流与[[变量替换公式]] ==&lt;br /&gt;
&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
&lt;br /&gt;
仿射耦合的逆变换为：对于公式 ({{EquationNote|1}}) 中的前向变换，经过代数变换即可得到公式 ({{EquationNote|2}}) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 损失函数与[[最大似然训练]] ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与[[信息论]]中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
&lt;br /&gt;
'''实际训练配置'''：Adam 优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
&lt;br /&gt;
== 优势 ==&lt;br /&gt;
&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度 CNN/ResNet，表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
&lt;br /&gt;
== 局限 ==&lt;br /&gt;
&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow 的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + ODE 描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图7：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 应用 =&lt;br /&gt;
&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在多个图像基准上验证了其密度估计与生成性能：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 数据集 !! 分辨率 !! 测试对数似然（bits/dim） !! 关键发现&lt;br /&gt;
|-&lt;br /&gt;
| CIFAR-10 || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 3.49 || 生成样本全局连贯，优于 VAE&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 4.28 || 有效建模大规模自然图像分布&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || 3.98 || 能捕捉前景/背景分割与光照一致性&lt;br /&gt;
|-&lt;br /&gt;
| LSUN（卧室/塔楼/教堂） || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 建模了室内外场景的语义结构&lt;br /&gt;
|-&lt;br /&gt;
| CelebA || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 潜在空间具有可解释的语义方向&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
与 VAE 相比，RealNVP 不依赖固定的重建损失（如 MSE），避免了过度平滑高频细节；与自回归模型（如 PixelRNN）相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图5：CelebA 生成样本与真实样本对比 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的潜在空间具有可解释的语义结构。给定四个样本的编码 &amp;lt;math&amp;gt;\mathbf{z}^{(1)}, \dots, \mathbf{z}^{(4)}&amp;lt;/math&amp;gt;，可在潜在空间中做球面插值：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合。在 CelebA 的类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，表明该空间具有类似词向量的线性语义结构。&lt;br /&gt;
&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 适合图像修复、去噪、着色等任务。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。&lt;br /&gt;
&lt;br /&gt;
== 在 NIS/NIS+ 因果[[涌现]]框架中的应用 ==&lt;br /&gt;
&lt;br /&gt;
在 [[NIS]]（[[神经信息挤压器]]）及 [[NIS+]] 中，RealNVP 被用作可逆编码器（INN），粗粒化[[策略]]被建模为两阶段过程：&lt;br /&gt;
&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的[[可逆神经网络]]（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图6：NIS 框架中 RealNVP 作为 INN 的位置示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使[[有效信息]]（[[EI]]）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
= 参考文献 =&lt;br /&gt;
&lt;br /&gt;
# Dinh L., Sohl-Dickstein J., Bengio S. Density Estimation Using Real NVP. International Conference on Learning Representations (ICLR), 2017. https://arxiv.org/abs/1605.08803&lt;br /&gt;
# Chen S. S., Gopinath R. A. Gaussianization. Advances in Neural Information Processing Systems (NIPS), 2000.&lt;br /&gt;
# Tabak E. G., Vanden-Eijnden E. Density Estimation by Dual Ascent of the Log-Likelihood. Communications in Mathematical Sciences, 2010, 8(1): 217-233.&lt;br /&gt;
# Dinh L., Krueger D., Bengio Y. NICE: Non-linear Independent Components Estimation. ICLR 2015 Workshop, 2014. https://arxiv.org/abs/1410.8516&lt;br /&gt;
# Draxler F., Wahl S., Schnörr C., Köthe U. On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows. Proceedings of the 41st International Conference on Machine Learning (ICML), PMLR 235, 2024.&lt;br /&gt;
# Ho J., Chen X., Srinivas A., Duan Y., Abbeel P. Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design. ICML, 2019.&lt;br /&gt;
# Kingma D. P., Dhariwal P. Glow: Generative Flow with Invertible 1×1 Convolutions. NeurIPS, 2018.&lt;br /&gt;
# Rezende D. J., Mohamed S. Variational Inference with Normalizing Flows. ICML, 2015.&lt;br /&gt;
# Papamakarios G., Pavlakou T., Murray I. Masked Autoregressive Flow for Density Estimation. NIPS, 2017.&lt;br /&gt;
# Grathwohl W., Chen R. T. Q., Bettencourt J., Sutskever I., Duvenaud D. FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models. ICLR, 2019.&lt;br /&gt;
# Chen R. T. Q., Behrmann J., Duvenaud D., Jacobsen J.-H. Residual Flows for Invertible Generative Modeling. NeurIPS, 2019.&lt;br /&gt;
# Papamakarios G., Nalisnick E., Rezende D. J., Mohamed S., Lakshminarayanan B. Normalizing Flows for Probabilistic Modeling and Inference. Journal of Machine Learning Research, 2021, 22(57): 1-64.&lt;br /&gt;
# Zhang J., Liu K. Neural Information Squeezer for [[Causal Emergence]]. [[Entropy]], 2022, 25(1): 26.&lt;br /&gt;
# Yang M., Wang Z., Liu K., Rong Y., Yuan B., Zhang J. Finding Emergence in Data by Maximizing [[Effective Information]]. National Science Review, 2025, 12(1): nwae279.&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写，[审阅者姓名] 审阅，[日期] 发布。&lt;br /&gt;
&lt;br /&gt;
'''贡献者'''：[[集智百科]]团队&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50682</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50682"/>
		<updated>2026-07-23T08:31:49Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：/* 署名 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值非体积保持变换）是一类基于标准化流（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的雅可比行列式都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
在因果涌现的研究中，RealNVP 常被用作可逆编码器（INN）的实现载体。以 NIS 和 NIS+ 为例，编码器被分解为两步：先用 RealNVP 的耦合层将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 无损地映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;（信息转换），再通过投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t&amp;lt;/math&amp;gt;（信息丢弃）。因此，RealNVP 的可逆性、参数共享和解析雅可比行列式，是 NIS 系列实现“可控维度、双向编码—解码”的技术基础；需要强调的是，宏观粗粒化本身是有损的，真正无损的仅是 INN 内部的信息转换阶段。更详细的 NIS/NIS+ 应用见后文。&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;归一化&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''加性耦合层'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：体积保持的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。BN 为批量归一化（Batch Normalization）操作的简写。|替代=|552x552px]]&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（coupling）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过 空间棋盘（checkerboard）与通道级（channel-wise）两种掩码实现，二者交替使用。&lt;br /&gt;
&lt;br /&gt;
如上图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个 checkerboard 掩码的耦合层Coupling（每个耦合层输出后接批量归一化BN）、压缩块Squeeze 、再 3 个 channel-wise 掩码的耦合层（同样每个后接批量归一化），最后通过变量分解 Factor Out 把当前特征一分为二；末尺度则只经过 4 个 checkerboard 耦合层（同样每个后接批量归一化）就进行 Factor Out。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合层：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
&lt;br /&gt;
图中的 '''Coupling''' 块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|344x344像素]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（通常是 CNN）。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——计算复杂度从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
&lt;br /&gt;
* '''空间棋盘模式'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''通道级掩码'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
&lt;br /&gt;
多尺度架构依赖两个关键操作：压缩（Squeeze）与变量分解（Factor Out）。&lt;br /&gt;
&lt;br /&gt;
'''压缩'''（squeezing）：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
&lt;br /&gt;
'''变量分解'''（factoring out）：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
&lt;br /&gt;
批量归一化（BatchNorm）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BatchNorm 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
&lt;br /&gt;
== 标准化流与变量替换公式 ==&lt;br /&gt;
&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
&lt;br /&gt;
仿射耦合的逆变换为：对于公式 ({{EquationNote|1}}) 中的前向变换，经过代数变换即可得到公式 ({{EquationNote|2}}) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 损失函数与最大似然训练 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与信息论中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
&lt;br /&gt;
'''实际训练配置'''：Adam 优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
&lt;br /&gt;
== 优势 ==&lt;br /&gt;
&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度 CNN/ResNet，表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
&lt;br /&gt;
== 局限 ==&lt;br /&gt;
&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow 的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + ODE 描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图7：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 应用 =&lt;br /&gt;
&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在多个图像基准上验证了其密度估计与生成性能：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 数据集 !! 分辨率 !! 测试对数似然（bits/dim） !! 关键发现&lt;br /&gt;
|-&lt;br /&gt;
| CIFAR-10 || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 3.49 || 生成样本全局连贯，优于 VAE&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 4.28 || 有效建模大规模自然图像分布&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || 3.98 || 能捕捉前景/背景分割与光照一致性&lt;br /&gt;
|-&lt;br /&gt;
| LSUN（卧室/塔楼/教堂） || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 建模了室内外场景的语义结构&lt;br /&gt;
|-&lt;br /&gt;
| CelebA || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 潜在空间具有可解释的语义方向&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
与 VAE 相比，RealNVP 不依赖固定的重建损失（如 MSE），避免了过度平滑高频细节；与自回归模型（如 PixelRNN）相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图5：CelebA 生成样本与真实样本对比 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的潜在空间具有可解释的语义结构。给定四个样本的编码 &amp;lt;math&amp;gt;\mathbf{z}^{(1)}, \dots, \mathbf{z}^{(4)}&amp;lt;/math&amp;gt;，可在潜在空间中做球面插值：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合。在 CelebA 的类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，表明该空间具有类似词向量的线性语义结构。&lt;br /&gt;
&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 适合图像修复、去噪、着色等任务。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。&lt;br /&gt;
&lt;br /&gt;
== 在 NIS/NIS+ 因果涌现框架中的应用 ==&lt;br /&gt;
&lt;br /&gt;
在 [[NIS]]（神经信息挤压器）及 [[NIS+]] 中，RealNVP 被用作可逆编码器（INN），粗粒化策略被建模为两阶段过程：&lt;br /&gt;
&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图6：NIS 框架中 RealNVP 作为 INN 的位置示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
= 参考文献 =&lt;br /&gt;
&lt;br /&gt;
# Dinh L., Sohl-Dickstein J., Bengio S. Density Estimation Using Real NVP. International Conference on Learning Representations (ICLR), 2017. https://arxiv.org/abs/1605.08803&lt;br /&gt;
# Chen S. S., Gopinath R. A. Gaussianization. Advances in Neural Information Processing Systems (NIPS), 2000.&lt;br /&gt;
# Tabak E. G., Vanden-Eijnden E. Density Estimation by Dual Ascent of the Log-Likelihood. Communications in Mathematical Sciences, 2010, 8(1): 217-233.&lt;br /&gt;
# Dinh L., Krueger D., Bengio Y. NICE: Non-linear Independent Components Estimation. ICLR 2015 Workshop, 2014. https://arxiv.org/abs/1410.8516&lt;br /&gt;
# Draxler F., Wahl S., Schnörr C., Köthe U. On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows. Proceedings of the 41st International Conference on Machine Learning (ICML), PMLR 235, 2024.&lt;br /&gt;
# Ho J., Chen X., Srinivas A., Duan Y., Abbeel P. Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design. ICML, 2019.&lt;br /&gt;
# Kingma D. P., Dhariwal P. Glow: Generative Flow with Invertible 1×1 Convolutions. NeurIPS, 2018.&lt;br /&gt;
# Rezende D. J., Mohamed S. Variational Inference with Normalizing Flows. ICML, 2015.&lt;br /&gt;
# Papamakarios G., Pavlakou T., Murray I. Masked Autoregressive Flow for Density Estimation. NIPS, 2017.&lt;br /&gt;
# Grathwohl W., Chen R. T. Q., Bettencourt J., Sutskever I., Duvenaud D. FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models. ICLR, 2019.&lt;br /&gt;
# Chen R. T. Q., Behrmann J., Duvenaud D., Jacobsen J.-H. Residual Flows for Invertible Generative Modeling. NeurIPS, 2019.&lt;br /&gt;
# Papamakarios G., Nalisnick E., Rezende D. J., Mohamed S., Lakshminarayanan B. Normalizing Flows for Probabilistic Modeling and Inference. Journal of Machine Learning Research, 2021, 22(57): 1-64.&lt;br /&gt;
# Zhang J., Liu K. Neural Information Squeezer for Causal Emergence. Entropy, 2022, 25(1): 26.&lt;br /&gt;
# Yang M., Wang Z., Liu K., Rong Y., Yuan B., Zhang J. Finding Emergence in Data by Maximizing Effective Information. National Science Review, 2025, 12(1): nwae279.&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
&lt;br /&gt;
本词条由 [[用户:黄一凡|黄一凡]] 编写，[审阅者姓名] 审阅，[日期] 发布。&lt;br /&gt;
&lt;br /&gt;
'''贡献者'''：集智百科团队&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50681</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50681"/>
		<updated>2026-07-23T08:27:56Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：调整图片位置&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值非体积保持变换）是一类基于标准化流（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的雅可比行列式都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
在因果涌现的研究中，RealNVP 常被用作可逆编码器（INN）的实现载体。以 NIS 和 NIS+ 为例，编码器被分解为两步：先用 RealNVP 的耦合层将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 无损地映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;（信息转换），再通过投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t&amp;lt;/math&amp;gt;（信息丢弃）。因此，RealNVP 的可逆性、参数共享和解析雅可比行列式，是 NIS 系列实现“可控维度、双向编码—解码”的技术基础；需要强调的是，宏观粗粒化本身是有损的，真正无损的仅是 INN 内部的信息转换阶段。更详细的 NIS/NIS+ 应用见后文。&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;归一化&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''加性耦合层'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：体积保持的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。BN 为批量归一化（Batch Normalization）操作的简写。|替代=|552x552px]]&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（coupling）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过 空间棋盘（checkerboard）与通道级（channel-wise）两种掩码实现，二者交替使用。&lt;br /&gt;
&lt;br /&gt;
如上图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个 checkerboard 掩码的耦合层Coupling（每个耦合层输出后接批量归一化BN）、压缩块Squeeze 、再 3 个 channel-wise 掩码的耦合层（同样每个后接批量归一化），最后通过变量分解 Factor Out 把当前特征一分为二；末尺度则只经过 4 个 checkerboard 耦合层（同样每个后接批量归一化）就进行 Factor Out。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合层：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
&lt;br /&gt;
图中的 '''Coupling''' 块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=|344x344像素]]{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（通常是 CNN）。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——计算复杂度从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
&lt;br /&gt;
* '''空间棋盘模式'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''通道级掩码'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
&lt;br /&gt;
多尺度架构依赖两个关键操作：压缩（Squeeze）与变量分解（Factor Out）。&lt;br /&gt;
&lt;br /&gt;
'''压缩'''（squeezing）：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
&lt;br /&gt;
'''变量分解'''（factoring out）：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
&lt;br /&gt;
批量归一化（BatchNorm）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BatchNorm 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
&lt;br /&gt;
== 标准化流与变量替换公式 ==&lt;br /&gt;
&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
&lt;br /&gt;
仿射耦合的逆变换为：对于公式 ({{EquationNote|1}}) 中的前向变换，经过代数变换即可得到公式 ({{EquationNote|2}}) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 损失函数与最大似然训练 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与信息论中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
&lt;br /&gt;
'''实际训练配置'''：Adam 优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
&lt;br /&gt;
== 优势 ==&lt;br /&gt;
&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度 CNN/ResNet，表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
&lt;br /&gt;
== 局限 ==&lt;br /&gt;
&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow 的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + ODE 描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图7：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 应用 =&lt;br /&gt;
&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在多个图像基准上验证了其密度估计与生成性能：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 数据集 !! 分辨率 !! 测试对数似然（bits/dim） !! 关键发现&lt;br /&gt;
|-&lt;br /&gt;
| CIFAR-10 || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 3.49 || 生成样本全局连贯，优于 VAE&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 4.28 || 有效建模大规模自然图像分布&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || 3.98 || 能捕捉前景/背景分割与光照一致性&lt;br /&gt;
|-&lt;br /&gt;
| LSUN（卧室/塔楼/教堂） || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 建模了室内外场景的语义结构&lt;br /&gt;
|-&lt;br /&gt;
| CelebA || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 潜在空间具有可解释的语义方向&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
与 VAE 相比，RealNVP 不依赖固定的重建损失（如 MSE），避免了过度平滑高频细节；与自回归模型（如 PixelRNN）相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图5：CelebA 生成样本与真实样本对比 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的潜在空间具有可解释的语义结构。给定四个样本的编码 &amp;lt;math&amp;gt;\mathbf{z}^{(1)}, \dots, \mathbf{z}^{(4)}&amp;lt;/math&amp;gt;，可在潜在空间中做球面插值：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合。在 CelebA 的类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，表明该空间具有类似词向量的线性语义结构。&lt;br /&gt;
&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 适合图像修复、去噪、着色等任务。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。&lt;br /&gt;
&lt;br /&gt;
== 在 NIS/NIS+ 因果涌现框架中的应用 ==&lt;br /&gt;
&lt;br /&gt;
在 [[NIS]]（神经信息挤压器）及 [[NIS+]] 中，RealNVP 被用作可逆编码器（INN），粗粒化策略被建模为两阶段过程：&lt;br /&gt;
&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图6：NIS 框架中 RealNVP 作为 INN 的位置示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
= 参考文献 =&lt;br /&gt;
&lt;br /&gt;
# Dinh L., Sohl-Dickstein J., Bengio S. Density Estimation Using Real NVP. International Conference on Learning Representations (ICLR), 2017. https://arxiv.org/abs/1605.08803&lt;br /&gt;
# Chen S. S., Gopinath R. A. Gaussianization. Advances in Neural Information Processing Systems (NIPS), 2000.&lt;br /&gt;
# Tabak E. G., Vanden-Eijnden E. Density Estimation by Dual Ascent of the Log-Likelihood. Communications in Mathematical Sciences, 2010, 8(1): 217-233.&lt;br /&gt;
# Dinh L., Krueger D., Bengio Y. NICE: Non-linear Independent Components Estimation. ICLR 2015 Workshop, 2014. https://arxiv.org/abs/1410.8516&lt;br /&gt;
# Draxler F., Wahl S., Schnörr C., Köthe U. On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows. Proceedings of the 41st International Conference on Machine Learning (ICML), PMLR 235, 2024.&lt;br /&gt;
# Ho J., Chen X., Srinivas A., Duan Y., Abbeel P. Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design. ICML, 2019.&lt;br /&gt;
# Kingma D. P., Dhariwal P. Glow: Generative Flow with Invertible 1×1 Convolutions. NeurIPS, 2018.&lt;br /&gt;
# Rezende D. J., Mohamed S. Variational Inference with Normalizing Flows. ICML, 2015.&lt;br /&gt;
# Papamakarios G., Pavlakou T., Murray I. Masked Autoregressive Flow for Density Estimation. NIPS, 2017.&lt;br /&gt;
# Grathwohl W., Chen R. T. Q., Bettencourt J., Sutskever I., Duvenaud D. FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models. ICLR, 2019.&lt;br /&gt;
# Chen R. T. Q., Behrmann J., Duvenaud D., Jacobsen J.-H. Residual Flows for Invertible Generative Modeling. NeurIPS, 2019.&lt;br /&gt;
# Papamakarios G., Nalisnick E., Rezende D. J., Mohamed S., Lakshminarayanan B. Normalizing Flows for Probabilistic Modeling and Inference. Journal of Machine Learning Research, 2021, 22(57): 1-64.&lt;br /&gt;
# Zhang J., Liu K. Neural Information Squeezer for Causal Emergence. Entropy, 2022, 25(1): 26.&lt;br /&gt;
# Yang M., Wang Z., Liu K., Rong Y., Yuan B., Zhang J. Finding Emergence in Data by Maximizing Effective Information. National Science Review, 2025, 12(1): nwae279.&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
&lt;br /&gt;
本词条由 [编写者姓名] 编写，[审阅者姓名] 审阅，[日期] 发布。&lt;br /&gt;
&lt;br /&gt;
'''贡献者'''：集智百科团队&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50680</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50680"/>
		<updated>2026-07-23T08:26:21Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：调整图片位置&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值非体积保持变换）是一类基于标准化流（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的雅可比行列式都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
在因果涌现的研究中，RealNVP 常被用作可逆编码器（INN）的实现载体。以 NIS 和 NIS+ 为例，编码器被分解为两步：先用 RealNVP 的耦合层将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 无损地映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;（信息转换），再通过投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t&amp;lt;/math&amp;gt;（信息丢弃）。因此，RealNVP 的可逆性、参数共享和解析雅可比行列式，是 NIS 系列实现“可控维度、双向编码—解码”的技术基础；需要强调的是，宏观粗粒化本身是有损的，真正无损的仅是 INN 内部的信息转换阶段。更详细的 NIS/NIS+ 应用见后文。&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;归一化&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''加性耦合层'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：体积保持的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。BN 为批量归一化（Batch Normalization）操作的简写。|替代=|753x753像素]]&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（coupling）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过 空间棋盘（checkerboard）与通道级（channel-wise）两种掩码实现，二者交替使用。&lt;br /&gt;
&lt;br /&gt;
如上图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个 checkerboard 掩码的耦合层Coupling（每个耦合层输出后接批量归一化BN）、压缩块Squeeze 、再 3 个 channel-wise 掩码的耦合层（同样每个后接批量归一化），最后通过变量分解 Factor Out 把当前特征一分为二；末尺度则只经过 4 个 checkerboard 耦合层（同样每个后接批量归一化）就进行 Factor Out。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合层：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
&lt;br /&gt;
图中的 '''Coupling''' 块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=]]这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（通常是 CNN）。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——计算复杂度从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
&lt;br /&gt;
* '''空间棋盘模式'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''通道级掩码'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
&lt;br /&gt;
多尺度架构依赖两个关键操作：压缩（Squeeze）与变量分解（Factor Out）。&lt;br /&gt;
&lt;br /&gt;
'''压缩'''（squeezing）：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
&lt;br /&gt;
'''变量分解'''（factoring out）：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
&lt;br /&gt;
批量归一化（BatchNorm）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BatchNorm 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
&lt;br /&gt;
== 标准化流与变量替换公式 ==&lt;br /&gt;
&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
&lt;br /&gt;
仿射耦合的逆变换为：对于公式 ({{EquationNote|1}}) 中的前向变换，经过代数变换即可得到公式 ({{EquationNote|2}}) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 损失函数与最大似然训练 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与信息论中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
&lt;br /&gt;
'''实际训练配置'''：Adam 优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
&lt;br /&gt;
== 优势 ==&lt;br /&gt;
&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度 CNN/ResNet，表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
&lt;br /&gt;
== 局限 ==&lt;br /&gt;
&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow 的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + ODE 描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图7：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 应用 =&lt;br /&gt;
&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在多个图像基准上验证了其密度估计与生成性能：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 数据集 !! 分辨率 !! 测试对数似然（bits/dim） !! 关键发现&lt;br /&gt;
|-&lt;br /&gt;
| CIFAR-10 || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 3.49 || 生成样本全局连贯，优于 VAE&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 4.28 || 有效建模大规模自然图像分布&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || 3.98 || 能捕捉前景/背景分割与光照一致性&lt;br /&gt;
|-&lt;br /&gt;
| LSUN（卧室/塔楼/教堂） || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 建模了室内外场景的语义结构&lt;br /&gt;
|-&lt;br /&gt;
| CelebA || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 潜在空间具有可解释的语义方向&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
与 VAE 相比，RealNVP 不依赖固定的重建损失（如 MSE），避免了过度平滑高频细节；与自回归模型（如 PixelRNN）相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图5：CelebA 生成样本与真实样本对比 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的潜在空间具有可解释的语义结构。给定四个样本的编码 &amp;lt;math&amp;gt;\mathbf{z}^{(1)}, \dots, \mathbf{z}^{(4)}&amp;lt;/math&amp;gt;，可在潜在空间中做球面插值：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合。在 CelebA 的类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，表明该空间具有类似词向量的线性语义结构。&lt;br /&gt;
&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 适合图像修复、去噪、着色等任务。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。&lt;br /&gt;
&lt;br /&gt;
== 在 NIS/NIS+ 因果涌现框架中的应用 ==&lt;br /&gt;
&lt;br /&gt;
在 [[NIS]]（神经信息挤压器）及 [[NIS+]] 中，RealNVP 被用作可逆编码器（INN），粗粒化策略被建模为两阶段过程：&lt;br /&gt;
&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图6：NIS 框架中 RealNVP 作为 INN 的位置示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
= 参考文献 =&lt;br /&gt;
&lt;br /&gt;
# Dinh L., Sohl-Dickstein J., Bengio S. Density Estimation Using Real NVP. International Conference on Learning Representations (ICLR), 2017. https://arxiv.org/abs/1605.08803&lt;br /&gt;
# Chen S. S., Gopinath R. A. Gaussianization. Advances in Neural Information Processing Systems (NIPS), 2000.&lt;br /&gt;
# Tabak E. G., Vanden-Eijnden E. Density Estimation by Dual Ascent of the Log-Likelihood. Communications in Mathematical Sciences, 2010, 8(1): 217-233.&lt;br /&gt;
# Dinh L., Krueger D., Bengio Y. NICE: Non-linear Independent Components Estimation. ICLR 2015 Workshop, 2014. https://arxiv.org/abs/1410.8516&lt;br /&gt;
# Draxler F., Wahl S., Schnörr C., Köthe U. On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows. Proceedings of the 41st International Conference on Machine Learning (ICML), PMLR 235, 2024.&lt;br /&gt;
# Ho J., Chen X., Srinivas A., Duan Y., Abbeel P. Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design. ICML, 2019.&lt;br /&gt;
# Kingma D. P., Dhariwal P. Glow: Generative Flow with Invertible 1×1 Convolutions. NeurIPS, 2018.&lt;br /&gt;
# Rezende D. J., Mohamed S. Variational Inference with Normalizing Flows. ICML, 2015.&lt;br /&gt;
# Papamakarios G., Pavlakou T., Murray I. Masked Autoregressive Flow for Density Estimation. NIPS, 2017.&lt;br /&gt;
# Grathwohl W., Chen R. T. Q., Bettencourt J., Sutskever I., Duvenaud D. FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models. ICLR, 2019.&lt;br /&gt;
# Chen R. T. Q., Behrmann J., Duvenaud D., Jacobsen J.-H. Residual Flows for Invertible Generative Modeling. NeurIPS, 2019.&lt;br /&gt;
# Papamakarios G., Nalisnick E., Rezende D. J., Mohamed S., Lakshminarayanan B. Normalizing Flows for Probabilistic Modeling and Inference. Journal of Machine Learning Research, 2021, 22(57): 1-64.&lt;br /&gt;
# Zhang J., Liu K. Neural Information Squeezer for Causal Emergence. Entropy, 2022, 25(1): 26.&lt;br /&gt;
# Yang M., Wang Z., Liu K., Rong Y., Yuan B., Zhang J. Finding Emergence in Data by Maximizing Effective Information. National Science Review, 2025, 12(1): nwae279.&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
&lt;br /&gt;
本词条由 [编写者姓名] 编写，[审阅者姓名] 审阅，[日期] 发布。&lt;br /&gt;
&lt;br /&gt;
'''贡献者'''：集智百科团队&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50679</id>
		<title>Realnvp</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50679"/>
		<updated>2026-07-23T08:24:10Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：尝试重定向&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#重定向 [[RealNVP]]&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50678</id>
		<title>Realnvp</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50678"/>
		<updated>2026-07-23T08:21:27Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：重定向页面至RealNVP&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#重定向 [[RealNVP]]&lt;br /&gt;
'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值非体积保持变换）是一类基于标准化流（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的雅可比行列式都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
在因果涌现的研究中，RealNVP 常被用作可逆编码器（INN）的实现载体。以 NIS 和 NIS+ 为例，编码器被分解为两步：先用 RealNVP 的耦合层将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 无损地映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;（信息转换），再通过投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t&amp;lt;/math&amp;gt;（信息丢弃）。因此，RealNVP 的可逆性、参数共享和解析雅可比行列式，是 NIS 系列实现“可控维度、双向编码—解码”的技术基础；需要强调的是，宏观粗粒化本身是有损的，真正无损的仅是 INN 内部的信息转换阶段。更详细的 NIS/NIS+ 应用见后文。&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;归一化&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''加性耦合层'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：体积保持的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（coupling）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过 空间棋盘（checkerboard）与通道级（channel-wise）两种掩码实现，二者交替使用。&lt;br /&gt;
&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。BN 为批量归一化（Batch Normalization）操作的简写。|替代=|753x753像素]]&lt;br /&gt;
&lt;br /&gt;
如上图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个 checkerboard 掩码的耦合层Coupling（每个耦合层输出后接批量归一化BN）、压缩块Squeeze 、再 3 个 channel-wise 掩码的耦合层（同样每个后接批量归一化），最后通过变量分解 Factor Out 把当前特征一分为二；末尺度则只经过 4 个 checkerboard 耦合层（同样每个后接批量归一化）就进行 Factor Out。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合层：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
&lt;br /&gt;
图中的 '''Coupling''' 块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}&lt;br /&gt;
这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（通常是 CNN）。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——计算复杂度从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
&lt;br /&gt;
[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=]]&lt;br /&gt;
&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
&lt;br /&gt;
* '''空间棋盘模式'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''通道级掩码'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
&lt;br /&gt;
多尺度架构依赖两个关键操作：压缩（Squeeze）与变量分解（Factor Out）。&lt;br /&gt;
&lt;br /&gt;
'''压缩'''（squeezing）：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
&lt;br /&gt;
'''变量分解'''（factoring out）：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
&lt;br /&gt;
批量归一化（BatchNorm）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BatchNorm 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
&lt;br /&gt;
== 标准化流与变量替换公式 ==&lt;br /&gt;
&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
&lt;br /&gt;
仿射耦合的逆变换为：对于公式 ({{EquationNote|1}}) 中的前向变换，经过代数变换即可得到公式 ({{EquationNote|2}}) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 损失函数与最大似然训练 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与信息论中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
&lt;br /&gt;
'''实际训练配置'''：Adam 优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
&lt;br /&gt;
== 优势 ==&lt;br /&gt;
&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度 CNN/ResNet，表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
&lt;br /&gt;
== 局限 ==&lt;br /&gt;
&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow 的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + ODE 描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图7：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 应用 =&lt;br /&gt;
&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在多个图像基准上验证了其密度估计与生成性能：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 数据集 !! 分辨率 !! 测试对数似然（bits/dim） !! 关键发现&lt;br /&gt;
|-&lt;br /&gt;
| CIFAR-10 || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 3.49 || 生成样本全局连贯，优于 VAE&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 4.28 || 有效建模大规模自然图像分布&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || 3.98 || 能捕捉前景/背景分割与光照一致性&lt;br /&gt;
|-&lt;br /&gt;
| LSUN（卧室/塔楼/教堂） || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 建模了室内外场景的语义结构&lt;br /&gt;
|-&lt;br /&gt;
| CelebA || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 潜在空间具有可解释的语义方向&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
与 VAE 相比，RealNVP 不依赖固定的重建损失（如 MSE），避免了过度平滑高频细节；与自回归模型（如 PixelRNN）相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图5：CelebA 生成样本与真实样本对比 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的潜在空间具有可解释的语义结构。给定四个样本的编码 &amp;lt;math&amp;gt;\mathbf{z}^{(1)}, \dots, \mathbf{z}^{(4)}&amp;lt;/math&amp;gt;，可在潜在空间中做球面插值：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合。在 CelebA 的类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，表明该空间具有类似词向量的线性语义结构。&lt;br /&gt;
&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 适合图像修复、去噪、着色等任务。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。&lt;br /&gt;
&lt;br /&gt;
== 在 NIS/NIS+ 因果涌现框架中的应用 ==&lt;br /&gt;
&lt;br /&gt;
在 [[NIS]]（神经信息挤压器）及 [[NIS+]] 中，RealNVP 被用作可逆编码器（INN），粗粒化策略被建模为两阶段过程：&lt;br /&gt;
&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图6：NIS 框架中 RealNVP 作为 INN 的位置示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
= 参考文献 =&lt;br /&gt;
&lt;br /&gt;
# Dinh L., Sohl-Dickstein J., Bengio S. Density Estimation Using Real NVP. International Conference on Learning Representations (ICLR), 2017. https://arxiv.org/abs/1605.08803&lt;br /&gt;
# Chen S. S., Gopinath R. A. Gaussianization. Advances in Neural Information Processing Systems (NIPS), 2000.&lt;br /&gt;
# Tabak E. G., Vanden-Eijnden E. Density Estimation by Dual Ascent of the Log-Likelihood. Communications in Mathematical Sciences, 2010, 8(1): 217-233.&lt;br /&gt;
# Dinh L., Krueger D., Bengio Y. NICE: Non-linear Independent Components Estimation. ICLR 2015 Workshop, 2014. https://arxiv.org/abs/1410.8516&lt;br /&gt;
# Draxler F., Wahl S., Schnörr C., Köthe U. On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows. Proceedings of the 41st International Conference on Machine Learning (ICML), PMLR 235, 2024.&lt;br /&gt;
# Ho J., Chen X., Srinivas A., Duan Y., Abbeel P. Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design. ICML, 2019.&lt;br /&gt;
# Kingma D. P., Dhariwal P. Glow: Generative Flow with Invertible 1×1 Convolutions. NeurIPS, 2018.&lt;br /&gt;
# Rezende D. J., Mohamed S. Variational Inference with Normalizing Flows. ICML, 2015.&lt;br /&gt;
# Papamakarios G., Pavlakou T., Murray I. Masked Autoregressive Flow for Density Estimation. NIPS, 2017.&lt;br /&gt;
# Grathwohl W., Chen R. T. Q., Bettencourt J., Sutskever I., Duvenaud D. FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models. ICLR, 2019.&lt;br /&gt;
# Chen R. T. Q., Behrmann J., Duvenaud D., Jacobsen J.-H. Residual Flows for Invertible Generative Modeling. NeurIPS, 2019.&lt;br /&gt;
# Papamakarios G., Nalisnick E., Rezende D. J., Mohamed S., Lakshminarayanan B. Normalizing Flows for Probabilistic Modeling and Inference. Journal of Machine Learning Research, 2021, 22(57): 1-64.&lt;br /&gt;
# Zhang J., Liu K. Neural Information Squeezer for Causal Emergence. Entropy, 2022, 25(1): 26.&lt;br /&gt;
# Yang M., Wang Z., Liu K., Rong Y., Yuan B., Zhang J. Finding Emergence in Data by Maximizing Effective Information. National Science Review, 2025, 12(1): nwae279.&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
&lt;br /&gt;
本词条由 [编写者姓名] 编写，[审阅者姓名] 审阅，[日期] 发布。&lt;br /&gt;
&lt;br /&gt;
'''贡献者'''：集智百科团队&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50677</id>
		<title>RealNVP</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=RealNVP&amp;diff=50677"/>
		<updated>2026-07-23T08:20:49Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：将Realnvp 的历史迁移过来&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值非体积保持变换）是一类基于标准化流（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的雅可比行列式都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
在因果涌现的研究中，RealNVP 常被用作可逆编码器（INN）的实现载体。以 NIS 和 NIS+ 为例，编码器被分解为两步：先用 RealNVP 的耦合层将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 无损地映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;（信息转换），再通过投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t&amp;lt;/math&amp;gt;（信息丢弃）。因此，RealNVP 的可逆性、参数共享和解析雅可比行列式，是 NIS 系列实现“可控维度、双向编码—解码”的技术基础；需要强调的是，宏观粗粒化本身是有损的，真正无损的仅是 INN 内部的信息转换阶段。更详细的 NIS/NIS+ 应用见后文。&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;归一化&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''加性耦合层'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：体积保持的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（coupling）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过 空间棋盘（checkerboard）与通道级（channel-wise）两种掩码实现，二者交替使用。&lt;br /&gt;
&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。BN 为批量归一化（Batch Normalization）操作的简写。|替代=|753x753像素]]&lt;br /&gt;
&lt;br /&gt;
如上图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个 checkerboard 掩码的耦合层Coupling（每个耦合层输出后接批量归一化BN）、压缩块Squeeze 、再 3 个 channel-wise 掩码的耦合层（同样每个后接批量归一化），最后通过变量分解 Factor Out 把当前特征一分为二；末尺度则只经过 4 个 checkerboard 耦合层（同样每个后接批量归一化）就进行 Factor Out。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合层：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
&lt;br /&gt;
图中的 '''Coupling''' 块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}&lt;br /&gt;
这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（通常是 CNN）。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——计算复杂度从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
&lt;br /&gt;
[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=]]&lt;br /&gt;
&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
&lt;br /&gt;
* '''空间棋盘模式'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''通道级掩码'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
&lt;br /&gt;
多尺度架构依赖两个关键操作：压缩（Squeeze）与变量分解（Factor Out）。&lt;br /&gt;
&lt;br /&gt;
'''压缩'''（squeezing）：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
&lt;br /&gt;
'''变量分解'''（factoring out）：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
&lt;br /&gt;
批量归一化（BatchNorm）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BatchNorm 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
&lt;br /&gt;
== 标准化流与变量替换公式 ==&lt;br /&gt;
&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
&lt;br /&gt;
仿射耦合的逆变换为：对于公式 ({{EquationNote|1}}) 中的前向变换，经过代数变换即可得到公式 ({{EquationNote|2}}) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 损失函数与最大似然训练 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与信息论中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
&lt;br /&gt;
'''实际训练配置'''：Adam 优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
&lt;br /&gt;
== 优势 ==&lt;br /&gt;
&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度 CNN/ResNet，表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
&lt;br /&gt;
== 局限 ==&lt;br /&gt;
&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow 的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + ODE 描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图7：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 应用 =&lt;br /&gt;
&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在多个图像基准上验证了其密度估计与生成性能：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 数据集 !! 分辨率 !! 测试对数似然（bits/dim） !! 关键发现&lt;br /&gt;
|-&lt;br /&gt;
| CIFAR-10 || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 3.49 || 生成样本全局连贯，优于 VAE&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 4.28 || 有效建模大规模自然图像分布&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || 3.98 || 能捕捉前景/背景分割与光照一致性&lt;br /&gt;
|-&lt;br /&gt;
| LSUN（卧室/塔楼/教堂） || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 建模了室内外场景的语义结构&lt;br /&gt;
|-&lt;br /&gt;
| CelebA || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 潜在空间具有可解释的语义方向&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
与 VAE 相比，RealNVP 不依赖固定的重建损失（如 MSE），避免了过度平滑高频细节；与自回归模型（如 PixelRNN）相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图5：CelebA 生成样本与真实样本对比 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的潜在空间具有可解释的语义结构。给定四个样本的编码 &amp;lt;math&amp;gt;\mathbf{z}^{(1)}, \dots, \mathbf{z}^{(4)}&amp;lt;/math&amp;gt;，可在潜在空间中做球面插值：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合。在 CelebA 的类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，表明该空间具有类似词向量的线性语义结构。&lt;br /&gt;
&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 适合图像修复、去噪、着色等任务。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。&lt;br /&gt;
&lt;br /&gt;
== 在 NIS/NIS+ 因果涌现框架中的应用 ==&lt;br /&gt;
&lt;br /&gt;
在 [[NIS]]（神经信息挤压器）及 [[NIS+]] 中，RealNVP 被用作可逆编码器（INN），粗粒化策略被建模为两阶段过程：&lt;br /&gt;
&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图6：NIS 框架中 RealNVP 作为 INN 的位置示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
= 参考文献 =&lt;br /&gt;
&lt;br /&gt;
# Dinh L., Sohl-Dickstein J., Bengio S. Density Estimation Using Real NVP. International Conference on Learning Representations (ICLR), 2017. https://arxiv.org/abs/1605.08803&lt;br /&gt;
# Chen S. S., Gopinath R. A. Gaussianization. Advances in Neural Information Processing Systems (NIPS), 2000.&lt;br /&gt;
# Tabak E. G., Vanden-Eijnden E. Density Estimation by Dual Ascent of the Log-Likelihood. Communications in Mathematical Sciences, 2010, 8(1): 217-233.&lt;br /&gt;
# Dinh L., Krueger D., Bengio Y. NICE: Non-linear Independent Components Estimation. ICLR 2015 Workshop, 2014. https://arxiv.org/abs/1410.8516&lt;br /&gt;
# Draxler F., Wahl S., Schnörr C., Köthe U. On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows. Proceedings of the 41st International Conference on Machine Learning (ICML), PMLR 235, 2024.&lt;br /&gt;
# Ho J., Chen X., Srinivas A., Duan Y., Abbeel P. Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design. ICML, 2019.&lt;br /&gt;
# Kingma D. P., Dhariwal P. Glow: Generative Flow with Invertible 1×1 Convolutions. NeurIPS, 2018.&lt;br /&gt;
# Rezende D. J., Mohamed S. Variational Inference with Normalizing Flows. ICML, 2015.&lt;br /&gt;
# Papamakarios G., Pavlakou T., Murray I. Masked Autoregressive Flow for Density Estimation. NIPS, 2017.&lt;br /&gt;
# Grathwohl W., Chen R. T. Q., Bettencourt J., Sutskever I., Duvenaud D. FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models. ICLR, 2019.&lt;br /&gt;
# Chen R. T. Q., Behrmann J., Duvenaud D., Jacobsen J.-H. Residual Flows for Invertible Generative Modeling. NeurIPS, 2019.&lt;br /&gt;
# Papamakarios G., Nalisnick E., Rezende D. J., Mohamed S., Lakshminarayanan B. Normalizing Flows for Probabilistic Modeling and Inference. Journal of Machine Learning Research, 2021, 22(57): 1-64.&lt;br /&gt;
# Zhang J., Liu K. Neural Information Squeezer for Causal Emergence. Entropy, 2022, 25(1): 26.&lt;br /&gt;
# Yang M., Wang Z., Liu K., Rong Y., Yuan B., Zhang J. Finding Emergence in Data by Maximizing Effective Information. National Science Review, 2025, 12(1): nwae279.&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
&lt;br /&gt;
本词条由 [编写者姓名] 编写，[审阅者姓名] 审阅，[日期] 发布。&lt;br /&gt;
&lt;br /&gt;
'''贡献者'''：集智百科团队&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50676</id>
		<title>Realnvp</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50676"/>
		<updated>2026-07-22T11:16:59Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：多尺度架构图图注修改&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值非体积保持变换）是一类基于标准化流（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的雅可比行列式都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
在因果涌现的研究中，RealNVP 常被用作可逆编码器（INN）的实现载体。以 NIS 和 NIS+ 为例，编码器被分解为两步：先用 RealNVP 的耦合层将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 无损地映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;（信息转换），再通过投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t&amp;lt;/math&amp;gt;（信息丢弃）。因此，RealNVP 的可逆性、参数共享和解析雅可比行列式，是 NIS 系列实现“可控维度、双向编码—解码”的技术基础；需要强调的是，宏观粗粒化本身是有损的，真正无损的仅是 INN 内部的信息转换阶段。更详细的 NIS/NIS+ 应用见后文。&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;归一化&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''加性耦合层'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：体积保持的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（coupling）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过 空间棋盘（checkerboard）与通道级（channel-wise）两种掩码实现，二者交替使用。&lt;br /&gt;
&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。BN 为批量归一化（Batch Normalization）操作的简写。|替代=|753x753像素]]&lt;br /&gt;
&lt;br /&gt;
如上图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入第一层 Scale 1。每个非末尺度内部依次经过 3 个 checkerboard 掩码的耦合层Coupling（每个耦合层输出后接批量归一化BN）、压缩块Squeeze 、再 3 个 channel-wise 掩码的耦合层（同样每个后接批量归一化），最后通过变量分解 Factor Out 把当前特征一分为二；末尺度则只经过 4 个 checkerboard 耦合层（同样每个后接批量归一化）就进行 Factor Out。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合层：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
&lt;br /&gt;
图中的 '''Coupling''' 块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}&lt;br /&gt;
这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（通常是 CNN）。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——计算复杂度从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
&lt;br /&gt;
[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=]]&lt;br /&gt;
&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
&lt;br /&gt;
* '''空间棋盘模式'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''通道级掩码'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
&lt;br /&gt;
多尺度架构依赖两个关键操作：压缩（Squeeze）与变量分解（Factor Out）。&lt;br /&gt;
&lt;br /&gt;
'''压缩'''（squeezing）：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
&lt;br /&gt;
'''变量分解'''（factoring out）：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
&lt;br /&gt;
批量归一化（BatchNorm）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BatchNorm 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
&lt;br /&gt;
== 标准化流与变量替换公式 ==&lt;br /&gt;
&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
&lt;br /&gt;
仿射耦合的逆变换为：对于公式 ({{EquationNote|1}}) 中的前向变换，经过代数变换即可得到公式 ({{EquationNote|2}}) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 损失函数与最大似然训练 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与信息论中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
&lt;br /&gt;
'''实际训练配置'''：Adam 优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
&lt;br /&gt;
== 优势 ==&lt;br /&gt;
&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度 CNN/ResNet，表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
&lt;br /&gt;
== 局限 ==&lt;br /&gt;
&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow 的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + ODE 描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图7：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 应用 =&lt;br /&gt;
&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在多个图像基准上验证了其密度估计与生成性能：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 数据集 !! 分辨率 !! 测试对数似然（bits/dim） !! 关键发现&lt;br /&gt;
|-&lt;br /&gt;
| CIFAR-10 || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 3.49 || 生成样本全局连贯，优于 VAE&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 4.28 || 有效建模大规模自然图像分布&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || 3.98 || 能捕捉前景/背景分割与光照一致性&lt;br /&gt;
|-&lt;br /&gt;
| LSUN（卧室/塔楼/教堂） || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 建模了室内外场景的语义结构&lt;br /&gt;
|-&lt;br /&gt;
| CelebA || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 潜在空间具有可解释的语义方向&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
与 VAE 相比，RealNVP 不依赖固定的重建损失（如 MSE），避免了过度平滑高频细节；与自回归模型（如 PixelRNN）相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图5：CelebA 生成样本与真实样本对比 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的潜在空间具有可解释的语义结构。给定四个样本的编码 &amp;lt;math&amp;gt;\mathbf{z}^{(1)}, \dots, \mathbf{z}^{(4)}&amp;lt;/math&amp;gt;，可在潜在空间中做球面插值：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合。在 CelebA 的类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，表明该空间具有类似词向量的线性语义结构。&lt;br /&gt;
&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 适合图像修复、去噪、着色等任务。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。&lt;br /&gt;
&lt;br /&gt;
== 在 NIS/NIS+ 因果涌现框架中的应用 ==&lt;br /&gt;
&lt;br /&gt;
在 [[NIS]]（神经信息挤压器）及 [[NIS+]] 中，RealNVP 被用作可逆编码器（INN），粗粒化策略被建模为两阶段过程：&lt;br /&gt;
&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图6：NIS 框架中 RealNVP 作为 INN 的位置示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
= 参考文献 =&lt;br /&gt;
&lt;br /&gt;
# Dinh L., Sohl-Dickstein J., Bengio S. Density Estimation Using Real NVP. International Conference on Learning Representations (ICLR), 2017. https://arxiv.org/abs/1605.08803&lt;br /&gt;
# Chen S. S., Gopinath R. A. Gaussianization. Advances in Neural Information Processing Systems (NIPS), 2000.&lt;br /&gt;
# Tabak E. G., Vanden-Eijnden E. Density Estimation by Dual Ascent of the Log-Likelihood. Communications in Mathematical Sciences, 2010, 8(1): 217-233.&lt;br /&gt;
# Dinh L., Krueger D., Bengio Y. NICE: Non-linear Independent Components Estimation. ICLR 2015 Workshop, 2014. https://arxiv.org/abs/1410.8516&lt;br /&gt;
# Draxler F., Wahl S., Schnörr C., Köthe U. On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows. Proceedings of the 41st International Conference on Machine Learning (ICML), PMLR 235, 2024.&lt;br /&gt;
# Ho J., Chen X., Srinivas A., Duan Y., Abbeel P. Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design. ICML, 2019.&lt;br /&gt;
# Kingma D. P., Dhariwal P. Glow: Generative Flow with Invertible 1×1 Convolutions. NeurIPS, 2018.&lt;br /&gt;
# Rezende D. J., Mohamed S. Variational Inference with Normalizing Flows. ICML, 2015.&lt;br /&gt;
# Papamakarios G., Pavlakou T., Murray I. Masked Autoregressive Flow for Density Estimation. NIPS, 2017.&lt;br /&gt;
# Grathwohl W., Chen R. T. Q., Bettencourt J., Sutskever I., Duvenaud D. FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models. ICLR, 2019.&lt;br /&gt;
# Chen R. T. Q., Behrmann J., Duvenaud D., Jacobsen J.-H. Residual Flows for Invertible Generative Modeling. NeurIPS, 2019.&lt;br /&gt;
# Papamakarios G., Nalisnick E., Rezende D. J., Mohamed S., Lakshminarayanan B. Normalizing Flows for Probabilistic Modeling and Inference. Journal of Machine Learning Research, 2021, 22(57): 1-64.&lt;br /&gt;
# Zhang J., Liu K. Neural Information Squeezer for Causal Emergence. Entropy, 2022, 25(1): 26.&lt;br /&gt;
# Yang M., Wang Z., Liu K., Rong Y., Yuan B., Zhang J. Finding Emergence in Data by Maximizing Effective Information. National Science Review, 2025, 12(1): nwae279.&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
&lt;br /&gt;
本词条由 [编写者姓名] 编写，[审阅者姓名] 审阅，[日期] 发布。&lt;br /&gt;
&lt;br /&gt;
'''贡献者'''：集智百科团队&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50675</id>
		<title>Realnvp</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50675"/>
		<updated>2026-07-22T11:12:54Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：调整图片位置&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值非体积保持变换）是一类基于标准化流（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的雅可比行列式都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
在因果涌现的研究中，RealNVP 常被用作可逆编码器（INN）的实现载体。以 NIS 和 NIS+ 为例，编码器被分解为两步：先用 RealNVP 的耦合层将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 无损地映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;（信息转换），再通过投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t&amp;lt;/math&amp;gt;（信息丢弃）。因此，RealNVP 的可逆性、参数共享和解析雅可比行列式，是 NIS 系列实现“可控维度、双向编码—解码”的技术基础；需要强调的是，宏观粗粒化本身是有损的，真正无损的仅是 INN 内部的信息转换阶段。更详细的 NIS/NIS+ 应用见后文。&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;归一化&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''加性耦合层'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：体积保持的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（coupling）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过 空间棋盘（checkerboard）与通道级（channel-wise）两种掩码实现，二者交替使用。&lt;br /&gt;
&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|RealNVP 多尺度分层架构示意图。|替代=|753x753像素]]&lt;br /&gt;
&lt;br /&gt;
如上图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入 Scale 1。每个非末尺度内部依次经过 3 个 checkerboard 掩码的耦合层（每个耦合层输出后接批量归一化）、Squeeze 压缩块、再 3 个 channel-wise 掩码的耦合层（同样每个后接批量归一化），最后通过 Factor Out 把当前特征一分为二；末尺度则只经过 4 个 checkerboard 耦合层（同样每个后接批量归一化）就进行 Factor Out。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合层：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
&lt;br /&gt;
图中的 '''Coupling''' 块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}&lt;br /&gt;
这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（通常是 CNN）。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——计算复杂度从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
&lt;br /&gt;
[[文件:仿射耦合.png|缩略图|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。|替代=]]&lt;br /&gt;
&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
&lt;br /&gt;
* '''空间棋盘模式'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''通道级掩码'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
&lt;br /&gt;
多尺度架构依赖两个关键操作：压缩（Squeeze）与变量分解（Factor Out）。&lt;br /&gt;
&lt;br /&gt;
'''压缩'''（squeezing）：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
&lt;br /&gt;
'''变量分解'''（factoring out）：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
&lt;br /&gt;
批量归一化（BatchNorm）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BatchNorm 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
&lt;br /&gt;
== 标准化流与变量替换公式 ==&lt;br /&gt;
&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
&lt;br /&gt;
仿射耦合的逆变换为：对于公式 ({{EquationNote|1}}) 中的前向变换，经过代数变换即可得到公式 ({{EquationNote|2}}) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 损失函数与最大似然训练 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与信息论中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
&lt;br /&gt;
'''实际训练配置'''：Adam 优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
&lt;br /&gt;
== 优势 ==&lt;br /&gt;
&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度 CNN/ResNet，表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
&lt;br /&gt;
== 局限 ==&lt;br /&gt;
&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow 的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + ODE 描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图7：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 应用 =&lt;br /&gt;
&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在多个图像基准上验证了其密度估计与生成性能：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 数据集 !! 分辨率 !! 测试对数似然（bits/dim） !! 关键发现&lt;br /&gt;
|-&lt;br /&gt;
| CIFAR-10 || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 3.49 || 生成样本全局连贯，优于 VAE&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 4.28 || 有效建模大规模自然图像分布&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || 3.98 || 能捕捉前景/背景分割与光照一致性&lt;br /&gt;
|-&lt;br /&gt;
| LSUN（卧室/塔楼/教堂） || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 建模了室内外场景的语义结构&lt;br /&gt;
|-&lt;br /&gt;
| CelebA || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 潜在空间具有可解释的语义方向&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
与 VAE 相比，RealNVP 不依赖固定的重建损失（如 MSE），避免了过度平滑高频细节；与自回归模型（如 PixelRNN）相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图5：CelebA 生成样本与真实样本对比 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的潜在空间具有可解释的语义结构。给定四个样本的编码 &amp;lt;math&amp;gt;\mathbf{z}^{(1)}, \dots, \mathbf{z}^{(4)}&amp;lt;/math&amp;gt;，可在潜在空间中做球面插值：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合。在 CelebA 的类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，表明该空间具有类似词向量的线性语义结构。&lt;br /&gt;
&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 适合图像修复、去噪、着色等任务。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。&lt;br /&gt;
&lt;br /&gt;
== 在 NIS/NIS+ 因果涌现框架中的应用 ==&lt;br /&gt;
&lt;br /&gt;
在 [[NIS]]（神经信息挤压器）及 [[NIS+]] 中，RealNVP 被用作可逆编码器（INN），粗粒化策略被建模为两阶段过程：&lt;br /&gt;
&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图6：NIS 框架中 RealNVP 作为 INN 的位置示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
= 参考文献 =&lt;br /&gt;
&lt;br /&gt;
# Dinh L., Sohl-Dickstein J., Bengio S. Density Estimation Using Real NVP. International Conference on Learning Representations (ICLR), 2017. https://arxiv.org/abs/1605.08803&lt;br /&gt;
# Chen S. S., Gopinath R. A. Gaussianization. Advances in Neural Information Processing Systems (NIPS), 2000.&lt;br /&gt;
# Tabak E. G., Vanden-Eijnden E. Density Estimation by Dual Ascent of the Log-Likelihood. Communications in Mathematical Sciences, 2010, 8(1): 217-233.&lt;br /&gt;
# Dinh L., Krueger D., Bengio Y. NICE: Non-linear Independent Components Estimation. ICLR 2015 Workshop, 2014. https://arxiv.org/abs/1410.8516&lt;br /&gt;
# Draxler F., Wahl S., Schnörr C., Köthe U. On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows. Proceedings of the 41st International Conference on Machine Learning (ICML), PMLR 235, 2024.&lt;br /&gt;
# Ho J., Chen X., Srinivas A., Duan Y., Abbeel P. Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design. ICML, 2019.&lt;br /&gt;
# Kingma D. P., Dhariwal P. Glow: Generative Flow with Invertible 1×1 Convolutions. NeurIPS, 2018.&lt;br /&gt;
# Rezende D. J., Mohamed S. Variational Inference with Normalizing Flows. ICML, 2015.&lt;br /&gt;
# Papamakarios G., Pavlakou T., Murray I. Masked Autoregressive Flow for Density Estimation. NIPS, 2017.&lt;br /&gt;
# Grathwohl W., Chen R. T. Q., Bettencourt J., Sutskever I., Duvenaud D. FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models. ICLR, 2019.&lt;br /&gt;
# Chen R. T. Q., Behrmann J., Duvenaud D., Jacobsen J.-H. Residual Flows for Invertible Generative Modeling. NeurIPS, 2019.&lt;br /&gt;
# Papamakarios G., Nalisnick E., Rezende D. J., Mohamed S., Lakshminarayanan B. Normalizing Flows for Probabilistic Modeling and Inference. Journal of Machine Learning Research, 2021, 22(57): 1-64.&lt;br /&gt;
# Zhang J., Liu K. Neural Information Squeezer for Causal Emergence. Entropy, 2022, 25(1): 26.&lt;br /&gt;
# Yang M., Wang Z., Liu K., Rong Y., Yuan B., Zhang J. Finding Emergence in Data by Maximizing Effective Information. National Science Review, 2025, 12(1): nwae279.&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
&lt;br /&gt;
本词条由 [编写者姓名] 编写，[审阅者姓名] 审阅，[日期] 发布。&lt;br /&gt;
&lt;br /&gt;
'''贡献者'''：集智百科团队&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:%E4%BB%BF%E5%B0%84%E8%80%A6%E5%90%88.png&amp;diff=50674</id>
		<title>文件:仿射耦合.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:%E4%BB%BF%E5%B0%84%E8%80%A6%E5%90%88.png&amp;diff=50674"/>
		<updated>2026-07-22T11:03:01Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50673</id>
		<title>Realnvp</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50673"/>
		<updated>2026-07-22T11:01:12Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：1. 调整目录结构 2. NIS相关表述修正 3. 重写了算法框架章节、重绘了多尺度架构图 4. 插入 `仿射耦合.png`；移除与 `Realnvp flow anim.gif` 内容重复的 `Coupling anim.gif` 5. 降低 gif 分辨率 6. 调整语言&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值非体积保持变换）是一类基于标准化流（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
= 原理概述 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 是一类标准化流：它将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，从而将数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。每一层变换的雅可比行列式都能高效解析计算，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
在因果涌现的研究中，RealNVP 常被用作可逆编码器（INN）的实现载体。以 NIS 和 NIS+ 为例，编码器被分解为两步：先用 RealNVP 的耦合层将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 无损地映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;（信息转换），再通过投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t&amp;lt;/math&amp;gt;（信息丢弃）。因此，RealNVP 的可逆性、参数共享和解析雅可比行列式，是 NIS 系列实现“可控维度、双向编码—解码”的技术基础；需要强调的是，宏观粗粒化本身是有损的，真正无损的仅是 INN 内部的信息转换阶段。更详细的 NIS/NIS+ 应用见后文。&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;归一化&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的关键创新是'''加性耦合层'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：体积保持的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 把若干可逆模块按顺序堆叠，形成从数据空间到潜在空间的完整映射。其网络主体由四个功能模块组成：'''仿射耦合层'''（coupling）负责可逆的特征变换，每个耦合层输出后接'''批量归一化'''（batch normalization）以稳定训练；'''压缩'''（squeeze）负责降低空间分辨率并把信息重排到通道；'''变量分解'''（factor out）负责在每个尺度末尾抽取一部分变量作为潜在编码。其中，仿射耦合层通过 空间棋盘（checkerboard）与通道级（channel-wise）两种掩码实现，二者交替使用。&lt;br /&gt;
&lt;br /&gt;
[[文件:Realnvp multiscale.svg|缩略图|center|RealNVP 多尺度分层架构示意图。]]&lt;br /&gt;
&lt;br /&gt;
如上图所示，输入图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 首先进入 Scale 1。每个非末尺度内部依次经过 3 个 checkerboard 掩码的耦合层（每个耦合层输出后接批量归一化）、Squeeze 压缩块、再 3 个 channel-wise 掩码的耦合层（同样每个后接批量归一化），最后通过 Factor Out 把当前特征一分为二；末尺度则只经过 4 个 checkerboard 耦合层（同样每个后接批量归一化）就进行 Factor Out。一半结果作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续传入下一尺度。不同尺度之间通过折线连接，最终把每个 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平为向量后按顺序拼接成完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。虽然各 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 的展平长度不同（依次为 &amp;lt;math&amp;gt;HWC/2, HWC/4, \dots, HWC/2^{L-1}&amp;lt;/math&amp;gt;），但它们的总和恰好等于输入维度 &amp;lt;math&amp;gt;HWC&amp;lt;/math&amp;gt;，因此整体变换保持维度不变。这种分层设计的意图是：早期尺度保留高分辨率的局部细节，后期尺度处理低分辨率的全局结构，并通过逐步分解减少后续层的计算量。&lt;br /&gt;
&lt;br /&gt;
正向流程可概括为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于每个压缩和分解步骤都要对空间或通道维度进行对半划分，各尺度的空间尺寸与通道数都必须是偶数；实际应用中通常把图像尺寸调整为偶数，或通过填充、裁剪等预处理满足该条件。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合层：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
&lt;br /&gt;
图中的 '''Coupling''' 块即仿射耦合层，它是 RealNVP 的基本可逆变换单元。&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}&lt;br /&gt;
这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（通常是 CNN）。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——计算复杂度从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
&lt;br /&gt;
[[文件:仿射耦合.png|缩略图|center|仿射耦合层示意图：正向（从左到右）将输入分块并变换，反向（从右到左）逐块还原。]]&lt;br /&gt;
&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层要先把输入分成两部分。针对图像数据，RealNVP 使用两种掩码来决定划分方式：&lt;br /&gt;
&lt;br /&gt;
* '''空间棋盘模式'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''通道级掩码'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
&lt;br /&gt;
多尺度架构依赖两个关键操作：压缩（Squeeze）与变量分解（Factor Out）。&lt;br /&gt;
&lt;br /&gt;
'''压缩'''（squeezing）：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
&lt;br /&gt;
'''变量分解'''（factoring out）：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 展平后拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，其总维度与输入维度相等。&lt;br /&gt;
&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''降低计算负担'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
&lt;br /&gt;
批量归一化（BatchNorm）应用于每个耦合层的输出之后，用于稳定深层训练。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BatchNorm 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
&lt;br /&gt;
== 标准化流与变量替换公式 ==&lt;br /&gt;
&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，每层内部运算可并行，因此整体采样速度高于自回归模型。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
&lt;br /&gt;
仿射耦合的逆变换为：对于公式 ({{EquationNote|1}}) 中的前向变换，经过代数变换即可得到公式 ({{EquationNote|2}}) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 损失函数与最大似然训练 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在标准正态分布中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与信息论中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
&lt;br /&gt;
'''实际训练配置'''：Adam 优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
&lt;br /&gt;
== 优势 ==&lt;br /&gt;
&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度 CNN/ResNet，表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
&lt;br /&gt;
== 局限 ==&lt;br /&gt;
&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow 的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + ODE 描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图7：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 应用 =&lt;br /&gt;
&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在多个图像基准上验证了其密度估计与生成性能：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 数据集 !! 分辨率 !! 测试对数似然（bits/dim） !! 关键发现&lt;br /&gt;
|-&lt;br /&gt;
| CIFAR-10 || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 3.49 || 生成样本全局连贯，优于 VAE&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 4.28 || 有效建模大规模自然图像分布&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || 3.98 || 能捕捉前景/背景分割与光照一致性&lt;br /&gt;
|-&lt;br /&gt;
| LSUN（卧室/塔楼/教堂） || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 建模了室内外场景的语义结构&lt;br /&gt;
|-&lt;br /&gt;
| CelebA || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 潜在空间具有可解释的语义方向&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
与 VAE 相比，RealNVP 不依赖固定的重建损失（如 MSE），避免了过度平滑高频细节；与自回归模型（如 PixelRNN）相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图5：CelebA 生成样本与真实样本对比 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的潜在空间具有可解释的语义结构。给定四个样本的编码 &amp;lt;math&amp;gt;\mathbf{z}^{(1)}, \dots, \mathbf{z}^{(4)}&amp;lt;/math&amp;gt;，可在潜在空间中做球面插值：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合。在 CelebA 的类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，表明该空间具有类似词向量的线性语义结构。&lt;br /&gt;
&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 适合图像修复、去噪、着色等任务。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。&lt;br /&gt;
&lt;br /&gt;
== 在 NIS/NIS+ 因果涌现框架中的应用 ==&lt;br /&gt;
&lt;br /&gt;
在 [[NIS]]（神经信息挤压器）及 [[NIS+]] 中，RealNVP 被用作可逆编码器（INN），粗粒化策略被建模为两阶段过程：&lt;br /&gt;
&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图6：NIS 框架中 RealNVP 作为 INN 的位置示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
= 参考文献 =&lt;br /&gt;
&lt;br /&gt;
# Dinh L., Sohl-Dickstein J., Bengio S. Density Estimation Using Real NVP. International Conference on Learning Representations (ICLR), 2017. https://arxiv.org/abs/1605.08803&lt;br /&gt;
# Chen S. S., Gopinath R. A. Gaussianization. Advances in Neural Information Processing Systems (NIPS), 2000.&lt;br /&gt;
# Tabak E. G., Vanden-Eijnden E. Density Estimation by Dual Ascent of the Log-Likelihood. Communications in Mathematical Sciences, 2010, 8(1): 217-233.&lt;br /&gt;
# Dinh L., Krueger D., Bengio Y. NICE: Non-linear Independent Components Estimation. ICLR 2015 Workshop, 2014. https://arxiv.org/abs/1410.8516&lt;br /&gt;
# Draxler F., Wahl S., Schnörr C., Köthe U. On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows. Proceedings of the 41st International Conference on Machine Learning (ICML), PMLR 235, 2024.&lt;br /&gt;
# Ho J., Chen X., Srinivas A., Duan Y., Abbeel P. Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design. ICML, 2019.&lt;br /&gt;
# Kingma D. P., Dhariwal P. Glow: Generative Flow with Invertible 1×1 Convolutions. NeurIPS, 2018.&lt;br /&gt;
# Rezende D. J., Mohamed S. Variational Inference with Normalizing Flows. ICML, 2015.&lt;br /&gt;
# Papamakarios G., Pavlakou T., Murray I. Masked Autoregressive Flow for Density Estimation. NIPS, 2017.&lt;br /&gt;
# Grathwohl W., Chen R. T. Q., Bettencourt J., Sutskever I., Duvenaud D. FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models. ICLR, 2019.&lt;br /&gt;
# Chen R. T. Q., Behrmann J., Duvenaud D., Jacobsen J.-H. Residual Flows for Invertible Generative Modeling. NeurIPS, 2019.&lt;br /&gt;
# Papamakarios G., Nalisnick E., Rezende D. J., Mohamed S., Lakshminarayanan B. Normalizing Flows for Probabilistic Modeling and Inference. Journal of Machine Learning Research, 2021, 22(57): 1-64.&lt;br /&gt;
# Zhang J., Liu K. Neural Information Squeezer for Causal Emergence. Entropy, 2022, 25(1): 26.&lt;br /&gt;
# Yang M., Wang Z., Liu K., Rong Y., Yuan B., Zhang J. Finding Emergence in Data by Maximizing Effective Information. National Science Review, 2025, 12(1): nwae279.&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
&lt;br /&gt;
本词条由 [编写者姓名] 编写，[审阅者姓名] 审阅，[日期] 发布。&lt;br /&gt;
&lt;br /&gt;
'''贡献者'''：集智百科团队&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:Realnvp_flow_anim.gif&amp;diff=50672</id>
		<title>文件:Realnvp flow anim.gif</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:Realnvp_flow_anim.gif&amp;diff=50672"/>
		<updated>2026-07-22T10:42:27Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：黄一凡上传文件:Realnvp flow anim.gif的新版本&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== 文件说明 ==&lt;br /&gt;
RealNVP 流过程演示动画&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:Realnvp_multiscale.svg&amp;diff=50671</id>
		<title>文件:Realnvp multiscale.svg</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:Realnvp_multiscale.svg&amp;diff=50671"/>
		<updated>2026-07-22T10:38:29Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：黄一凡上传文件:Realnvp multiscale.svg的新版本&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== 文件说明 ==&lt;br /&gt;
RealNVP 的多尺度架构&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50668</id>
		<title>Realnvp</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50668"/>
		<updated>2026-07-20T06:42:24Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：重编排标题&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值非体积保持变换）是一类基于标准化流（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
更形式化地说，RealNVP 将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，将服从复杂分布的 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。而 RealNVP 的关键设计在于：'''每一层变换的雅可比行列式都能高效解析计算'''，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
在因果涌现的研究框架中，RealNVP 起到重要作用。在 [[NIS]]（神经信息挤压器）和 [[NIS+]] 中，它被选作可逆编码器（INN）的实现载体，负责将微观状态无损地映射到潜在空间，再从中粗粒化出宏观状态。理解 RealNVP 的机制，是理解 NIS 系列工作如何实现信息不丢、参数复用、维度可控的技术前提。&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;归一化&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的核心 trick 是'''加性耦合层'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：体积保持的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 算法框架 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 的完整架构由四种模块递归堆叠而成：'''仿射耦合层'''、'''掩码卷积分区'''、'''多尺度压缩与分解'''，以及'''耦合层批量归一化'''。正向传播（编码）流程为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[文件:Realnvp multiscale.svg]]&lt;br /&gt;
&lt;br /&gt;
如上图所示，RealNVP 的多尺度编码从原始图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 开始，逐尺度交替使用 checkerboard 与 channel-wise 掩码的仿射耦合层，并通过 Squeeze 减半空间分辨率；每个尺度末端的 Factor Out 将一半变量作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 拼接为完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合层：可逆的&amp;quot;条件变形&amp;quot; ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层是 RealNVP 的基本单元。给定 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维输入 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt;，输入被分为两部分：前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维&amp;quot;被动&amp;quot;（不变），后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维&amp;quot;主动&amp;quot;（被变形）：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}&lt;br /&gt;
[[文件:Coupling anim.gif|缩略图|332x332像素|仿射耦合层的前向与逆向过程示意]]&lt;br /&gt;
这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（通常是 CNN）。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——计算复杂度从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 掩码卷积的两种分区模式 ==&lt;br /&gt;
&lt;br /&gt;
上述将输入分成两部分的操作可以按不同方式实现。针对图像数据，RealNVP 设计了两种掩码：&lt;br /&gt;
&lt;br /&gt;
* '''空间棋盘模式'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''通道级掩码'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 多尺度架构：分层打包潜在变量 ==&lt;br /&gt;
&lt;br /&gt;
直接对高维图像（如 &amp;lt;math&amp;gt;64\times64\times3&amp;lt;/math&amp;gt;）进行全维度耦合计算代价较高。RealNVP 采用 VGG 网络&amp;quot;降采样同时加深通道&amp;quot;的策略，引入两个关键操作：&lt;br /&gt;
&lt;br /&gt;
'''压缩'''（squeezing）：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
&lt;br /&gt;
'''变量分解'''（factoring out）：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''计算减负'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 批量归一化：训练深层的稳定器 ==&lt;br /&gt;
&lt;br /&gt;
在耦合层之间插入批量归一化（BatchNorm），对每个维度做线性缩放：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BatchNorm 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
&lt;br /&gt;
= 数学原理 =&lt;br /&gt;
&lt;br /&gt;
== 标准化流与变量替换公式 ==&lt;br /&gt;
&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，所有步骤均可并行。&lt;br /&gt;
&lt;br /&gt;
== 仿射耦合的雅可比行列式与可逆性 ==&lt;br /&gt;
&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
&lt;br /&gt;
仿射耦合的逆变换为：对于公式 ({{EquationNote|1}}) 中的前向变换，经过代数变换即可得到公式 ({{EquationNote|2}}) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
== 加性耦合、仿射耦合与体积保持 ==&lt;br /&gt;
&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 损失函数与最大似然训练 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在 Gaussian 中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与信息论中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
&lt;br /&gt;
'''实际训练配置'''：Adam 优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
&lt;br /&gt;
= 优缺点 =&lt;br /&gt;
&lt;br /&gt;
== 优势 ==&lt;br /&gt;
&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度 CNN/ResNet，表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
&lt;br /&gt;
== 局限 ==&lt;br /&gt;
&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow 的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 与相关框架的关系 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + ODE 描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图7：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 应用/实例 =&lt;br /&gt;
&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在多个图像基准上验证了其密度估计与生成性能：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 数据集 !! 分辨率 !! 测试对数似然（bits/dim） !! 关键发现&lt;br /&gt;
|-&lt;br /&gt;
| CIFAR-10 || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 3.49 || 生成样本全局连贯，优于 VAE&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 4.28 || 有效建模大规模自然图像分布&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || 3.98 || 能捕捉前景/背景分割与光照一致性&lt;br /&gt;
|-&lt;br /&gt;
| LSUN（卧室/塔楼/教堂） || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 建模了室内外场景的语义结构&lt;br /&gt;
|-&lt;br /&gt;
| CelebA || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 潜在空间具有可解释的语义方向&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
与 VAE 相比，RealNVP 不依赖固定的重建损失（如 MSE），避免了过度平滑高频细节；与自回归模型（如 PixelRNN）相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图5：CelebA 生成样本与真实样本对比 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的潜在空间具有可解释的语义结构。给定四个样本的编码 &amp;lt;math&amp;gt;\mathbf{z}^{(1)}, \dots, \mathbf{z}^{(4)}&amp;lt;/math&amp;gt;，可在潜在空间中做球面插值：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合。在 CelebA 的类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，表明该空间具有类似词向量的线性语义结构。&lt;br /&gt;
&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 适合图像修复、去噪、着色等任务。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。&lt;br /&gt;
&lt;br /&gt;
== 在 NIS/NIS+ 因果涌现框架中的应用 ==&lt;br /&gt;
&lt;br /&gt;
在 [[NIS]]（神经信息挤压器）及 [[NIS+]] 中，RealNVP 被用作可逆编码器（INN），粗粒化策略被建模为两阶段过程：&lt;br /&gt;
&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图6：NIS 框架中 RealNVP 作为 INN 的位置示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
= 参考文献 =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
&lt;br /&gt;
本词条由 [编写者姓名] 编写，[审阅者姓名] 审阅，[日期] 发布。&lt;br /&gt;
&lt;br /&gt;
'''贡献者'''：集智百科团队&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50667</id>
		<title>Realnvp</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50667"/>
		<updated>2026-07-19T15:35:19Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= 简介 =&lt;br /&gt;
&lt;br /&gt;
'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值非体积保持变换）是一类基于标准化流（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
更形式化地说，RealNVP 将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，将服从复杂分布的 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。而 RealNVP 的关键设计在于：'''每一层变换的雅可比行列式都能高效解析计算'''，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
在因果涌现的研究框架中，RealNVP 起到重要作用。在 [[NIS]]（神经信息挤压器）和 [[NIS+]] 中，它被选作可逆编码器（INN）的实现载体，负责将微观状态无损地映射到潜在空间，再从中粗粒化出宏观状态。理解 RealNVP 的机制，是理解 NIS 系列工作如何实现信息不丢、参数复用、维度可控的技术前提。&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
&lt;br /&gt;
第一条主线是可逆变换的&amp;quot;分层堆叠&amp;quot;思想。从 1990 年代的独立成分分析（ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;归一化&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
第二条主线是耦合层的发明与升级。2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的核心 trick 是'''加性耦合层'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：体积保持的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 主要内容 =&lt;br /&gt;
&lt;br /&gt;
== 算法框架 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的完整架构由四种模块递归堆叠而成：'''仿射耦合层'''、'''掩码卷积分区'''、'''多尺度压缩与分解'''，以及'''耦合层批量归一化'''。正向传播（编码）流程为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[文件:Realnvp multiscale.svg]]&lt;br /&gt;
&lt;br /&gt;
如上图所示，RealNVP 的多尺度编码从原始图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 开始，逐尺度交替使用 checkerboard 与 channel-wise 掩码的仿射耦合层，并通过 Squeeze 减半空间分辨率；每个尺度末端的 Factor Out 将一半变量作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 拼接为完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
=== 仿射耦合层：可逆的&amp;quot;条件变形&amp;quot; ===&lt;br /&gt;
&lt;br /&gt;
仿射耦合层是 RealNVP 的基本单元。给定 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维输入 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt;，输入被分为两部分：前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维&amp;quot;被动&amp;quot;（不变），后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维&amp;quot;主动&amp;quot;（被变形）：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}&lt;br /&gt;
[[文件:Coupling anim.gif|缩略图|332x332像素|仿射耦合层的前向与逆向过程示意]]&lt;br /&gt;
这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（通常是 CNN）。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——计算复杂度从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== 掩码卷积的两种分区模式 ===&lt;br /&gt;
&lt;br /&gt;
上述将输入分成两部分的操作可以按不同方式实现。针对图像数据，RealNVP 设计了两种掩码：&lt;br /&gt;
&lt;br /&gt;
* '''空间棋盘模式'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''通道级掩码'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 多尺度架构：分层打包潜在变量 ===&lt;br /&gt;
&lt;br /&gt;
直接对高维图像（如 &amp;lt;math&amp;gt;64\times64\times3&amp;lt;/math&amp;gt;）进行全维度耦合计算代价较高。RealNVP 采用 VGG 网络&amp;quot;降采样同时加深通道&amp;quot;的策略，引入两个关键操作：&lt;br /&gt;
&lt;br /&gt;
'''压缩'''（squeezing）：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
&lt;br /&gt;
'''变量分解'''（factoring out）：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''计算减负'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 批量归一化：训练深层的稳定器 ===&lt;br /&gt;
&lt;br /&gt;
在耦合层之间插入批量归一化（BatchNorm），对每个维度做线性缩放：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BatchNorm 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
&lt;br /&gt;
== 数学原理 ==&lt;br /&gt;
&lt;br /&gt;
=== 标准化流与变量替换公式 ===&lt;br /&gt;
&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，所有步骤均可并行。&lt;br /&gt;
&lt;br /&gt;
=== 仿射耦合的雅可比行列式与可逆性 ===&lt;br /&gt;
&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
&lt;br /&gt;
仿射耦合的逆变换为：对于公式 ({{EquationNote|1}}) 中的前向变换，经过代数变换即可得到公式 ({{EquationNote|2}}) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
=== 加性耦合、仿射耦合与体积保持 ===&lt;br /&gt;
&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
=== 损失函数与最大似然训练 ===&lt;br /&gt;
&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在 Gaussian 中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与信息论中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
&lt;br /&gt;
'''实际训练配置'''：Adam 优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
&lt;br /&gt;
== 优缺点 ==&lt;br /&gt;
&lt;br /&gt;
=== 优势 ===&lt;br /&gt;
&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度 CNN/ResNet，表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
&lt;br /&gt;
=== 局限 ===&lt;br /&gt;
&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow 的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
=== 与相关框架的关系 ===&lt;br /&gt;
&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + ODE 描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图7：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 应用/实例 =&lt;br /&gt;
&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在多个图像基准上验证了其密度估计与生成性能：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 数据集 !! 分辨率 !! 测试对数似然（bits/dim） !! 关键发现&lt;br /&gt;
|-&lt;br /&gt;
| CIFAR-10 || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 3.49 || 生成样本全局连贯，优于 VAE&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 4.28 || 有效建模大规模自然图像分布&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || 3.98 || 能捕捉前景/背景分割与光照一致性&lt;br /&gt;
|-&lt;br /&gt;
| LSUN（卧室/塔楼/教堂） || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 建模了室内外场景的语义结构&lt;br /&gt;
|-&lt;br /&gt;
| CelebA || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 潜在空间具有可解释的语义方向&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
与 VAE 相比，RealNVP 不依赖固定的重建损失（如 MSE），避免了过度平滑高频细节；与自回归模型（如 PixelRNN）相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图5：CelebA 生成样本与真实样本对比 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的潜在空间具有可解释的语义结构。给定四个样本的编码 &amp;lt;math&amp;gt;\mathbf{z}^{(1)}, \dots, \mathbf{z}^{(4)}&amp;lt;/math&amp;gt;，可在潜在空间中做球面插值：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合。在 CelebA 的类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，表明该空间具有类似词向量的线性语义结构。&lt;br /&gt;
&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 适合图像修复、去噪、着色等任务。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。&lt;br /&gt;
&lt;br /&gt;
== 在 NIS/NIS+ 因果涌现框架中的应用 ==&lt;br /&gt;
&lt;br /&gt;
在 [[NIS]]（神经信息挤压器）及 [[NIS+]] 中，RealNVP 被用作可逆编码器（INN），粗粒化策略被建模为两阶段过程：&lt;br /&gt;
&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图6：NIS 框架中 RealNVP 作为 INN 的位置示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
= 参考文献 =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
&lt;br /&gt;
本词条由 [编写者姓名] 编写，[审阅者姓名] 审阅，[日期] 发布。&lt;br /&gt;
&lt;br /&gt;
'''贡献者'''：集智百科团队&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50666</id>
		<title>Realnvp</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50666"/>
		<updated>2026-07-19T15:30:56Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：更新图片注解&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= 简介 =&lt;br /&gt;
&lt;br /&gt;
'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值非体积保持变换）是一类基于标准化流（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
更形式化地说，RealNVP 将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，将服从复杂分布的 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。而 RealNVP 的关键设计在于：'''每一层变换的雅可比行列式都能高效解析计算'''，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
在因果涌现的研究框架中，RealNVP 起到重要作用。在 [[NIS]]（神经信息挤压器）和 [[NIS+]] 中，它被选作可逆编码器（INN）的实现载体，负责将微观状态无损地映射到潜在空间，再从中粗粒化出宏观状态。理解 RealNVP 的机制，是理解 NIS 系列工作如何实现信息不丢、参数复用、维度可控的技术前提。&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
&lt;br /&gt;
== 第一条脉络：可逆变换的&amp;quot;分层堆叠&amp;quot;思想 ==&lt;br /&gt;
&lt;br /&gt;
从 1990 年代的独立成分分析（ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;归一化&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 第二条脉络：耦合层的发明与升级 ==&lt;br /&gt;
&lt;br /&gt;
2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的核心 trick 是'''加性耦合层'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：体积保持的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 主要内容 =&lt;br /&gt;
&lt;br /&gt;
== 算法框架 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的完整架构由四种模块递归堆叠而成：'''仿射耦合层'''、'''掩码卷积分区'''、'''多尺度压缩与分解'''，以及'''耦合层批量归一化'''。正向传播（编码）流程为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[文件:Realnvp multiscale.svg]]&lt;br /&gt;
&lt;br /&gt;
如上图所示，RealNVP 的多尺度编码从原始图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 开始，逐尺度交替使用 checkerboard 与 channel-wise 掩码的仿射耦合层，并通过 Squeeze 减半空间分辨率；每个尺度末端的 Factor Out 将一半变量作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 拼接为完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
=== 仿射耦合层：可逆的&amp;quot;条件变形&amp;quot; ===&lt;br /&gt;
&lt;br /&gt;
仿射耦合层是 RealNVP 的基本单元。给定 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维输入 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt;，输入被分为两部分：前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维&amp;quot;被动&amp;quot;（不变），后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维&amp;quot;主动&amp;quot;（被变形）：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}&lt;br /&gt;
[[文件:Coupling anim.gif|缩略图|332x332像素|仿射耦合层的前向与逆向过程示意]]&lt;br /&gt;
这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（通常是 CNN）。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——计算复杂度从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== 掩码卷积的两种分区模式 ===&lt;br /&gt;
&lt;br /&gt;
上述将输入分成两部分的操作可以按不同方式实现。针对图像数据，RealNVP 设计了两种掩码：&lt;br /&gt;
&lt;br /&gt;
* '''空间棋盘模式'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''通道级掩码'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 多尺度架构：分层打包潜在变量 ===&lt;br /&gt;
&lt;br /&gt;
直接对高维图像（如 &amp;lt;math&amp;gt;64\times64\times3&amp;lt;/math&amp;gt;）进行全维度耦合计算代价较高。RealNVP 采用 VGG 网络&amp;quot;降采样同时加深通道&amp;quot;的策略，引入两个关键操作：&lt;br /&gt;
&lt;br /&gt;
'''压缩'''（squeezing）：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
&lt;br /&gt;
'''变量分解'''（factoring out）：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''计算减负'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 批量归一化：训练深层的稳定器 ===&lt;br /&gt;
&lt;br /&gt;
在耦合层之间插入批量归一化（BatchNorm），对每个维度做线性缩放：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BatchNorm 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
&lt;br /&gt;
== 数学原理 ==&lt;br /&gt;
&lt;br /&gt;
=== 标准化流与变量替换公式 ===&lt;br /&gt;
&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，所有步骤均可并行。&lt;br /&gt;
&lt;br /&gt;
=== 仿射耦合的雅可比行列式与可逆性 ===&lt;br /&gt;
&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
&lt;br /&gt;
仿射耦合的逆变换为：对于公式 ({{EquationNote|1}}) 中的前向变换，经过代数变换即可得到公式 ({{EquationNote|2}}) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
=== 加性耦合、仿射耦合与体积保持 ===&lt;br /&gt;
&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
=== 损失函数与最大似然训练 ===&lt;br /&gt;
&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在 Gaussian 中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与信息论中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
&lt;br /&gt;
'''实际训练配置'''：Adam 优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
&lt;br /&gt;
== 优缺点 ==&lt;br /&gt;
&lt;br /&gt;
=== 优势 ===&lt;br /&gt;
&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度 CNN/ResNet，表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
&lt;br /&gt;
=== 局限 ===&lt;br /&gt;
&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow 的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
=== 与相关框架的关系 ===&lt;br /&gt;
&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + ODE 描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图7：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 应用/实例 =&lt;br /&gt;
&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在多个图像基准上验证了其密度估计与生成性能：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 数据集 !! 分辨率 !! 测试对数似然（bits/dim） !! 关键发现&lt;br /&gt;
|-&lt;br /&gt;
| CIFAR-10 || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 3.49 || 生成样本全局连贯，优于 VAE&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 4.28 || 有效建模大规模自然图像分布&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || 3.98 || 能捕捉前景/背景分割与光照一致性&lt;br /&gt;
|-&lt;br /&gt;
| LSUN（卧室/塔楼/教堂） || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 建模了室内外场景的语义结构&lt;br /&gt;
|-&lt;br /&gt;
| CelebA || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 潜在空间具有可解释的语义方向&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
与 VAE 相比，RealNVP 不依赖固定的重建损失（如 MSE），避免了过度平滑高频细节；与自回归模型（如 PixelRNN）相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图5：CelebA 生成样本与真实样本对比 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的潜在空间具有可解释的语义结构。给定四个样本的编码 &amp;lt;math&amp;gt;\mathbf{z}^{(1)}, \dots, \mathbf{z}^{(4)}&amp;lt;/math&amp;gt;，可在潜在空间中做球面插值：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合。在 CelebA 的类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，表明该空间具有类似词向量的线性语义结构。&lt;br /&gt;
&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 适合图像修复、去噪、着色等任务。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。&lt;br /&gt;
&lt;br /&gt;
== 在 NIS/NIS+ 因果涌现框架中的应用 ==&lt;br /&gt;
&lt;br /&gt;
在 [[NIS]]（神经信息挤压器）及 [[NIS+]] 中，RealNVP 被用作可逆编码器（INN），粗粒化策略被建模为两阶段过程：&lt;br /&gt;
&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图6：NIS 框架中 RealNVP 作为 INN 的位置示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
= 参考文献 =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
&lt;br /&gt;
本词条由 [编写者姓名] 编写，[审阅者姓名] 审阅，[日期] 发布。&lt;br /&gt;
&lt;br /&gt;
'''贡献者'''：集智百科团队&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50665</id>
		<title>Realnvp</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50665"/>
		<updated>2026-07-19T15:27:44Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：增加动图注解&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= 简介 =&lt;br /&gt;
&lt;br /&gt;
'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值非体积保持变换）是一类基于标准化流（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|上图示意 RealNVP 的正向与反向流程：正向通过复合可逆变换将数据分布映射为标准高斯分布；反向则从高斯分布采样并逐层逆变换还原为数据。]]&lt;br /&gt;
更形式化地说，RealNVP 将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，将服从复杂分布的 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。而 RealNVP 的关键设计在于：'''每一层变换的雅可比行列式都能高效解析计算'''，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
在因果涌现的研究框架中，RealNVP 起到重要作用。在 [[NIS]]（神经信息挤压器）和 [[NIS+]] 中，它被选作可逆编码器（INN）的实现载体，负责将微观状态无损地映射到潜在空间，再从中粗粒化出宏观状态。理解 RealNVP 的机制，是理解 NIS 系列工作如何实现信息不丢、参数复用、维度可控的技术前提。&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
&lt;br /&gt;
== 第一条脉络：可逆变换的&amp;quot;分层堆叠&amp;quot;思想 ==&lt;br /&gt;
&lt;br /&gt;
从 1990 年代的独立成分分析（ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;归一化&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 第二条脉络：耦合层的发明与升级 ==&lt;br /&gt;
&lt;br /&gt;
2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的核心 trick 是'''加性耦合层'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：体积保持的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 主要内容 =&lt;br /&gt;
&lt;br /&gt;
== 算法框架 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的完整架构由四种模块递归堆叠而成：'''仿射耦合层'''、'''掩码卷积分区'''、'''多尺度压缩与分解'''，以及'''耦合层批量归一化'''。正向传播（编码）流程为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[文件:Realnvp multiscale.svg]]&lt;br /&gt;
&lt;br /&gt;
如上图所示，RealNVP 的多尺度编码从原始图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 开始，逐尺度交替使用 checkerboard 与 channel-wise 掩码的仿射耦合层，并通过 Squeeze 减半空间分辨率；每个尺度末端的 Factor Out 将一半变量作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 拼接为完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
=== 仿射耦合层：可逆的&amp;quot;条件变形&amp;quot; ===&lt;br /&gt;
&lt;br /&gt;
仿射耦合层是 RealNVP 的基本单元。给定 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维输入 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt;，输入被分为两部分：前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维&amp;quot;被动&amp;quot;（不变），后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维&amp;quot;主动&amp;quot;（被变形）：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}&lt;br /&gt;
[[文件:Coupling anim.gif|缩略图|332x332像素|上图示意仿射耦合层的前向与逆向过程：输入被分为两部分，一部分保持不变并作为条件输入 &amp;lt;nowiki&amp;gt;&amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/nowiki&amp;gt;''{1:d}&amp;lt;nowiki&amp;gt;&amp;lt;/math&amp;gt;&amp;lt;/nowiki&amp;gt;，另一部分 &amp;lt;nowiki&amp;gt;&amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/nowiki&amp;gt;''{d+1:D}&amp;lt;nowiki&amp;gt;&amp;lt;/math&amp;gt;&amp;lt;/nowiki&amp;gt; 由神经网络生成的缩放 &amp;lt;nowiki&amp;gt;&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;&amp;lt;/nowiki&amp;gt; 与平移 &amp;lt;nowiki&amp;gt;&amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;&amp;lt;/nowiki&amp;gt; 参数进行变换；逆向时只需用同一组参数执行减法和除法即可恢复原始输入。]]&lt;br /&gt;
这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（通常是 CNN）。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——计算复杂度从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图2：仿射耦合层前向/逆向流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 掩码卷积的两种分区模式 ===&lt;br /&gt;
&lt;br /&gt;
上述将输入分成两部分的操作可以按不同方式实现。针对图像数据，RealNVP 设计了两种掩码：&lt;br /&gt;
&lt;br /&gt;
* '''空间棋盘模式'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''通道级掩码'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 多尺度架构：分层打包潜在变量 ===&lt;br /&gt;
&lt;br /&gt;
直接对高维图像（如 &amp;lt;math&amp;gt;64\times64\times3&amp;lt;/math&amp;gt;）进行全维度耦合计算代价较高。RealNVP 采用 VGG 网络&amp;quot;降采样同时加深通道&amp;quot;的策略，引入两个关键操作：&lt;br /&gt;
&lt;br /&gt;
'''压缩'''（squeezing）：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
&lt;br /&gt;
'''变量分解'''（factoring out）：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''计算减负'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 批量归一化：训练深层的稳定器 ===&lt;br /&gt;
&lt;br /&gt;
在耦合层之间插入批量归一化（BatchNorm），对每个维度做线性缩放：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BatchNorm 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
&lt;br /&gt;
== 数学原理 ==&lt;br /&gt;
&lt;br /&gt;
=== 标准化流与变量替换公式 ===&lt;br /&gt;
&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，所有步骤均可并行。&lt;br /&gt;
&lt;br /&gt;
=== 仿射耦合的雅可比行列式与可逆性 ===&lt;br /&gt;
&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
&lt;br /&gt;
仿射耦合的逆变换为：对于公式 ({{EquationNote|1}}) 中的前向变换，经过代数变换即可得到公式 ({{EquationNote|2}}) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
=== 加性耦合、仿射耦合与体积保持 ===&lt;br /&gt;
&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
=== 损失函数与最大似然训练 ===&lt;br /&gt;
&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在 Gaussian 中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与信息论中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
&lt;br /&gt;
'''实际训练配置'''：Adam 优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
&lt;br /&gt;
== 优缺点 ==&lt;br /&gt;
&lt;br /&gt;
=== 优势 ===&lt;br /&gt;
&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度 CNN/ResNet，表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
&lt;br /&gt;
=== 局限 ===&lt;br /&gt;
&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow 的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
=== 与相关框架的关系 ===&lt;br /&gt;
&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + ODE 描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图7：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 应用/实例 =&lt;br /&gt;
&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在多个图像基准上验证了其密度估计与生成性能：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 数据集 !! 分辨率 !! 测试对数似然（bits/dim） !! 关键发现&lt;br /&gt;
|-&lt;br /&gt;
| CIFAR-10 || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 3.49 || 生成样本全局连贯，优于 VAE&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 4.28 || 有效建模大规模自然图像分布&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || 3.98 || 能捕捉前景/背景分割与光照一致性&lt;br /&gt;
|-&lt;br /&gt;
| LSUN（卧室/塔楼/教堂） || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 建模了室内外场景的语义结构&lt;br /&gt;
|-&lt;br /&gt;
| CelebA || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 潜在空间具有可解释的语义方向&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
与 VAE 相比，RealNVP 不依赖固定的重建损失（如 MSE），避免了过度平滑高频细节；与自回归模型（如 PixelRNN）相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图5：CelebA 生成样本与真实样本对比 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的潜在空间具有可解释的语义结构。给定四个样本的编码 &amp;lt;math&amp;gt;\mathbf{z}^{(1)}, \dots, \mathbf{z}^{(4)}&amp;lt;/math&amp;gt;，可在潜在空间中做球面插值：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合。在 CelebA 的类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，表明该空间具有类似词向量的线性语义结构。&lt;br /&gt;
&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 适合图像修复、去噪、着色等任务。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。&lt;br /&gt;
&lt;br /&gt;
== 在 NIS/NIS+ 因果涌现框架中的应用 ==&lt;br /&gt;
&lt;br /&gt;
在 [[NIS]]（神经信息挤压器）及 [[NIS+]] 中，RealNVP 被用作可逆编码器（INN），粗粒化策略被建模为两阶段过程：&lt;br /&gt;
&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图6：NIS 框架中 RealNVP 作为 INN 的位置示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
= 参考文献 =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
&lt;br /&gt;
本词条由 [编写者姓名] 编写，[审阅者姓名] 审阅，[日期] 发布。&lt;br /&gt;
&lt;br /&gt;
'''贡献者'''：集智百科团队&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50664</id>
		<title>Realnvp</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50664"/>
		<updated>2026-07-19T15:21:13Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：宏观流程图加了一段介绍；优化文字&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= 简介 =&lt;br /&gt;
&lt;br /&gt;
'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值非体积保持变换）是一类基于标准化流（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以概括为：'''用一个完全可逆的神经网络，将复杂的数据分布逐步变换为简单的高斯分布。由于变换可逆，模型既可正向精确计算数据的概率密度，也可反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|RealNVP流过程演示]]&lt;br /&gt;
更形式化地说，RealNVP 将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，将服从复杂分布的 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。由于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。而 RealNVP 的关键设计在于：'''每一层变换的雅可比行列式都能高效解析计算'''，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
在因果涌现的研究框架中，RealNVP 起到重要作用。在 [[NIS]]（神经信息挤压器）和 [[NIS+]] 中，它被选作可逆编码器（INN）的实现载体，负责将微观状态无损地映射到潜在空间，再从中粗粒化出宏观状态。理解 RealNVP 的机制，是理解 NIS 系列工作如何实现信息不丢、参数复用、维度可控的技术前提。&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 建立在早期标准化流工作之上，其发展有两条主线。&lt;br /&gt;
&lt;br /&gt;
== 第一条脉络：可逆变换的&amp;quot;分层堆叠&amp;quot;思想 ==&lt;br /&gt;
&lt;br /&gt;
从 1990 年代的独立成分分析（ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换将复杂数据&amp;quot;归一化&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 第二条脉络：耦合层的发明与升级 ==&lt;br /&gt;
&lt;br /&gt;
2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次使深度可逆网络可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的核心 trick 是'''加性耦合层'''：将输入分成两半，其中一半直接保持不变，另一半则加上一个以第一半为条件的神经网络输出。由于该变换的雅可比行列式恒为 1，它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
&lt;br /&gt;
2017 年的 RealNVP 将加性耦合扩展为仿射耦合，即以前一半变量为条件，对后一半变量先进行缩放再进行平移。这一改动使变换不再保持体积（non-volume preserving），增强了模型的表达能力，同时保留了计算上的便利性。后续的理论工作（Draxler et al., 2024）证明：体积保持的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的优势。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 主要内容 =&lt;br /&gt;
&lt;br /&gt;
== 算法框架 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的完整架构由四种模块递归堆叠而成：'''仿射耦合层'''、'''掩码卷积分区'''、'''多尺度压缩与分解'''，以及'''耦合层批量归一化'''。正向传播（编码）流程为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[文件:Realnvp multiscale.svg]]&lt;br /&gt;
&lt;br /&gt;
如上图所示，RealNVP 的多尺度编码从原始图像 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 开始，逐尺度交替使用 checkerboard 与 channel-wise 掩码的仿射耦合层，并通过 Squeeze 减半空间分辨率；每个尺度末端的 Factor Out 将一半变量作为 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 抽出，另一半继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 拼接为完整潜变量 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
=== 仿射耦合层：可逆的&amp;quot;条件变形&amp;quot; ===&lt;br /&gt;
&lt;br /&gt;
仿射耦合层是 RealNVP 的基本单元。给定 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维输入 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt;，输入被分为两部分：前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维&amp;quot;被动&amp;quot;（不变），后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维&amp;quot;主动&amp;quot;（被变形）：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}&lt;br /&gt;
[[文件:Coupling anim.gif|缩略图|332x332像素|仿射耦合演示]]&lt;br /&gt;
这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个深度神经网络（通常是 CNN）。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。由于 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式即为对角线元素的乘积——计算复杂度从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。此外，逆变换为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图2：仿射耦合层前向/逆向流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 掩码卷积的两种分区模式 ===&lt;br /&gt;
&lt;br /&gt;
上述将输入分成两部分的操作可以按不同方式实现。针对图像数据，RealNVP 设计了两种掩码：&lt;br /&gt;
&lt;br /&gt;
* '''空间棋盘模式'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''通道级掩码'''（channel-wise masking）：将前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），每个维度在不同层中分别作为被动变量和主动变量，信息得以充分混合。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 多尺度架构：分层打包潜在变量 ===&lt;br /&gt;
&lt;br /&gt;
直接对高维图像（如 &amp;lt;math&amp;gt;64\times64\times3&amp;lt;/math&amp;gt;）进行全维度耦合计算代价较高。RealNVP 采用 VGG 网络&amp;quot;降采样同时加深通道&amp;quot;的策略，引入两个关键操作：&lt;br /&gt;
&lt;br /&gt;
'''压缩'''（squeezing）：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上重排，将 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 重排为 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
&lt;br /&gt;
'''变量分解'''（factoring out）：在每个尺度结束时，将当前表示的一半维度提取为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
该设计具有以下优势：&lt;br /&gt;
# '''层次化表示'''：早期提取的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期提取的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''计算减负'''：越早提取的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 批量归一化：训练深层的稳定器 ===&lt;br /&gt;
&lt;br /&gt;
在耦合层之间插入批量归一化（BatchNorm），对每个维度做线性缩放：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。此外，BatchNorm 起到训练深耦合层堆叠时的稳定作用——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，使梯度稳定传播。&lt;br /&gt;
&lt;br /&gt;
== 数学原理 ==&lt;br /&gt;
&lt;br /&gt;
=== 标准化流与变量替换公式 ===&lt;br /&gt;
&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
&lt;br /&gt;
上式中，第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量编码后分布与标准高斯的接近程度；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部使空间扩张（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的求和：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
采样时通过逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，所有步骤均可并行。&lt;br /&gt;
&lt;br /&gt;
=== 仿射耦合的雅可比行列式与可逆性 ===&lt;br /&gt;
&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，因而雅可比矩阵具有分块三角结构：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
&lt;br /&gt;
上述性质是 RealNVP 可处理性的关键：计算行列式不需要对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。因此 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质仍然保持。&lt;br /&gt;
&lt;br /&gt;
仿射耦合的逆变换为：对于公式 ({{EquationNote|1}}) 中的前向变换，经过代数变换即可得到公式 ({{EquationNote|2}}) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需按逆变换公式进行减法和除法即可。因此'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
=== 加性耦合、仿射耦合与体积保持 ===&lt;br /&gt;
&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合对应于平移变换——只改变位置，不拉伸空间；RealNVP 的仿射耦合则允许缩放与平移的组合，更适合匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
=== 损失函数与最大似然训练 ===&lt;br /&gt;
&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
损失函数包含两项：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：促使编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在 Gaussian 中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：促使模型在数据密集的区域扩张体积、在稀疏区域收缩体积——这与信息论中将码空间分配给高概率事件的原则一致。&lt;br /&gt;
&lt;br /&gt;
从对偶视角看（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化推前分布与先验之间的反向 KL。&lt;br /&gt;
&lt;br /&gt;
'''实际训练配置'''：Adam 优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
&lt;br /&gt;
== 优缺点 ==&lt;br /&gt;
&lt;br /&gt;
=== 优势 ===&lt;br /&gt;
&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度高于自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度 CNN/ResNet，表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''综合多种模型特性'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的生成能力。&lt;br /&gt;
&lt;br /&gt;
=== 局限 ===&lt;br /&gt;
&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等先进自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow 的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
=== 与相关框架的关系 ===&lt;br /&gt;
&lt;br /&gt;
RealNVP 在标准化流的发展中处于承上启下的位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 刷新了图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + ODE 描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 在密度估计、去量化和耦合函数上的短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，说明 NICE 存在理论局限。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图7：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 应用/实例 =&lt;br /&gt;
&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在多个图像基准上验证了其密度估计与生成性能：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 数据集 !! 分辨率 !! 测试对数似然（bits/dim） !! 关键发现&lt;br /&gt;
|-&lt;br /&gt;
| CIFAR-10 || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 3.49 || 生成样本全局连贯，优于 VAE&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 4.28 || 有效建模大规模自然图像分布&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || 3.98 || 能捕捉前景/背景分割与光照一致性&lt;br /&gt;
|-&lt;br /&gt;
| LSUN（卧室/塔楼/教堂） || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 建模了室内外场景的语义结构&lt;br /&gt;
|-&lt;br /&gt;
| CelebA || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 潜在空间具有可解释的语义方向&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
与 VAE 相比，RealNVP 不依赖固定的重建损失（如 MSE），避免了过度平滑高频细节；与自回归模型（如 PixelRNN）相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图5：CelebA 生成样本与真实样本对比 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的潜在空间具有可解释的语义结构。给定四个样本的编码 &amp;lt;math&amp;gt;\mathbf{z}^{(1)}, \dots, \mathbf{z}^{(4)}&amp;lt;/math&amp;gt;，可在潜在空间中做球面插值：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合。在 CelebA 的类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，表明该空间具有类似词向量的线性语义结构。&lt;br /&gt;
&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 互为逆映射（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 适合图像修复、去噪、着色等任务。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于模型学习了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。&lt;br /&gt;
&lt;br /&gt;
== 在 NIS/NIS+ 因果涌现框架中的应用 ==&lt;br /&gt;
&lt;br /&gt;
在 [[NIS]]（神经信息挤压器）及 [[NIS+]] 中，RealNVP 被用作可逆编码器（INN），粗粒化策略被建模为两阶段过程：&lt;br /&gt;
&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''该阶段信息不丢失'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图6：NIS 框架中 RealNVP 作为 INN 的位置示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NIS 论文给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不损失信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，显著压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使有效信息（EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文指出：'''可逆神经网络作为编码器和解码器是必需的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
= 参考文献 =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
&lt;br /&gt;
本词条由 [编写者姓名] 编写，[审阅者姓名] 审阅，[日期] 发布。&lt;br /&gt;
&lt;br /&gt;
'''贡献者'''：集智百科团队&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50659</id>
		<title>Realnvp</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=Realnvp&amp;diff=50659"/>
		<updated>2026-07-13T11:04:56Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= 简介 =&lt;br /&gt;
&lt;br /&gt;
'''RealNVP'''（'''Real-valued Non-Volume Preserving'''，实值非体积保持变换）是一类基于标准化流（normalizing flows）的深度生成模型，由 Laurent Dinh、Jascha Sohl-Dickstein 与 Samy Bengio 于 2017 年提出&amp;lt;ref name=&amp;quot;Dinh2017&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Sohl-Dickstein J.|author3=Bengio S.|title=Density Estimation Using Real NVP|journal=International Conference on Learning Representations (ICLR)|year=2017|url=https://arxiv.org/abs/1605.08803}}&amp;lt;/ref&amp;gt;。它的核心思想可以直觉地概括为：'''用一个完全可逆的神经网络，把复杂的数据分布一步步&amp;quot;揉&amp;quot;回简单的高斯分布；由于变换可逆，我们既能正向精确计算数据的概率密度，也能反向直接从高斯分布采样生成新数据。'''&lt;br /&gt;
[[文件:Realnvp flow anim.gif|缩略图|332x332像素|RealNVP流过程演示]]&lt;br /&gt;
更形式化地说，RealNVP 将一系列可逆映射 &amp;lt;math&amp;gt;f_1, \dots, f_n&amp;lt;/math&amp;gt; 复合为整体变换 &amp;lt;math&amp;gt;f = f_n \circ \cdots \circ f_1&amp;lt;/math&amp;gt;，把服从复杂分布的 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 映射到标准正态分布 &amp;lt;math&amp;gt;\mathbf{z} = f(\mathbf{x}) \sim \mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;。得益于可逆性，生成样本时只需从正态分布采样 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;，再通过逆映射 &amp;lt;math&amp;gt;\mathbf{x} = f^{-1}(\mathbf{z})&amp;lt;/math&amp;gt; 还原为数据。而 RealNVP 的关键设计在于：'''每一层变换的雅可比行列式都能高效解析计算'''，因此整体对数似然 &amp;lt;math&amp;gt;\log p_X(\mathbf{x})&amp;lt;/math&amp;gt; 可直接求出，无需借助变分下界或对抗训练。&lt;br /&gt;
&lt;br /&gt;
在因果涌现的研究框架中，RealNVP 扮演着关键角色。在 [[NIS]]（神经信息挤压器）和 [[NIS+]] 中，它被选作可逆编码器（INN）的实现载体，负责将微观状态无损地映射到潜在空间，再从中粗粒化出宏观状态。理解 RealNVP 的机制，是理解 NIS 系列工作&amp;quot;为何能做到信息不丢、参数复用、维度可控&amp;quot;的技术前提。&lt;br /&gt;
&lt;br /&gt;
= 历史渊源 =&lt;br /&gt;
&lt;br /&gt;
RealNVP 的诞生并非凭空出现，它是对早期标准化流工作的一次关键升级。要理解它的价值，只需抓住两条脉络。&lt;br /&gt;
&lt;br /&gt;
== 第一条脉络：可逆变换的&amp;quot;分层堆叠&amp;quot;思想 ==&lt;br /&gt;
&lt;br /&gt;
从 1990 年代的独立成分分析（ICA），到 2000 年 Chen 与 Gopinath 的'''高斯化'''（Gaussianization）&amp;lt;ref name=&amp;quot;Chen2000&amp;quot;&amp;gt;{{cite conference|author1=Chen S. S.|author2=Gopinath R. A.|title=Gaussianization|conference=Advances in Neural Information Processing Systems (NIPS)|year=2000}}&amp;lt;/ref&amp;gt;，研究者一直尝试用可逆变换把复杂数据&amp;quot;归一化&amp;quot;到简单分布。2010 年，Tabak 与 Vanden-Eijnden 正式提出了&amp;quot;标准化流&amp;quot;（normalizing flow）的数学框架——通过一系列可逆映射，让基分布像水流一样&amp;quot;流动&amp;quot;成目标分布&amp;lt;ref name=&amp;quot;Tabak2010&amp;quot;&amp;gt;{{cite journal|author1=Tabak E. G.|author2=Vanden-Eijnden E.|title=Density Estimation by Dual Ascent of the Log-Likelihood|journal=Communications in Mathematical Sciences|year=2010|volume=8|issue=1|pages=217-233}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 第二条脉络：耦合层的发明与升级 ==&lt;br /&gt;
&lt;br /&gt;
2014 年，Dinh 等人提出了 '''NICE'''（Non-linear Independent Components Estimation），首次让深度可逆网络真正可训练&amp;lt;ref name=&amp;quot;NICE2014&amp;quot;&amp;gt;{{cite journal|author1=Dinh L.|author2=Krueger D.|author3=Bengio Y.|title=NICE: Non-linear Independent Components Estimation|journal=ICLR 2015 Workshop|year=2014|url=https://arxiv.org/abs/1410.8516}}&amp;lt;/ref&amp;gt;。NICE 的核心 trick 是'''加性耦合层'''：把输入分成两半，一半原封不动，另一半加上一个以第一半为条件的神经网络输出。这就像两个人搬家，A 负责看地图（不变），B 负责搬箱子（被 A 指挥）。这个变换的雅可比行列式恒为 1，意味着它'''保持体积'''（volume preserving），但表达能力因此受限。&lt;br /&gt;
&lt;br /&gt;
2017 年的 RealNVP 只做了一处改动，却带来了质变：把&amp;quot;加&amp;quot;改为&amp;quot;先缩放再平移&amp;quot;——即'''仿射耦合'''。这一改动让变换不再保持体积（non-volume preserving），模型因此获得了更强的表达能力，同时保留了计算上的全部优点。后续的理论工作（Draxler et al., 2024）甚至证明：体积保持的流'''不是'''分布的普适逼近器，而仿射耦合流在良好条件下是&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot;&amp;gt;{{cite journal|author1=Draxler F.|author2=Wahl S.|author3=Schnörr C.|author4=Köthe U.|title=On the Universality of Volume-Preserving and Coupling-Based Normalizing Flows|journal=Proceedings of the 41st International Conference on Machine Learning (ICML)|year=2024|publisher=PMLR 235}}&amp;lt;/ref&amp;gt;。这从理论上解释了 RealNVP 相对于 NICE 的深层必要性。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图1：NICE 的加性耦合 vs RealNVP 的仿射耦合（2D 示意图） --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 主要内容 =&lt;br /&gt;
&lt;br /&gt;
== 算法框架 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的完整架构像一条流水线，由四种模块递归堆叠而成：'''仿射耦合层'''、'''掩码卷积分区'''、'''多尺度压缩与分解'''，以及'''耦合层批量归一化'''。整体正向传播（编码）流程可直观理解为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} = \mathbf{h}^{(0)} \;\xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{压缩}}\; \xrightarrow{\text{耦合层} \times n}\; \xrightarrow{\text{分解出一半}}\; \mathbf{z}^{(1)} \;\longrightarrow\; \cdots \;\longrightarrow\; \mathbf{z}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[文件:Realnvp multiscale.svg]]&lt;br /&gt;
&lt;br /&gt;
=== 仿射耦合层：可逆的&amp;quot;条件变形&amp;quot; ===&lt;br /&gt;
&lt;br /&gt;
仿射耦合层是 RealNVP 的基本单元。给定 &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; 维输入 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt;，我们把它切成两部分：前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维&amp;quot;被动&amp;quot;（不变），后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维&amp;quot;主动&amp;quot;（被变形）：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{y}_{1:d} = \mathbf{x}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{y}_{d+1:D} = \mathbf{x}_{d+1:D} \odot \exp\!\bigl(\mathbf{s}(\mathbf{x}_{1:d})\bigr) + \mathbf{t}(\mathbf{x}_{1:d})&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|1}}}}&lt;br /&gt;
[[文件:Coupling anim.gif|缩略图|332x332像素|仿射耦合演示]]&lt;br /&gt;
这里 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt;（scale，缩放）和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt;（translation，平移）是两个普通的深度神经网络（通常是 CNN）。&amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积。关键直觉在于：'''因为 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 的输入只来自不变的那一半，输出只作用在可变的那一半，整个变换的雅可比矩阵是下三角块结构，行列式就是对角线元素的乘积'''——计算复杂度从 &amp;lt;math&amp;gt;O(D^3)&amp;lt;/math&amp;gt; 骤降到 &amp;lt;math&amp;gt;O(D)&amp;lt;/math&amp;gt;。更妙的是，逆变换也极其简单：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{cases}&lt;br /&gt;
\mathbf{x}_{1:d} = \mathbf{y}_{1:d} \\[6pt]&lt;br /&gt;
\mathbf{x}_{d+1:D} = \bigl(\mathbf{y}_{d+1:D} - \mathbf{t}(\mathbf{y}_{1:d})\bigr) \odot \exp\!\bigl(-\mathbf{s}(\mathbf{y}_{1:d})\bigr)&lt;br /&gt;
\end{cases}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|2}}}}&lt;br /&gt;
&lt;br /&gt;
无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需把它们算出来的数&amp;quot;减回去、除回去&amp;quot;即可。这保证了'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图2：仿射耦合层前向/逆向流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 掩码卷积的两种分区模式 ===&lt;br /&gt;
&lt;br /&gt;
上述&amp;quot;切两半&amp;quot;可以按不同方式实现。针对图像数据，RealNVP 设计了两种掩码：&lt;br /&gt;
&lt;br /&gt;
* '''空间棋盘模式'''（checkerboard masking）：像国际象棋棋盘一样，按坐标 &amp;lt;math&amp;gt;(i+j)&amp;lt;/math&amp;gt; 的奇偶性决定像素是否属于&amp;quot;被动&amp;quot;集合。相邻像素被分到不同集合，能充分利用局部相关性。&lt;br /&gt;
* '''通道级掩码'''（channel-wise masking）：直接把前一半通道设为被动，后一半为主动。这通常在&amp;quot;压缩&amp;quot;操作后使用，因为此时空间分辨率已降低，通道间的语义分工更明确。&lt;br /&gt;
&lt;br /&gt;
通过交替堆叠不同掩码的耦合层（至少三层），确保每个维度都有机会&amp;quot;被动&amp;quot;和&amp;quot;主动&amp;quot;，信息得以充分混合。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图3：Checkerboard masking 与 Channel-wise masking 的对比示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 多尺度架构：分层打包潜在变量 ===&lt;br /&gt;
&lt;br /&gt;
直接对高维图像（如 &amp;lt;math&amp;gt;64\times64\times3&amp;lt;/math&amp;gt;）做全维度耦合，计算量巨大。RealNVP 借鉴了 VGG 网络&amp;quot;降采样同时加深通道&amp;quot;的思想，引入两个关键操作：&lt;br /&gt;
&lt;br /&gt;
'''压缩'''（squeezing）：将空间上每个 &amp;lt;math&amp;gt;2\times2&amp;lt;/math&amp;gt; 的小块在通道方向上&amp;quot;拉直&amp;quot;，把 &amp;lt;math&amp;gt;s\times s\times c&amp;lt;/math&amp;gt; 变成 &amp;lt;math&amp;gt;s/2 \times s/2 \times 4c&amp;lt;/math&amp;gt;。空间分辨率减半，通道数四倍——信息被重新排列，但没有丢失。&lt;br /&gt;
&lt;br /&gt;
'''变量分解'''（factoring out）：在每个尺度结束时，把当前表示的一半维度直接&amp;quot;拆出来&amp;quot;作为潜在变量 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt;，剩下的一半 &amp;lt;math&amp;gt;\mathbf{h}^{(i)}&amp;lt;/math&amp;gt; 继续进入下一尺度。最终所有 &amp;lt;math&amp;gt;\mathbf{z}^{(i)}&amp;lt;/math&amp;gt; 拼接成完整的潜在编码 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
这种设计有三重好处：&lt;br /&gt;
# '''层次化表示'''：早期拆出的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 对应局部纹理（如边缘、颜色），后期拆出的对应全局语义（如物体姿态、场景结构）。&lt;br /&gt;
# '''梯度分散'''：损失信号能直接传递到所有尺度，避免梯度在深层消失。&lt;br /&gt;
# '''计算减负'''：越早拆出的维度越多，后续层需要处理的特征图越小。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图4：多尺度架构流程图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 批量归一化：稳定深层的&amp;quot;刹车片&amp;quot; ===&lt;br /&gt;
&lt;br /&gt;
在耦合层之间插入批量归一化（BatchNorm），对每个维度做线性缩放：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x} \mapsto \frac{\mathbf{x} - \tilde{\boldsymbol{\mu}}}{\sqrt{\tilde{\boldsymbol{\sigma}}^2 + \epsilon}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
它的雅可比行列式是各维度缩放因子的乘积，可直接加入对数似然。更重要的是，BatchNorm 充当了训练深耦合层堆叠时的&amp;quot;稳定器&amp;quot;——它抑制了由缩放参数 &amp;lt;math&amp;gt;\exp(\mathbf{s})&amp;lt;/math&amp;gt; 引起的数值爆炸，让梯度健康传播。&lt;br /&gt;
&lt;br /&gt;
== 数学原理 ==&lt;br /&gt;
&lt;br /&gt;
=== 标准化流与变量替换公式 ===&lt;br /&gt;
&lt;br /&gt;
标准化流的核心是概率论中的'''变量替换公式'''（change of variables）。设 &amp;lt;math&amp;gt;\mathbf{z} \sim p_Z(\mathbf{z})&amp;lt;/math&amp;gt; 是已知先验（通常取标准正态 &amp;lt;math&amp;gt;\mathcal{N}(0, \mathbf{I})&amp;lt;/math&amp;gt;），&amp;lt;math&amp;gt;f = g^{-1}&amp;lt;/math&amp;gt; 是从数据 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 到 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 的可逆映射（编码器）。则 &amp;lt;math&amp;gt;\mathbf{x}&amp;lt;/math&amp;gt; 的密度为：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
p_X(\mathbf{x}) = p_Z\!\bigl(f(\mathbf{x})\bigr)\;\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|3}}}}&lt;br /&gt;
&lt;br /&gt;
取对数得到训练用的目标：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|4}}}}&lt;br /&gt;
&lt;br /&gt;
'''直觉解读'''：第一项 &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt; 衡量&amp;quot;把数据压进 Gaussian 后有多像 Gaussian&amp;quot;；第二项（对数雅可比行列式）则是'''体积补偿项'''——如果 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 在某个局部把空间&amp;quot;撑大&amp;quot;了（&amp;lt;math&amp;gt;\det &amp;gt; 1&amp;lt;/math&amp;gt;），密度必须相应降低，反之亦然。当流由 &amp;lt;math&amp;gt;K&amp;lt;/math&amp;gt; 个变换复合而成 &amp;lt;math&amp;gt;f = f_K \circ \cdots \circ f_1&amp;lt;/math&amp;gt; 时，总对数密度是各层对数行列式的简单求和：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\log p_X(\mathbf{x}) = \log p_Z\!\bigl(f(\mathbf{x})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
采样时只需逆向进行：&amp;lt;math&amp;gt;\mathbf{z} \sim p_Z&amp;lt;/math&amp;gt;，&amp;lt;math&amp;gt;\mathbf{x} = g(\mathbf{z}) = f_1^{-1} \circ \cdots \circ f_K^{-1}(\mathbf{z})&amp;lt;/math&amp;gt;，所有步骤均可并行。&lt;br /&gt;
&lt;br /&gt;
=== 仿射耦合的雅可比行列式与可逆性 ===&lt;br /&gt;
&lt;br /&gt;
仿射耦合层对前 &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt; 维进行恒等映射，对后 &amp;lt;math&amp;gt;D-d&amp;lt;/math&amp;gt; 维进行仿射映射，使得雅可比矩阵具有分块三角结构：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top} =&lt;br /&gt;
\begin{bmatrix}&lt;br /&gt;
\mathbf{I}_d &amp;amp; \mathbf{0} \\[8pt]&lt;br /&gt;
\dfrac{\partial \mathbf{y}_{d+1:D}}{\partial \mathbf{x}_{1:d}^\top} &amp;amp; \operatorname{diag}\!\bigl(\exp[\mathbf{s}(\mathbf{x}_{1:d})]\bigr)&lt;br /&gt;
\end{bmatrix}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于左上角是单位阵，整个矩阵的行列式等于右下角对角阵的行列式：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\det\!\left(\frac{\partial \mathbf{y}}{\partial \mathbf{x}^\top}\right) = \exp\!\left(\sum_{j=1}^{D-d} s_j(\mathbf{x}_{1:d})\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|5}}}}&lt;br /&gt;
&lt;br /&gt;
'''这是 RealNVP 的数学灵魂'''：计算行列式'''完全不需要'''对神经网络 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求导。这意味着 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 可以是任意复杂的深度残差网络，而不会破坏密度评估的可处理性。多个耦合层复合时，行列式相乘、逆变换按相反顺序复合，性质完美保持。&lt;br /&gt;
&lt;br /&gt;
仿射耦合的逆变换同样简单：对于公式 ({{EquationNote|1}}) 中的前向变换，经过简单代数变换即可得到公式 ({{EquationNote|2}}) 中的逆变换。无需对 &amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 求逆，只需把它们算出来的数&amp;quot;减回去、除回去&amp;quot;即可。这保证了'''编码（求密度）和解码（采样）同样高效'''。&lt;br /&gt;
&lt;br /&gt;
=== 加性耦合、仿射耦合与体积保持 ===&lt;br /&gt;
&lt;br /&gt;
下表对比了三种耦合方式：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 耦合类型 !! 变换 !! 雅可比行列式 !! 体积行为&lt;br /&gt;
|-&lt;br /&gt;
| '''加性'''（NICE） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; || 体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''乘法''' || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \mathbf{s}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod s_j&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|-&lt;br /&gt;
| '''仿射'''（RealNVP） || &amp;lt;math&amp;gt;\mathbf{y}_2 = \mathbf{x}_2 \odot \exp(\mathbf{s}(\mathbf{x}_1)) + \mathbf{t}(\mathbf{x}_1)&amp;lt;/math&amp;gt; || &amp;lt;math&amp;gt;\prod \exp(s_j)&amp;lt;/math&amp;gt; || 非体积保持&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
其中 &amp;lt;math&amp;gt;\odot&amp;lt;/math&amp;gt; 为逐元素乘积，体现为空间的拉伸。NICE 的加性耦合像&amp;quot;平移&amp;quot;——只搬动位置，不拉伸空间；RealNVP 的仿射耦合则允许&amp;quot;缩放+平移&amp;quot;，能更好地匹配真实数据中不同维度、不同尺度的变化。后续理论表明，缺乏这种&amp;quot;非体积保持&amp;quot;能力的流模型，在表达能力上存在本质限制&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
=== 损失函数与最大似然训练 ===&lt;br /&gt;
&lt;br /&gt;
RealNVP 通过最大化数据对数似然来训练，等价于最小化数据分布与模型分布的前向 KL 散度：&lt;br /&gt;
&lt;br /&gt;
{{NumBlk|:|&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) = -\mathbb{E}_{p^*(\mathbf{x})}\left[\log p_Z\!\bigl(f(\mathbf{x})\bigr) + \log\left|\det\!\left(\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
|{{EquationRef|6}}}}&lt;br /&gt;
&lt;br /&gt;
对于训练集 &amp;lt;math&amp;gt;\{\mathbf{x}^{(i)}\}_{i=1}^N&amp;lt;/math&amp;gt;，使用蒙特卡洛近似：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathcal{L}(\theta) \approx -\frac{1}{N}\sum_{i=1}^N \left[\log p_Z\!\bigl(f(\mathbf{x}^{(i)})\bigr) + \sum_{k=1}^K \log\left|\det\!\left(\frac{\partial f_k}{\partial \mathbf{x}_k^\top}\right)\right|\right]&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''损失的两项各司其职'''：&lt;br /&gt;
* '''先验项''' &amp;lt;math&amp;gt;\log p_Z(f(\mathbf{x}))&amp;lt;/math&amp;gt;：鼓励编码后的 &amp;lt;math&amp;gt;\mathbf{z}&amp;lt;/math&amp;gt; 聚集在 Gaussian 中心附近，相当于对潜在空间的正则化。&lt;br /&gt;
* '''行列式项'''：鼓励模型在数据密集的区域&amp;quot;扩张&amp;quot;体积、在稀疏区域&amp;quot;收缩&amp;quot;体积——这与信息论中&amp;quot;把码空间分配给高概率事件&amp;quot;的原则一致。&lt;br /&gt;
&lt;br /&gt;
一个有趣的对偶视角（Papamakarios et al., 2021）&amp;lt;ref name=&amp;quot;Papamakarios2021&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Nalisnick E.|author3=Rezende D. J.|author4=Mohamed S.|author5=Lakshminarayanan B.|title=Normalizing Flows for Probabilistic Modeling and Inference|journal=Journal of Machine Learning Research|year=2021|volume=22|issue=57|pages=1-64}}&amp;lt;/ref&amp;gt;：在数据上最小化前向 KL，等价于在潜在空间中最小化&amp;quot;推前分布&amp;quot;与先验之间的反向 KL。&lt;br /&gt;
&lt;br /&gt;
'''实际训练配置'''：Adam 优化器，权重衰减 &amp;lt;math&amp;gt;5\times10^{-5}&amp;lt;/math&amp;gt;，批量大小 64，先验为标准高斯。图像数据通常先经 logit 变换 &amp;lt;math&amp;gt;\operatorname{logit}(\alpha + (1-\alpha)\mathbf{x}/256)&amp;lt;/math&amp;gt; 处理，以消除像素边界的边界效应。&lt;br /&gt;
&lt;br /&gt;
== 优缺点 ==&lt;br /&gt;
&lt;br /&gt;
=== 优势 ===&lt;br /&gt;
&lt;br /&gt;
* '''精确且可处理的对数似然'''：无需变分下界或对抗训练，直接给出数据的精确密度值，适用于异常检测、贝叶斯推断等需要显式概率的场景。&lt;br /&gt;
* '''并行采样与推断'''：编码和解码均可在维度上完全并行，速度远超自回归模型。&lt;br /&gt;
* '''语义潜在空间'''：同维度的潜在表示支持属性插值与操控，适合表示学习。&lt;br /&gt;
* '''架构灵活'''：&amp;lt;math&amp;gt;\mathbf{s}&amp;lt;/math&amp;gt; 和 &amp;lt;math&amp;gt;\mathbf{t}&amp;lt;/math&amp;gt; 网络可使用任意深度 CNN/ResNet，表达能力不受耦合结构本身限制。&lt;br /&gt;
* '''弥合模型鸿沟'''：兼具自回归模型的精确似然、VAE 的快速采样、以及不依赖固定重建损失的清晰生成。&lt;br /&gt;
&lt;br /&gt;
=== 局限 ===&lt;br /&gt;
&lt;br /&gt;
* '''似然性能未达最优'''：在 bits/dim 上仍略逊于 PixelRNN 等顶尖自回归基线。&lt;br /&gt;
* '''维度保持约束'''：单层流无法降维，需通过多尺度架构的&amp;quot;分解&amp;quot;操作间接实现。&lt;br /&gt;
* '''仿射耦合的表达力上限'''：相比后续 Flow++ 的 Logistic 混合 CDF 耦合、Neural Spline Flows 的有理二次样条耦合，仿射函数形式较简单&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot;&amp;gt;{{cite journal|author1=Ho J.|author2=Chen X.|author3=Srinivas A.|author4=Duan Y.|author5=Abbeel P.|title=Flow++: Improving Flow-Based Generative Models with Variational Dequantization and Architecture Design|journal=ICML|year=2019}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
* '''均匀去量化的次优性'''：对离散像素加均匀噪声的做法，后来被变分去量化改进。&lt;br /&gt;
* '''分区固定性'''：掩码模式在训练前固定，Glow 的可逆 &amp;lt;math&amp;gt;1\times1&amp;lt;/math&amp;gt; 卷积通过学习置换提供了更灵活的分区&amp;lt;ref name=&amp;quot;Glow2018&amp;quot;&amp;gt;{{cite journal|author1=Kingma D. P.|author2=Dhariwal P.|title=Glow: Generative Flow with Invertible 1×1 Convolutions|journal=NeurIPS|year=2018}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
=== 与相关框架的关系 ===&lt;br /&gt;
&lt;br /&gt;
RealNVP 处于标准化流发展谱系的枢纽位置：&lt;br /&gt;
&lt;br /&gt;
'''前驱工作'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心贡献 !! 与 RealNVP 的关系&lt;br /&gt;
|-&lt;br /&gt;
| '''NICE'''&amp;lt;ref name=&amp;quot;NICE2014&amp;quot; /&amp;gt; || 2014 || 加性耦合层 || RealNVP 的直接前身，将加性耦合扩展为仿射耦合&lt;br /&gt;
|-&lt;br /&gt;
| '''Rezende &amp;amp; Mohamed NFs'''&amp;lt;ref name=&amp;quot;Rezende2015&amp;quot;&amp;gt;{{cite journal|author1=Rezende D. J.|author2=Mohamed S.|title=Variational Inference with Normalizing Flows|journal=ICML|year=2015}}&amp;lt;/ref&amp;gt; || 2015 || 标准化流术语 + 平面流/径向流 || 提供了变分推断中标准化流的理论框架&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''后继发展'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 方法 !! 年份 !! 核心创新 !! 与 RealNVP 的对比&lt;br /&gt;
|-&lt;br /&gt;
| '''MAF'''&amp;lt;ref name=&amp;quot;MAF2017&amp;quot;&amp;gt;{{cite journal|author1=Papamakarios G.|author2=Pavlakou T.|author3=Murray I.|title=Masked Autoregressive Flow for Density Estimation|journal=NIPS|year=2017}}&amp;lt;/ref&amp;gt; || 2017 || 全自回归条件 || 密度评估更灵活，但采样需顺序进行&lt;br /&gt;
|-&lt;br /&gt;
| '''Glow'''&amp;lt;ref name=&amp;quot;Glow2018&amp;quot; /&amp;gt; || 2018 || 可逆 1×1 卷积 + actnorm || 全面刷新图像生成基准&lt;br /&gt;
|-&lt;br /&gt;
| '''FFJORD'''&amp;lt;ref name=&amp;quot;FFJORD2019&amp;quot;&amp;gt;{{cite journal|author1=Grathwohl W.|author2=Chen R. T. Q.|author3=Bettencourt J.|author4=Sutskever I.|author5=Duvenaud D.|title=FFJORD: Free-form Continuous Dynamics for Scalable Reversible Generative Models|journal=ICLR|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 连续时间流 + ODE 描述 || 消除架构约束，但需数值积分&lt;br /&gt;
|-&lt;br /&gt;
| '''Flow++'''&amp;lt;ref name=&amp;quot;FlowPlusPlus2019&amp;quot; /&amp;gt; || 2019 || 变分去量化 + Logistic 混合 CDF 耦合 || 改进 RealNVP 的三大短板&lt;br /&gt;
|-&lt;br /&gt;
| '''Residual Flows'''&amp;lt;ref name=&amp;quot;ResidualFlows2019&amp;quot;&amp;gt;{{cite journal|author1=Chen R. T. Q.|author2=Behrmann J.|author3=Duvenaud D.|author4=Jacobsen J.-H.|title=Residual Flows for Invertible Generative Modeling|journal=NeurIPS|year=2019}}&amp;lt;/ref&amp;gt; || 2019 || 无偏行列式估计 || 将残差网络引入可逆建模&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''理论分析'''&lt;br /&gt;
&lt;br /&gt;
* '''Draxler et al.'''（ICML 2024）&amp;lt;ref name=&amp;quot;Draxler2024&amp;quot; /&amp;gt;：证明耦合型标准化流（含 RealNVP）在良好条件下是分布普适的；体积保持流则不是，揭示了 NICE 的理论局限。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图7：标准化流发展谱系简图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 应用/实例 =&lt;br /&gt;
&lt;br /&gt;
== 自然图像密度估计与生成 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 在多个图像基准上验证了其密度估计与生成能力：&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 数据集 !! 分辨率 !! 测试对数似然（bits/dim） !! 关键发现&lt;br /&gt;
|-&lt;br /&gt;
| CIFAR-10 || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 3.49 || 生成样本全局连贯，优于 VAE&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;32 \times 32&amp;lt;/math&amp;gt; || 4.28 || 有效建模大规模自然图像分布&lt;br /&gt;
|-&lt;br /&gt;
| ImageNet || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || 3.98 || 能捕捉前景/背景分割与光照一致性&lt;br /&gt;
|-&lt;br /&gt;
| LSUN（卧室/塔楼/教堂） || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 建模了室内外场景的语义结构&lt;br /&gt;
|-&lt;br /&gt;
| CelebA || &amp;lt;math&amp;gt;64 \times 64&amp;lt;/math&amp;gt; || — || 潜在空间具有可解释的语义方向&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
与 VAE 相比，RealNVP 不依赖固定的重建损失（如 MSE），因此不会过度平滑高频细节；与自回归模型（如 PixelRNN）相比，RealNVP 的采样在维度上完全并行，速度更快。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图5：CelebA 生成样本与真实样本对比 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 潜在空间插值与语义操控 ==&lt;br /&gt;
&lt;br /&gt;
RealNVP 的潜在空间展现出良好的语义结构。给定四个样本的编码 &amp;lt;math&amp;gt;\mathbf{z}^{(1)}, \dots, \mathbf{z}^{(4)}&amp;lt;/math&amp;gt;，可在潜在空间中做球面插值：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{z}(\phi, \phi') = \cos\phi\,(\cos\phi'\,\mathbf{z}^{(1)} + \sin\phi'\,\mathbf{z}^{(2)}) + \sin\phi\,(\cos\phi'\,\mathbf{z}^{(3)} + \sin\phi'\,\mathbf{z}^{(4)})&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
解码后能看到姿态、表情、背景等属性的平滑过渡，而非简单的像素混合。在 CelebA 的类条件模型中，沿着特定方向移动潜在编码可系统性地操控性别、眼镜、微笑等属性，暗示该空间具有类似词向量的线性语义结构。&lt;br /&gt;
&lt;br /&gt;
== 重建设置中的推断 ==&lt;br /&gt;
&lt;br /&gt;
由于编码器 &amp;lt;math&amp;gt;f&amp;lt;/math&amp;gt; 与解码器 &amp;lt;math&amp;gt;g&amp;lt;/math&amp;gt; 完美互逆（&amp;lt;math&amp;gt;\mathbf{x} = g(f(\mathbf{x}))&amp;lt;/math&amp;gt;），RealNVP 天然适合图像修复、去噪、着色等任务。以图像修复为例：已知像素 &amp;lt;math&amp;gt;\mathbf{x}_O&amp;lt;/math&amp;gt; 固定，对缺失像素 &amp;lt;math&amp;gt;\mathbf{x}_H&amp;lt;/math&amp;gt; 用梯度上升最大化 &amp;lt;math&amp;gt;\log p_X(\mathbf{x}_O, \mathbf{x}_H)&amp;lt;/math&amp;gt;，并加入少量高斯噪声扰动：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\mathbf{x}_{H}^{(t+1)} = \mathbf{x}_{H}^{(t)} + \alpha_t \left(\frac{\partial \log p_X(\mathbf{x}_O, \mathbf{x}_{H}^{(t)})}{\partial \mathbf{x}_H} + \boldsymbol{\epsilon}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
由于模型掌握了全数据分布的精确密度，它能对任意形状的掩码产生合理的语义补全，而非仅记忆特定训练掩码。&lt;br /&gt;
&lt;br /&gt;
== 在 NIS/NIS+ 因果涌现框架中的应用 ==&lt;br /&gt;
&lt;br /&gt;
这是 RealNVP 在因果涌现研究中的核心应用场景。在 [[NIS]]（神经信息挤压器）及 [[NIS+]] 中，粗粒化策略被建模为两阶段过程：&lt;br /&gt;
&lt;br /&gt;
# '''无损变换'''：由 RealNVP 耦合层堆叠构成的可逆神经网络（INN）&amp;lt;math&amp;gt;\psi_\alpha: \mathbb{R}^p \to \mathbb{R}^p&amp;lt;/math&amp;gt; 将微观状态 &amp;lt;math&amp;gt;\mathbf{x}_t&amp;lt;/math&amp;gt; 映射到等维潜在空间 &amp;lt;math&amp;gt;\mathbf{x}'_t&amp;lt;/math&amp;gt;。'''此阶段不丢失任何信息'''。&lt;br /&gt;
# '''信息丢弃'''：投影算子 &amp;lt;math&amp;gt;\chi_{p,q}&amp;lt;/math&amp;gt; 丢弃 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 个维度，得到宏观状态 &amp;lt;math&amp;gt;\mathbf{y}_t = \chi_{p,q}(\psi_\alpha(\mathbf{x}_t))&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
解码时，将宏观预测 &amp;lt;math&amp;gt;\hat{\mathbf{y}}_{t+1}&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 维标准高斯噪声拼接，通过逆 INN &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 重构微观状态。由于 RealNVP 的编码器与解码器只是方向反转，'''参数完全共享'''，模型参数量直接减半。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- 图6：NIS 框架中 RealNVP 作为 INN 的位置示意图 --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NIS 论文明确给出选用 RealNVP 的三点依据&amp;lt;ref name=&amp;quot;NIS2022&amp;quot;&amp;gt;{{cite journal|author1=Zhang J.|author2=Liu K.|title=Neural Information Squeezer for Causal Emergence|journal=Entropy|year=2022|volume=25|issue=1|page=26}}&amp;lt;/ref&amp;gt;：&lt;br /&gt;
# '''精确可逆性'''：微观到潜在的变换不丢信息，信息丢弃量 &amp;lt;math&amp;gt;p-q&amp;lt;/math&amp;gt; 成为显式可控的超参数。&lt;br /&gt;
# '''参数共享'''：&amp;lt;math&amp;gt;\psi_\alpha&amp;lt;/math&amp;gt; 与 &amp;lt;math&amp;gt;\psi_\alpha^{-1}&amp;lt;/math&amp;gt; 共用一套参数，极大压缩模型规模。&lt;br /&gt;
# '''行列式可解析'''：三角雅可比结构使得有效信息（EI）上界和最优宏观维度 &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; 的存在性等核心定理得以严格推导。&lt;br /&gt;
&lt;br /&gt;
消融实验表明，若将 RealNVP 替换为普通前馈网络或 VAE，多步预测误差显著上升；在分布外泛化测试中，仅基于 RealNVP 的 NIS/NIS+ 保持了低误差。论文结论明确指出：'''可逆神经网络作为编码器和解码器是必要的'''。在 NIS+ 中，RealNVP 继续承担编码–解码功能，而 EI 最大化等改进发生在投影后的宏观动力学空间上，不改动 INN 模块本身&amp;lt;ref name=&amp;quot;NISPlus2025&amp;quot;&amp;gt;{{cite journal|author1=Yang M.|author2=Wang Z.|author3=Liu K.|author4=Rong Y.|author5=Yuan B.|author6=Zhang J.|title=Finding Emergence in Data by Maximizing Effective Information|journal=National Science Review|year=2025|volume=12|issue=1|page=nwae279}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
= 参考文献 =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= 署名 =&lt;br /&gt;
&lt;br /&gt;
本词条由 [编写者姓名] 编写，[审阅者姓名] 审阅，[日期] 发布。&lt;br /&gt;
&lt;br /&gt;
'''贡献者'''：集智百科团队&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:Realnvp_multiscale.svg&amp;diff=50658</id>
		<title>文件:Realnvp multiscale.svg</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=%E6%96%87%E4%BB%B6:Realnvp_multiscale.svg&amp;diff=50658"/>
		<updated>2026-07-13T11:00:43Z</updated>

		<summary type="html">&lt;p&gt;黄一凡：RealNVP 的多尺度架构&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== 文件说明 ==&lt;br /&gt;
RealNVP 的多尺度架构&lt;/div&gt;</summary>
		<author><name>黄一凡</name></author>
	</entry>
</feed>