๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #55

Multi-Layer Perceptron

The Multi-Layer Perceptron (MLP) stacks several layers of neurons โ€” with non-linear activations between them โ€” to represent functions a single Perceptron fundamentally cannot, including XOR. It is the foundational architecture every more specialized network (CNNs, Transformers) builds on.

Structure

\[ \mathbf{h}_1 = \phi(\mathbf{W}_1\mathbf{x} + \mathbf{b}_1), \qquad \mathbf{h}_2 = \phi(\mathbf{W}_2\mathbf{h}_1 + \mathbf{b}_2), \qquad \mathbf{y} = \psi(\mathbf{W}_3\mathbf{h}_2+\mathbf{b}_3) \]

Each layer takes the previous layer's output as its input, applies a matrix multiplication (weights) and bias (see Matrix Multiplication), then a non-linear activation \(\phi\). The final layer typically uses a different activation \(\psi\) suited to the task (e.g. softmax for classification). Layers between the input and output are called hidden layers because their values aren't directly observed โ€” they're intermediate representations the network builds for itself.

Solving XOR with an MLP

A well-known hand-constructed solution uses 2 hidden neurons: hidden neuron 1 computes OR, hidden neuron 2 computes NAND, and the output neuron computes AND of those two hidden outputs. The combination (\(A \text{ OR } B\)) AND (\(A \text{ NAND } B\)) is exactly XOR โ€” the hidden layer built an intermediate representation where the problem becomes linearly separable, even though it wasn't in the raw input space.

Diagram โ€” MLP Architecture

Input Hidden Output

Every neuron in one layer connects to every neuron in the next ("fully connected") โ€” each connection has its own learnable weight.

Code

import torch
import torch.nn as nn

class MLP(nn.Module):
    def __init__(self):
        super().__init__()
        self.hidden = nn.Linear(2, 3)   # input layer -> hidden layer (2 inputs, 3 hidden neurons)
        self.output = nn.Linear(3, 1)   # hidden layer -> output layer

    def forward(self, x):
        h = torch.relu(self.hidden(x))     # hidden layer + non-linear activation
        y = torch.sigmoid(self.output(h))   # output layer + sigmoid for binary classification
        return y

model = MLP()
x = torch.tensor([[0.0, 1.0]])
print(model(x))

Why Depth Matters โ€” Connecting Back to Linear Transformations

Recall from Linear Transformations: without a non-linear activation between layers, stacking matrix multiplications collapses into a single linear transformation, no matter how many layers you add. The activation functions between an MLP's layers are exactly what prevent that collapse โ€” they're what let a 2-hidden-layer MLP represent functions (like XOR) that no purely linear model, of any depth, ever could.

Common Mistakes

  • Assuming more hidden layers always improve performance โ€” beyond a task's actual complexity, extra depth mainly adds overfitting risk and training difficulty (vanishing gradients) without benefit.
  • Forgetting the output layer's activation should match the task: sigmoid for binary classification, softmax for multi-class, no activation (or identity) for unbounded regression outputs.

Interview Relevance

Q: "Why does adding a single hidden layer let an MLP solve XOR, when no single-layer Perceptron can?" The hidden layer learns its own intermediate, non-linear representation of the input. Even though the original two inputs aren't linearly separable for XOR, the hidden layer's output space can be โ€” the network effectively learns a transformation into a space where the final linear output layer's job becomes solvable.

Practice Question

An MLP has an input layer of 10 features, one hidden layer of 20 neurons, and an output layer of 3 classes. How many learnable weights (ignore biases) does this network have in total?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Multi-Layer Perceptron โ€“ FAQs

Quick answers about learning Multi-Layer Perceptron in Deep Learning.

This free note from CodingNow 2.0 explains Multi-Layer Perceptron in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Multi-Layer Perceptron, is 100% free with no signup required.
With focused practice, most students grasp Multi-Layer Perceptron in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now