Spaces:
Running
Running
Delete pages/14_MNIST.py
Browse files- pages/14_MNIST.py +0 -92
pages/14_MNIST.py
DELETED
|
@@ -1,92 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import torch
|
| 3 |
-
import torch.nn as nn
|
| 4 |
-
import torch.optim as optim
|
| 5 |
-
import torchvision
|
| 6 |
-
import torchvision.transforms as transforms
|
| 7 |
-
import matplotlib.pyplot as plt
|
| 8 |
-
|
| 9 |
-
# Define the neural network
|
| 10 |
-
class Net(nn.Module):
|
| 11 |
-
def __init__(self):
|
| 12 |
-
super(Net, self).__init__()
|
| 13 |
-
self.fc1 = nn.Linear(28 * 28, 128)
|
| 14 |
-
self.fc2 = nn.Linear(128, 64)
|
| 15 |
-
self.fc3 = nn.Linear(64, 10)
|
| 16 |
-
|
| 17 |
-
def forward(self, x):
|
| 18 |
-
x = x.view(-1, 28 * 28)
|
| 19 |
-
x = torch.relu(self.fc1(x))
|
| 20 |
-
x = torch.relu(self.fc2(x))
|
| 21 |
-
x = self.fc3(x)
|
| 22 |
-
return x
|
| 23 |
-
|
| 24 |
-
# Function to train the model
|
| 25 |
-
def train_model(num_epochs):
|
| 26 |
-
# Define transformations
|
| 27 |
-
transform = transforms.Compose([
|
| 28 |
-
transforms.ToTensor(),
|
| 29 |
-
transforms.Normalize((0.5,), (0.5,))
|
| 30 |
-
])
|
| 31 |
-
|
| 32 |
-
# Load datasets
|
| 33 |
-
trainset = torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transform)
|
| 34 |
-
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
|
| 35 |
-
|
| 36 |
-
testset = torchvision.datasets.MNIST(root='./data', train=False, download=True, transform=transform)
|
| 37 |
-
testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=False)
|
| 38 |
-
|
| 39 |
-
# Initialize the network, loss function, and optimizer
|
| 40 |
-
net = Net()
|
| 41 |
-
criterion = nn.CrossEntropyLoss()
|
| 42 |
-
optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
|
| 43 |
-
|
| 44 |
-
# Track loss over epochs
|
| 45 |
-
loss_values = []
|
| 46 |
-
|
| 47 |
-
# Training loop
|
| 48 |
-
for epoch in range(num_epochs):
|
| 49 |
-
running_loss = 0.0
|
| 50 |
-
for i, data in enumerate(trainloader, 0):
|
| 51 |
-
inputs, labels = data
|
| 52 |
-
optimizer.zero_grad()
|
| 53 |
-
outputs = net(inputs)
|
| 54 |
-
loss = criterion(outputs, labels)
|
| 55 |
-
loss.backward()
|
| 56 |
-
optimizer.step()
|
| 57 |
-
running_loss += loss.item()
|
| 58 |
-
|
| 59 |
-
# Append average loss for this epoch
|
| 60 |
-
loss_values.append(running_loss / len(trainloader))
|
| 61 |
-
st.write(f'Epoch {epoch + 1}, Loss: {running_loss / len(trainloader):.3f}')
|
| 62 |
-
|
| 63 |
-
st.write('Finished Training')
|
| 64 |
-
|
| 65 |
-
# Plot the loss values
|
| 66 |
-
plt.figure(figsize=(10, 5))
|
| 67 |
-
plt.plot(range(1, num_epochs + 1), loss_values, marker='o')
|
| 68 |
-
plt.title('Training Loss over Epochs')
|
| 69 |
-
plt.xlabel('Epoch')
|
| 70 |
-
plt.ylabel('Loss')
|
| 71 |
-
st.pyplot(plt)
|
| 72 |
-
|
| 73 |
-
# Evaluate the network on the test data
|
| 74 |
-
correct = 0
|
| 75 |
-
total = 0
|
| 76 |
-
with torch.no_grad():
|
| 77 |
-
for data in testloader:
|
| 78 |
-
images, labels = data
|
| 79 |
-
outputs = net(images)
|
| 80 |
-
_, predicted = torch.max(outputs.data, 1)
|
| 81 |
-
total += labels.size(0)
|
| 82 |
-
correct += (predicted == labels).sum().item()
|
| 83 |
-
|
| 84 |
-
st.write(f'Accuracy of the network on the 10000 test images: {100 * correct / total}%')
|
| 85 |
-
|
| 86 |
-
# Streamlit interface
|
| 87 |
-
st.title('MNIST Digit Classification with PyTorch')
|
| 88 |
-
num_epochs = st.number_input('Enter number of epochs:', min_value=1, max_value=100, value=10)
|
| 89 |
-
if st.button('Run'):
|
| 90 |
-
train_model(num_epochs)
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|