GNN using PyTorch
- ashvineek9
- Nov 9, 2021
- 2 min read
Spektral is an easy library used to create graph neural network for graph classification. A graph is a mathematical object that represents relations between entities. We call the entities "nodes" and the relations "edges".
Colab Code
Run this code in colab to install spektral library.
!pip install spektralImport all the neccessary packages needed
import numpy as np
import tensorflow as tf
import spektralDatasets:
We will be using Cora dataset. It contains 2708 scientific publications. It has seven different classes. The dataset contains 5429 links referencing to the different citations used by the scientific publication.

The raw files can be downloaded from here.
data= spektral.datasets.citation.Citation(name='Cora', random_split=False, normalize_x=False, dtype=np.float32)Split the dataset into train, validation and test.
train_mask, val_mask, test_mask = data.mask_te, data.mask_va, data.mask_teCheck the graph info.
data.graphsOutput
[Graph(n_nodes=2708, n_node_features=1433, n_edge_features=None, n_labels=7)]Get Adjacent nodes present in the graph.
adj = data.graphs[0].a
print(adj)Output:
(0, 633) 1.0
(0, 1862) 1.0
(0, 2582) 1.0
(1, 2) 1.0
(1, 652) 1.0
...Get the features of each nodes present in the graph.
features = data.graphs[0].x
featuresOutput:
array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]], dtype=float32)Checking the shape of the feature vector.
features.shapeOutput:
(2708, 1433)Get all the labels from the graph.
labels = data.graphs[0].y
labelsOutput:
array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 1., 0., 0.],
[0., 0., 0., ..., 1., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]], dtype=float32)Check the label shape.
labels.shapeOutput:
(2708, 7)The adjacent matrix need to be converted from sparse matrix to dense so that it can be passed to the GNN model for training.
adj = adj.todense() + np.eye(adj.shape[0])
adj = adj.astype('float32')Let's check number of samples present in training samples, validation samples and test samples.
np.sum(train_mak), np.sum(val_mask), np.sum(test_mask)Output:
(1000, 500, 1000)Next section will be on Graph Neural Network.









Comments