TensorTonic
Problems
Conditional Flow Matching Loss
Diffusion ModelsFlow MatchingLoss Functions
Medium

Compute the conditional flow matching loss of a small linear velocity model on straight paths between data and noise.

xt=(1−t) xdata+t xnoise,u=xnoise−xdata,v=xtW+t wtime+b,ℓ=1D∑d=1D(vd−ud)2x_t = (1 - t)\, x_{\text{data}} + t\, x_{\text{noise}}, \qquad u = x_{\text{noise}} - x_{\text{data}}, \qquad v = x_t W + t\, w_{\text{time}} + b, \qquad \ell = \frac{1}{D} \sum_{d=1}^{D} (v_d - u_d)^2xt​=(1−t)xdata​+txnoise​,u=xnoise​−xdata​,v=xt​W+twtime​+b,ℓ=D1​d=1∑D​(vd​−ud​)2

Here x_data holds the data samples xdatax_{\text{data}}xdata​ with shape (B, D), x_noise holds the paired Gaussian noise samples xnoisex_{\text{noise}}xnoise​ with the same shape, and t holds one time ttt in [0, 1] per sample with shape (B,). Time 0 is the data end of the path and time 1 is the noise end. The target velocity uuu is the constant speed of the straight path from data to noise. The toy velocity model multiplies xtx_txt​ by weight WWW (shape (D, D)), adds the per-sample time times time_weight wtimew_{\text{time}}wtime​ (shape (D,)) and adds bias bbb (shape (D,)). The per-sample loss ℓ\ellℓ is the mean squared error over the D features, and the batch loss is the mean of ℓ\ellℓ over the B samples.

Return a dict with the keys "x_t" and "target" holding PyTorch tensors of shape (B, D), "per_sample_loss" holding a tensor of shape (B,), all with the dtype and device of x_data, and "loss" holding the batch loss as a Python float.

Examples

Input: x_data = [[1.0,2.0],[-1.0,0.5]], x_noise = [[0.2,-0.4],[0.6,1.0]], t = [0.25,0.75], weight = [[0.5,-0.1],[0.2,0.3]], time_weight = [1.0,-0.5], bias = [0.0,0.1]

Output: {"x_t":[[0.8,1.4],[0.2,0.875]],"target":[[-0.8,-2.4],[1.6,0.5]],"loss":2.744577,"per_sample_loss":[5.182063,0.307091]}

Explanation: The first sample sits at t = 0.25, so x_t = 0.75 * (1, 2) + 0.25 * (0.2, -0.4) = (0.8, 1.4) and its target velocity is (0.2, -0.4) - (1, 2) = (-0.8, -2.4). The model predicts (0.93, 0.315), which gives a per-sample loss of 5.1821.

Input: x_data = [[0.5,-0.5,1.0],[2.0,0.0,-1.0]], x_noise = [[1.0,1.0,-1.0],[-0.3,0.8,0.4]], t = [0.0,1.0], weight = [[0.1,0.0,0.2],[0.0,-0.3,0.1],[0.4,0.2,0.0]], time_weight = [0.5,0.5,0.5], bias = [0.0,0.0,0.0]

Output: {"x_t":[[0.5,-0.5,1.0],[-0.3,0.8,0.4]],"target":[[0.5,1.5,-2.0],[-2.3,0.8,1.4]],"loss":2.5164,"per_sample_loss":[1.8425,3.1903]}

Input: x_data = [[1.0],[0.0],[-2.0]], x_noise = [[0.0],[1.0],[0.5]], t = [0.1,0.5,0.9], weight = [[0.0]], time_weight = [2.0], bias = [-1.0]

Output: {"x_t":[[0.9],[0.5],[0.25]],"target":[[-1.0],[1.0],[2.5]],"loss":1.31,"per_sample_loss":[0.04,1.0,2.89]}

Hint 1

t[:, None] turns the (B,) times into a (B, 1) column.

Hint 2

((a - b) ** 2).mean(dim=1) averages a squared error over the features of each row.

Requirements

  • Use PyTorch tensor operations.
  • Evaluate the velocity model at the interpolated point x_t, multiplying the row vector by weight on the right.
  • Leave all input tensors unchanged.

Constraints

  • x_data and x_noise are floating-point tensors of shape (B, D) with B between 1 and 4 and D between 1 and 5.
  • t is a tensor of shape (B,) with every value in [0, 1], and the endpoints 0 and 1 can occur.
  • weight has shape (D, D), and time_weight and bias have shape (D,).
  • All tensors share one dtype, float32 or float64, and all values are finite.
Try Similar Problems
Denoising Score Matching LossMediumMin-SNR Loss WeightingMediumAnnealed Langevin DynamicsMediumNoise Prediction LossEasyGaussian Mixture ScoreMediumCosine Noise ScheduleMediumRescale to Zero Terminal SNRMediumTime-Conditioned Residual BlockMediumHigher-Order Derivatives & Loss CurvatureMediumLoss FunctionsMediumBradley-Terry Reward Model LossMediumREINFORCE with BaselineMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array

You must run your code first.

Conditional Flow Matching Loss

Diffusion ModelsFlow MatchingLoss Functions
Medium

Compute the conditional flow matching loss of a small linear velocity model on straight paths between data and noise.

xt=(1−t) xdata+t xnoise,u=xnoise−xdata,v=xtW+t wtime+b,ℓ=1D∑d=1D(vd−ud)2x_t = (1 - t)\, x_{\text{data}} + t\, x_{\text{noise}}, \qquad u = x_{\text{noise}} - x_{\text{data}}, \qquad v = x_t W + t\, w_{\text{time}} + b, \qquad \ell = \frac{1}{D} \sum_{d=1}^{D} (v_d - u_d)^2xt​=(1−t)xdata​+txnoise​,u=xnoise​−xdata​,v=xt​W+twtime​+b,ℓ=D1​d=1∑D​(vd​−ud​)2

Here x_data holds the data samples xdatax_{\text{data}}xdata​ with shape (B, D), x_noise holds the paired Gaussian noise samples xnoisex_{\text{noise}}xnoise​ with the same shape, and t holds one time ttt in [0, 1] per sample with shape (B,). Time 0 is the data end of the path and time 1 is the noise end. The target velocity uuu is the constant speed of the straight path from data to noise. The toy velocity model multiplies xtx_txt​ by weight WWW (shape (D, D)), adds the per-sample time times time_weight wtimew_{\text{time}}wtime​ (shape (D,)) and adds bias bbb (shape (D,)). The per-sample loss ℓ\ellℓ is the mean squared error over the D features, and the batch loss is the mean of ℓ\ellℓ over the B samples.

Return a dict with the keys "x_t" and "target" holding PyTorch tensors of shape (B, D), "per_sample_loss" holding a tensor of shape (B,), all with the dtype and device of x_data, and "loss" holding the batch loss as a Python float.

Examples

Input: x_data = [[1.0,2.0],[-1.0,0.5]], x_noise = [[0.2,-0.4],[0.6,1.0]], t = [0.25,0.75], weight = [[0.5,-0.1],[0.2,0.3]], time_weight = [1.0,-0.5], bias = [0.0,0.1]

Output: {"x_t":[[0.8,1.4],[0.2,0.875]],"target":[[-0.8,-2.4],[1.6,0.5]],"loss":2.744577,"per_sample_loss":[5.182063,0.307091]}

Explanation: The first sample sits at t = 0.25, so x_t = 0.75 * (1, 2) + 0.25 * (0.2, -0.4) = (0.8, 1.4) and its target velocity is (0.2, -0.4) - (1, 2) = (-0.8, -2.4). The model predicts (0.93, 0.315), which gives a per-sample loss of 5.1821.

Input: x_data = [[0.5,-0.5,1.0],[2.0,0.0,-1.0]], x_noise = [[1.0,1.0,-1.0],[-0.3,0.8,0.4]], t = [0.0,1.0], weight = [[0.1,0.0,0.2],[0.0,-0.3,0.1],[0.4,0.2,0.0]], time_weight = [0.5,0.5,0.5], bias = [0.0,0.0,0.0]

Output: {"x_t":[[0.5,-0.5,1.0],[-0.3,0.8,0.4]],"target":[[0.5,1.5,-2.0],[-2.3,0.8,1.4]],"loss":2.5164,"per_sample_loss":[1.8425,3.1903]}

Input: x_data = [[1.0],[0.0],[-2.0]], x_noise = [[0.0],[1.0],[0.5]], t = [0.1,0.5,0.9], weight = [[0.0]], time_weight = [2.0], bias = [-1.0]

Output: {"x_t":[[0.9],[0.5],[0.25]],"target":[[-1.0],[1.0],[2.5]],"loss":1.31,"per_sample_loss":[0.04,1.0,2.89]}

Hint 1

t[:, None] turns the (B,) times into a (B, 1) column.

Hint 2

((a - b) ** 2).mean(dim=1) averages a squared error over the features of each row.

Requirements

  • Use PyTorch tensor operations.
  • Evaluate the velocity model at the interpolated point x_t, multiplying the row vector by weight on the right.
  • Leave all input tensors unchanged.

Constraints

  • x_data and x_noise are floating-point tensors of shape (B, D) with B between 1 and 4 and D between 1 and 5.
  • t is a tensor of shape (B,) with every value in [0, 1], and the endpoints 0 and 1 can occur.
  • weight has shape (D, D), and time_weight and bias have shape (D,).
  • All tensors share one dtype, float32 or float64, and all values are finite.
Try Similar Problems
Denoising Score Matching LossMediumMin-SNR Loss WeightingMediumAnnealed Langevin DynamicsMediumNoise Prediction LossEasyGaussian Mixture ScoreMediumCosine Noise ScheduleMediumRescale to Zero Terminal SNRMediumTime-Conditioned Residual BlockMediumHigher-Order Derivatives & Loss CurvatureMediumLoss FunctionsMediumBradley-Terry Reward Model LossMediumREINFORCE with BaselineMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array

You must run your code first.
Problems
Loading...
1 / 1