-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifier.py
More file actions
163 lines (111 loc) · 4.35 KB
/
Copy pathclassifier.py
File metadata and controls
163 lines (111 loc) · 4.35 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import time
import sys
import numpy as np
import pandas as pd
import torch.nn as nn
import torch
from sklearn.model_selection import train_test_split
from torch.utils.data import Dataset, DataLoader
import torch.optim as optim
torch.manual_seed(42)
# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device = torch.device('cuda')
print(f"GPU used --> {torch.cuda.get_device_name(0)}")
df = pd.read_csv("fashion-mnist_train.csv")
X = df.iloc[:, 1:].values
y = df.iloc[:, 0 ].values
X = X/255.0
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7, random_state=42)
class CustomDataset(Dataset):
def __init__(self, features, labels):
self.features = torch.tensor(features, dtype = torch.float32)
self.labels = torch.tensor(labels , dtype = torch.long)
def __len__(self):
return len(self.features)
def __getitem__(self,index):
return self.features[index], self.labels[index]
train_dataset = CustomDataset(X_train,y_train)
test_dataset = CustomDataset(X_test,y_test)
batchSize = 100
trainLoader = DataLoader(train_dataset, batch_size = batchSize, shuffle = True)
testLoader = DataLoader(test_dataset , batch_size = batchSize, shuffle = False)
class myNN(nn.Module):
def __init__(self,num_features):
super().__init__()
self.model = nn.Sequential(
nn.Linear(num_features, num_features//2),
nn.ReLU(),
nn.Linear(num_features//2,num_features//4),
nn.ReLU(),
nn.Linear(num_features//4,10)
)
def forward(self, x):
return self.model(x)
epochs = 100
learning_rate = 0.1
model = myNN(X_train.shape[1])
model = model.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(),lr = learning_rate)
def render(lines):
sys.stdout.write(f"\033[{len(lines)}A")
sys.stdout.write("\n".join(lines) + "\n")
sys.stdout.flush()
lines = [
"+-----------------------------------------+",
"|......Training beginning in : 5 sec .....|",
"+-----------------------------------------+"
]
print("\n".join(lines))
for t in range(5, -1, -1):
render([
"+-----------------------------------------+",
f"|......Training beginning in : {t} sec .....|",
"+-----------------------------------------+"
])
time.sleep(1)
print()
startTrain = time.time()
for epoch in range(epochs):
total_epoch_loss = 0
start = time.time()
for batch_features, batch_labels in trainLoader:
batch_features, batch_labels = batch_features.to(device), batch_labels.to(device)
output = model(batch_features)
loss = criterion(output, batch_labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_epoch_loss += loss.item()
end = time.time()
avg_loss = total_epoch_loss/len(trainLoader)
tame = end-start
print(f"Epoch: [{epoch+1}] Loss: [{avg_loss:.5f}] Time Taken: {tame:.4f} sec")
endTrain = time.time()
timeTrain = endTrain-startTrain
model.eval()
total = 0
correct = 0
with torch.no_grad():
for batch_features, batch_labels in testLoader:
batch_features, batch_labels = batch_features.to(device), batch_labels.to(device)
output = model(batch_features)
_, predicted = torch.max(output, 1)
total += batch_labels.shape[0]
correct += (predicted == batch_labels).sum().item()
print()
print( "==============================================================================")
print(f"Accuracy: {(correct/total)*100:.2f}% Total Time taken training: {timeTrain:.5f} sec")
print( "==============================================================================")
total = 0
correct = 0
for batch_features, batch_labels in trainLoader:
batch_features, batch_labels = batch_features.to(device), batch_labels.to(device)
output = model(batch_features)
_, predicted = torch.max(output, 1)
total += batch_labels.shape[0]
correct += (predicted == batch_labels).sum().item()
print()
print( "==============================================================================")
print(f"Accuracy: {(correct/total)*100:.2f}% Total Time taken training: {timeTrain:.5f} sec")
print( "==============================================================================")