Practice Project 15

Deep Learning Image & Tabular Classification Project

Build, train, and tune deep neural network architectures using PyTorch. Build multi-layer perceptrons (MLP) for tabular classification and convolutional networks (CNN) for image tasks.

Domain / Environment
Computer Vision / Tabular / Conda VM
Difficulty
Advanced (4/5)
Course Module
Deep Learning & Neural Networks
Deliverables
Model files (.pth) & training loss curve pngs
1. Neural Network Architectures

The diagram below displays the two neural network architectures used in this project. The MLP uses dense linear layers and dropout for tabular classification, while the CNN uses convolutional and pooling layers for image classification.

Tabular Multi-Layer Perceptron (MLP) Input Layer (Features: 3) Linear (3 -> 16) + ReLU Dropout (p=0.2) + Linear (16 -> 8) + ReLU Linear (8 -> 1) Output Layer (Sigmoid Probability) Image Convolutional Network (CNN) Input Image (Grid: 1 x 28 x 28) Conv2d (1 -> 8, kernel=3) + MaxPool2d(2) Conv2d (8 -> 16, kernel=3) + MaxPool2d(2) Flatten() + Linear (400 -> 32) + ReLU Linear (32 -> 10) + LogSoftmax (Classes)
2. Part 1: Step-by-Step Action Items & Key Execution Steps
STEP 1

Activate Python Virtual Environment

Point the terminal execution environment to the course conda sandbox environment.

$ conda activate ds_ai_ml
This targets active python libraries to the isolated virtual sandbox.
STEP 2

Create Project Folders inside Linux VM

Create a dedicated folder for the project files inside your guest VM home folder directory.

$ mkdir -p ~/Projects/deep_learning && cd ~/Projects/deep_learning
This sets up the working directory layout for the neural network code.
STEP 3

Install PyTorch via Pip

Install PyTorch CPU dependencies inside the active conda session.

$ pip install torch torchvision pandas numpy scikit-learn matplotlib
This installs PyTorch, torchvision dataset utilities, scikit-learn, and plotting tools.
STEP 4

Create network training script file in VS Code

Launch VS Code and create the network training script file.

Launch VS Code via terminal "code ." -> New File -> Type: train_networks.py -> Paste Python code -> Save file
This registers the neural network architecture configurations and training loops in `train_networks.py`.
STEP 5

Run Neural Network Training Script

Execute the script to train both the MLP and CNN models.

$ python train_networks.py
This runs the script, prints training loss values for each epoch, and saves the trained models to disk.
STEP 6

Open and verify generated training curves

Open the training curves plot using the default Linux desktop photo viewer to verify convergence.

$ xdg-open loss_curves.png
This command loads the image viewer application on the VM desktop to display the loss curve plot.
3. Deep Learning Pipeline Flow

The flowchart below outlines the neural network training pipeline. It details the steps from raw data preprocessing and splitting to model fitting and saving the weights to disk.

1. Loaders Prepare data loaders and batch inputs DataLoader(batch) 2. Define Model Configure layers, relu activations nn.Module class 3. Train Loop Iterate epochs, optimize loss loss.backward() 4. Regularize Monitor validation loss for early stop early_stopping() 5. Save weights Save model weights to .pth files torch.save()
4. Part 2: Complete Deliverable Assets & Production Templates

To run the training script, we need the raw files and the PyTorch training pipeline. Below is a line-by-line explanation of the code, followed by the combined template files.

Step-by-Step Code Construction

Lines 1 - 7

Import PyTorch and neural network layers

Include system packages, PyTorch tensor structures, layers, and optimizer APIs in the script.

import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader, TensorDataset import numpy as np import matplotlib.pyplot as plt
These imports load core PyTorch neural network modules, optimizers, and dataloader classes.
Lines 8 - 25

Define Tabular MLP Network Architecture

Inherit from `nn.Module` to build a multi-layer perceptron with dropout for tabular data.

class TabularMLP(nn.Module): def __init__(self, input_dim): super(TabularMLP, self).__init__() self.fc1 = nn.Linear(input_dim, 16) self.fc2 = nn.Linear(16, 8) self.fc3 = nn.Linear(8, 1) self.dropout = nn.Dropout(p=0.2) self.relu = nn.ReLU() self.sigmoid = nn.Sigmoid() def forward(self, x): x = self.relu(self.fc1(x)) x = self.dropout(x) x = self.relu(self.fc2(x)) x = self.sigmoid(self.fc3(x)) return x
This configures a feedforward network with two hidden layers, ReLU activations, dropout regularization, and a Sigmoid output layer.
Lines 26 - 48

Define Image CNN Network Architecture

Inherit from `nn.Module` to build a convolutional neural network (CNN) with pooling layers for image classification.

class SimpleCNN(nn.Module): def __init__(self): super(SimpleCNN, self).__init__() self.conv1 = nn.Conv2d(1, 8, kernel_size=3, padding=1) self.pool = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(8, 16, kernel_size=3, padding=1) self.fc1 = nn.Linear(16 * 7 * 7, 32) self.fc2 = nn.Linear(32, 10) self.relu = nn.ReLU() def forward(self, x): x = self.pool(self.relu(self.conv1(x))) x = self.pool(self.relu(self.conv2(x))) x = x.view(-1, 16 * 7 * 7) x = self.relu(self.fc1(x)) x = self.fc2(x) return x
This configures a CNN with 2 convolutional layers, max pooling, flattening, and dense linear output layers.
Lines 49 - 65

Train Model Epoch Loop

Define the forward pass, calculate loss, run backpropagation, and update weights using the optimizer.

optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, targets) loss.backward() optimizer.step()
This executes backpropagation, calculating loss gradients and updating model weights using the Adam optimizer.

Production templates

1. Save PyTorch Modeling Script (Save as ~/Projects/deep_learning/train_networks.py):

# train_networks.py - Train MLP and CNN using PyTorch import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader, TensorDataset import numpy as np import matplotlib.pyplot as plt # 1. Define Tabular MLP Model class TabularMLP(nn.Module): def __init__(self, input_dim): super(TabularMLP, self).__init__() self.fc1 = nn.Linear(input_dim, 16) self.fc2 = nn.Linear(16, 8) self.fc3 = nn.Linear(8, 1) self.dropout = nn.Dropout(p=0.2) self.relu = nn.ReLU() self.sigmoid = nn.Sigmoid() def forward(self, x): x = self.relu(self.fc1(x)) x = self.dropout(x) x = self.relu(self.fc2(x)) x = self.sigmoid(self.fc3(x)) return x # 2. Define Image CNN Model class SimpleCNN(nn.Module): def __init__(self): super(SimpleCNN, self).__init__() self.conv1 = nn.Conv2d(1, 8, kernel_size=3, padding=1) self.pool = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(8, 16, kernel_size=3, padding=1) self.fc1 = nn.Linear(16 * 7 * 7, 32) self.fc2 = nn.Linear(32, 10) self.relu = nn.ReLU() def forward(self, x): x = self.pool(self.relu(self.conv1(x))) x = self.pool(self.relu(self.conv2(x))) x = x.view(-1, 16 * 7 * 7) x = self.relu(self.fc1(x)) x = self.fc2(x) return x def main(): print("=== Generating Synthetic Training Datasets ===") # Tabular synthetic data (100 samples, 3 features) X_tab = np.random.randn(100, 3).astype(np.float32) y_tab = np.random.choice([0.0, 1.0], size=(100, 1)).astype(np.float32) tab_dataset = TensorDataset(torch.tensor(X_tab), torch.tensor(y_tab)) tab_loader = DataLoader(tab_dataset, batch_size=16, shuffle=True) # Image synthetic data (100 samples, 1 channel, 28x28 size) X_img = np.random.randn(100, 1, 28, 28).astype(np.float32) y_img = np.random.randint(0, 10, size=(100,)).astype(np.int64) img_dataset = TensorDataset(torch.tensor(X_img), torch.tensor(y_img)) img_loader = DataLoader(img_dataset, batch_size=16, shuffle=True) # --- Train Tabular MLP --- print("\n=== Training Tabular MLP Model ===") mlp_model = TabularMLP(input_dim=3) mlp_criterion = nn.BCELoss() mlp_optimizer = optim.Adam(mlp_model.parameters(), lr=0.01) mlp_losses = [] mlp_model.train() for epoch in range(10): epoch_loss = 0.0 for inputs, targets in tab_loader: mlp_optimizer.zero_grad() outputs = mlp_model(inputs) loss = mlp_criterion(outputs, targets) loss.backward() mlp_optimizer.step() epoch_loss += loss.item() * inputs.size(0) epoch_loss /= len(X_tab) mlp_losses.append(epoch_loss) print(f"Epoch {epoch+1}/10 | MLP Training Loss: {epoch_loss:.4f}") # --- Train Image CNN --- print("\n=== Training Image CNN Model ===") cnn_model = SimpleCNN() cnn_criterion = nn.CrossEntropyLoss() cnn_optimizer = optim.Adam(cnn_model.parameters(), lr=0.01) cnn_losses = [] cnn_model.train() for epoch in range(10): epoch_loss = 0.0 for inputs, targets in img_loader: cnn_optimizer.zero_grad() outputs = cnn_model(inputs) loss = cnn_criterion(outputs, targets) loss.backward() cnn_optimizer.step() epoch_loss += loss.item() * inputs.size(0) epoch_loss /= len(X_img) cnn_losses.append(epoch_loss) print(f"Epoch {epoch+1}/10 | CNN Training Loss: {epoch_loss:.4f}") # Save models to disk torch.save(mlp_model.state_dict(), "tabular_mlp.pth") torch.save(cnn_model.state_dict(), "image_cnn.pth") print("\nModel weights saved as tabular_mlp.pth and image_cnn.pth.") # Plot loss curves plt.style.use('dark_background') plt.figure(figsize=(10, 5)) plt.plot(range(1, 11), mlp_losses, label="MLP Tabular Loss", color="#38bdf8", linewidth=2) plt.plot(range(1, 11), cnn_losses, label="CNN Image Loss", color="#34d399", linewidth=2) plt.title("Neural Networks Training Loss Curves", color="#f8fafc") plt.xlabel("Epochs", color="#94a3b8") plt.ylabel("Loss", color="#94a3b8") plt.legend() plt.grid(True, linestyle="--", color="#334155", alpha=0.5) curves_filename = "loss_curves.png" plt.savefig(curves_filename, facecolor="#0f172a", edgecolor="none") print(f"Training loss curves saved to: {curves_filename}") print("=== Deep Learning Model Training Successfully Completed! ===") if __name__ == "__main__": main()
5. Deliverables Summary

Verify that the following configurations and outputs exist inside your project workspace.

Created Files / Templates

  • ~/Projects/deep_learning/train_networks.py - Model training script.
  • ~/Projects/deep_learning/tabular_mlp.pth - Saved MLP model weights.
  • ~/Projects/deep_learning/image_cnn.pth - Saved CNN model weights.
  • ~/Projects/deep_learning/loss_curves.png - Training loss curves plot.

Verification Artifacts / Execution Proof

  • Epoch logs printed showing decreasing training loss curves.
  • Correct shapes verified for linear outputs and activation classes.
  • Serialized model files saved successfully to disk.
6. Closing Explanation: Why We Did This & What It Accomplishes

Architectural Intent & Operational Impact

Why We Did This

What This Accomplishes