Deep learning has four major architecture families — CNNs, RNNs, LSTMs, and Transformers — and choosing the wrong one for your task is one of the most common beginner mistakes. This guide explains how each architecture works, its strengths and limitations, and the right scenarios for each.
Fully Connected Networks (MLP)
The simplest architecture: layers of neurons where every neuron connects to every neuron in the next layer. Input → Dense → Activation → Dense → Output. Good for tabular data where features don’t have spatial or temporal relationships. Weakness: doesn’t scale to images or sequences because the number of parameters explodes.
import torch.nn as nn
mlp = nn.Sequential(
nn.Linear(128, 256), nn.ReLU(), nn.Dropout(0.3),
nn.Linear(256, 128), nn.ReLU(), nn.Dropout(0.3),
nn.Linear(128, 10)
)
Convolutional Neural Networks (CNN)
CNNs use convolutional filters that slide across spatial dimensions, sharing weights across positions. This gives them two key properties: local connectivity (each filter sees a small patch) and translation invariance (a cat detector works regardless of where the cat is in the image). They’re the gold standard for image data and any data with local spatial structure.
class ConvNet(nn.Module):
def __init__(self, num_classes=10):
super().__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 32, kernel_size=3, padding=1), # 3 in, 32 out
nn.BatchNorm2d(32), nn.ReLU(),
nn.MaxPool2d(2, 2), # halve spatial dims
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64), nn.ReLU(),
nn.MaxPool2d(2, 2),
)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(64 * 8 * 8, 512), nn.ReLU(), nn.Dropout(0.5),
nn.Linear(512, num_classes)
)
def forward(self, x):
return self.classifier(self.features(x))
Use CNNs for: image classification, object detection, image segmentation, any task where local spatial patterns matter (including 1D CNNs for time series).
Recurrent Neural Networks (RNN)
RNNs process sequences by maintaining a hidden state that gets updated at each time step, giving the network a “memory” of previous inputs. The hidden state is passed forward, making predictions that depend on the entire history.
rnn = nn.RNN(input_size=64, hidden_size=128, num_layers=2,
batch_first=True, dropout=0.3)
output, h_n = rnn(x) # x: (batch, seq_len, input_size)
Limitation: vanilla RNNs suffer from the vanishing gradient problem — information from early time steps fades as sequences get longer. For any sequence longer than ~10-20 steps, use LSTM instead.
LSTM – Long Short-Term Memory
LSTMs solve the vanishing gradient problem with a cell state (long-term memory) and gates that control what to remember and forget. The forget gate decides what to erase, the input gate decides what new information to add, and the output gate decides what to output.
lstm = nn.LSTM(input_size=64, hidden_size=256, num_layers=2,
batch_first=True, dropout=0.3, bidirectional=True)
output, (h_n, c_n) = lstm(x)
# Bidirectional: processes sequence forward AND backward, doubles hidden_size
Use LSTMs for: time series forecasting, sequence classification, language modeling (pre-transformer), speech recognition. Bidirectional LSTMs are significantly more powerful when you have access to the full sequence at once.
Transformers
Transformers replaced RNNs for most NLP tasks after 2018. Instead of sequential processing, self-attention computes relationships between all positions simultaneously — solving the vanishing gradient problem and enabling parallelisation.
encoder_layer = nn.TransformerEncoderLayer(
d_model=512, nhead=8, dim_feedforward=2048, dropout=0.1, batch_first=True)
transformer_encoder = nn.TransformerEncoder(encoder_layer, num_layers=6)
# Or use HuggingFace pretrained transformers
from transformers import AutoModel
bert = AutoModel.from_pretrained("bert-base-uncased")
Architecture Decision Guide
Choose CNN when: your input is images or 2D data with local spatial patterns, or 1D signals with local patterns (EEG, audio spectrograms). Choose LSTM/RNN when: your data is sequential and temporal order matters, your sequences are short-to-medium length (under ~500 steps), or you need interpretable hidden states. Choose Transformer when: your data is text or any long sequence where long-range dependencies matter, you can access pretrained models (almost always yes), or you have enough data for training from scratch. Use MLP when: your data is tabular with no meaningful spatial or temporal structure, or as the classification head on top of other architectures.
Hybrid Architectures in 2026
Modern architectures often combine building blocks. Vision Transformer (ViT) applies transformers to image patches — outperforming CNNs with large data. Speech models use CNN frontends to process raw audio into spectrograms, then transformer encoders for sequence modeling. Time series transformers like Temporal Fusion Transformer use LSTM-style gating with transformer attention for interpretable forecasting.
Conclusion
The architecture landscape has simplified considerably in 2026: transformers dominate text and are increasingly competitive on images; CNNs remain strong for efficient vision applications; LSTMs persist for time series where transformers are overkill. The practical advice: start with a pretrained transformer from HuggingFace for any NLP task, an EfficientNet or ResNet pretrained on ImageNet for any vision task, and LightGBM (not a neural network at all) for tabular data. Neural architectures are for when you’ve exhausted gradient boosting on your tabular problem.

