# Why Do Neural Nets Need Non-Linearity?

> Why stacked linear layers are not enough, what an activation function does, and how ReLU, sigmoid and tanh differ, explained for your AI interview.

**In short:** Without a non-linear step between layers, a stack of layers is just one linear formula, so extra depth adds nothing. An activation function such as ReLU adds the bend that lets a network learn curves, thresholds and combinations of inputs.

AI and LLM interview question Q440, Classic ML and deep-learning basics. Beginner, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/why-do-neural-nets-need-non-linearity

## Say it in 30 seconds

Without a non-linear step between layers, stacking layers in a neural network adds no real power. Two linear layers collapse into one linear formula. An activation function, such as ReLU, adds the bend that lets each layer build a new shape from the last. With that bend, the network can learn thresholds, curves and combinations of inputs.

## Say it in 2 minutes

Each neuron does a simple job. It multiplies its inputs by weights, adds them up and adds a bias. That is a linear calculation, a straight-line rule. If every layer did only that, each layer would feed a straight-line rule into the next one, and the whole network would still be one linear formula. Google's machine learning course makes exactly this point.

The fix is a non-linear activation function between the layers. ReLU is the usual starting point: a negative value becomes zero and a positive value passes through. That bend changes the shape the next layer sees, and stacking bends lets the network model complicated relationships between inputs and outputs.

Here is a real case. A card fraud model should keep its risk score low for normal spending, then climb sharply after a burst of fast transactions. A linear rule averages that into one slope and misses the threshold. With a bend between the layers, the network can follow it. Sigmoid squashes outputs into 0 to 1, which suits a probability at the end. ReLU is cheap to compute and less prone to the vanishing gradient problem, where the training signal fades to almost nothing in deep stacks. So Google's course recommends starting with it.

So the honest answer is that depth only pays off with a bend. Without one, a deep network has the same reach as a single layer.

## What is non-linearity, in plain words?

A linear rule is a straight line: change the input by a fixed amount and the output changes by a fixed amount too. A neuron in a network does a linear calculation. It multiplies each input by a weight, adds them up and adds a bias.

Non-linear means the output does not follow a straight line. It can flatten, bend, or switch on past a threshold. The step that adds this is an activation function, applied to each neuron's result before it goes to the next layer.

Think of a credit analyst adding points on a scoring form. Adding points at every stage is still just adding, so the whole form is one big sum. A rule that drops every negative score changes the shape of the total, and later stages can react to that new shape. In a network, the activation function plays the part of that rule.

- **Linear calculation:** Multiply each input by a weight, add them up, add a bias. A straight-line rule.
- **Weight:** A number the training adjusts to set how much each input counts.
- **Bias:** A number added after the sum, which shifts the result.
- **Activation function:** The non-linear step applied to each neuron's sum before the next layer.
- **ReLU:** Keeps positive values and turns negative values into 0. Short for rectified linear unit.

## Why does stacking linear layers not help?

Take two linear layers. The first turns the input into a number, and the second turns that number into an output. Put the two formulas together and you get one linear formula with the same reach. So a network with no bends is no stronger than a single layer, however many layers you add.

*Diagram, Two paths: with and without a bend: Top path: two linear layers that give one linear formula out. Bottom path: a bend between the layers, so the output can flatten and then rise. Only the bottom path can fit a real threshold.*
1. The first layer's result feeds straight into the second.
2. Both layers together come out as one linear formula.
3. The first layer's result goes into the bend.
4. The bend passes the result on to the second layer.
5. Because of the bend, the output can flatten and then rise.

Here is the same idea in code. The functions below give a made-up risk score for x, a claim size. Run it and read the table.

```python
# Made-up claims-risk score for one input x (say, a claim size). The numbers are invented.
def layer1(x):
    return x - 2              # linear: shift the input

def layer2(h):
    return 3 * h + 1          # linear: scale and shift again

def one_layer(x):
    return 3 * x - 5          # the two layers above, merged into one

def relu(v):
    return max(0, v)          # non-linear: negative signals become 0

print(f"{'x':>3} {'two_layers':>10} {'one_layer':>10} {'with_relu':>10}")
for x in [0, 1, 2, 3, 4]:
    two = layer2(layer1(x))
    one = one_layer(x)
    bent = layer2(relu(layer1(x)))
    print(f"{x:>3} {two:>10} {one:>10} {bent:>10}")
```

Output:

```text
  x two_layers  one_layer  with_relu
  0         -5         -5          1
  1         -2         -2          1
  2          1          1          1
  3          4          4          4
  4          7          7          7
```

The two_layers and one_layer columns match on every row, so the second layer added nothing. The with_relu column stays flat at 1 for small inputs, then rises, which a linear formula cannot do.

## What does the bend buy you in real work?

Real data is full of thresholds. In a card fraud model (made-up numbers), normal spending should barely move the risk score. The score should climb once a card is used more than three times in ten minutes. A linear rule averages that into one slope, so it misreads both the ordinary days and the burst. A bend lets the model stay flat, then rise at the threshold.

> "Linear calculations performed on the output of linear calculations are also linear, which means this model cannot learn nonlinearities." (Google, machine learning course (nodes and hidden layers))
>
> "Stacking nonlinearities on nonlinearities lets us model very complicated relationships between the inputs and the predicted outputs." (Google, machine learning course (activation functions))

- **Sigmoid: Squashes to 0 to 1.** Gives a probability-like output, so it suits the end of a model. Its training signal fades in deep stacks.
- **Tanh: Squashes to -1 to 1.** Its range is -1 to 1, so outputs can be negative. Like sigmoid, its training signal fades in deep stacks.
- **ReLU: Zero or the input.** Cheap to compute, and its signal fades less in deep stacks. Google's course still recommends starting with it.

> **Tip:** In an interview, say what the bend does to the shape the next layer sees. Skip the claim that the bend makes the model smarter on its own.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Without a bend, stacked linear layers collapse into one linear formula.
- [ ] An activation function such as ReLU adds the bend after each layer's sum.
- [ ] The bend lets the network learn thresholds, curves and interactions.
- [ ] The common choices: sigmoid, tanh and ReLU, and when each fits.
- [ ] A real case, such as a fraud burst that a straight line misses.
- [ ] Google's course recommends starting with ReLU; sigmoid suits a probability at the output.

**Common traps**

- Trap: "Adding more layers makes the network smarter by itself." Better: Depth only helps with a bend between layers. Without one, extra layers collapse into one.
- Trap: "Any activation works, so pick one at random." Better: Sigmoid and tanh are more prone to fading in deep stacks. Google's course recommends starting with ReLU.
- Trap: "Non-linear means the model is complicated." Better: It means the output does not follow a linear formula of the input. The bend is a small, cheap step.

## Can you spot the problem?

**Spot the collapse:** A junior engineer builds a three-layer churn model. Which snippet would collapse into one linear formula, so the depth adds nothing?

A. h1 = relu(w1 * x + b1); h2 = relu(w2 * h1 + b2); h3 = w3 * h2 + b3
B. h1 = w1 * x + b1; h2 = w2 * h1 + b2; h3 = w3 * h2 + b3
C. h1 = sigmoid(w1 * x + b1); h2 = tanh(w2 * h1 + b2); h3 = relu(w3 * h2 + b3)
D. h1 = max(0, w1 * x); h2 = w2 * h1; h3 = w3 * h2

**Answer:** B. The second snippet has no activation. Each layer is a weighted sum, so the three collapse into one linear formula. The other three each have a bend, so the network can learn shapes a straight line cannot.

**Quick check:** A fraud model's risk should stay flat for normal spending, then rise sharply after a burst. Which change makes that shape possible?

A. Add more linear layers with no activation
B. Add an activation function such as ReLU between the layers
C. Train for more epochs
D. Lower the learning rate

**Answer:** B. Only a bend between layers lets the output change shape. More linear layers still collapse into one linear formula, and training length or learning rate do not change what the layers can express.

## Can you answer these from memory?

**Flash cards**

- **Why do neural nets need non-linearity?** Without it, stacked layers collapse into one linear formula, so depth adds nothing.
- **What is an activation function?** The non-linear step applied to each neuron's sum before the next layer.
- **What does ReLU do?** Keeps positive values and turns negative values into 0.
- **What do stacked linear layers equal?** One linear layer: the same reach, no extra power.
- **Sigmoid's output range?** 0 to 1, so it suits a probability at the end of a model.
- **What does Google's course recommend starting with?** ReLU. It is cheap to compute, and its training signal fades less than sigmoid or tanh in deep stacks.

## Likely follow-up questions

- Q415 What is gradient descent? https://designervenkat.online/interview/ai/what-is-gradient-descent
- Q417 What does the learning rate do? https://designervenkat.online/interview/ai/what-does-the-learning-rate-do
- Q418 What is backpropagation? https://designervenkat.online/interview/ai/what-is-backpropagation
- Q437 Softmax vs sigmoid? https://designervenkat.online/interview/ai/softmax-vs-sigmoid
- Q438 Why does weight initialisation matter? https://designervenkat.online/interview/ai/why-does-weight-initialisation-matter

## Learn it in depth

- [Activation functions: the bends](https://designervenkat.online/learn/machine-learning/activation-functions)
