Autoencoder forward passEasy

Autoencoder forward pass

Background

An autoencoder learns to compress and reconstruct its own input. An encoder maps the input xx down to a low-dimensional latent code zz (the bottleneck), and a decoder maps zz back up to a reconstruction x^\hat x. Because the latent layer is smaller than the input, the network is forced to keep only the most informative structure — making autoencoders useful for dimensionality reduction, denoising, and representation learning.

Problem statement

Implement autoencoder_forward(x, W_enc, b_enc, W_dec, b_dec) for one forward pass with a ReLU encoder and a linear decoder:

z=ReLU(Wencx+benc)z = \operatorname{ReLU}(W_{\text{enc}}\, x + b_{\text{enc}}) x^=Wdecz+bdec\hat x = W_{\text{dec}}\, z + b_{\text{dec}}

Return the tuple (reconstruction, latent) = (x^,z)(\hat x, z).

Input

  • x — input vector, shape (n,).
  • W_enc, b_enc — encoder weights (d, n) and bias (d,), where d < n is the latent size.
  • W_dec, b_dec — decoder weights (n, d) and bias (n,).

Output

A tuple (reconstruction, latent):

  • reconstructionnp.ndarray of shape (n,), the decoded x^\hat x.
  • latentnp.ndarray of shape (d,), the bottleneck code zz.

Examples

Example 1

Input:  x = [1.0, 2.0]
        W_enc = [[0.5, 0.5]], b_enc = [0.0]      # latent size 1
        W_dec = [[1.0], [1.0]], b_dec = [0.0, 0.0]
Output: reconstruction = [1.5, 1.5], latent = [1.5]

Explanation: the encoder maps [1,2][1,2] to z=ReLU(0.51+0.52)=1.5z=\operatorname{ReLU}(0.5\cdot1+0.5\cdot2)=1.5; the decoder broadcasts that single number back to both outputs, giving the (lossy) reconstruction [1.5,1.5][1.5,1.5].

Constraints

  • Apply ReLU only in the encoder; the decoder is linear (no activation).
  • Latent dimension is W_enc.shape[0]; reconstruction dimension is W_dec.shape[0] (= n).
  • Return (reconstruction, latent) in that order.

Notes

  • The bottleneck being smaller than the input is what makes the autoencoder learn — an identity-sized latent could just copy the input.
  • For data normalized to [0,1][0,1] (e.g. pixels) a sigmoid decoder is common; here the decoder is linear so the reconstruction can take any real value.
Python
Loading...

This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.

  • Reference example
  • Identity weights give perfect reconstruction
  • Encoder ReLU clamps negative pre-activations
  • Latent and reconstruction have the expected shapes