DL Notes
Understanding different types of layers in neural networks - input, hidden, and output layers, dense connections, and how to design effective architectures.
Neural networks are organized into layers — structured groups of neurons that process information sequentially. Understanding layer types and their roles is fundamental to designing effective deep learning architectures.
Network Architecture Overview
| Input Layer | ───→ | Hidden Layer1 | ───→ | Hidden Layer2 | ───→ | Output Layer |
|---|---|---|---|---|---|---|
| (features) | (patterns) | (abstractions) | (predictions) |
The Input Layer
The input layer receives raw data and passes it to the first hidden layer without computation.
import torch.nn as nn
# Input layer is implicit in PyTorch - defined by first Linear layer
model = nn.Sequential(
nn.Linear(784, 256), # 784 input features (28x28 image flattened)
nn.ReLU(),
nn.Linear(256, 10) # 10 output classes
)Hidden Layers (Dense/Fully Connected)
Hidden layers perform the actual computation. Each neuron connects to every neuron in the previous layer.
The Output Layer
The output layer design depends on the task:
import torch.nn as nn
# Binary Classification: 1 neuron + Sigmoid
binary_output = nn.Sequential(nn.Linear(64, 1), nn.Sigmoid())
# Multi-class Classification: k neurons + Softmax
multiclass_output = nn.Sequential(nn.Linear(64, 10), nn.Softmax(dim=1))
# Regression: 1 neuron, no activation
regression_output = nn.Linear(64, 1)
# Multi-label: k neurons + Sigmoid (independent probabilities)
multilabel_output = nn.Sequential(nn.Linear(64, 5), nn.Sigmoid())How Layers Learn Hierarchical Features
| Layer 1: Low-level features | edges, colors, simple patterns |
| Layer 2: Mid-level features | textures, shapes, object parts |
| Layer 3: High-level features | objects, faces, scenes |
| Output: Task-specific | class probabilities |
Layer Types Comparison
| Layer Type | Parameters | Use Case | Example |
|---|---|---|---|
| Dense (Linear) | W, b | General features | Classification |
| Convolutional | Filters, b | Spatial patterns | Image recognition |
| Recurrent (LSTM) | Gates, states | Sequential data | Text, time series |
| Attention | Q, K, V matrices | Long-range deps | Transformers |
| Normalization | gamma, beta | Stabilize training | BatchNorm |
| Dropout | drop rate | Regularization | Prevent overfitting |
| Embedding | Lookup table | Categorical data | Word embeddings |
Determining Architecture Size
| Factor | Guideline |
|---|---|
| Simple patterns | 1-2 hidden layers |
| Complex patterns | 3-5 hidden layers |
| Very complex (images, NLP) | 10-100+ layers with skip connections |
| Neurons per layer | Start with 2x input, decrease toward output |
| Rule of thumb | Parameters < 10x training samples |
Practical Architecture Pattern
import torch
import torch.nn as nn
class PracticalClassifier(nn.Module):
def __init__(self, input_features, num_classes):
super().__init__()
self.features = nn.Sequential(
nn.Linear(input_features, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(512, 256),
nn.BatchNorm1d(256),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256, 128),
nn.BatchNorm1d(128),
nn.ReLU(),
nn.Dropout(0.2),
)
self.classifier = nn.Sequential(
nn.Linear(128, 64),
nn.ReLU(),
nn.Linear(64, num_classes)
)
def forward(self, x):
features = self.features(x)
return self.classifier(features)
model = PracticalClassifier(50, 5)
x = torch.randn(32, 50)
print(f"Output shape: {model(x).shape}")Width vs Depth Trade-off
Skip Connections (ResNet Pattern)
Interview Questions
- Why do we need hidden layers?
Without hidden layers, the network is a linear transformation. Hidden layers with non-linear activations enable learning complex, non-linear decision boundaries.
- What is the Universal Approximation Theorem?
A network with one hidden layer of sufficient width can approximate any continuous function. However, deeper networks achieve the same with exponentially fewer neurons.
- How do you choose the number of neurons per layer?
Start wide, narrow toward output. Use cross-validation. Too few neurons underfit; too many overfit and waste computation.
- What happens without skip connections in very deep networks?
Gradients vanish or explode through many layers, making training nearly impossible. Skip connections allow gradients to flow directly through the network.
- Why is depth more powerful than width?
Deep networks learn compositional, hierarchical representations. Each layer builds on previous abstractions, making them exponentially more efficient for structured data.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Layers in Neural Networks.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Deep Learning topic.
Search Terms
deep-learning, deep learning, deep, learning, neural, network, fundamentals, layers
Related Deep Learning Topics