-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
162 lines (137 loc) · 4.58 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
import torch
import torch.optim as optim
import torch.nn.functional as F
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_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