Week 3
Experiment 3
ANN, DNN, CNN
Neural Network Architectures with PyTorch
← Back to Course Home
📋

Laboratory Overview

↑ Go Up

This laboratory introduces students to neural network architectures—from basic Artificial Neural Networks (ANN) to Deep Neural Networks (DNN) and Convolutional Neural Networks (CNN). Through hands-on implementation in PyTorch on the NVIDIA Jetson Orin Nano, students will develop a deep understanding of how neural networks learn, train, and make predictions on real-world data.

What You'll Learn

  • Tensor Operations: Master PyTorch's fundamental data structures and operations
  • Network Architecture: Design and implement multi-layer neural networks
  • Training Process: Understand forward propagation, backpropagation, and optimization
  • Model Evaluation: Learn validation techniques and prevent overfitting
  • Real-World Application: Train models on Fashion-MNIST dataset
  • Model Deployment: Save, load, and deploy trained models on edge devices

💡 Why This Matters

Neural networks are the foundation of modern AI applications—from computer vision and natural language processing to autonomous vehicles and medical diagnosis. By mastering PyTorch implementation on edge devices like the Jetson Orin Nano, you're preparing for real-world AI deployment scenarios where models must run efficiently on resource-constrained hardware.

Lab Structure

This laboratory consists of six progressive parts, each building upon previous concepts:

  • Part 1: Tensors in PyTorch - Foundation of all neural network computations
  • Part 2: Neural Networks in PyTorch - Building network architectures
  • Part 3: Training Neural Networks - Learning through optimization
  • Part 4: Fashion-MNIST Classification - Real-world image recognition
  • Part 5: Inference and Validation - Model evaluation and generalization
  • Part 6: Saving and Loading Models - Model persistence and deployment
🎯

Learning Objectives

↑ Go Up

By the end of this laboratory session, you will be able to:

  • Understand and manipulate PyTorch tensors for neural network operations including creation, indexing, reshaping, and matrix multiplication.
  • Design and implement neural network architectures using PyTorch's nn.Module class, including defining layers, activation functions, and forward propagation.
  • Train neural networks effectively by implementing complete training loops with loss functions, optimizers, and backpropagation.
  • Apply neural networks to real-world datasets such as Fashion-MNIST for image classification tasks and evaluate model performance.
  • Implement validation techniques including training/validation splits, dropout regularization, and performance monitoring to prevent overfitting.
  • Save and load trained models for deployment and inference, understanding model persistence and checkpointing strategies.
  • Deploy neural networks on edge devices by running PyTorch models on Jetson Orin Nano for practical AI applications.
📚

Background

↑ Go Up

Introduction to Neural Networks

Artificial Neural Networks (ANNs) are computational models inspired by the human brain's structure and function. They consist of interconnected nodes (neurons) organized in layers that process information. Neural networks have revolutionized fields such as computer vision, natural language processing, and speech recognition due to their ability to learn complex patterns from data.

The fundamental building block of neural networks is the artificial neuron, which receives multiple inputs, applies weights to them, sums them up, adds a bias term, and passes the result through an activation function. When many neurons are connected together in layers, they can learn to represent highly complex functions through a process called training.

Understanding Tensors

Tensors are the fundamental data structure in deep learning frameworks like PyTorch. A tensor is essentially a multi-dimensional array that can represent scalars (0D), vectors (1D), matrices (2D), or higher-dimensional data (3D and beyond). In neural networks, tensors are used to represent inputs, outputs, weights, and intermediate computations.

PyTorch tensors are similar to NumPy arrays but with two key advantages: they can run on GPUs for accelerated computing, and they support automatic differentiation for gradient computation during backpropagation. Understanding tensor operations like reshaping, indexing, and matrix multiplication is essential for building and debugging neural networks.

Neural Network Architectures

A basic neural network consists of three types of layers: input layer, hidden layers, and output layer. The input layer receives the raw data, hidden layers perform intermediate computations through weighted connections and activation functions, and the output layer produces the final prediction.

Deep Neural Networks (DNNs) are neural networks with multiple hidden layers, allowing them to learn hierarchical representations of data. Each layer learns increasingly abstract features: early layers might detect edges in images, middle layers detect shapes, and later layers recognize complete objects.

Convolutional Neural Networks (CNNs) are specialized architectures designed for processing grid-like data such as images. They use convolutional layers that apply filters to detect local patterns, pooling layers to reduce dimensionality, and fully connected layers for final classification. While this lab focuses on fully connected networks, understanding CNNs is important for advanced computer vision applications.

The Training Process

Training a neural network involves finding the optimal weights that minimize the difference between predicted outputs and actual targets. This process uses the following key components:

Forward Propagation: Input data flows through the network layer by layer, with each neuron computing a weighted sum of its inputs, adding a bias, and applying an activation function. The final layer produces predictions.

Loss Function: Measures how far the network's predictions are from the true values. Common loss functions include Mean Squared Error (MSE) for regression and Cross-Entropy Loss for classification. The goal of training is to minimize this loss.

Backpropagation: An algorithm that computes gradients of the loss function with respect to each weight in the network. It uses the chain rule from calculus to efficiently propagate error signals backward through the network, determining how much each weight contributed to the error.

Optimization: Once gradients are computed, an optimizer (like Stochastic Gradient Descent or Adam) updates the weights in the direction that reduces the loss. This process repeats for many iterations (epochs) until the network converges to a good solution.

Model Evaluation and Validation

To ensure our neural network generalizes well to new, unseen data, we must carefully evaluate its performance. Simply achieving low training loss is not enough—the model might be overfitting, meaning it memorizes the training data but fails on new examples.

Training vs Validation Split: We divide our dataset into training data (used to update weights) and validation data (used to evaluate performance). By monitoring both training and validation loss, we can detect overfitting: if training loss decreases but validation loss increases, the model is memorizing rather than learning general patterns.

Dropout Regularization: A technique to prevent overfitting by randomly "dropping out" (setting to zero) a fraction of neurons during training. This forces the network to learn robust features that don't depend on any single neuron, improving generalization. During inference (testing), all neurons are active, and their outputs are scaled appropriately.

Model Persistence

After training a neural network, we need to save the learned weights so we can use the model later without retraining. PyTorch provides mechanisms to save and load model state, including weights, biases, and optimizer state. This is crucial for deploying models to production environments, such as edge devices like the Jetson Orin Nano.

Understanding model persistence also enables checkpointing during long training runs, allowing you to resume training if interrupted and to save the best-performing model encountered during training.

Why PyTorch?

PyTorch is one of the most popular deep learning frameworks because of its intuitive, Pythonic design and dynamic computational graphs. Unlike static frameworks, PyTorch builds the computation graph on-the-fly, making it easier to debug and allowing for dynamic architectures that change based on input. Its strong support for GPU acceleration and extensive ecosystem of tools make it ideal for both research and production applications.

🎬

Pre-lab Preparation

↑ Go Up

Before starting the laboratory exercises, watch the following video tutorials from Udacity's "Introduction to Deep Learning with PyTorch" course. These videos provide essential background knowledge and practical demonstrations of the concepts you'll implement in this lab.

📺 Required Video Tutorials

Source: Udacity - Introduction to Deep Learning with PyTorch

Chapter: Introduction to PyTorch

📺 Required Video Tutorials

Watch all videos from Udacity - Introduction to Deep Learning with PyTorch

Chapter 3: Training Neural Networks

Chapter 4: Fashion-MNIST Example

Chapter 5: Validation & Dropout

📝 Pre-lab Quiz

Instructions: Complete this quiz after watching the required videos to assess your readiness for the lab. These questions test your understanding of key concepts you'll need during the exercises.

Question 1: What is a tensor in PyTorch?

  • A) A specialized type of neural network
  • B) A multi-dimensional array that can run on GPUs and supports automatic differentiation
  • C) A training algorithm for neural networks
  • D) A visualization tool for network architectures

Question 2: What is the purpose of an activation function in a neural network?

  • A) To normalize input data
  • B) To introduce non-linearity so the network can learn complex patterns
  • C) To calculate the loss during training
  • D) To save model weights to disk

Question 3: What does backpropagation compute?

  • A) The final output of the neural network
  • B) The gradients of the loss function with respect to network weights
  • C) The accuracy of the model on test data
  • D) The optimal learning rate for training

Question 4: Why do we use a validation set during training?

  • A) To increase the amount of training data
  • B) To speed up the training process
  • C) To detect overfitting and evaluate generalization performance
  • D) To initialize network weights

Question 5: What is the purpose of dropout regularization?

  • A) To remove unnecessary layers from the network
  • B) To randomly deactivate neurons during training to prevent overfitting
  • C) To drop outliers from the training data
  • D) To reduce the learning rate during training

Question 6: Which loss function is commonly used for multi-class classification?

  • A) Mean Squared Error (MSE)
  • B) Binary Cross-Entropy
  • C) Cross-Entropy Loss (Negative Log-Likelihood)
  • D) Mean Absolute Error (MAE)

Question 7: What does the softmax function output?

  • A) Binary values (0 or 1)
  • B) Probability distribution over classes that sums to 1
  • C) The maximum value from the input
  • D) Normalized feature values

Question 8: Why do we save trained models?

  • A) To visualize the network architecture
  • B) To deploy the model for inference without retraining
  • C) To increase model accuracy
  • D) To reduce memory usage during training

Note: Discuss your answers with your lab instructor before beginning the practical exercises. Understanding these concepts is crucial for successfully completing the lab.

⚙️

Lab Procedure

↑ Go Up

This laboratory is divided into six parts, each focusing on a specific aspect of neural network implementation in PyTorch. Complete each part sequentially, as they build upon each other. Each part includes exercises that you should attempt before viewing the solutions.

⚠️ Important Instructions:

1. Attempt exercises independently first - Try to solve each problem before looking at solutions.
2. Solutions are password-protected - Your instructor will provide the password when appropriate.
3. Execute code on Jetson Orin Nano - All exercises are designed to run on your edge device.
4. Document your work - Take screenshots and notes for your lab report.

Part 1: Tensors in PyTorch

In this part, you'll learn the fundamentals of PyTorch tensors, which are the building blocks of all neural network computations. You'll practice creating tensors, performing basic operations, reshaping, indexing, and matrix multiplication—skills essential for implementing neural networks.

Key Learning Points:

  • Create and initialize tensors using various methods
  • Perform tensor operations: addition, multiplication, reshaping
  • Understand tensor indexing and slicing
  • Apply matrix multiplication for neural network computations

Part 2: Neural Networks in PyTorch

This part introduces you to building neural network architectures using PyTorch's powerful nn.Module class. You'll learn how to define network layers, apply activation functions, and implement the forward propagation process. Understanding this structure is crucial for creating custom neural networks.

Key Learning Points:

  • Build neural networks using nn.Module and nn.Sequential
  • Define fully connected (linear) layers with appropriate dimensions
  • Apply activation functions (ReLU, Sigmoid, Softmax)
  • Implement forward propagation through custom networks

Part 3: Training Neural Networks

Now you'll learn how to train neural networks through the complete training process. This includes defining loss functions, choosing optimizers, implementing training loops, and performing backpropagation. You'll see how networks learn to minimize loss through iterative weight updates.

Key Learning Points:

  • Implement complete training loops with forward and backward passes
  • Use loss functions (CrossEntropyLoss, NLLLoss) appropriately
  • Apply optimizers (SGD, Adam) to update network weights
  • Monitor training progress through loss curves
  • Understand gradient computation and backpropagation

Part 4: Fashion-MNIST Classification

In this practical application, you'll train a neural network to classify images from the Fashion-MNIST dataset, which contains 10 categories of clothing items. This real-world task demonstrates how neural networks can learn to recognize patterns in images and make accurate predictions.

Key Learning Points:

  • Load and preprocess the Fashion-MNIST dataset
  • Design network architectures suitable for image classification
  • Train models on multi-class classification tasks
  • Visualize predictions and analyze classification results
  • Compare performance with different network configurations

Part 5: Inference and Validation

Learn how to properly evaluate neural network performance using validation techniques. You'll implement training/validation splits, monitor overfitting, and use dropout regularization. This part teaches you how to ensure your models generalize well to new, unseen data.

Key Learning Points:

  • Split datasets into training and validation sets
  • Implement validation during the training process
  • Detect overfitting by comparing training vs validation loss
  • Apply dropout regularization to prevent overfitting
  • Calculate accuracy and other performance metrics

Part 6: Saving and Loading Models

The final part covers model persistence—saving your trained models and loading them for later use. This is essential for deploying models to production environments and for checkpointing during long training runs. You'll learn how to save both model architecture and learned weights.

Key Learning Points:

  • Save trained model weights using PyTorch's state_dict
  • Load pre-trained models for inference
  • Implement model checkpointing during training
  • Understand the difference between saving weights vs. entire models
  • Deploy saved models on edge devices (Jetson Orin Nano)

💡 Tips for Success

  • Work sequentially - Each part builds on previous concepts
  • Experiment - Try modifying code to see how changes affect results
  • Debug systematically - Check tensor shapes and print intermediate outputs
  • Take notes - Document interesting findings and errors for your report
  • Ask questions - Consult your instructor when stuck
🔧

Lab Materials

↑ Go Up

Hardware Requirements

  • Platform: Jetson Orin Nano Developer Kit
  • Memory: Minimum 4GB RAM available
  • Storage: At least 2GB free space for datasets
  • Connection: Internet access for downloading Fashion-MNIST dataset

Software Prerequisites

Verify the following are installed on your Jetson Orin Nano:

  • Python: Version 3.8 or higher
  • PyTorch: Version compatible with Jetson (with CUDA support)
  • torchvision: For dataset loading and transformations
  • NumPy: For numerical operations
  • Matplotlib: For visualization (optional but recommended)

Verification command:

python3 -c "import torch; print(f'PyTorch: {torch.__version__}'); print(f'CUDA Available: {torch.cuda.is_available()}')"

Datasets

  • Fashion-MNIST: Automatically downloaded by torchvision when running the exercises
  • Size: Approximately 30MB compressed
  • Classes: 10 categories (T-shirt, Trouser, Pullover, Dress, Coat, Sandal, Shirt, Sneaker, Bag, Ankle boot)
  • Images: 60,000 training + 10,000 test images (28x28 grayscale)

Included Files

All necessary code, datasets, and helper functions are included in the six lab parts. Each HTML file is self-contained and ready to execute on your Jetson Orin Nano through a Jupyter Notebook interface.

  • Part 1-6 Exercise HTML files (executable as Jupyter notebooks)
  • Part 1-5 Solution HTML files (password-protected)
  • All required helper functions included within each part
  • Inline documentation and explanations
⚠️ Setup Verification:

Before starting the lab exercises, ensure your Jetson Orin Nano is properly configured with PyTorch and CUDA support. Run the verification command above to confirm your setup. If you encounter issues, consult your instructor before proceeding.

📖

References & Resources

↑ Go Up

Primary Course Material

Source: Udacity - Introduction to Deep Learning with PyTorch

Chapter: Introduction to PyTorch

Additional Resources

  • PyTorch Documentation: https://pytorch.org/docs/
  • PyTorch Tutorials: https://pytorch.org/tutorials/
  • Fashion-MNIST Dataset: https://github.com/zalandoresearch/fashion-mnist
  • NVIDIA Jetson Documentation: https://developer.nvidia.com/embedded/jetson
📄

Lab Report Requirements

↑ Go Up

Students must submit a comprehensive lab report demonstrating their understanding of neural network implementation and training in PyTorch. The report should showcase practical skills acquired through the six laboratory parts and include evidence of all completed exercises.

⚠️ Submission Deadline:

Submit your completed lab report by [Insert Deadline - Typically 1 week after lab session]. Late submissions will be penalized according to course policy (10% per day, maximum 3 days).

Report Structure

Your lab report must include the following sections:

1. Title Page & Formatting (5 points)

  • Lab title, your name, student ID, date, course name, and instructor name
  • Professional formatting with clear headers and page numbers

2. Objectives (10 points)

  • List all learning objectives
  • Briefly explain why each is important (1-2 sentences each)

3. Procedure & Results (50 points)

For each of the 6 parts:

  • Include code snippets with clear outputs
  • Provide screenshots of key results
  • Add plots where applicable (loss curves, accuracy graphs, etc.)
  • Explain what each part demonstrates

4. Discussion (20 points)

  • Analyze your experimental results
  • Compare different approaches you tried
  • Discuss the impact of hyperparameters (learning rate, network depth, dropout, etc.)
  • Support all statements with evidence from your experiments

5. Challenges & Solutions (10 points)

  • Describe problems you encountered
  • Explain your debugging process
  • Reflect on what you learned from solving these challenges

6. Conclusion (5 points)

  • Summarize key learnings
  • Reflect on the most challenging concepts
  • Discuss potential applications of this knowledge

📋 Submission Checklist

Before submitting, ensure you have:

  • ✓ Completed all 6 lab parts with working, tested code
  • ✓ Included clear screenshots of all outputs, plots, and visualizations
  • ✓ Answered all discussion questions thoroughly with supporting evidence
  • ✓ Documented challenges and solutions in detail
  • ✓ Checked all code for errors and verified all functions execute correctly
  • ✓ Formatted report professionally with clear section headers and page numbers
  • ✓ Referenced all sources and datasets used
  • ✓ Proofread for grammar, spelling, and technical accuracy
  • ✓ Verified all images are clear, properly labeled, and referenced in text
  • ✓ Included your name and student ID on all pages

📤 Submission Format

  • File Format: Submit report as PDF document (required)
  • Code Files: Include Jupyter notebooks (.ipynb) in a separate ZIP file
  • File Naming Convention:
    • Report: Week3_[YourLastName]_[StudentID].pdf
    • Code: Week3_[YourLastName]_[StudentID]_Code.zip
    • Example: Week3_Ahmed_202012345.pdf
  • Submission Method: Upload to University LMS (Blackboard/Moodle)
  • File Size Limit: Maximum 50MB total
    • If exceeded, compress images or use PDF compression tools
    • Ensure PDF is searchable text, not scanned images
  • Required Components:
    • 1. Main PDF lab report
    • 2. ZIP file containing all Jupyter notebooks with outputs
    • 3. Any modified helper files (if applicable)
Important:
  • Ensure PDF is searchable and not password-protected
  • All code must be properly commented and executable
  • Include all necessary imports and dependencies
  • Test that your notebooks run completely from top to bottom

📊 Grading Rubric

Component Points Criteria
Title Page & Formatting 5 Complete, professional
Objectives 10 Clear, comprehensive
Procedure & Results 50 All parts complete, correct code, proper outputs
Discussion 20 Thoughtful analysis, supported by results
Challenges & Solutions 10 Detailed problem-solving process
Conclusion 5 Reflective, insightful
Total 100

Grading Notes:

  • All code must execute without errors for full credit
  • Screenshots must be clear, properly labeled, and referenced
  • All discussion questions must be answered with supporting evidence
  • Late penalty: 10% per day (up to 3 days)
  • Plagiarism will result in zero credit