Skip to content

Positional Encodings

Positional encodings are fixed transformations applied to the input data before being fed into a neural network. They increase the model's accuracy by tackling low-frequency biases. When defining a neural model, simply use a IL.nn.Implicitequential object to add your encoding before your neural network. Note that the encoding size and the input dimension of the network should match:

# Add an encoding made of Fourier Features to a simple multi-layer perceptron
model = IL.nn.ImplicitSequential(
    RandomFourierEncoding(geometry,1000),
    MultiLayerPerceptron(1000, 128, 4)
)

FourierEncoding(input_dim, encoded_dim=10)

Bases: ImplicitModel

Positional encoding used in neural radiance fields. Maps the input \(x\) to

\[[\sin(x),\, \cos(x),\, \sin(2x),\, \cos(2x),\, ...,\, \sin(2^n\,x),\, \cos(2^n\,x)]\]

Parameters:

Name Type Description Default
input_dim int

dimension of the input points (usually 2 or 3).

required
encoded_dim int

number of frequencies (i.e. half the size of the final encoded vector). Defaults to 10

10

GaussianPointDistanceEncoding(points, stdv=1.0, trainable=False)

Bases: PointDistanceEncoding

Variant of the PointDistanceEncoding where distance are then fed into a Gaussian kernel:

\[x \mapsto e^{ - \frac{||x-p||^2}{\sigma}}\]

Parameters:

Name Type Description Default
points Tensor

The points consider for encoding

required
stdv float

standard deviation of the Gaussian kernel. Defaults to 1.

1.0
trainable bool

whether the points can be trained as model parameters. Defaults to False.

False

HyperplaneEncoding(points, normals, trainable=False)

Bases: ImplicitModel

Encoding that considers points with normals sampled on the geometry and applies the signed distance to each corresponding hyperplanes:

\[x \mapsto \langle n, x-p \rangle\]

where \((p,n)\) are points and normals.

Parameters:

Name Type Description Default
points Tensor

the points to consider

required
normals Tensor

the normals associated with each point. Will be normalized.

required
trainable bool

whether the points and normals can be trained as model parameters. Defaults to False.

False

LipschitzFourierEncoding(input_dim, encoded_dim, k_coeff_lip=1.0)

Bases: ImplicitModel

Frequency encoding but each frequency is normalized so that the final function is 1-Lipschitz. Maps the input \(x\) to

\[\frac{1}{\sqrt{2L}} [\sin(x),\, \cos(x),\, \sin(2x)/2,\, \cos(2x)/2,\, ...,\, \sin(2^n\,x)/2^n,\, \cos(2^n\,x)/2^n]\]
References

Geometry Processing with Neural Fields, Yang et al., 2023

Parameters:

Name Type Description Default
input_dim int

dimension of the input points (usually 2 or 3).

required
encoded_dim int

number of frequencies (i.e. half the size of the final encoded vector). Defaults to 10.

required
k_coeff_lip float

Lipschitz coefficient of the layer. Defaults to 1.

1.0

PointDistanceEncoding(points, trainable=False)

Bases: ImplicitModel

Encodes the input point x with its l2 distance to sampled points \(p\) in space:

\[x \mapsto ||x-p||_2\]

Parameters:

Name Type Description Default
points Tensor

The points consider for encoding

required
trainable bool

whether the points can be trained as model parameters. Defaults to False.

False

RandomFourierEncoding(input_dim, encoded_dim, stdv=1.0)

Bases: ImplicitModel

Encodes a given position into different frequencies of trigonometric functions :

\[x \mapsto [\cos(2\pi b x), \sin(2 \pi b x)]\]

where \(b\) is sampled from a normal distribution of zero mean (and provided standard deviation)

Parameters:

Name Type Description Default
input_dim int

dimension of the input points (usually 2 or 3).

required
encoded_dim int

number of frequencies (i.e. half the size of the final encoded vector. the tensor b will have shape (geometry.dim, encoded_dim)

required
stdv float

Standard variation of the normal distribution used to compute b. Defaults to 1..

1.0

Raises:

Type Description
ValueError

Fails if the encoding size is not an even number.

References

MultiResDenseGridEncoding(input_dim, domain=None, n_levels=4, n_features_per_level=2, base_resolution=16, finest_resolution=128, smooth=False)

Bases: ImplicitModel

Multiresolution encoding using dense grid of parameters.

The resolutions of the grids are determined as logarithmically increasing between the base resolution and the finest resolution. For instance, the defaults parameters generate 4 grids from resolutions 16 to 128, which generates grids of resolution 16, 32, 64 and 128.

Warning

The number of parameters can quickly blow up if large resolutions are used. The default finest_resolution of 128 already reaches in the millions. For better scaling, see the MultiResHashGridEncoding class.

Warning

As this encoding relies on F.grid_sample, it cannot be differentiated twice. That means that any loss on the derivatives of the neural implicit will fail if the model contains this encoding.

Parameters:

Name Type Description Default
input_dim int

dimension of the input. This class only supports 2D or 3D inputs.

required
domain AABB

domain of the grid defined as an axis-aligned bounding box. If not provided, the domain will be [0,1]^dim. Defaults to None.

None
n_levels int

number of grid levels. Defaults to 4.

4
n_features_per_level int

number of features per grid level. Defaults to 2.

2
base_resolution int

coarsest grid resolution. Defaults to 16.

16
finest_resolution int

finest grid resolution. Defaults to 128.

128
smooth bool

whether to consider a smoothstep interpolation instead of a linear one. Defaults to False.

False

MultiResHashGridEncoding(input_dim, domain=None, n_levels=16, n_features_per_level=2, log2_hashmap_size=15, base_resolution=16, finest_resolution=512)

Bases: ImplicitModel

Multi-resolution hash encoding from Nvidia's instant NGP project. Instead of considering dense grids, considers a fixed-size hashmap of feature values at each resolution, enabling higher resolution without exploding the parameter count.

Warning

This encoding cannot be differentiated twice. That means that any loss on the derivatives of the neural implicit will fail if the model contains this encoding.

References

[1] Instant Neural Graphics Primitives with a Multiresolution Hash Encoding, Müller et al., 2022
[2] https://nvlabs.github.io/instant-ngp
[3] https://github.com/Ending2015a/hash-grid-encoding/tree/master

Parameters:

Name Type Description Default
dim int

dimension of the input (usually 2 or 3). This class supports at most 7D data.

required
domain AABB

domain of the grid defined as an axis-aligned bounding box. If not provided, the domain will be [0,1]^dim. Defaults to None.

None
n_levels int

number of grid levels. Defaults to 16.

16
n_features_per_level int

number of features per grid level. Defaults to 2.

2
log2_hashmap_size int

maximum size of the hashmap of each level in log2 scale. According to [1], this value can be set to 14 ~ 24 depending on your problem size. Defaults to 15.

15
base_resolution int

coarsest grid resolution. Defaults to 16.

16
finest_resolution int

finest grid resolution. According to [1], this value can be set to 512 ~ 524288. Defaults to 512.

512