Score a denoising autoencoder against the clean input
A denoising autoencoder is fed a corrupted input and asked to rebuild the original. The narrow hidden layer forces it to learn structure instead of copying pixels.
Write dae_loss(x, mask, W1, b1, W2, b2). x is the clean batch (B, D) with values in [0, 1]. mask is (B, D) of 0s and 1s (0 means the value was dropped). W1 is (D, k), b1 is (k,), W2 is (k, D), b2 is (D,).
- corrupted = x * mask
- h = relu(corrupted @ W1 + b1)
- x_hat = sigmoid(h @ W2 + b2)
Return the mean squared error between x_hat and the CLEAN x, averaged over all B*D entries, as a float.
def dae_loss(x, mask, W1, b1, W2, b2)Example
with all weights and biases zero, x_hat is 0.5 everywhere, so x = [[1, 0]] gives a loss of 0.25.
Understand the idea
A denoising autoencoder is shown a damaged copy of the input and must rebuild the original, like restoring a photo with scratches. Because it never sees the clean version at the input, it has to learn real structure rather than copy pixels. The key rule is that the loss compares the output with the CLEAN data, not the damaged copy. Worked example: x = [[1, 1]], mask = [[1, 0]], so the corrupted input is [[1, 0]]. Suppose all weights are zero and b2 = [2, 2], so x_hat = sigmoid(2) = 0.8808 everywhere. Against clean x: each error is (1 - 0.8808)^2 = 0.0142, mean 0.0142. Against the corrupted input you would get (0.0142 + 0.7758) / 2 = 0.3950, a different and wrong target. Rule: corrupted = x * mask, h = relu(corrupted W1 + b1), x_hat = sigmoid(h W2 + b2), loss = mean of (x_hat - x)^2 over all B * D entries. Denoising objectives are the idea behind masked autoencoders and BERT-style masked pretraining.