neuralop.layers.embeddings.SinusoidalEmbedding
- class neuralop.layers.embeddings.SinusoidalEmbedding(in_channels: int, num_frequencies: int = None, embedding_type: str = 'transformer', max_positions: int = 10000)[source]
Sinusoidal positional embedding for enriching coordinate inputs with spectral information.
This class provides sinusoidal positional embeddings in two styles: Transformer-style and NeRF-style. It lifts low-dimensional coordinates into a richer spectral representation by encoding them as periodic functions (sines and cosines) at multiple frequencies.
The embedding enhances a model’s ability to capture fine-scale variations and high-frequency dynamics by providing a hierarchy of frequency components alongside the original coordinates.
- Parameters:
- in_channelsint
Number of input channels to embed (dimensionality of input coordinates)
- num_freqsint, optional
Number of frequency levels L in the embedding. Each level contributes a sine and cosine pair, resulting in 2L output channels per input channel. By default, set to the number of input channels.
- embedding_type{‘transformer’, ‘nerf’}, optional
Type of embedding to apply, by default ‘transformer’
Transformer-style [1]: For each input coordinate p and frequency level k (0 ≤ k < L): - g(p)_{2k} = sin(p / max_positions^{k/L}) - g(p)_{2k+1} = cos(p / max_positions^{k/L})
NeRF-style [2]: For each input coordinate p and frequency level k (0 ≤ k < L): - g(p)_{2k} = sin(2^k * π * p) - g(p)_{2k+1} = cos(2^k * π * p)
- max_positionsint, optional
Maximum number of positions for transformer-style encoding, by default 10000. Only used when embedding_type=’transformer’.
- Attributes:
out_channelsrequired property for linking/composing model layers
Methods
forward(x)Notes
Input shape: (batch, n_in, in_channels) or (n_in, in_channels)
Output shape: (batch, n_in, 2*num_freqs*in_channels) or (n_in, 2*num_freqs*in_channels)
Ensure the highest frequency satisfies the Nyquist criterion: - Transformer: f_max < N/2 where N is the number of sampling points - NeRF: 2^{L-1} < N/2, i.e., L < 1 + log₂(N/2)
References
[1]Vaswani, A. et al. “Attention Is All You Need”. NeurIPS 2017, https://proceedings.neurips.cc/paper_files/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf
[2]Mildenhall, B. et al. “NeRF: Representing Scenes as Neural Radiance Fields for View Synthesis”. ArXiv 2020, https://arxiv.org/pdf/2003.08934
Examples
See examples/layers/plot_sinusoidal_embeddings.py for comprehensive visualizations
- forward(x)[source]
- Parameters:
- x: torch.Tensor
shape (n_in, self.in_channels) or (batch, n_in, self.in_channels)