Created
May 1, 2019 23:48
-
-
Save imirzadeh/81089a224f762678056fdae730183317 to your computer and use it in GitHub Desktop.
gcn.py
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
# import required packages | |
import torch | |
import torch.nn.functional as F | |
from torch_geometric.datasets import Planetoid | |
from torch_geometric.data import DataLoader | |
from torch_geometric.nn import GCNConv | |
# load Cora dataset | |
dataset = Planetoid(root='/tmp/Cora', name='Cora') | |
loader = DataLoader(dataset, batch_size=32, shuffle=True) | |
# define GCN model | |
class GCN(torch.nn.Module): | |
def __init__(self): | |
super(Net, self).__init__() | |
self.conv1 = GCNConv(dataset.num_features, 16) | |
self.conv2 = GCNConv(16, dataset.num_classes) | |
def forward(self, data): | |
x, edge_index = data.x, data.edge_index | |
x = self.conv1(x, edge_index) | |
x = F.relu(x) | |
x = F.dropout(x, training=self.training) | |
x = self.conv2(x, edge_index) | |
return F.log_softmax(x, dim=1) | |
# multi-gpu support | |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | |
model = GCN().to(device) | |
data = dataset[0].to(device) | |
optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4) | |
# train model for 100 epochs | |
model.train() | |
for epoch in range(100): | |
optimizer.zero_grad() | |
out = model(data) | |
loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask]) | |
loss.backward() | |
optimizer.step() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment