Skip to content

← Generative Models step 1 of 11

Medium End-to-End

Autoencoder

An autoencoder maps an input to a latent representation, then maps that representation back to the input's shape. Its reconstruction loss measures how closely the output matches the original input. The input itself is the target, so no separate label array is needed.

Here, evaluate one encoder layer and one decoder layer using supplied parameters. Do not train the model or update its weights.

Your task

Implement autoencoder(x, W_e, b_e, W_d, b_d) for floating-point inputs:

  • x has shape (N, D): N examples, each with D features.
  • W_e has shape (D, H) and b_e has shape (H,).
  • W_d has shape (H, D) and b_d has shape (D,).

All dimensions are positive. H is the latent width; it need not be smaller than D. Compute:

latent = ReLU(x @ W_e + b_e)
reconstruction = latent @ W_d + b_d
loss = mean((x - reconstruction)**2)

ReLU replaces negative encoder outputs with zero. The decoder is linear: apply no ReLU, sigmoid or other activation to its output. Reconstructions may contain negative values. Use W_d as supplied; it is not necessarily the transpose of W_e.

Return a dictionary with these keys:

  • "latent": a tensor of shape (N, H).
  • "reconstruction": a tensor of shape (N, D).
  • "loss": a Python float, averaged over all N * D squared errors.

Preserve all inputs. Do not sum per-example errors without also dividing by D, and do not take a square root.

Example

For x = [[1, 2]], W_e = [[1], [1]] and b_e = [0], the latent representation is [[3]]. With W_d = [[1, 1]] and b_d = [0, 0], the reconstruction is [[3, 3]]. The squared errors are [4, 1], giving a loss of 2.5.

A smaller latent width provides a bottleneck, but it does not by itself guarantee useful features or accurate reconstruction. Those depend on the parameters and how the model is trained.