-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.py
150 lines (130 loc) · 4.59 KB
/
Model.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
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
import numpy as np
import os
import torch
import torchvision
from time import time
from torchvision import transforms
from torch import nn
from torch.optim import Adam
from torch.utils.data import DataLoader, Dataset
from torch.utils.data.sampler import SubsetRandomSampler
from torch.autograd import Variable
import matplotlib.pyplot as plt
import tqdm
import pandas as pd
class PlutusData(Dataset):
def __init__(self, csv_file, root_dir, transform):
path = os.path.join(root_dir, csv_file)
dat = np.loadtxt(path, delimiter = ",", dtype = np.float32, skiprows = 1)
self.inp = torch.from_numpy(dat[:, 3:11])
self.oup = torch.from_numpy(dat[:, 1:3])
self.samples = dat.shape[0]
self.transform = transform
def __getitem__(self, index):
return self.inp[index], self.oup[index]
def __len__(self):
return self.samples
#network Architecture
class PlutusNet(nn.Module):
def __init__(self, input_size, hidden_size):
super().__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.b1 = nn.Sigmoid()
self.fc2 = nn.Linear(hidden_size, hidden_size)
self.b2 = nn.ReLU()
self.fc3 = nn.Linear(hidden_size, hidden_size)
self.b3 = nn.ReLU()
self.fc4 = nn.Linear(hidden_size, 1)
def forward(self,x):
ex = self.fc1(x)
rev = self.fc1(x)
ex = self.b1(x)
rev = self.b1(x)
ex = self.fc2(x)
rev = self.fc2(x)
ex = self.b2(x)
rev = self.b2(x)
ex = self.fc3(x)
rev = self.fc3(x)
ex = self.b3(x)
rev = self.b3(x)
ex = self.fc4(x)
rev = self.fc4(x)
return torch.cat((ex, rev),1)
#load and normalize data
def constructTransform(mean, std):
mean = (mean,)
std = (std,)
normalization_transforms = [torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(mean, std)]
transform = transforms.Compose(normalization_transforms)
return transform
#train test split
def splitData(dataset):
dataset_size = len(data_set)
indices = list(range(dataset_size))
dataset_size = len(data_set)
indices = list(range(dataset_size))
np.random.shuffle(indices)
split = int(np.floor(test_split * dataset_size))
train_indices, test_indices = indices[split:], indices[:split]
return train_indices, test_indices
def get_device():
if torch.cuda.is_available():
device = torch.device('cuda:0')
else:
device = torch.device('cpu') # don't have GPU
return device
def train(model, x, y, optimizer, criterion):
model.zero_grad()
output = model(x)
loss =criterion(output,y)
loss.backward()
optimizer.step()
return loss, output
def preprocess(fileName):
rawFile = pd.read_csv(fileName)
print(rawFile.dtypes)
rawFile.to_csv("data_new.csv", index = False)
transform = constructTransform(mean = 0, std = 1)
#change the directory to your own when testing, ../content is colab specific
preprocess("data.csv")
data_set = PlutusData("data_new.csv", "../content", transform = transform)
batch_size = 512
test_split = 0.2
train_indices, test_indices = splitData(PlutusData)
train_sampler = SubsetRandomSampler(train_indices)
test_sampler = SubsetRandomSampler(test_indices)
train_loader = torch.utils.data.DataLoader(data_set, batch_size = batch_size, sampler = train_sampler)
test_loader = torch.utils.data.DataLoader(data_set, batch_size = batch_size, sampler = test_sampler)
dataiter = iter(train_loader)
features, labels = dataiter.next()
print("features: ", features, "Label: ", labels)
plutusNet = PlutusNet(input_size = 8, hidden_size = 8)
criterion = nn.MSELoss()
EPOCHS = 500
optimizer = Adam(plutusNet.parameters(), lr = 0.0005)
device = get_device()
loss_tracker = []
tune = 0.000001
for epoch in range(EPOCHS):
loss = torch.tensor([100])
progress_bar = tqdm.notebook.tqdm(train_loader, ncols=1000)
for i, (features, labels) in enumerate(progress_bar):
x_train = Variable(features.view(-1, 8))
labels = Variable(labels)
optimizer.zero_grad()
outputs = plutusNet(x_train)
loss = criterion(outputs, labels)
loss = torch.sqrt(loss + tune)
loss.backward()
optimizer.step()
loss_tracker.append(loss.data)
if (i+1) % 100 == 0 or (i+1) == len(train_loader):
progress_bar.set_description('Epoch [%d/%d], Step [%d/%d], Val, Training Loss: %.4f'
%(epoch+1, EPOCHS, i+1, len(train_loader), loss.data))
plt.plot(loss_tracker)
plt.ylabel("Loss")
plt.xlabel("Step Number")
plt.title("Loss over time")
plt.show()