Examples and Intuitions I — Neural Networks as Logical Gates
A simple example of applying neural networks is predicting logical operations like AND and OR. By choosing appropriate weights and bias, a single logistic neuron can simulate these gates. This illustrates the power of neural networks to represent complex functions by stacking simple units.
Neural Networks as Logical Gates
A single logistic neuron can simulate logical gates.
By adjusting:
- Bias (threshold)
- Weights (importance of inputs)
we can model:
- AND
- OR
- NOT
Neural networks are powerful because stacking these simple units allows us to represent much more complex functions.
Implementing the AND Operator
The logical AND operator is true only when:
Otherwise, it is false.
| Result | ||
|---|---|---|
| 0 | 0 | 0 |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 1 | 1 |
Network Structure
graph LR
%% Input Layer
subgraph Input Layer
x0(((x0)))
x1(((x1)))
x2(((x2)))
end
%% Hidden Layer 1
subgraph Hidden Layer 1
a1{a1}
end
%% Output Layer
subgraph Output Layer
y(((hθx)))
end
%% Connections: Input → Hidden 1
x0 --> a1
x1 --> a1
x2 --> a1
%% Connections: Hidden 2 → Output
a1 --> y
Our small neural network looks like:
Where: is the bias unit
Choosing the Weights
Consider weight matrix:
The hypothesis becomes:
Evaluating All Input Combinations
| Expected | |||
|---|---|---|---|
| 0 | 0 | 0 | |
| 1 | 0 | 0 | |
| 0 | 1 | 0 | |
| 1 | 1 | 1 |
Conclusion
With this choice of weights:
the neural network behaves exactly like an AND gate.
Implementing the OR Operator
The logical OR operator is true when:
- , or
- , or both
| Result | ||
|---|---|---|
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 1 | 1 |
We can implement OR using a different set of weights:
The hypothesis becomes:
Evaluating All Input Combinations
| Expected | |||
|---|---|---|---|
| 0 | 0 | 0 | |
| 1 | 0 | 0 | |
| 0 | 1 | 0 | |
| 1 | 1 | 1 |
Conclusion
With this choice of weights:
the same neural network behaves exactly like an OR gate.
Implementing Not Gate ( )
graph LR
%% Input Layer
subgraph Input Layer
x0(((x0)))
x1(((x1)))
end
%% Hidden Layer 1
subgraph Hidden Layer 1
a1{a1}
end
%% Output Layer
subgraph Output Layer
y(((hθx)))
end
%% Connections: Input → Hidden 1
x0 --> a1
x1 --> a1
%% Connections: Hidden 2 → Output
a1 --> y
The logical NOT operator is true when:
and vice versa
| Result | |
|---|---|
| 0 | 1 |
| 1 | 0 |
We can implement NOT using weights:
The hypothesis becomes:
| Expected | ||
|---|---|---|
| 0 | 1 | |
| 1 | 0 |
Summary
We can use weight to simulate Logic gates with Neural networks
AND
OR
NOT
NOR = NOT OR
Modern Relevance
The AND and OR gates here are the simplest possible proof of the universal approximation theorem: a network with enough neurons can represent any Boolean function, and therefore any computable function.
In practice this matters because:
- Attention heads as learned logic gates. Research has found that individual attention heads in transformers implement recognizable patterns — some heads track subject-verb agreement, others copy previous tokens, others implement "previous token" lookup. These are more complex than AND/OR but structurally the same idea: a set of weights that fires on specific input patterns.
- Gating mechanisms. LSTM gates (forget, input, output) and GRU gates are literally sigmoid units computing something close to the soft-AND shown here. The "hard" threshold at 0.5 becomes a differentiable soft gate during training.
- Mixture of Experts (MoE) routing. GPT-4 and Mixtral use a gating network to route each token to one of N expert FFN layers. The router is a small neural network making a soft-argmax decision — a direct descendant of the decision boundary logic demonstrated here.
The weights -30, 20, 20 for AND feel arbitrary, but they encode a precise geometric fact: the decision boundary is a hyperplane that cuts between (1,1) and all other inputs. Modern weight matrices encode millions of such hyperplanes simultaneously.
Related Posts
- Vectorized Neural Networks Model Representation — the matrix form these logic-gate examples are built with
- Examples and Intuitions II — extends the logic-gate intuition to a network needing a hidden layer
