Browse and solve 1,200+ machine learning coding challenges on Deep-ML. Filter by difficulty (beginner, intermediate, advanced) and category (linear algebra, machine learning, deep learning, NLP, computer vision). Write Python solutions in the browser and get instant feedback against test cases.
Deep-ML | Trajectory Straightness and Reflow PairsTrajectory Straightness and Reflow Pairs
The one-dimensional trajectory 0 -> 1 -> 1 has finite-difference velocities 2 and 0 while the displacement is 1, so the straightness is ((2-1)^2 + (0-1)^2)/2 = 1 and the cost is 1^2 = 1. Under a constant field every Euler path is a straight line: four steps of 0.25 * [1, 2] move each point by [1, 2], the straightness is 0, the cost is 1 + 4 = 5, and along the linear interpolant the field equals the displacement, so the coupling straightness is 0 as well.
Measure How Straight a Flow Is, Then Rewire the Pairs
Implement four numpy functions for rectified flows. Norms are over the feature axis and every scalar is returned as a Python float.
straightness(traj): traj is (n_steps + 1, n, d), trajectories on the grid tk=k/nsteps. With vk=nsteps(xk+1−xk) and the displacement x1−x0 (last minus first point), return
S=nstepsn1∑k=0nsteps−1∑i=1nvk(i)−(x1(i)−x0(i))2
transport_cost(x0, x1): mean over the n samples of ∥x1−x0∥2 for (n, d) arrays.
reflow_pairs(x0, v_fn, n_steps): forward Euler from t=0 to t=1 in n_steps equal steps, xk+1=xk+nsteps1v(xk,tk), where v_fn(x, t) returns an (n, d) velocity. Return {"x0": (n, d), "x1": final points (n, d), "traj": (n_steps + 1, n, d), "straightness": straightness(traj), "transport_cost": transport_cost(x0, x1)}.
coupling_straightness(x0, x1, v_fn, n_steps): with xt=(1−t)x0+tx1 at tk=k/nsteps for k=0,…,nsteps−1, return the mean over k and samples of ∥v(xtk,tk)−(x1−x0)∥2.