-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
169 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import os.path as osp | ||
|
||
import torch | ||
from torch_geometric.datasets import BitcoinOTC | ||
from torch_geometric.nn import SignedGCN | ||
|
||
name = 'BitcoinOTC-1' | ||
path = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data', name) | ||
dataset = BitcoinOTC(path, edge_window_size=1) | ||
|
||
# Generate dataset. | ||
pos_edge_indices, neg_edge_indices = [], [] | ||
for data in dataset: | ||
pos_edge_indices.append(data.edge_index[:, data.edge_attr > 0]) | ||
neg_edge_indices.append(data.edge_index[:, data.edge_attr < 0]) | ||
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | ||
pos_edge_index = torch.cat(pos_edge_indices, dim=1).to(device) | ||
neg_edge_index = torch.cat(neg_edge_indices, dim=1).to(device) | ||
|
||
# Build and train model. | ||
model = SignedGCN(64, 64, num_layers=2, lamb=5).to(device) | ||
optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4) | ||
|
||
train_pos_edge_index, test_pos_edge_index = model.split_edges(pos_edge_index) | ||
train_neg_edge_index, test_neg_edge_index = model.split_edges(neg_edge_index) | ||
x = model.create_spectral_features(train_pos_edge_index, train_neg_edge_index) | ||
|
||
|
||
def train(): | ||
model.train() | ||
optimizer.zero_grad() | ||
z = model(x, train_pos_edge_index, train_neg_edge_index) | ||
loss = model.loss(z, train_pos_edge_index, train_neg_edge_index) | ||
loss.backward() | ||
optimizer.step() | ||
return loss.item() | ||
|
||
|
||
def test(): | ||
model.eval() | ||
with torch.no_grad(): | ||
z = model(x, train_pos_edge_index, train_neg_edge_index) | ||
return model.test(z, test_pos_edge_index, test_neg_edge_index) | ||
|
||
|
||
for epoch in range(201): | ||
loss = train() | ||
auc, f1 = test() | ||
print('Epoch: {:03d}, Loss: {:.4f}, AUC: {:.4f}, F1: {:.4f}'.format( | ||
epoch, loss, auc, f1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters