Hitesh Sahu
Hitesh SahuHitesh Sahu
  1. Home
  2. ›
  3. posts
  4. ›
  5. …

  6. ›
  7. 4 Model Example II

Loading ⏳
Fetching content, this won’t take long…


💡 Did you know?

🍌 Bananas are berries, but strawberries are not.

🍪 This website uses cookies

No personal data is stored on our servers however third party tools Google Analytics cookies to measure traffic and improve your website experience. Learn more

Loading ⏳
Fetching content, this won’t take long…


💡 Did you know?

🍯 Honey never spoils — archaeologists found 3,000-year-old jars still edible.
AI-DeepLearning

    AI-AgenticAI

    AI-DeepLearning
    • Deep Learning Path 🤖


    • Neural Network Hypothesis and Intuition


    • Forward Propagation in Neural Networks


    • Vectorized Neural Networks Model Representation


    • Examples and Intuitions I — Neural Networks as Logical Gates


    • Examples and Intuitions II — Building XNOR with a Hidden Layer


    • Multiclass Classification with Neural Networks


    • Cost Function for Neural Networks


    • Backpropagation Algorithm


    • Gradient Checking and Random Initialization


    • Training a Neural Network


    • Revision Cheat Sheet


    • AI-DeepLearning Index


    AI-GenAI

    AI-Infrastructure

    AI-Machine-Learning

    AI-Math

    AWS

    Azure

    kubernetes

    Management

    Programming

    Terraform

    Z_Appendix

Cover Image for Examples and Intuitions II — Building XNOR with a Hidden Layer
AI-DeepLearning

Examples and Intuitions II — Building XNOR with a Hidden Layer

In the previous section, we saw how to implement basic logical gates (AND, OR, NOR) using single neurons. However, some functions like XOR and XNOR cannot be represented by a single neuron. In this post, we will see how adding a hidden layer allows us to model the XNOR function.

Data Science
Machine Learning
Deep Learning
Neural Networks
Artificial Intelligence
Computational Graphs
← Previous

Examples and Intuitions I — Neural Networks as Logical Gates

Next →

Multiclass Classification with Neural Networks

Complex Logical Gates with Neural Networks

Implementing XNOR

The logical XNOR operator outputs 1 when:

  • x1=0x_1 = 0x1​=0 and x2=0x_2 = 0x2​=0, or
  • x1=1x_1 = 1x1​=1 and x2=1x_2 = 1x2​=1

In other words, when both inputs are the same.

(x1∧x2)∨(¬x1∧¬x2)(x_1 \land x_2) \lor (\neg x_1 \land \neg x_2)(x1​∧x2​)∨(¬x1​∧¬x2​)

A single neuron cannot represent XNOR.
We need a hidden layer.

x1x_1x1​x2x_2x2​Result
001
100
010
111

Network Architecture

[x0x1x2]→[a1(2)a2(2)]→a(3)→hΘ(x)\begin{bmatrix} x_0 \\ x_1 \\ x_2 \end{bmatrix} \rightarrow \begin{bmatrix} a^{(2)}_1 \\ a^{(2)}_2 \end{bmatrix} \rightarrow a^{(3)} \rightarrow h_\Theta(x)​x0​x1​x2​​​→[a1(2)​a2(2)​​]→a(3)→hΘ​(x)

Where:

  • Hidden unit 1 implements AND
  • Hidden unit 2 implements NOR
  • Output layer implements OR

First Layer (AND + NOR)

We combine AND and NOR into one matrix:

Θ(1)=[−30202010−20−20]\Theta^{(1)} = \begin{bmatrix} -30 & 20 & 20 \\ 10 & -20 & -20 \end{bmatrix}Θ(1)=[−3010​20−20​20−20​]

This gives:

a(2)=g(Θ(1)x)a^{(2)} = g(\Theta^{(1)} x)a(2)=g(Θ(1)x)

So the hidden layer computes:

  • a1(2)a^{(2)}_1a1(2)​ behaves like AND
  • a2(2)a^{(2)}_2a2(2)​ behaves like NOR

Second Layer (OR)

The output layer computes: OR(AND,NOR)\text{OR}(\text{AND}, \text{NOR})OR(AND,NOR)

Now we combine the hidden outputs using OR:

Θ(2)=[−102020]\Theta^{(2)} = \begin{bmatrix} -10 & 20 & 20 \end{bmatrix}Θ(2)=[−10​20​20​]

This gives:

a(3)=g(Θ(2)a(2))a^{(3)} = g(\Theta^{(2)} a^{(2)})a(3)=g(Θ(2)a(2))

Final hypothesis:

hΘ(x)=a(3)h_\Theta(x) = a^{(3)}hΘ​(x)=a(3)

Full Computation

The forward propagation is:

a(2)=g(Θ(1)x)a^{(2)} = g(\Theta^{(1)} x)a(2)=g(Θ(1)x) a(3)=g(Θ(2)a(2))a^{(3)} = g(\Theta^{(2)} a^{(2)})a(3)=g(Θ(2)a(2)) hΘ(x)=a(3)h_\Theta(x) = a^{(3)}hΘ​(x)=a(3)

Key Insight

  • Single logistic neuron → can model AND, OR, NOR
  • Cannot model XOR or XNOR
  • Adding one hidden layer enables nonlinear decision boundaries

This is the first concrete example of why hidden layers matter.


Modern Relevance

The XNOR example proves the critical point: a single neuron cannot model XOR/XNOR (non-linearly separable problems), but one hidden layer can. This is why deep networks exist.

Scaled to production:

  • Depth = compositionality. GPT-4 uses 96+ transformer layers. Each layer transforms the representation of each token, progressively building from low-level patterns (word co-occurrence in early layers) to abstract semantics (reasoning over concepts in late layers). The hidden layer in XNOR is the toy version of this representation building.
  • Why depth beats width. You can approximate any function with one wide hidden layer (universal approximation theorem) but you need exponentially more neurons than a deeper network. This is why modern LLMs are deep (80–128 layers) rather than just wide. The XNOR proof hints at this — the two-layer solution is more parameter-efficient than any single-layer alternative.
  • ReLU and non-linearity. The sigmoid used here is replaced by GeLU in modern transformers. The shape is different but the role is identical: introduce non-linearity between affine transformations. Without any activation function, stacking layers collapses to a single linear transformation — the key mathematical reason non-linearity is not optional.

The Θ matrices with their carefully chosen integer weights here become 4096×4096 weight matrices learned by gradient descent over trillions of tokens — but the role they play is the same: compute a non-linear decision function that no shallower architecture could represent.


Related Posts

  • Examples and Intuitions I — the simpler single-layer gates this XNOR example builds on
  • Multiclass Classification with Neural Networks — generalizing from binary XNOR to multiple output classes
Hitesh Sahu
Written by Hitesh Sahu, a passionate developer and blogger.

Fri Feb 27 2026

Share This on

← Previous

Examples and Intuitions I — Neural Networks as Logical Gates

Next →

Multiclass Classification with Neural Networks

AI-DeepLearning/4-Model-Example-II
Let's work together
hiteshkrsahu@gmail.com
Munich 🥨, Germany 🇩🇪, EU
Playstore
Hitesh Sahu's apps on Google Play Store
Need Help?
Let's Connect
Navigation
  Home/About
  Skills
  Work/Projects
  Lab/Experiments
  Contribution
  Awards
  Art/Sketches
  Thoughts
  Contact
Links
  Sitemap
  Legal Notice
  Privacy Policy

Made with

NextJS logo

NextJS by

hitesh Sahu

| © 2026 All rights reserved.