Neural Network Layers
What are layers?
A single perceptron is a basic decision-maker, like one person making a simple yes/no choice. Real-world problems are too complex for one neuron. The solution is to organize many neurons into layers.
A layer is a group of neurons working side by side. Each layer passes its outputs forward to the next, so the network can build up increasingly complex features. There are three main types:
- Input layer: Receives the raw features (). No math happens here; it just passes the values forward. It has one neuron per input feature.
- Hidden layers: The computational engine of the network, sitting between the input and output layers. Each neuron performs a linear transformation followed by a non-linear activation. "Deep" learning gets its name from stacking many hidden layers, which lets the network learn complex, non-linear patterns.
- Output layer: Produces the final prediction (). Its shape depends on the task:
- Binary classification: one neuron with a sigmoid activation.
- Multi-class classification: one neuron per class with a softmax activation.
- Regression: one neuron with no activation (a plain linear output).
Input layer Hidden layer Output layer
x1 ──────┬───▶ (neuron) ──┐
╳ ├───▶ (neuron) ───▶ ŷ
x2 ──────┴───▶ (neuron) ──┘
Why this matters
Choosing layers is how you design a neural network. The number of hidden layers controls how complex the patterns can be, and the output layer has to match your task. Get the output layer wrong and the network can't produce a sensible answer, no matter how well everything else is set up.
What a neuron does
Every neuron in a hidden or output layer performs two steps:
- Linear transformation: Compute the weighted sum of its inputs plus a bias, , exactly like a perceptron.
- Non-linear activation: Pass through an activation function to get the neuron's output, .
Example: predicting whether a build will pass
Let's walk through a tiny network that predicts whether a software build will pass (1) or fail (0), based on two features scaled between 0 and 1:
- : Test coverage ratio.
- : Amount of code changed.
The network
- Input layer: 2 nodes (, ).
- Hidden layer: 2 neurons with ReLU activation.
- Output layer: 1 neuron with sigmoid activation (the probability of passing).
Step 1: forward propagation
Take a commit with (80% coverage) and (a small change).
Hidden neuron 1, with weights , and bias :
Applying ReLU: .
Hidden neuron 2, with weights , and bias :
Applying ReLU: . This neuron is switched off for this input.
Output neuron, taking and with weights , and bias :
Applying sigmoid:
The network predicts a 60.5% probability that the build passes.
Step 2: calculate the loss
Suppose the build actually passed (). We measure the error with binary cross-entropy:
See loss functions for more on this formula.
Step 3: backpropagation and optimization
The network computes the gradient of the loss with respect to every weight and bias using the chain rule. An optimizer then updates each weight a small step in the direction that reduces the loss:
Here is the learning rate. Repeat this across thousands of commits, and the weights gradually settle on values that predict builds well. See backpropagation for a worked example.
Building layers in code
In Keras, each layer is one line. This is the same 2-2-1 network from the example:
import tensorflow as tf
from tensorflow.keras import layers
model = tf.keras.Sequential([
layers.Input(shape=(2,)), # Input layer: 2 features
layers.Dense(2, activation='relu'), # Hidden layer: 2 neurons
layers.Dense(1, activation='sigmoid'), # Output layer: probability of passing
])
model.summary()
Dense means fully connected: every neuron receives input from every neuron in the previous layer.
Specialized layer types
As deep learning evolved, specialized layers were invented for specific kinds of data:
| Layer type | Primary use | How it works |
|---|---|---|
| Convolutional (CNNs) | Images and video | Slides small learnable filters across the input to detect spatial features like edges and textures. |
| Recurrent (RNNs) and attention (Transformers) | Text, speech, time series | Captures relationships across time steps or between words in a sequence. |
| Pooling | Downsampling in CNNs | Shrinks feature maps (for example, max pooling) to cut computation and overfitting. |
| Normalization (batch norm, layer norm) | Training stability | Normalizes layer inputs to stabilize and speed up training. |
| Dropout | Regularization | Randomly zeroes some neuron outputs during training to prevent overfitting. |
Common mistakes
Mismatching the output layer and the task
The output layer must match what you're predicting.
# Wrong: ReLU can't produce a probability (and can output more than 1)
layers.Dense(1, activation='relu') # for a yes/no question
# Correct
layers.Dense(1, activation='sigmoid') # Binary classification
layers.Dense(10, activation='softmax') # 10-class classification
layers.Dense(1) # Regression: no activation
Starting too big
A network with many huge layers is slow to train and overfits easily. Start small, and add layers or neurons only when the model is clearly underfitting.
Summary
- Neural networks organize neurons into input, hidden, and output layers.
- Each neuron computes a weighted sum plus bias, then applies an activation function.
- Hidden layers learn intermediate features; stacking more of them makes the network "deep."
- The output layer's size and activation depend on the task: sigmoid for binary, softmax for multi-class, linear for regression.