-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
executable file
·178 lines (154 loc) · 5.54 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from __future__ import print_function
import torch
import argparse
import numpy as np
import pandas as pd
import sys
import os
BATCH_SIZE = 8192
EPOCHS = 100000
LEARNING_RATE = 0.0001
count = 0
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = torch.nn.Linear(38, 36)
self.dense1_bn = torch.nn.BatchNorm1d(36)
self.fc2 = torch.nn.Linear(36, 32)
self.dense2_bn = torch.nn.BatchNorm1d(32)
self.fc3 = torch.nn.Linear(32, 32)
self.dense3_bn = torch.nn.BatchNorm1d(32)
self.fc4 = torch.nn.Linear(32, 16)
self.dense4_bn = torch.nn.BatchNorm1d(16)
self.fc5 = torch.nn.Linear(16, 8)
self.dense5_bn = torch.nn.BatchNorm1d(8)
self.fc6 = torch.nn.Linear(8, 3)
self.softmax = torch.nn.Softmax(dim=1)
def forward(self, x):
x = self.fc1(x)
x = torch.nn.functional.relu(x)
x = torch.nn.functional.dropout(x)
x = self.fc2(x)
x = self.dense2_bn(x)
x = torch.nn.functional.relu(x)
x = torch.nn.functional.dropout(x)
x = self.fc3(x)
x = self.dense3_bn(x)
x = torch.nn.functional.relu(x)
x = torch.nn.functional.dropout(x)
x = self.fc4(x)
x = self.dense4_bn(x)
x = torch.nn.functional.relu(x)
x = torch.nn.functional.dropout(x)
x = self.fc5(x)
x = self.dense5_bn(x)
x = torch.nn.functional.relu(x)
x = torch.nn.functional.dropout(x)
x = self.fc6(x)
x = self.softmax(x)
return x
# pass
def create_tensor(req):
to_drop=[]
global count
array_now = req.loc[:,['Age', 'Agency_Text1', 'Agency_Text2',
'Agency_Text3', 'Agency_Text4', 'Sex_Code_Text1', 'Sex_Code_Text2',
'Ethnic_Code_Text1', 'Ethnic_Code_Text2', 'Ethnic_Code_Text3',
'Ethnic_Code_Text4', 'Ethnic_Code_Text5', 'Ethnic_Code_Text6',
'Ethnic_Code_Text7', 'Ethnic_Code_Text8', 'Ethnic_Code_Text9',
'Language1', 'Language2', 'LegalStatus1', 'LegalStatus2',
'LegalStatus3', 'LegalStatus4', 'LegalStatus5', 'LegalStatus6',
'LegalStatus7', 'CustodyStatus1', 'CustodyStatus2',
'CustodyStatus3', 'CustodyStatus4', 'CustodyStatus5',
'CustodyStatus6', 'MaritalStatus1', 'MaritalStatus2',
'MaritalStatus3', 'MaritalStatus4', 'MaritalStatus5',
'MaritalStatus6', 'MaritalStatus7']].values
array_now = torch.cuda.FloatTensor(array_now)
array_now.cuda()
result_now = req["DecileScore"].values
# print("result_now: ", result_now)
result_now =torch.cuda.LongTensor(result_now)
result_now.cuda()
return array_now, result_now
# Read the data
parser = argparse.ArgumentParser(description='Algo_bias')
parser.add_argument('--batch-size', type=int, default=BATCH_SIZE, metavar='N',
help='input batch size for training (default: 9)')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
parser.add_argument('--epochs', type=int, default=EPOCHS, metavar='N',
help='number of epochs to train (default: 10)')
parser.add_argument('--lr', type=float, default=LEARNING_RATE, metavar='LR',
help='learning rate (default: 0.01)')
# parser.add_argument('--momentum', type=float, default=SGD_MOMENTUM, metavar='M',
# help='SGD momentum (default: 0.5)')
args = parser.parse_args()
use_cuda = not args.no_cuda and torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
model = Net().to(device)
print("Would you like to restore a previously saved model? (y/n)")
choice = raw_input()
if (choice=='y' or choice=='Y'):
path = raw_input("Enter path: ")
model.load_state_dict(torch.load(path))
print("\n")
train_flag = True
print("Train? (y for train, n for test)")
choice = raw_input()
if (choice =='n' or choice=='N'):
df = pd.read_csv("data/out-test.csv")
BATCH_SIZE = df.shape[0]
EPOCHS = 1
train_flag = False
model = model.eval()
else:
df = pd.read_csv("data/out-train.csv")
# cols = np.array([3,7,8,9,10,13,14,15,16,17,18,20,23,28])
# cols += 1
# df = df[df.columns[cols]]
predicted = []
correct = 0
if train_flag:
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr = LEARNING_RATE)
# scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', factor = 0.01, patience = 1000, verbose = True)
for j in xrange(EPOCHS):
losses=0.0
global count
count = 0
df = df.sample(frac=1).reset_index(drop=True)
for i in xrange(((df.shape[0])/BATCH_SIZE) +1):
if not train_flag:
if (count>=(df.shape[0])-1):
break
curr = i * BATCH_SIZE
if train_flag:
req = df.loc[curr:curr+BATCH_SIZE-1, :]
else:
req = df.loc[curr:curr+BATCH_SIZE, :]
final_tensor, result = create_tensor(req)
if train_flag:
optimizer.zero_grad()
output = model(final_tensor)
loss = criterion(output, result)
loss.backward()
optimizer.step()
# scheduler.step(loss)
losses+=loss.data[0]
else:
output = model(final_tensor)
_,predicted = torch.max(output.data, 1)
correct += (predicted == result).sum().item()
count+=BATCH_SIZE
if count>df.shape[0]:
count=df.shape[0]
print("Batches done: "+str(count)+"/"+str(df.shape[0]),end="\r")
losses/=count
if train_flag:
print("\nIteration "+str(j+1)+"/"+str(EPOCHS)+" done")
print("\nLoss: "+ str(losses))
if train_flag:
os.system("mkdir -p checkpoints")
torch.save(model.state_dict(), "checkpoints/"+str(j))
if not train_flag:
print("Accuracy of the network on the 10000 test cases:" + str( 100 * correct / df.shape[0]))