Tokens
Each mesh point becomes a vector of numbers describing where it is and what it carries.
Outcome: Turn mesh points into input features: position, normal, flow condition, Fourier features; explain an embedding: an MLP that lifts each point's features to d numbers; build a token tensor of shape (N, d) in PyTorch; say what absolute coordinates as inputs tie a model to, and what can replace them.
Builds on Grids, meshes and point clouds, A first field surrogate
PointNet (grids, meshes and point clouds) applied one shared MLP to every point before its maximum over points. That per-point MLP is an embedding, and its output, one vector per point, a token. Here it reads the inputs of the neural field, a surface point’s position and the design parameters (, ).
For a pressure tap on a wind-tunnel car, the token holds where the tap sits and which way it faces; the pressure it reads is what the model learns to predict.
From raw features to an embedding
The raw features of a point are the known numbers: , , , the surface normal, the distance to the wall, the flow condition, and Fourier features of the position. An embedding is a small MLP, or a single linear layer, that maps these raw features to numbers, 128 in this lesson. It is applied to every point separately. No information passes between points yet: the embedding only puts every point into the same learned -dimensional space, where the next layers can compare and mix them.
Each run of the download has 2,000 surface points. Each point gets 3 coordinates, 3 normal components, and , and 24 Fourier features: 32 raw numbers, the inputs of the site's neural field. An MLP lifts them to .
DefinitionToken
The neural field used fixed frequencies, doubling at each octave (Equation 4-6). Example 6-1 draws them at random, the other option studied by Tancik et al. (2020). In their tests both beat no mapping, random Gaussian frequencies did best, and among random distributions the scale mattered much more than the shape. The raw features are therefore normalised first (normalising), and the Fourier frequencies chosen to match the smallest feature to be resolved.
import torch
from torch import nn
from aircraft import load_runs, scale_designs, split_runs
d = load_runs()
train, _, _, _ = split_runs(d)
mu = d["xyz"][train].reshape(-1, 3).mean(axis=0)1
sd = d["xyz"][train].reshape(-1, 3).std(axis=0)
i = 0 # one run
xyz = torch.tensor((d["xyz"][i] - mu) / sd) # (2000, 3)
normal = torch.tensor(d["normal"][i]) # unit vectors
N, d_tok = len(xyz), 128 # points, token size
# alpha and sweep, scaled as in 4.3, the same for every point.
cond = torch.tensor(scale_designs(d, train)[0][i])
cond = cond.expand(N, 2)2
# Fourier features with 12 random frequencies.
torch.manual_seed(0)
B = torch.randn(3, 12) * 4.03
fx = xyz @ B
raw = torch.cat([xyz, normal, cond, fx.sin(), fx.cos()],
dim=-1) # (N, 32)4
# The embedding: one small MLP applied to every point.
embed = nn.Sequential(nn.Linear(raw.shape[-1], d_tok),
nn.SiLU(), nn.Linear(d_tok, d_tok))
tokens = embed(raw) # (N, d): one token per point5
print(raw.shape, tokens.shape)
- The positions are normalised with the mean and standard deviation of the training runs, as in Example 4-9; so are the two conditions, by
scale_designs. expandrepeats the same two numbers on every point, as a view, without copying them.- 3 coordinates, 3 normal components, 2 conditions, 12 sines and 12 cosines: 32 raw features per point.
nn.Linearacts on the last axis, so the embedding is applied to each point separately.
Example 6-1 prints torch.Size([2000, 32]) torch.Size([2000, 128]): 32 raw numbers, then a token of 128, for each of the run's 2,000 points.
Positional encodings
The attention layers of the next lessons mix tokens without knowing where they are, unless the tokens say so. Any way of putting position into the token is a positional encoding. Coordinates with Fourier features, as above, are a common choice for meshes and point clouds. Language models, and some surrogates after them, use rotary position embeddings instead (RoPE; Su et al. 2021), described in the next lesson. Grid models make one token per patch of cells; CAD-native models (working on the computer-aided design model itself) make one per surface patch (the CAD lesson).
Absolute coordinates as inputs, such as those of Example 6-1, tie a model to how its training data were written down: the origin, the axes and the units. That is harmless while every case is stored in one frame, as the course’s runs are. But the same problem, moved, turned together with its free stream, or written in millimetres, arrives as numbers the model may never have seen. Relative positions remove the origin, as on the edges of a graph network. Coordinates divided by a reference length remove the units (normalising). The rotation is handled by one of the routes of symmetries: invariant inputs, equivariant layers, augmentation or a canonical frame.