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,ℓ=21σ2d=1∑D(sd+σϵd)2Here x0 holds the clean samples x0 with shape (B, D), noise holds the standard Gaussian noise ϵ with the same shape, and sigmas holds one noise level σ per sample with shape (B,). The toy score model multiplies x~ by weight W (shape (D, D)), adds bias b (shape (D,)) and divides by σ to give the predicted score s. The target score is −ϵ/σ, and the weighting λ(σ)=σ2 multiplies half the squared error summed over the D features, which gives the per-sample loss ℓ. The batch loss is the mean of ℓ 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.
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]}
sigmas[:, None] gives a (B, 1) column that broadcasts over the features.
x @ weight + bias applies a linear model to every row at once.
.pow(2).sum(dim=1) gives one squared error per row, and .item() turns a one-element tensor into a Python float.
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
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,ℓ=21σ2d=1∑D(sd+σϵd)2Here x0 holds the clean samples x0 with shape (B, D), noise holds the standard Gaussian noise ϵ with the same shape, and sigmas holds one noise level σ per sample with shape (B,). The toy score model multiplies x~ by weight W (shape (D, D)), adds bias b (shape (D,)) and divides by σ to give the predicted score s. The target score is −ϵ/σ, and the weighting λ(σ)=σ2 multiplies half the squared error summed over the D features, which gives the per-sample loss ℓ. The batch loss is the mean of ℓ 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.
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]}
sigmas[:, None] gives a (B, 1) column that broadcasts over the features.
x @ weight + bias applies a linear model to every row at once.
.pow(2).sum(dim=1) gives one squared error per row, and .item() turns a one-element tensor into a Python float.
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array