v9.1.1
Menu

Autoencoder Bottleneck

easyFREEGenerative ModelsMachine Learning 2

Implement the forward pass of a simple autoencoder with one hidden (bottleneck) layer.

An autoencoder has:

  • Encoder: z=ReLU(We⋅x+be)z = \text{ReLU}(W_e \cdot x + b_e) — compress input to bottleneck
  • Decoder: x^=Wd⋅z+bd\hat{x} = W_d \cdot z + b_d — reconstruct from bottleneck

Given input xx, encoder weights/bias, and decoder weights/bias, compute both the bottleneck representation zz and the reconstruction x^\hat{x}.

Return a tuple (z, x_hat) both as lists rounded to 4 decimal places.

Example

Input
x = [1, 2, 3]
W_e = [[0.5, 0.5, 0.5]]
b_e = [0]
W_d = [[1], [1], [1]]
b_d = [0, 0, 0]
Output
([3.0], [3.0, 3.0, 3.0])
Reasoning
  • Inputs: x=[1,2,3]x = [1, 2, 3] has din=3d_{in} = 3 features. WeW_e is a 1×31 \times 3 matrix, so the bottleneck has a single unit (dhidden=1d_{hidden} = 1) and be=[0]b_e = [0]. WdW_d is 3×13 \times 1: it maps that one unit back to 3 features, with bd=[0,0,0]b_d = [0, 0, 0].
  • Encoder: the pre-activation is 0.5⋅1+0.5⋅2+0.5⋅3+0=0.5+1+1.5=30.5 \cdot 1 + 0.5 \cdot 2 + 0.5 \cdot 3 + 0 = 0.5 + 1 + 1.5 = 3. ReLU keeps positive values unchanged, max⁡(0,3)=3\max(0, 3) = 3, so zz = [3.0].
  • Decoder: each row of WdW_d holds a single weight 11, so x^i=1⋅3+0=3\hat{x}_i = 1 \cdot 3 + 0 = 3 for i=1,2,3i = 1, 2, 3, giving [3.0, 3.0, 3.0]. The decoder has no ReLU.
  • Output: the tuple (z,x^)(z, \hat{x}) with lengths dhiddend_{hidden} and dind_{in}, every entry rounded to 4 decimal places.
  • Notice: the reconstruction is not [1,2,3][1, 2, 3]. One number can only remember a weighted total of the input, so the differences between features are lost. A negative pre-activation would make that unit 00.

Constraints

  • x: 1D list (d_in)
  • W_e: 2D list (d_hidden x d_in), b_e: 1D list (d_hidden)
  • W_d: 2D list (d_in x d_hidden), b_d: 1D list (d_in)
  • Return (z, x_hat) both rounded to 4 decimal places

Learn this in

More from Machine Learning 2

solution.py

Test results

Sign in to run your code against the test cases.