Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Comments and Docstrings to main.py and api.py #153

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from fastapi import FastAPI, UploadFile, File
from PIL import Image
import torch
from torchvision import transforms
from fastapi import FastAPI, File, UploadFile
from main import Net # Importing Net class from main.py
from PIL import Image
from torchvision import transforms

# Load the model
model = Net()
Expand Down
22 changes: 19 additions & 3 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
from PIL import Image
"""
This module contains the implementation of a neural network model using PyTorch. It includes the data loading and preprocessing step,
definition of the neural network architecture, and the training process for the MNIST dataset. It also contains code to save the trained model.
"""

import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from PIL import Image
from torch.utils.data import DataLoader
import numpy as np
from torchvision import datasets, transforms

# Step 1: Load MNIST Data and Preprocess
# The 'transform' variable defines a series of preprocessing steps to be applied to the MNIST dataset images.
# It converts images to PyTorch tensors and normalizes them so that the pixel values are in the range [-1, 1].
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
Expand All @@ -17,6 +24,12 @@

# Step 2: Define the PyTorch Model
class Net(nn.Module):
"""
The Net class defines a simple feed-forward neural network model with one hidden layer.
Methods:
__init__: Initializes the layers of the neural network.
forward: Computes the forward pass of the network given an input tensor.
"""
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(28 * 28, 128)
Expand All @@ -31,8 +44,11 @@ def forward(self, x):
return nn.functional.log_softmax(x, dim=1)

# Step 3: Train the Model
# 'model' is an instance of the Net class which represents the neural network model.
model = Net()
# 'optimizer' defines the optimization algorithm (Stochastic Gradient Descent) used to update model parameters.
optimizer = optim.SGD(model.parameters(), lr=0.01)
# 'criterion' is the loss function used to measure how well the model performs during training. Here, Negative Log Likelihood Loss is used.
criterion = nn.NLLLoss()

# Training loop
Expand Down
Loading