-
Notifications
You must be signed in to change notification settings - Fork 27
/
ppi_geniepath.py
58 lines (45 loc) · 1.81 KB
/
ppi_geniepath.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
import os.path as osp
import torch
from torch_geometric.datasets import PPI
from torch_geometric.data import DataLoader
from sklearn import metrics
# from model_geniepath import GeniePath as Net
from model_geniepath import GeniePathLazy as Net
path = osp.join(osp.dirname(osp.realpath(__file__)), 'data', 'PPI')
train_dataset = PPI(path, split='train')
val_dataset = PPI(path, split='test')
test_dataset = PPI(path, split='test')
train_loader = DataLoader(train_dataset, batch_size=1, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=2, shuffle=False)
test_loader = DataLoader(test_dataset, batch_size=2, shuffle=False)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = Net(train_dataset.num_features, train_dataset.num_classes, device).to(device)
loss_op = torch.nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.005)
def train():
model.train()
total_loss = 0
for data in train_loader:
num_graphs = data.num_graphs
data.batch = None
data = data.to(device)
optimizer.zero_grad()
loss = loss_op(model(data.x, data.edge_index), data.y)
total_loss += loss.item() * num_graphs
loss.backward()
optimizer.step()
return total_loss / len(train_loader.dataset)
def test(loader):
model.eval()
total_micro_f1 = 0
for data in loader:
with torch.no_grad():
out = model(data.x.to(device), data.edge_index.to(device))
pred = (out > 0).float().cpu()
micro_f1 = metrics.f1_score(data.y, pred, average='micro')
total_micro_f1 += micro_f1 * data.num_graphs
return total_micro_f1 / len(loader.dataset)
for epoch in range(1, 101):
loss = train()
acc = test(val_loader)
print('Epoch: {:02d}, Loss: {:.4f}, Acc: {:.4f}'.format(epoch, loss, acc))