-
Notifications
You must be signed in to change notification settings - Fork 6
/
train.py
234 lines (207 loc) · 7.06 KB
/
train.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import torch
import torch.optim as optim
import torch.nn.functional as F
import torch.nn as nn
from metrics import accuracy
import time
from time import perf_counter
from utils import get_data_loaders
def train_mlp(model,
train_features,
train_labels,
val_features,
val_labels,
epochs,
weight_decay,
lr,
dropout,
bs):
optimizer = optim.Adam(model.parameters(), lr=lr,
weight_decay=weight_decay)
train_loader, val_loader = get_data_loaders(train_features,
train_labels,
val_features,
val_labels,
bs)
t = perf_counter()
max_acc_val = 0
best_epoch = 0
for epoch in range(epochs):
for feats, labels in train_loader:
model.train()
optimizer.zero_grad()
output = model(feats)
loss_train = F.cross_entropy(output, labels)
loss_train.backward()
optimizer.step()
train_time = perf_counter()-t
with torch.no_grad():
model.eval()
output = model(val_features)
acc_val = accuracy(output, val_labels)
return model, acc_val, train_time
def train_gfnn(model,
train_features,
train_labels,
val_features,
val_labels,
epochs,
weight_decay,
lr,
bs,
patience=50,
verbose=True):
optimizer = optim.Adam(model.parameters(),
lr=lr,
weight_decay=weight_decay)
num_class = model.num_class
train_loader, val_loader = get_data_loaders(train_features,
train_labels,
val_features,
val_labels,
bs)
best = 0
best_ep = 0
wait = 0
loss_func = nn.CrossEntropyLoss()
t = perf_counter()
for epoch in range(epochs):
train_corrects = 0
for xl, y in train_loader:
model.train()
optimizer.zero_grad()
output = model(xl)
loss_train = loss_func(output, y)
loss_train.backward()
optimizer.step()
train_corrects += output.argmax(1).eq(y).double().sum()
# Early stopping
with torch.no_grad():
model.eval()
corrects = 0
for xl, y in val_loader:
output = model(xl)
loss_val = loss_func(output, y)
corrects += output.argmax(1).eq(y).double().sum()
acc_val = corrects.item()/val_labels.size(-1)
if acc_val > best:
if verbose:
print("Epoch\t{} - Val acc: {:.4f}".format(epoch, acc_val))
best = acc_val
best_ep = epoch
wait = 0
torch.save(model.state_dict(), 'best_gfnn.pkl')
else:
wait += 1
if wait == patience:
print("Early stopping at epoch {}".format(epoch))
break
train_time = perf_counter()-t
with torch.no_grad():
print("Loading at epoch {}".format(best_ep))
model.load_state_dict(torch.load('best_gfnn.pkl'))
model.eval()
corrects = 0
for xl, y in val_loader:
output = model(xl)
loss_val = loss_func(output, y)
corrects += output.argmax(1).eq(y).double().sum()
acc_val = corrects.item()/val_labels.size(-1)
acc_train = train_corrects.item()/train_labels.size(-1)
return model, acc_val, train_time
def train_regression(model,
train_features,
train_labels,
val_features,
val_labels,
epochs,
weight_decay,
lr,
dropout):
optimizer = optim.Adam(model.parameters(), lr=lr,
weight_decay=weight_decay)
t = perf_counter()
max_acc_val = 0
best_epoch = 0
for epoch in range(epochs):
model.train()
optimizer.zero_grad()
output = model(train_features)
loss_train = F.cross_entropy(output, train_labels)
loss_train.backward()
optimizer.step()
train_time = perf_counter()-t
with torch.no_grad():
model.eval()
output = model(val_features)
acc_val = accuracy(output, val_labels)
return model, acc_val, train_time
def test_regression(model, test_features, test_labels):
model.eval()
return accuracy(model(test_features), test_labels)
def train_gcn(model,
adj,
features,
labels,
idx_train,
idx_val,
epochs,
weight_decay,
lr,
dropout):
optimizer = optim.Adam(model.parameters(),
lr=lr,
weight_decay=weight_decay)
t = perf_counter()
for epoch in range(epochs):
model.train()
optimizer.zero_grad()
output = model(features, adj)
loss_train = F.nll_loss(output[idx_train], labels[idx_train])
acc_train = accuracy(output[idx_train], labels[idx_train])
loss_train.backward()
optimizer.step()
train_time = perf_counter()-t
with torch.no_grad():
model.eval()
output = model(features, adj)
acc_val = accuracy(output[idx_val], labels[idx_val])
return model, acc_val, train_time
def test_gcn(model, adj, features, labels, idx_test):
model.eval()
output = model(features, adj)
acc_test = accuracy(output[idx_test], labels[idx_test])
return acc_test
def train_kgcn(model,
adj,
features,
labels,
idx_train,
idx_val,
epochs,
weight_decay,
lr,
dropout):
optimizer = optim.Adam(model.parameters(),
lr=lr,
weight_decay=weight_decay)
t = perf_counter()
for epoch in range(epochs):
model.train()
optimizer.zero_grad()
output = model(features, adj)
loss_train = F.cross_entropy(output[idx_train], labels[idx_train])
acc_train = accuracy(output[idx_train], labels[idx_train])
loss_train.backward()
optimizer.step()
train_time = perf_counter()-t
with torch.no_grad():
model.eval()
output = model(features, adj)
acc_val = accuracy(output[idx_val], labels[idx_val])
return model, acc_val, train_time
def test_kgcn(model, adj, features, labels, idx_test):
model.eval()
output = F.softmax(model(features, adj), dim=1)
acc_test = accuracy(output[idx_test], labels[idx_test])
return acc_test