Module 3: Neural Networks & Deep Learning
8. Integrative Exercise: Designing Neural Network Architectures
Description
This is the final lesson of Module 3, where you'll apply everything you learned about neural networks. The goal is NOT to memorize definitions; the goal is for you to be able to reason about which architecture to use to solve real problems, design basic neural networks, and identify common mistakes.
In this exercise you'll work with:
- Classifying problems: Given a problem, choose an architecture (CNN, RNN/LSTM, Feedforward) and justify it.
- Designing architectures: Specify layers, neurons, activation functions.
- Identifying mistakes: Detect what's wrong in proposed architectures.
- Interpreting models: Read technical documentation and extract key information.
Instructions
For each problem:
- Identify the type of data (table, image, sequence).
- Choose an architecture (Feedforward, CNN, RNN/LSTM, or Transformer where applicable).
- Design the network (layers, neurons per layer, activation functions).
- Justify your decision in 1-2 sentences.
Recommended answer format:
Problem X:
- Type of data: [Table / Image / Sequence]
- Architecture: [Feedforward / CNN / RNN/LSTM]
- Design:
- Input layer: [number of neurons]
- Hidden layers: [specify layers, neurons, activations]
- Output layer: [number of neurons, activation]
- Justification: [Why that architecture and design]
Part 1: Classify problems and choose an architecture
Problem 1: Classify X-ray images (healthy lung vs pneumonia)
Context: You have 10,000 X-ray images of lungs (256×256 pixels, grayscale). Each image is labeled as "healthy" or "pneumonia".
Task: Design an architecture to classify new images.
See solution
Type of data: Images (256×256 pixels, grayscale)
Architecture: CNN (Convolutional Neural Network)
Design:
Input layer: 256×256×1 (1 channel: grayscale)
↓
Conv Layer 1: 32 filters 3×3, ReLU activation
↓
Pooling (2×2): Reduces to 128×128
↓
Conv Layer 2: 64 filters 3×3, ReLU activation
↓
Pooling (2×2): Reduces to 64×64
↓
Conv Layer 3: 128 filters 3×3, ReLU activation
↓
Pooling (2×2): Reduces to 32×32
↓
Flatten: Converts to a 1D vector
↓
Fully Connected Layer: 128 neurons, ReLU activation
↓
Fully Connected Layer: 64 neurons, ReLU activation
↓
Output layer: 1 neuron, Sigmoid activation (probability of pneumonia)
Justification:
- CNNs are ideal for images (they capture spatial patterns: edges, textures, shapes).
- The convolutional layers detect features (edges, spots in the lungs).
- Pooling reduces resolution (less compute) while keeping important information.
- The fully connected layers at the end combine the features and classify.
- Sigmoid at the output → binary classification (healthy vs pneumonia).
Alternative (better in practice):
- Use ResNet or EfficientNet pre-trained on ImageNet (transfer learning) and do fine-tuning with your 10,000 X-ray images. This is more effective than training from scratch.
Problem 2: Predict the sale price of a house
Context: You have data on 50,000 sold houses with 8 features: size (m²), location (latitude, longitude), number of rooms, number of bathrooms, year built, garage (yes/no), sale price.
Task: Design an architecture to predict the price of a new house.
See solution
Type of data: Tabular data (a table with 8 features per house)
Architecture: Feedforward (Fully Connected)
Design:
Input layer: 8 neurons (8 features)
↓
Hidden layer 1: 64 neurons, ReLU activation
↓
Hidden layer 2: 32 neurons, ReLU activation
↓
Hidden layer 3: 16 neurons, ReLU activation
↓
Output layer: 1 neuron, Linear activation (price: a number without restrictions)
Justification:
- Tabular data (without spatial or temporal structure) → Feedforward is appropriate.
- 3 hidden layers with decreasing sizes (64 → 32 → 16) is a common pattern for tables.
- ReLU in the hidden layers (the standard for Deep Learning).
- Linear output (regression: the price can be any positive number, without restrictions).
Parameters:
- Input → Hidden1: 8 × 64 = 512 weights
- Hidden1 → Hidden2: 64 × 32 = 2,048 weights
- Hidden2 → Hidden3: 32 × 16 = 512 weights
- Hidden3 → Output: 16 × 1 = 16 weights
- Biases: 64 + 32 + 16 + 1 = 113
- Total: ~3,200 parameters (small → fast, little risk of overfitting with 50K examples).
Problem 3: Classify product reviews as positive, neutral or negative
Context: You have 100,000 product reviews (text in Spanish). Each review is labeled as "positive", "neutral" or "negative".
Task: Design an architecture to classify new reviews.
See solution
Type of data: Text (sequences of words)
Modern architecture (recommended): Transformer (e.g. BERT pre-trained in Spanish)
Traditional architecture (if you don't use Transformers): RNN/LSTM
Design (with LSTM, the traditional approach):
Input: A sequence of words (e.g. 100 words, each word represented as a 300-dimensional embedding)
↓
Embedding Layer: Converts words into vectors (300 dimensions)
↓
LSTM Layer 1: 128 units
↓
LSTM Layer 2: 64 units
↓
Fully Connected Layer: 32 neurons, ReLU activation
↓
Output layer: 3 neurons (positive, neutral, negative), Softmax activation
Justification (LSTM):
- Text is a sequence (temporal dependencies: previous words influence meaning).
- LSTM captures temporal dependencies (better than a simple RNN because it avoids vanishing gradients).
- Softmax at the output → multiclass classification (3 categories).
Design (with a Transformer, the modern approach):
Input: A sequence of words (tokenization)
↓
BERT pre-trained in Spanish (12 Transformer layers)
↓
Fully Connected Layer: 64 neurons, ReLU activation
↓
Output layer: 3 neurons, Softmax activation
Justification (Transformer/BERT):
- Transformers are better than RNNs for text (faster, they capture long-range dependencies better).
- A pre-trained BERT has already learned general characteristics of Spanish → you only need fine-tuning (adjusting the last layers with your 100K reviews).
- The recommended approach in 2024.
Problem 4: Detect objects in images (where the dogs, cats, cars are)
Context: You have 50,000 images (512×512 pixels, RGB) with object annotations (bounding boxes: x, y coordinates, width, height, class).
Task: Design an architecture to detect objects in new images.
See solution
Type of data: Images (512×512 pixels, RGB)
Architecture: An Object Detection CNN (e.g. YOLO, Faster R-CNN, EfficientDet)
Design (conceptual, using YOLO as an example):
Input: A 512×512×3 image (RGB)
↓
CNN backbone (e.g. ResNet-50): Extracts features from the image
↓
Detection Head: Predicts bounding boxes (x, y, width, height) and classes (dog, cat, car)
↓
Output: A list of detected objects with coordinates and class
Justification:
- Images → CNN.
- Object detection requires specialized architectures (YOLO, Faster R-CNN) that predict multiple bounding boxes per image.
- It is NOT simple classification (which only predicts one class per image). It's detection (predicting multiple objects with locations).
Practical approach:
- Use a pre-trained model (e.g. YOLOv8, EfficientDet) on the COCO dataset (an object detection dataset).
- Fine-tune with your 50K images if your classes (dogs, cats, cars) aren't in COCO or you need to adapt to your domain.
Problem 5: Predict a product's sales next month (time series)
Context: You have sales data from the last 3 years (36 months). Each month has: units sold, price, promotions (yes/no), season (summer, autumn, etc.).
Task: Design an architecture to predict next month's sales.
See solution
Type of data: A time series (a sequence of months with features)
Architecture: RNN/LSTM or time-series-specific models (e.g. ARIMA, Prophet)
Design (with LSTM):
Input: A sequence of 12 months (each month: 4 features: sales, price, promotions, season)
↓
LSTM Layer 1: 64 units
↓
LSTM Layer 2: 32 units
↓
Fully Connected Layer: 16 neurons, ReLU activation
↓
Output layer: 1 neuron, Linear activation (sales prediction: a number)
Justification:
- A time series → a sequence with temporal dependencies (sales in previous months influence future sales).
- LSTM captures temporal dependencies (e.g. if there was a promotion 2 months ago, it can affect current sales).
- Linear output → regression (sales: a number without restrictions).
Alternative:
- Time-series-specific models like ARIMA or Prophet (from Facebook) sometimes work better than LSTMs for time series with clear trends and seasonality.
- Transformers for time series (e.g. the Temporal Fusion Transformer) are more modern than LSTMs.
Part 2: Identify mistakes in proposed architectures
Case 1: Image classification with Feedforward
Problem: Classify images of dogs vs cats (64×64 pixels, RGB).
Proposed architecture:
Input: 64×64×3 = 12,288 neurons (all the pixels)
↓
Hidden layer 1: 1,000 neurons, ReLU activation
↓
Hidden layer 2: 500 neurons, ReLU activation
↓
Output: 1 neuron, Sigmoid activation
Question: What's wrong?
See answer
Problems:
-
It uses Feedforward instead of a CNN: Images have spatial structure (nearby pixels are related). Feedforward treats each pixel independently → it does NOT capture spatial patterns (edges, textures).
-
Too many parameters: Input → Hidden1: 12,288 × 1,000 = 12.3 million weights (in the first layer alone) → a lot of memory, a lot of compute, a risk of overfitting.
Solution:
- Use a CNN (e.g. 3-4 convolutional layers + pooling + fully connected at the end).
- CNNs have fewer parameters (small filters: 3×3) and they capture spatial patterns.
Case 2: Text classification with a CNN
Problem: Classify product reviews as positive or negative (text).
Proposed architecture:
Input: A sequence of 100 words (300-dimensional embeddings)
↓
Conv Layer 1: 64 filters 3×3
↓
Conv Layer 2: 128 filters 3×3
↓
Fully Connected: 32 neurons, ReLU
↓
Output: 1 neuron, Sigmoid
Question: Does it work? Is it optimal?
See answer
Does it work? Yes, partially. CNNs can be used for text (1D convolutions capture local patterns of words).
Is it optimal? No.
Problems:
-
CNNs are less common for text: In modern NLP, Transformers (BERT, GPT) are better than CNNs because they capture long-range dependencies (e.g. the word at position 1 influences the word at position 50).
-
CNNs capture local patterns: Useful if the order of nearby words matters a lot (e.g. "not good" vs "very good"). But for complex sentiment analysis, Transformers are better.
Solution:
- Use a pre-trained BERT (a Transformer) and do fine-tuning → better accuracy than a CNN or an RNN.
Case 3: A deep network with Sigmoid in the hidden layers
Problem: Image classification (dogs vs cats).
Proposed architecture:
Input: 64×64×3 (an image)
↓
Conv Layers (50 layers): Sigmoid activation
↓
Fully Connected: 128 neurons, Sigmoid
↓
Output: 1 neuron, Sigmoid
Question: What's wrong?
See answer
Problem: Vanishing gradients.
- Sigmoid compresses gradients (derivative < 1 at almost every point).
- In a deep network (50 layers), the gradients are multiplied through many layers → they become nearly 0 → the early layers don't learn.
Solution:
- Change Sigmoid to ReLU in the hidden layers (ReLU doesn't compress gradients: derivative = 1 for z > 0).
- Sigmoid only in the output layer (binary classification).
Part 3: Interpret model documentation
Case 1: ResNet-50
Description (simplified):
- Architecture: A CNN with 50 layers (convolutional + fully connected).
- Parameters: 25.6 million.
- Input: 224×224 pixel images, RGB.
- Output: 1,000 classes (ImageNet: dog, cat, car, airplane, etc.).
- Characteristics: It uses "skip connections" (residual connections) to train very deep networks.
Questions:
- What type of data is ResNet-50 for?
- Why does it have so many parameters?
- Can you use it to classify medical images (X-rays)?
See answers
1. What type of data?
- Images (a CNN).
2. Why so many parameters?
- 50 layers → many connections between layers → many weights (25.6M).
- Trade-off: More parameters → more expressiveness (better accuracy) BUT more compute, more memory.
3. Can you use it for X-rays?
-
Yes, through transfer learning:
- ResNet-50 is pre-trained on ImageNet (natural photos: dogs, cats, cars).
- You use ResNet-50 as a base (keeping the convolutional layers that detect general features: edges, textures).
- You replace the last layer (output layer: 1,000 classes → 2 classes: healthy vs pneumonia).
- You fine-tune (adjust) with your X-ray data (you only adjust the last layers; the first ones stay frozen).
-
Advantage: You need less data (e.g. 1,000-10,000 X-rays) than training ResNet from scratch (which would require millions of images).
Summary of the exercise
What you practiced:
- Classifying problems: Identifying the type of data (table, image, sequence) and choosing an architecture (Feedforward, CNN, RNN/LSTM, Transformer).
- Designing architectures: Specifying layers, neurons, activation functions.
- Identifying mistakes: Detecting common problems (using feedforward for images, sigmoid in the hidden layers of a deep network, etc.).
- Interpreting models: Reading technical documentation and extracting key information (number of layers, parameters, type of data).
Evidence of success:
- You can choose the correct architecture for a new problem (without consulting the lessons).
- You can justify your choice (why a CNN for images, an RNN for sequences, etc.).
- You can identify common mistakes in proposed architectures.
Next step: With this exercise you finish Module 3 (Neural Networks & Deep Learning). In Module 4 (Transformers) you'll see the modern architecture that revolutionized NLP and that is the foundation of ChatGPT, BERT, and all modern LLMs.
Additional resources
-
Kaggle: Competitions — A platform with ML/DL competitions for practicing architecture design. In English.
-
Papers With Code: Browse State-of-the-Art — State-of-the-art models for different problems (image classification, NLP, etc.). In English.
-
Hugging Face Models — Thousands of pre-trained models (BERT, ResNet, etc.) with documentation. In English.
-
Fast.ai Course — A practical Deep Learning course with architecture design exercises. In English. With code (PyTorch).