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 error logs to the training loop #12

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 15 additions & 7 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from PIL import Image
import torch
import torch.nn as nn
Expand All @@ -6,6 +7,10 @@
from torch.utils.data import DataLoader
import numpy as np

# Set up logging
logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger('training')

# Step 1: Load MNIST Data and Preprocess
transform = transforms.Compose([
transforms.ToTensor(),
Expand Down Expand Up @@ -35,14 +40,17 @@ def forward(self, x):
optimizer = optim.SGD(model.parameters(), lr=0.01)
criterion = nn.NLLLoss()

# Training loop
# Training loop with error logging
epochs = 3
for epoch in range(epochs):
for images, labels in trainloader:
optimizer.zero_grad()
output = model(images)
loss = criterion(output, labels)
loss.backward()
optimizer.step()
try:
for images, labels in trainloader:
optimizer.zero_grad()
output = model(images)
loss = criterion(output, labels)
loss.backward()
optimizer.step()
except Exception as e:
logger.error("Exception occurred", exc_info=True)

torch.save(model.state_dict(), "mnist_model.pth")
Loading