All notes

Neural networks: perceptron, MLP and backpropagation

GATE DA150 min read

Free preview: 30% of the note

The single neuron

Definition. A neuron computes a weighted sum of its inputs plus a bias, then applies an activation function gg:

z=w⊤x+b=∑iwixi+b,a=g(z).z = w^\top x + b = \sum_i w_i x_i + b, \qquad a = g(z).

  • With g(z)=zg(z) = z the neuron is linear regression.
  • With g=σg = \sigma (sigmoid) and cross-entropy loss it is logistic regression.
  • With a step function it is the perceptron.

So the familiar linear models are one-neuron networks. Neural networks gain power by stacking many neurons with non-linear activations.

Geometry. The set w⊤x+b=0w^\top x + b = 0 is a hyperplane. The vector ww is perpendicular to it, and the distance from the origin to the plane is ∣b∣/∥w∥\lvert b\rvert / \lVert w\rVert. A single neuron with a step or sigmoid activation can therefore only separate classes with a straight boundary.

The perceptron and its learning rule

Model. y^=1\hat y = 1 if w⊤x+b>0w^\top x + b > 0, otherwise y^=0\hat y = 0.

Learning rule (for one example at a time, learning rate η\eta):

w←w+η (y−y^) x,b←b+η (y−y^).w \leftarrow w + \eta\,(y - \hat y)\,x, \qquad b \leftarrow b + \eta\,(y - \hat y).

When the prediction is right, y−y^=0y - \hat y = 0 and nothing changes. A missed positive adds xx to ww; a false positive subtracts it.

Perceptron convergence theorem. If the data is linearly separable, the rule reaches a separating hyperplane after finitely many updates. Suppose every point satisfies ∥x∥≤R\lVert x\rVert \le R and some unit vector separates the data with margin γ\gamma. Then the number of mistakes is at most (R/γ)2(R/\gamma)^2. If the data is not separable, the rule never settles.

Solved example: learning AND

Example 1. Train on AND with w=(0,0)w = (0, 0), b=0b = 0 and η=1\eta = 1, visiting the points in the order (0,0),(0,1),(1,0),(1,1)(0,0), (0,1), (1,0), (1,1). The target is 1 only for (1,1)(1,1). Each row shows the point, the score zz, the prediction, and the weights after the step.

EpochPointzzy^\hat yyyNew wwNew bb
1(1,1)001(1, 1)1
2(0,0)110(1, 1)0
2(0,1)110(1, 0)−1
2(1,1)001(2, 1)0
3(0,1)110(2, 0)−1
3(1,0)110(1, 0)−2
3(1,1)−101(2, 1)−1
4(1,0)110(1, 1)−2
4(1,1)001(2, 2)−1
5(0,1)110(2, 1)−2

The table lists only the steps that made a mistake. In epoch 6 every point is correct: the scores are −2,−1,0,1-2, -1, 0, 1, so only (1,1)(1,1) has z>0z > 0. The perceptron has learned AND with w=(2,1)w = (2, 1) and b=−2b = -2.

What one perceptron cannot do

XOR outputs 1 for (0,1)(0,1) and (1,0)(1,0) only. Suppose a line separated it. Then:

  • b≤0b \le 0, from (0,0)(0,0);
  • w2+b>0w_2 + b > 0 and w1+b>0w_1 + b > 0;
  • w1+w2+b≤0w_1 + w_2 + b \le 0.

Adding the two middle inequalities gives w1+w2+2b>0w_1 + w_2 + 2b > 0. So w1+w2+b>−b≥0w_1 + w_2 + b > -b \ge 0, which contradicts the last line. No single perceptron computes XOR. This was the classic argument (Minsky and Papert, 1969) that motivated hidden layers.

Activation functions

Nameg(z)g(z)g′(z)g'(z)RangeNotes
Step1 if z>0z > 0, else 00 (undefined at 0){0, 1}no useful gradient
Sigmoidσ(z)=11+e−z\sigma(z) = \frac{1}{1 + e^{-z}}σ(z)(1−σ(z))\sigma(z)(1 - \sigma(z))(0, 1)max slope 0.25 at z=0z = 0
Tanhez−e−zez+e−z\frac{e^z - e^{-z}}{e^z + e^{-z}}1−tanh⁡2z1 - \tanh^2 z(−1, 1)zero-centred; slope 1 at 0
ReLUmax⁡(0,z)\max(0, z)1 if z>0z > 0, 0 if z<0z < 0[0,∞)[0, \infty)cheap; can "die"
Leaky ReLUzz if z>0z > 0, else αz\alpha z1 or α\alphaR\mathbb{R}keeps a small gradient
Linearzz1R\mathbb{R}used at regression outputs

Useful identities.

  • σ(−z)=1−σ(z)\sigma(-z) = 1 - \sigma(z).
  • tanh⁡(z)=2σ(2z)−1\tanh(z) = 2\sigma(2z) - 1.
  • σ(0)=0.5\sigma(0) = 0.5 and σ′(0)=0.25\sigma'(0) = 0.25.
  • tanh⁡(0)=0\tanh(0) = 0 and tanh⁡′(0)=1\tanh'(0) = 1.

Example 2: derivative values. At z=0.5z = 0.5: σ(0.5)≈0.6225\sigma(0.5) \approx 0.6225, so σ′(0.5)≈0.6225×0.3775≈0.235\sigma'(0.5) \approx 0.6225 \times 0.3775 \approx 0.235. At z=4z = 4: σ(4)≈0.982\sigma(4) \approx 0.982, so σ′(4)≈0.018\sigma'(4) \approx 0.018. A saturated sigmoid passes back almost no gradient.

Dying ReLU. If a ReLU unit's input is negative for every training example, its gradient is always 0 and it never recovers. Leaky ReLU and careful learning rates prevent this.

Output layers and losses

TaskOutput activationLoss
Regressionlinearsquared error 12(y^−y)2\frac12(\hat y - y)^2
Binary classificationsigmoidbinary cross-entropy −[yln⁡y^+(1−y)ln⁡(1−y^)]-[y\ln\hat y + (1 - y)\ln(1 - \hat y)]
KK-class classificationsoftmaxcross-entropy −ln⁡y^true-\ln \hat y_{\text{true}}

Softmax. softmax⁡(z)k=ezk∑jezj\operatorname{softmax}(z)_k = \dfrac{e^{z_k}}{\sum_j e^{z_j}}. The outputs are positive and sum to 1. Adding the same constant to every zkz_k leaves them unchanged, and implementations subtract max⁡kzk\max_k z_k for numerical safety.

You have read the free 30% of this note

Sign in with Google to keep reading. It is free, takes one tap, and you also get:

  • 8 practice questions from this note
  • Every note in full, plus the PDF library
  • Official past papers as timed tests, with explanations
  • An AI tutor for doubts, in Hindi or English
  • Your progress, mistakes and study plan saved on every device
Continue reading, free