-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
42 lines (38 loc) · 1.17 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
import torch.nn.functional as F
def gcn_train(model, optimizer, data, labels, mask_train, params):
model.train()
min_loss = 9999
patience = 0
for epoch in range(params['epoch']):
optimizer.zero_grad()
out = model.forward(data)
loss = F.nll_loss(out[mask_train], labels[mask_train])
loss.backward()
optimizer.step()
if loss < min_loss:
min_loss = loss
patience = 0
else:
patience = patience + 1
if patience > params['patience']:
break
weights = [model.conv1.lin.weight, model.conv2.lin.weight]
return weights
def gcn_plabels_train(model, optimizer, data, labels, mask_train, params):
model.train()
min_loss = 9999
patience = 0
for epoch in range(params['epoch']):
optimizer.zero_grad()
out = model.forward(data)
loss = F.nll_loss(out[mask_train], labels[mask_train])
loss.backward()
optimizer.step()
if loss < min_loss:
min_loss = loss
patience = 0
else:
patience = patience + 1
if patience > params['patience']:
break
return