-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f739b0d
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.optim as optim | ||
import pandas as pd | ||
from torch.utils.data import Dataset, DataLoader | ||
from tqdm import tqdm | ||
from sklearn.metrics import accuracy_score, f1_score | ||
|
||
# Define a custom dataset | ||
class MNISTDataset(Dataset): | ||
def __init__(self, csv_file): | ||
self.data = pd.read_csv(csv_file) | ||
self.labels = self.data.iloc[:, 0].values | ||
self.images = self.data.iloc[:, 1:].values.reshape(-1, 28, 28).astype('float32') | ||
|
||
def __len__(self): | ||
return len(self.data) | ||
|
||
def __getitem__(self, idx): | ||
image = torch.tensor(self.images[idx]).unsqueeze(0) | ||
label = torch.tensor(self.labels[idx], dtype=torch.long) | ||
return image, label | ||
|
||
# Load the data | ||
train_data_path = 'mnist_train.csv' # Replace with your CSV file path | ||
train_dataset = MNISTDataset(train_data_path) | ||
|
||
test_data_path = 'mnist_test.csv' | ||
test_dataset = MNISTDataset(test_data_path) | ||
|
||
# DataLoader objects | ||
batch_size = 64 | ||
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True) | ||
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False) | ||
|
||
# Define a model architecture | ||
class SimpleNN(nn.Module): | ||
def __init__(self): | ||
super(SimpleNN, self).__init__() | ||
self.fc1 = nn.Linear(28 * 28, 128) | ||
self.fc2 = nn.Linear(128, 64) | ||
self.fc3 = nn.Linear(64, 10) | ||
|
||
def forward(self, x): | ||
x = x.view(-1, 28 * 28) | ||
x = torch.relu(self.fc1(x)) | ||
x = torch.relu(self.fc2(x)) | ||
x = self.fc3(x) | ||
return x | ||
|
||
# Initialize network, loss function, and optimizer | ||
model = SimpleNN() | ||
criterion = nn.CrossEntropyLoss() | ||
optimizer = optim.Adam(model.parameters(), lr=0.001) | ||
|
||
# Training function | ||
def train_model(model, train_loader, criterion, optimizer, num_epochs=5): | ||
for epoch in range(num_epochs): | ||
model.train() | ||
running_loss = 0.0 | ||
all_labels = [] | ||
all_preds = [] | ||
for images, labels in tqdm(train_loader, desc=f"Epoch {epoch+1}/{num_epochs}"): | ||
outputs = model(images) | ||
loss = criterion(outputs, labels) | ||
optimizer.zero_grad() | ||
loss.backward() | ||
optimizer.step() | ||
|
||
running_loss += loss.item() | ||
_, preds = torch.max(outputs, 1) | ||
all_labels.extend(labels.cpu().numpy()) | ||
all_preds.extend(preds.cpu().numpy()) | ||
|
||
train_accuracy = accuracy_score(all_labels, all_preds) | ||
train_f1 = f1_score(all_labels, all_preds, average='weighted') | ||
print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {running_loss/len(train_loader):.4f}, Train Accuracy: {train_accuracy:.4f}, Train F1 Score: {train_f1:.4f}") | ||
|
||
# Evaluation function | ||
def evaluate_model(model, test_loader): | ||
model.eval() | ||
all_labels = [] | ||
all_preds = [] | ||
with torch.no_grad(): | ||
for images, labels in tqdm(test_loader, desc="Evaluating"): | ||
outputs = model(images) | ||
_, preds = torch.max(outputs, 1) | ||
all_labels.extend(labels.cpu().numpy()) | ||
all_preds.extend(preds.cpu().numpy()) | ||
|
||
test_accuracy = accuracy_score(all_labels, all_preds) | ||
test_f1 = f1_score(all_labels, all_preds, average='weighted') | ||
print(f"Test Accuracy: {test_accuracy:.4f}, Test F1 Score: {test_f1:.4f}") | ||
|
||
# Train and evaluate the model | ||
train_model(model, train_loader, criterion, optimizer, num_epochs=5) | ||
evaluate_model(model, test_loader) |
Binary file not shown.
Binary file not shown.