-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_script.py
97 lines (82 loc) · 3.38 KB
/
example_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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 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 = 'dir_1/dir_2/dir_3' # Replace with train CSV file path
train_dataset = MNISTDataset(train_data_path)
test_data_path = 'dir_1/dir_2/dir_3' # Replace with test CSV file path
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)