Rectified Flow from Scratch

How it was built

Part 1: Straight-Path Interpolants

Define the linear interpolant from noise at t=0 to data at t=1, the constant target velocity along that path, and the seeded time and noise samplers that generate training trajectories.

Part 2: Flow-Matching Objective

Assemble a flow-matching minibatch of interpolated states, times, and target velocities, then score a predicted velocity field with mean squared error.

Part 3: Velocity MLP

Embed scalar times as sinusoidal Fourier features and implement a two-hidden-layer velocity network as a seeded parameter dict with a functional forward pass.

Part 4: Mixture Data and Training

Build a fixed-seed 2-D Gaussian mixture and train the velocity MLP with manual SGD on the flow-matching loss.

Part 5: ODE Samplers

Integrate the learned velocity ODE from noise at t=0 to data at t=1 using evenly spaced timesteps and both Euler and Heun predictor-corrector steps.

Part 6: Evaluation, Reflow, and Experiment

Score generated samples by nearest-neighbor MSE, construct rectified (noise, generated-data) pairs, and run the capstone train-and-sample experiment against a pure-noise baseline.