MLP Forward Pass
Implement the forward pass of a two-layer Multi-Layer Perceptron (MLP).
Given input , two weight matrices , and two bias vectors , :
- Hidden layer:
- Output layer:
Where applied element-wise.
Return the output vector, rounded to 4 decimal places.
Example
x = [1, 2] W1 = [[0.5, 0.3], [-0.2, 0.8]] b1 = [0.1, -0.1] W2 = [[0.4, -0.5]] b2 = [0.2]
[0.03]
- Inputs: is a vector with features. is , so the hidden layer has units; is , so there is a single output ().
- Hidden pre-activations: each hidden unit takes the dot product of its row of with , plus its bias. Unit 1: . Unit 2: .
- ReLU: both values are positive, so passes through unchanged.
- Output layer: , with no activation applied.
- Result: one output unit gives a length-1 list,
[0.03]after rounding to 4 decimal places. - Notice that ReLU only matters when a pre-activation is negative: with and , the first hidden unit is and gets zeroed, so only the second unit (value ) reaches the output.
Constraints
- x: 1D list (input vector, d_in features)
- W1: 2D list (d_hidden x d_in), b1: 1D list (d_hidden)
- W2: 2D list (d_out x d_hidden), b2: 1D list (d_out)
- Return 1D list of output values rounded to 4 decimal places