- Published on
理解 3D 中的位置编码
引言
在 3D 表示学习里,原始坐标只是位置索引,还算不上好的表示。 对于 NeRF、点云网络和各类坐标网络来说,模型既要分清局部几何细节,又要在全局上维持稳定的空间区分。 编码过于平滑,细节会被抹掉;过于依赖周期结构,远距离位置又可能在特征空间里错误对齐。
因此,位置编码的作用并不只是“对坐标做一次变换”,而是在输入阶段显式引入空间归纳偏置。 它决定了模型如何感知尺度,如何比较不同位置之间的相似性,也决定了哪些空间关系更容易被后续网络利用。
Fourier features 是这一问题最有代表性的起点。 它为坐标网络提供了强有力的多尺度先验,但周期性也带来了额外的全局歧义。 后续的 PMPE 设计,可以看作是围绕这一缺陷展开的修正; 而更广义的可学习空间编码,则进一步把“如何组织空间”从手工设计推进到任务自适应的范畴。
Fourier Features:经典基线
经典的 Fourier 位置编码 (Mildenhall et al., 2020) 会将坐标映射为一组不同频率的正弦与余弦响应:
这一编码之所以有效,关键在于它把原本简单的坐标轴展开成了多尺度特征。 低频通道随位置变化较慢,更适合表达粗粒度的空间结构; 高频通道响应更敏感,能够放大小尺度的位置差异。 因此模型不必完全依赖后续网络从原始坐标中自行构造多尺度表示,编码本身已经将这类结构显式写入输入特征。
这里所谓“频率”,并不是说几何对象本身在振动,而是指编码对坐标变化的响应速度。 频率越低,特征变化越平缓,更适合描述整体形状;频率越高,特征变化越剧烈,更容易刻画边界、尖角和细薄结构。 Fourier features 之所以成为坐标网络中的标准起点,关键就在于它用一种很直接的方式提供了这种多尺度先验。
这个可视化可以从两个层面来看。Positional Encoding 面板展示的是单个坐标如何被展开成多通道表示; Dot-product Similarity 面板展示的则是编码后不同位置之间的相似性结构。 对于依赖点积相似度的模型,尤其是 attention 架构,这个视角很重要,因为它直接反映了编码如何组织空间邻近关系。
Traditional Fourier / NeRF Positional Encoding
This demo shows how a 1D position x in [-1, 1] is expanded into many sine/cosine channels with different frequencies. The left plot shows the encoded values. The right plot shows how similar two positions look after encoding.
Vertical axis: input position x. Horizontal axis: encoding channel. White or dark line: the currently selected position.
Horizontal axis: position x_j. Vertical axis: position x_i. The cross shows the selected position.
This sparkline is the encoded feature vector of the selected x. Each point corresponds to one cosine or sine channel.
- More frequencies means more fine-scale variation, so nearby positions become easier to distinguish.
- The main diagonal in the similarity plot is bright because a position is always most similar to itself.
- Stripe-like off-diagonal patterns come from periodicity. They hint that some far-away positions can still look similar after encoding.
不过,Fourier features 的局限也正来自它最核心的结构假设。 正弦和余弦都是周期函数,因此相距较远的位置仍可能在部分通道上产生相近响应。 把这一点放到相似度图中,就会表现为主对角线之外反复出现的条纹结构。 换句话说,Fourier 编码能够有效增强局部细节分辨率,却未必总能稳定地区分全局位置。
PMPE:降低周期性歧义
PMPE,即 Phase-Modulated Positional Encoding (Roblox Research, 2025),要处理的就是 Fourier 编码里的周期性歧义。 只要位置表示主要由周期函数构成,远距离位置之间就可能出现本不该有的相似。
它的核心做法,是在原有 Fourier 分支之外再引入一条 phase-modulated 分支:
从形式上看,Fourier 分支继续提供多尺度细节响应,phase-modulated 分支则用来打破单纯周期结构带来的重复对齐。 PMPE 追求的不是引入“更多频率”,而是减少那些并不反映真实空间邻近性的远距离相似。
这一点在下面的对比图里很直观。相较于纯 Fourier 编码,主对角线附近的强响应依然被保留下来,而对角线之外反复出现的条纹则明显减弱。 这说明 PMPE 并没有牺牲局部细节分辨能力,而是在全局范围内对相似性几何做了更有约束的整理。
Traditional Fourier vs PMPE
This demo compares the classic Fourier-style positional encoding with a phase-modulated version. The goal is to show how PMPE keeps structured variation while reducing misleading global periodic similarity.
Traditional Fourier / NeRF Encoding
Vertical axis: input position x. Horizontal axis: encoding channel.
Horizontal axis: position x_j. Vertical axis: position x_i.
Phase-Modulated Positional Encoding (PMPE)
Vertical axis: input position x. Horizontal axis: encoding channel.
Horizontal axis: position x_j. Vertical axis: position x_i.
- In the traditional Fourier version, repeated stripe-like off-diagonal patterns often appear in the similarity matrix because of periodicity.
- In the PMPE version, the similarity structure is usually more spatially coherent, meaning distant positions are less likely to look accidentally similar.
- The core goal of PMPE is not to remove detail, but to improve how global spatial separation is reflected in the encoded space.
对于 3D 任务,这种修正很重要,因为位置编码往往位于邻域构造、相似性计算以及 attention 之前。 如果本应彼此分离的远距离位置在编码后仍表现出过高相似度,后续模型就可能混合原本不应耦合的信号。 PMPE 的价值正在于此:它直接针对 Fourier 编码中的结构性歧义进行修正,同时保留了后者在局部细节表达上的优势。
当编码变成可学习之后,会发生什么
PMPE 解决的是周期性歧义,但一旦把这件事放进真实模型,讨论重点就会继续往前走。 问题不再只是“如何修正 Fourier 编码的缺陷”,而是“哪些频率、哪些相位、哪些空间结构还要手工设计, 哪些部分应该交给训练去学习”。在Cube3D (Roblox Research, 2025) 中延续了PMPE的思路并结合了可学习频率的设计,进一步推动了位置编码的工程化实现。
Cube3D:PMPE 的工程化实现
在实际工程中,Cube3D 采用的是一种组合式位置编码:它没有直接回到传统 Fourier 编码,也没有只保留 PMPE,而是把两者结合起来,构成最终使用的嵌入形式:
其中
这里 是可学习参数, 则是 carrier term。
这样写的原因,可以从 Cube 的目标反推出来。Roblox 的 Cube 把这套编码放在 shape tokenizer 的入口: 先从 mesh 表面采样点云,再把点送进 perceiver-style transformer。 如果位置表示在 cross-attention 里无法稳定地区分远距离点,后面的 latent 建模和重建质量都会受到影响。
和 分别承担了不同职责。 前者保留 Fourier 分支的基本表达能力,后者引入额外的相位结构,用来打破单纯周期函数带来的重复对齐。
在官方实现里,carrier term 会沿着通道维度变化,用来调节 phase branch 的偏移; 论文也明确提到,这样做是为了避免不同分支之间发生 resonance。
下面这个 demo 用简化的一维设定重现同样的思路。第一幅图看的是通道响应,第二幅图看的是相似性场如何围绕某个位置展开。 为了更容易展示,这里的实现把 换成了固定的频率基底,但要看的仍然是“Fourier 分支保留细节、phase branch 约束全局相似性”这一组合关系。
Cube3D-Style PMPE
This demo visualizes a simplified 1D version of a Cube3D-style phase-modulated positional encoding. It combines a learnable Fourier-like branch with a structured phase branch, then adds them together to form the final embedding.
Vertical axis: input position x. Horizontal axis: encoding channel. The highlighted line marks the selected position.
Horizontal axis: position x_j. Vertical axis: position x_i. The cross shows the selected position.
This sparkline shows the full embedding vector of the selected x, including the raw coordinate channel and the combined cosine/sine channels.
- Frequencies control how many combined channels are used.
- Seed changes the learnable Fourier-like projection, which slightly changes the detailed stripe pattern.
- The main idea is that the final embedding is formed by adding a Fourier-like branch and a phase-modulated branch together.
Learnable Fourier Features
频率基底可学这类方法直接把频率基底本身交给训练。 模型不再使用预设的一组频率,而是根据目标信号去调整 basis。 这样既保留了 Fourier features 的多尺度特性,也让表示更贴近数据本身。
在 demo 里,紫色曲线是目标信号,绿色曲线是当前拟合结果。模型形式是:
这里学习的不只是系数 ,还包括每个 basis element 的频率 和相位 。 Basis count 决定基底数量,Learning rate 决定更新速度,Seed 控制初始化。 右侧的 frequency chips 显示的是当前学到的频率大小。
Relative Position Bias
Relative Position Bias 学的不是“点在哪里”,而是“相对位移该怎样影响注意力”。 它通常写成 b(dx, dy),或者更一般地写成 ,然后直接加到 attention score 里,或同时进入注意力分支和特征分支。 这样一来,模型关心的就不再是绝对坐标,而是“另一个点离我多远、朝哪个方向”。
Point Transformer 把 3D 相对坐标 送进一个小 MLP, 并且实验里 relative encoding 明显优于 absolute encoding (Zhao et al., 2021)。 换句话说,它学到的本质上是一张“位移偏好图”。
这个 demo 画出的就是这种偏好图的 toy 版本。横纵轴是 (dx, dy),颜色表示某种相对位移会被增强还是抑制。 Locality 控制作用范围,Anisotropy 控制方向拉伸,Rotation 控制主方向。 它没有复现完整训练过程,但已经足够说明关键区别:relative bias 依赖的是 displacement,而不是 world coordinates。
Hash Grids as Spatial Memory
Hash grid 则走向另一种思路:与其继续设计函数,不如直接学习一套多分辨率的空间存储。 点的位置先落到不同分辨率的 grid 上,再通过哈希查表和插值取出局部特征,最后拼成表示。 相比 Fourier,这更像一套稀疏、局部、可扩展的空间记忆。 这一类 multi-resolution hash grid encoding 最有代表性的工作是 Müller 等人的 Instant-NGP (Müller et al., 2022)。 它把坐标映射到多层哈希表中的可训练特征,再通过插值和拼接形成最终表示。 这在工程中会大大降低由于fourier encoding引入的维度增加带来的计算负担,同时也能更灵活地适应不同任务的空间结构。
在这个 tab 里,十字线标出 anchor point,热力图颜色表示 anchor embedding 与其他位置 embedding 的 cosine similarity。 底层实现是一个简化版 multi-resolution hash grid:每层有自己的分辨率,grid vertex 通过 hash 映射到有限 table, 查询点对邻近顶点做双线性插值,再把各层结果拼接起来。Levels 决定尺度层数,Table size 影响哈希碰撞,Anchor x/y 控制观察中心, Seed 用来重新采样 toy table。
Learned Spatial Encoding Examples
A compact browser-side demo of three learnable or task-adaptive encoding ideas: learnable Fourier features, learned relative bias, and a toy multi-resolution hash grid.
Purple is the target signal. Green is the learned Fourier model. The basis frequencies are trainable.
These are the current learned frequency magnitudes in cycles per unit interval.
结语
如果把这篇文章压缩成一个问题, 其实就是:怎样让模型既看清局部细节,又不要在全局上把本不该相似的位置混在一起。 Fourier features 第一次比较系统地给出了答案,它用多尺度正弦基底增强了坐标表示,但也把周期性带来的全局歧义一并引入了模型。 后来的 PMPE,以及 Cube3D 这类工程化实现,本质上都在沿着同一条线继续推进:尽量保留 Fourier 的细节表达能力, 同时把相似性结构整理得更符合真实空间关系。
再往后,问题就不再只是“该选哪一种位置编码”,而是“哪些部分应该继续手工设计,哪些部分应该交给训练去学”。 频率基底可以学,相对偏置可以学,空间存储方式也可以学。 到了这一步,位置编码已经不再只是坐标前的一层固定变换,而开始成为 3D 模型组织空间、利用局部性并适配具体任务的一部分。