Skip to content
Graph Neural Networks
  • AI Chat
  • Code
  • Report
  • !pip install -q torch
    %%capture
    import os
    import torch
    os.environ['TORCH'] = torch.__version__
    os.environ['PYTHONWARNINGS'] = "ignore"
    !pip install torch-scatter -f https://data.pyg.org/whl/torch-${TORCH}.html
    !pip install torch-sparse -f https://data.pyg.org/whl/torch-${TORCH}.html
    !pip install git+https://github.com/pyg-team/pytorch_geometric.git

    Creating Simple Graph

    import networkx as nx
    H = nx.DiGraph()
    H.add_nodes_from([
      (0, {"color": "blue", "size": 250}),
      (1, {"color": "yellow", "size": 400}),
      (2, {"color": "orange", "size": 150}),
      (3, {"color": "red", "size": 600})
    ])
    for node in H.nodes(data=True):
      print(node)
    H.add_edges_from([
      (0, 1),
      (1, 2),
      (1, 0),
      (1, 3),
      (2, 3),
      (3,0)
    ])
    print(H.edges())

    Directed Graph

    node_colors = nx.get_node_attributes(H, "color").values()
    colors = list(node_colors)
    node_sizes = nx.get_node_attributes(H, "size").values()
    sizes = list(node_sizes)
    nx.draw(H, with_labels=True, node_color=colors, node_size=sizes)
    G = H.to_undirected()
    print(G.edges())

    Undirected Graph

    nx.draw(G, with_labels=True, node_color=colors, node_size=sizes)

    Node Classification with Graph Neural Networks

    from torch_geometric.datasets import Planetoid
    from torch_geometric.transforms import NormalizeFeatures
    
    dataset = Planetoid(root='data/Planetoid', name='Cora', transform=NormalizeFeatures())
    
    print(f'Dataset: {dataset}:')
    print('======================')
    print(f'Number of graphs: {len(dataset)}')
    print(f'Number of features: {dataset.num_features}')
    print(f'Number of classes: {dataset.num_classes}')
    
    data = dataset[0]  # Get the first graph object.
    print(data)

    Training a Graph Neural Network (GNN)

    from torch_geometric.nn import GCNConv
    import torch.nn.functional as F
    
    class GCN(torch.nn.Module):
        def __init__(self, hidden_channels):
            super().__init__()
            torch.manual_seed(1234567)
            self.conv1 = GCNConv(dataset.num_features, hidden_channels)
            self.conv2 = GCNConv(hidden_channels, dataset.num_classes)
    
        def forward(self, x, edge_index):
            x = self.conv1(x, edge_index)
            x = x.relu()
            x = F.dropout(x, p=0.5, training=self.training)
            x = self.conv2(x, edge_index)
            return x
    
    model = GCN(hidden_channels=16)
    print(model)

    Visulizing Untrained GCN network

    7-dimensional node embeddings onto a 2D plane.