Face Emoji Week 4

    Face Emoji Week 4

    Week 4 content of face emoji

    By AI Club on 3/10/2025
    0

    Week 4: Introduction to PyTorch

    Hello AI Club members! Welcome to Week 4 of our Face-Emoji project. So far, we've built a solid foundation by detecting faces and facial landmarks. This week, we're taking a step back from facial processing to learn something crucial for our next steps: PyTorch!

    What We'll Learn Today

    • Understanding the basics of PyTorch

    • Building a simple neural network

    • Training a model on synthetic data

    • Setting the foundation for emotion detection

    File you will need: week4_code.py

    Setup Instructions

    Before we start coding, we need to install PyTorch:

    For Windows Users:

    1. Open Anaconda Powershell Prompt

    2. Activate your environment: conda activate face-emoji

    3. Install PyTorch: pip install torch matplotlib

    For macOS Users:

    1. Open Terminal

    2. Install PyTorch using pip: python3.12 -m pip install torch matplotlib

    Understanding Neural Networks

    A neural network is a series of mathematical operations inspired by the human brain. At its simplest:

    1. Input Layer: Takes in our data (like facial landmark coordinates)

    2. Hidden Layers: Process the data through weights and activation functions

    3. Output Layer: Produces the final prediction (like emotion classifications)

    Each connection between neurons has a "weight" that gets adjusted during training to make better predictions.

    PyTorch Basics

    Tensors

    Tensors are the fundamental data structure in PyTorch - similar to arrays or matrices. They can have different dimensions:

    • 1D tensor: A vector (like a list of numbers)

    • 2D tensor: A matrix (like a table of numbers)

    • 3D+ tensor: Higher dimensional data (like images or video)

    # Creating tensors

    import torch

    # From Python list

    x = torch.tensor([1, 2, 3, 4])

    # All zeros

    zeros = torch.zeros(3, 4) # 3x4 matrix of zeros

    # Random values

    random = torch.rand(2, 2) # 2x2 matrix of random values

    Neural Network Layers

    PyTorch makes it easy to build neural networks using nn.Module:

    import torch.nn as nn

    # Linear layer (fully connected)

    linear = nn.Linear(in_features=10, out_features=5)

    # Activation functions

    relu = nn.ReLU() sigmoid = nn.Sigmoid()

    The Training Process

    Training a neural network involves:

    1. Forward Pass: Data flows through the network to make predictions

    2. Loss Calculation: Comparing predictions to actual values

    3. Backward Pass: Computing gradients to see how to adjust weights

    4. Optimization: Updating weights to improve predictions

    The Code Walkthrough

    The provided code includes:

    1. Data Generation: Creates a simple synthetic dataset where points are classified based on their position

    2. Model Definition: A simple neural network with one hidden layer

    3. Training Function: A skeleton for you to implement the training loop

    4. Visualization: Code to view the data and decision boundary

    Understanding the SimpleClassifier

    class SimpleClassifier(nn.Module):

    def init(self, input_size=2, hidden_size=8, output_size=1):

    super(SimpleClassifier, self).__init__()

    # Define the layers

    self.layer1 = nn.Linear(input_size, hidden_size)

    self.activation = nn.ReLU()

    self.layer2 = nn.Linear(hidden_size, output_size)

    def forward(self, x):

    x = self.layer1(x)

    x = self.activation(x)

    x = self.layer2(x)

    return x

    This neural network:

    • Takes 2 input features

    • Has 8 neurons in the hidden layer with ReLU activation

    • Outputs a single value (for binary classification)

    The forward method defines how data flows through the network.

    Your Task: Implement the Training Loop

    We've provided the model architecture and data, but you need to implement the training function. You should ONLY update the train function. Here's what you'll need to do:

    1. Initialize the Loss Function: For binary classification, use nn.BCEWithLogitsLoss()

    2. Initialize the Optimizer: Use torch.optim.SGD or torch.optim.Adam

    3. Forward Pass: Feed the data through the model

    4. Compute Loss: Calculate how far off the predictions are

    5. Backward Pass: Compute gradients with loss.backward()

    6. Optimization Step: Update weights with optimizer.step()

    The code has specific TODO sections for each of these parts.

    Tips for Implementing the Training Loop

    Loss Function

    For binary classification, we use Binary Cross Entropy loss:

    loss_fn = nn.BCEWithLogitsLoss()

    Optimizer

    Adam is a good default optimizer:

    optimizer = torch.optim.Adam(model.parameters(), lr=0.01)

    Forward Pass, Loss Calculation, and Backward Pass

    # Forward pass

    outputs = model(X)

    # Compute loss loss = loss_fn(outputs, y)

    # Backward pass and optimization

    optimizer.zero_grad()

    # Clear previous gradients

    loss.backward()

    # Compute gradients

    optimizer.step()

    # Update weights

    Understanding the Dataset

    Our synthetic dataset is deliberately simple:

    • Each point has 2 features (x and y coordinates)

    • Points are classified as 0 or 1 based on whether x1 + x2 > 0

    • We add some noise to make it more realistic

    This simple dataset lets you focus on understanding PyTorch without the complexity of facial landmarks.

    Resources for Learning More

    To understand PyTorch better, check out:

    1. PyTorch Official Documentation: https://pytorch.org/docs/stable/index.html

    2. PyTorch Tutorials: https://pytorch.org/tutorials/beginner/basics/intro.html

    3. Neural Network Visualization: https://playground.tensorflow.org/

    4. [Recommended watching]: https://www.youtube.com/watch?v=tHL5STNJKag&pp=ygUYY3JlYXRpbmcgbm4gd2l0aCBweXRvcmNo

    Looking Ahead

    Next week, we'll apply what we've learned to facial landmarks. Instead of classifying points in 2D space, we'll use the landmarks we extracted from faces to classify emotions!

    Happy coding! 🧠

    Comments