-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNN_pytorch.py
More file actions
179 lines (121 loc) · 5.41 KB
/
NN_pytorch.py
File metadata and controls
179 lines (121 loc) · 5.41 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 16 09:02:36 2019
@author: peterawest
"""
import random
import torch
import numpy as np
import math
from torch import nn
MAX_LENGTH = 100
sm = torch.nn.Softmax(dim=0)
sig = torch.nn.Sigmoid()
sig = torch.nn.Sigmoid()
class NN_PT_2(nn.Module):
def __init__(self, input_dim, hidden_dim = 10, PW = False):
super(NN_PT_2, self).__init__()
self.PW = PW
if self.PW:
input_dim = input_dim + 1
self.linear0 = torch.nn.Linear(input_dim, hidden_dim)
self.linear1 = torch.nn.Linear(hidden_dim, 1)
def forward(self, x):
if self.PW:
input_tens = torch.cat([torch.tensor(x[0]).float(),torch.tensor([x[1]]).float()] )
outputs = self.linear1( sig( self.linear0( input_tens)))
else:
outputs = self.linear1( sig( self.linear0( torch.tensor(x).float() ) ) )
return sig(outputs)
class NN_PT_3(nn.Module):
def __init__(self, input_dim, hidden_dim_0 = 50, hidden_dim_1 = 10, PW = False):
super(NN_PT_3, self).__init__()
self.PW = PW
if self.PW:
input_dim = input_dim + 1
self.linear0 = torch.nn.Linear(input_dim, hidden_dim_0)
self.linear1 = torch.nn.Linear(hidden_dim_0, hidden_dim_1)
self.linear2 = torch.nn.Linear(hidden_dim_1, 1)
def forward(self, x):
if self.PW:
input_tens = torch.cat([torch.tensor(x[0]).float(),torch.tensor([x[1]]).float()] )
outputs = self.linear2 ( sig( self.linear1( sig( self.linear0( input_tens)))))
else:
outputs = self.linear1( sig( self.linear0( torch.tensor(x).float() ) ) )
return sig(outputs)
class NN_PT_propensity_model():
def __init__(self, n_it = 100000, val_interval = 1000, batch_size = 1, lr = 0.001, input_dim = 768, layers = 2,
experiment_name = 'NN', PW = False):
if layers ==2:
self.model = NN_PT_2(input_dim, PW = PW)
elif layers == 3:
self.model = NN_PT_3(input_dim, PW = PW)
else:
assert(False)
self.batch_size = batch_size
self.val_interval = val_interval
self.n_it = n_it # number of training iterations
self.lr = lr
assert(val_interval < n_it)
self.experiment_name = experiment_name
def fit(self, dataset):
self.learning_curve = []
opt = torch.optim.Adam(self.model.parameters(), lr=self.lr)
opt.zero_grad()
min_val_loss = None
### loop over sgd iterations
for it, (x,label,_) in enumerate(dataset.train_epoch(size=self.n_it)):
# ex = random.randint(0, len(X_train) - 1)
# x = X_train[ex]
# label = Z_train[ex]
logit = self.model.forward(x)
loss = -(float(label)*torch.log(logit) + float(1-label)*torch.log(1-logit))
if math.isnan(loss.item()):
print('Is NAN! iteration: {}',format(it))
self.lr = self.lr/2.
print('reloading model, dropping lr to {}'.format(self.lr))
self.load_best()
opt = torch.optim.Adam(self.model.parameters(), lr=self.lr)
opt.zero_grad()
continue
loss.backward()
if (it % self.batch_size == 0) and it > 0:
torch.cuda.empty_cache()
opt.step()
opt.zero_grad()
torch.cuda.empty_cache()
if (it % self.val_interval == 0):
torch.cuda.empty_cache()
with torch.no_grad():
#print('starting validation')
val_loss = 0
for val_i, (x, label,_) in enumerate(dataset.valid_epoch()):
# x = X_val[val_i]
# label = Z_val[val_i]
logit = self.model.forward(x)
loss = -(float(label)*torch.log(logit) + float((1-label))*torch.log(1-logit))
val_loss += float(loss.item())
self.learning_curve += [val_loss]
#print('val_loss: {}'.format(val_loss))
if min_val_loss is None or val_loss < min_val_loss:
min_val_loss = val_loss
self.save_best()
# torch.save(self.model.state_dict(),'best.pt')
elif val_loss > 1.5*min_val_loss:
break
self.load_best()
# self.model.load_state_dict(torch.load('best.pt'))
def load_best(self):
self.model.load_state_dict(torch.load('{}_best.pt'.format(self.experiment_name)))
def save_best(self):
torch.save(self.model.state_dict(),'{}_best.pt'.format(self.experiment_name))
def score(self, X):
n_ex = len(X)
scores = np.zeros(n_ex)
for i in range(len(X)):
x = X[i]
with torch.no_grad():
logit = self.model.forward(x).item()
scores[i] = logit
return scores