#173Why nonlinearity — collapsing two linear layersMediumNeural NetworksActivation FunctionsLinear Algebra
Why nonlinearity — collapsing two linear layers
Background
The headline argument for activations: two stacked linear layers are just one linear layer. With no nonlinearity between them,
so the pair is equivalent to a single layer with weights and bias . Depth without nonlinearity buys nothing.
Problem statement
Implement collapse(W1, b1, W2, b2) returning the single equivalent layer (W, b).
Input
W1— shape(h, i),b1— lengthh(first layer).W2— shape(o, h),b2— lengtho(second layer).
Output
Returns a tuple (W, b) where W is shape (o, i) and b is shape (o,), such that W @ x + b equals applying both linear layers in sequence.
Examples
Example 1
Input: W1 = [[1, 2], [0, 1]], b1 = [1, 0], W2 = [[1, 0], [1, 1]], b2 = [0, 1]
Output: W = [[1, 2], [1, 3]], b = [1, 2]
Explanation: and .
Constraints
- (matrix product) and .
- Return
Wof shape(o, i)andbof shape(o,).
Notes
- This is exactly why hidden layers need a nonlinear activation: insert a ReLU/sigmoid between the layers and this collapse no longer holds, so depth starts to add real expressive power.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •worked example
- •reference rectangular weight
- •sample identity collapse