Autoencoder forward pass
Background
An autoencoder learns to compress and reconstruct its own input. An encoder maps the input down to a low-dimensional latent code (the bottleneck), and a decoder maps back up to a reconstruction . 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:
Return the tuple (reconstruction, latent) = .
Input
x— input vector, shape(n,).W_enc,b_enc— encoder weights(d, n)and bias(d,), whered < nis the latent size.W_dec,b_dec— decoder weights(n, d)and bias(n,).
Output
A tuple (reconstruction, latent):
reconstruction—np.ndarrayof shape(n,), the decoded .latent—np.ndarrayof shape(d,), the bottleneck code .
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 to ; the decoder broadcasts that single number back to both outputs, giving the (lossy) reconstruction .
Constraints
- Apply ReLU only in the encoder; the decoder is linear (no activation).
- Latent dimension is
W_enc.shape[0]; reconstruction dimension isW_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 (e.g. pixels) a sigmoid decoder is common; here the decoder is linear so the reconstruction can take any real value.
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