-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example
PMLP
on Cora dataset (#7543)
Co-authored-by: rusty1s <matthias.fey@tu-dortmund.de>
- Loading branch information
1 parent
09cb440
commit 0549077
Showing
3 changed files
with
71 additions
and
4 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,58 @@ | ||
import os.path as osp | ||
|
||
import torch | ||
import torch.nn.functional as F | ||
|
||
import torch_geometric.transforms as T | ||
from torch_geometric.datasets import Planetoid | ||
from torch_geometric.nn import PMLP | ||
|
||
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | ||
|
||
path = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data', 'Planetoid') | ||
dataset = Planetoid(path, name='Cora', transform=T.NormalizeFeatures()) | ||
data = dataset[0].to(device) | ||
|
||
model = PMLP( | ||
in_channels=dataset.num_features, | ||
hidden_channels=16, | ||
out_channels=dataset.num_classes, | ||
num_layers=2, | ||
dropout=0.5, | ||
norm=False, | ||
).to(device) | ||
|
||
optimizer = torch.optim.Adam(model.parameters(), weight_decay=5e-4, lr=0.01) | ||
|
||
|
||
def train(): | ||
model.train() | ||
optimizer.zero_grad() | ||
out = model(data.x) # MLP during training. | ||
loss = F.cross_entropy(out[data.train_mask], data.y[data.train_mask]) | ||
loss.backward() | ||
optimizer.step() | ||
return float(loss) | ||
|
||
|
||
@torch.no_grad() | ||
def test(): | ||
model.eval() | ||
out = model(data.x, data.edge_index) | ||
pred = out.argmax(dim=-1) | ||
|
||
accs = [] | ||
for mask in [data.train_mask, data.val_mask, data.test_mask]: | ||
accs.append(int((pred[mask] == data.y[mask]).sum()) / int(mask.sum())) | ||
return accs | ||
|
||
|
||
best_val_acc = final_test_acc = 0 | ||
for epoch in range(1, 201): | ||
loss = train() | ||
train_acc, val_acc, tmp_test_acc = test() | ||
if val_acc > best_val_acc: | ||
best_val_acc = val_acc | ||
test_acc = tmp_test_acc | ||
print(f'Epoch: {epoch:03d}, Train: {train_acc:.4f}, Val: {val_acc:.4f}, ' | ||
f'Test: {test_acc:.4f}') |
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