TensorTonic
Study Plans
Study Plan
Denoising Score Matching Loss
Diffusion ModelsScore MatchingLoss Functions
Medium

Compute the noise-weighted denoising score matching loss of a small linear score model for a batch of clean samples.

x~=x0+σϵ,s=x~W+bσ,ℓ=12 σ2∑d=1D(sd+ϵdσ)2\tilde x = x_0 + \sigma \epsilon, \qquad s = \frac{\tilde x W + b}{\sigma}, \qquad \ell = \frac{1}{2}\, \sigma^2 \sum_{d=1}^{D} \left( s_d + \frac{\epsilon_d}{\sigma} \right)^2x~=x0​+σϵ,s=σx~W+b​,ℓ=21​σ2d=1∑D​(sd​+σϵd​​)2

Here x0 holds the clean samples x0x_0x0​ with shape (B, D), noise holds the standard Gaussian noise ϵ\epsilonϵ with the same shape, and sigmas holds one noise level σ\sigmaσ per sample with shape (B,). The toy score model multiplies x~\tilde xx~ by weight WWW (shape (D, D)), adds bias bbb (shape (D,)) and divides by σ\sigmaσ to give the predicted score sss. The target score is −ϵ/σ-\epsilon / \sigma−ϵ/σ, and the weighting λ(σ)=σ2\lambda(\sigma) = \sigma^2λ(σ)=σ2 multiplies half the squared error summed over the D features, which gives the per-sample loss ℓ\ellℓ. The batch loss is the mean of ℓ\ellℓ over the B samples.

Return a dict with exactly the keys "loss" and "per_sample_loss", where "loss" holds the batch loss as a Python float and "per_sample_loss" holds a PyTorch tensor of shape (B,) with the dtype and device of x0.

Examples

Input: x0 = [[1.0,-0.5],[0.2,0.8]], noise = [[0.4,-1.2],[1.0,0.5]], sigmas = [0.5,2.0], weight = [[0.5,0.1],[-0.2,0.3]], bias = [0.1,-0.1]

Output: {"loss":2.188425,"per_sample_loss":[2.01125,2.3656]}

Explanation: The first sample is noised to (1.2, -1.1) with sigma 0.5, its target score is (-0.8, 2.4) and the model predicts (1.84, -0.62), giving a weighted loss of 2.0112. The batch loss is the mean of 2.0112 and 2.3656.

Input: x0 = [[0.3,-0.7,1.5]], noise = [[1.0,-2.0,0.5]], sigmas = [10.0], weight = [[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]], bias = [0.0,0.0,0.0]

Output: {"loss":2.625,"per_sample_loss":[2.625]}

Input: x0 = [[0.5],[-1.0],[2.0]], noise = [[1.0],[-0.5],[0.0]], sigmas = [0.01,1.0,25.0], weight = [[-0.9]], bias = [0.05]

Output: {"loss":0.70363,"per_sample_loss":[0.174641,0.405,1.53125]}

Hint 1

sigmas[:, None] gives a (B, 1) column that broadcasts over the features.

Hint 2

x @ weight + bias applies a linear model to every row at once.

Hint 3

.pow(2).sum(dim=1) gives one squared error per row, and .item() turns a one-element tensor into a Python float.

Requirements

  • Use PyTorch tensor operations.
  • Use each sample's own sigma for its noisy input, its score, its target and its weight σ2\sigma^2σ2.
  • Multiply the row vector x~\tilde xx~ by weight on the right, so the output is x~W\tilde x Wx~W and never x~W⊤\tilde x W^\topx~W⊤.
  • Leave all input tensors unchanged.

Constraints

  • x0 and noise are floating-point tensors of shape (B, D) with B between 1 and 4 and D between 1 and 5.
  • sigmas is a tensor of shape (B,) with every value strictly positive, between 0.01 and 50.
  • weight has shape (D, D) and bias has shape (D,).
  • All tensors share one dtype, float32 or float64, and all values are finite.
Try Similar Problems
Annealed Langevin DynamicsMediumConditional Flow Matching LossMediumGaussian Mixture ScoreMediumMin-SNR Loss WeightingMediumNoise Prediction LossEasyCosine 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

You must run your code first.

Denoising Score Matching Loss

Diffusion ModelsScore MatchingLoss Functions
Medium

Compute the noise-weighted denoising score matching loss of a small linear score model for a batch of clean samples.

x~=x0+σϵ,s=x~W+bσ,ℓ=12 σ2∑d=1D(sd+ϵdσ)2\tilde x = x_0 + \sigma \epsilon, \qquad s = \frac{\tilde x W + b}{\sigma}, \qquad \ell = \frac{1}{2}\, \sigma^2 \sum_{d=1}^{D} \left( s_d + \frac{\epsilon_d}{\sigma} \right)^2x~=x0​+σϵ,s=σx~W+b​,ℓ=21​σ2d=1∑D​(sd​+σϵd​​)2

Here x0 holds the clean samples x0x_0x0​ with shape (B, D), noise holds the standard Gaussian noise ϵ\epsilonϵ with the same shape, and sigmas holds one noise level σ\sigmaσ per sample with shape (B,). The toy score model multiplies x~\tilde xx~ by weight WWW (shape (D, D)), adds bias bbb (shape (D,)) and divides by σ\sigmaσ to give the predicted score sss. The target score is −ϵ/σ-\epsilon / \sigma−ϵ/σ, and the weighting λ(σ)=σ2\lambda(\sigma) = \sigma^2λ(σ)=σ2 multiplies half the squared error summed over the D features, which gives the per-sample loss ℓ\ellℓ. The batch loss is the mean of ℓ\ellℓ over the B samples.

Return a dict with exactly the keys "loss" and "per_sample_loss", where "loss" holds the batch loss as a Python float and "per_sample_loss" holds a PyTorch tensor of shape (B,) with the dtype and device of x0.

Examples

Input: x0 = [[1.0,-0.5],[0.2,0.8]], noise = [[0.4,-1.2],[1.0,0.5]], sigmas = [0.5,2.0], weight = [[0.5,0.1],[-0.2,0.3]], bias = [0.1,-0.1]

Output: {"loss":2.188425,"per_sample_loss":[2.01125,2.3656]}

Explanation: The first sample is noised to (1.2, -1.1) with sigma 0.5, its target score is (-0.8, 2.4) and the model predicts (1.84, -0.62), giving a weighted loss of 2.0112. The batch loss is the mean of 2.0112 and 2.3656.

Input: x0 = [[0.3,-0.7,1.5]], noise = [[1.0,-2.0,0.5]], sigmas = [10.0], weight = [[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]], bias = [0.0,0.0,0.0]

Output: {"loss":2.625,"per_sample_loss":[2.625]}

Input: x0 = [[0.5],[-1.0],[2.0]], noise = [[1.0],[-0.5],[0.0]], sigmas = [0.01,1.0,25.0], weight = [[-0.9]], bias = [0.05]

Output: {"loss":0.70363,"per_sample_loss":[0.174641,0.405,1.53125]}

Hint 1

sigmas[:, None] gives a (B, 1) column that broadcasts over the features.

Hint 2

x @ weight + bias applies a linear model to every row at once.

Hint 3

.pow(2).sum(dim=1) gives one squared error per row, and .item() turns a one-element tensor into a Python float.

Requirements

  • Use PyTorch tensor operations.
  • Use each sample's own sigma for its noisy input, its score, its target and its weight σ2\sigma^2σ2.
  • Multiply the row vector x~\tilde xx~ by weight on the right, so the output is x~W\tilde x Wx~W and never x~W⊤\tilde x W^\topx~W⊤.
  • Leave all input tensors unchanged.

Constraints

  • x0 and noise are floating-point tensors of shape (B, D) with B between 1 and 4 and D between 1 and 5.
  • sigmas is a tensor of shape (B,) with every value strictly positive, between 0.01 and 50.
  • weight has shape (D, D) and bias has shape (D,).
  • All tensors share one dtype, float32 or float64, and all values are finite.
Try Similar Problems
Annealed Langevin DynamicsMediumConditional Flow Matching LossMediumGaussian Mixture ScoreMediumMin-SNR Loss WeightingMediumNoise Prediction LossEasyCosine 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

You must run your code first.