-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
123 lines (93 loc) · 3.37 KB
/
main.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
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
import pandas as pd
import numpy as np
import torch
from torch.utils.data.sampler import SubsetRandomSampler
from torch.utils.data import Dataset
from torch import nn
# Class to load .cs file and convert into tensors
class CustomDataset(Dataset):
def __init__(self, path):
self.data = pd.read_csv(path)
self.y = torch.tensor(self.data['target'].values.astype(np.float32))
self.x = torch.tensor(self.data.loc[:, self.data.columns != 'target'].values.astype(np.float32))
def __getitem__(self, index):
X, Y = self.x[index], self.y[index]
return X, Y
def __len__(self):
return (len(self.x))
dataset = CustomDataset("/home/surya/PycharmProjects/clevland_heart_disease/heart.csv")
# Defining Hyper parameters
batch_size = 100
test_split = 0.2
shuffle_dataset = True
random_seed = 42
dataset_size = len(dataset)
# Splitting Dataset into train and test.
indices = list(range(dataset_size))
split = int(np.floor(test_split * dataset_size))
if shuffle_dataset:
np.random.seed(random_seed)
np.random.shuffle(indices)
train_indices, test_indices = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_indices)
test_sampler = SubsetRandomSampler(test_indices)
train_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, sampler=train_sampler)
test_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, sampler=test_sampler)
# Network Parameters
input_size = 13
hidden_size = [56, 56]
output_size = 1
device = "cuda" if torch.cuda.is_available() else "cpu"
class Network(nn.Module):
def __init__(self):
super().__init__()
self.hidden = nn.Linear(input_size, hidden_size[0])
self.output = nn.Linear(hidden_size[1], output_size)
self.softmax = nn.Softmax()
self.relu = nn.ReLU()
def forward(self, x):
x = self.hidden(x)
x = self.relu(x)
x = self.output(x)
x = self.softmax(x)
return x
network = Network()
network.cuda()
criterion = nn.BCELoss()
criterion.cuda()
optimizer = torch.optim.Adam(network.parameters(), lr=0.01)
def train(dataloader, network, criterion, optimizer):
size = len(dataloader)
network.train()
for batch, (X, y) in enumerate(dataloader):
X, y = X.to(device), y.to(device)
y = y.unsqueeze(1)
pred = network(X)
loss = criterion(pred, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if batch % 100 == 0:
loss, current = loss.item(), batch * len(X)
print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
def test(dataloader, model, loss_fn):
size = len(dataloader.dataset)
num_batches = len(dataloader)
network.eval()
test_loss, correct = 0, 0
with torch.no_grad():
for X, y in dataloader:
X, y = X.to(device), y.to(device)
y = y.unsqueeze(1)
pred = network(X)
test_loss += loss_fn(pred, y).item()
correct += (pred.argmax(1) == y).type(torch.float).sum().item()
test_loss /= num_batches
correct /= size
print(f"Test Error: \n Accuracy: {(100 * correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")
epochs = 300
for t in range(epochs):
print(f"Epoch {t + 1}\n-------------------------------")
train(train_loader, network, criterion, optimizer)
test(test_loader, network, criterion)
print("Done!")