liveBook · Manning

Graph convolutional network

As explained in Machine Learning Algorithms in Depth

A graph convolutional network is a graph neural network that computes node representations by aggregating information from graph neighborhoods.

A graph convolutional network (GCN) is a graph neural network that learns node representations by passing information across graph relationships. It is suited to relational data such as citation networks, social networks, knowledge graphs, and molecular structures. section 11.4 section 11.5

Represent a graph with the following objects:

  • G=(V,E) contains vertices V and edges E; its adjacency matrix A records whether pairs of nodes are connected. section 11.4
  • X is the node-feature matrix, with shape N×F for N nodes and F features per node. section 11.4
  • Optional edge features form an M×S matrix E, where M is the number of edges and S is the feature size. section 11.4
  • Distinguish node classification within one graph from whole-graph classification, such as classifying a molecule. section 11.4

A GCN layer combines the node features with a learned weight matrix while normalizing graph connectivity. One forward expression is H = D^(-1/2)(A+I)D^(-1/2)XW + b. The identity matrix adds self-connections, allowing each node to retain its own features while incorporating neighborhood information. section 11.6 section 11.4

Classifying documents in CORA

Represent each CORA document as a graph node, each citation as an edge, and each document's word-presence indicators as a binary row in X. A GCN uses these citation relationships and document features to classify nodes. section 11.4

Listing 11.4 Graph convolutional neural network for classifying citation graphs
import numpy as np
import pandas as pd

import tensorflow as tf
from tensorflow import keras

import networkx as nx
from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import LabelEncoder
from sklearn.utils import shuffle
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split

from spektral.layers import GCNConv

from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dropout, Dense
from tensorflow.keras import Sequential
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import TensorBoard, EarlyStopping
from tensorflow.keras.regularizers import l2

import os
from collections import Counter
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt

tf.keras.utils.set_random_seed(42)

SAVE_PATH = "/content/drive/MyDrive/Colab Notebooks/data/"
DATA_PATH = "/content/drive/MyDrive/data/cora/"

column_names = ["paper_id"] + [f"term_{idx}" for idx in range(1433)] + 
 ["subject"]
node_df = pd.read_csv(DATA_PATH + "cora.content", sep="\t", header=None, 
 names=column_names)
print("Node df shape:", node_df.shape)

edge_df = pd.read_csv(DATA_PATH + "cora.cites", sep="\t", header=None, 
 names=["target", "source"])
print("Edge df shape:", edge_df.shape)

nodes = node_df.iloc[:,0].tolist()     
labels = node_df.iloc[:,-1].tolist()  #1
X = node_df.iloc[:,1:-1].values        #1

X = np.array(X,dtype=int)
N = X.shape[0] #the number of nodes
F = X.shape[1] #the size of node features

edge_list = [(x, y) for x, y in zip(edge_df['target'],
 edge_df['source'])]     #2

num_classes = len(set(labels))

print('Number of nodes:', N)
print('Number of features of each node:', F)
print('Labels:', set(labels))
print('Number of classes:', num_classes)

def sample_data(labels, limit=20, val_num=500, test_num=1000):
    label_counter = dict((l, 0) for l in labels)
    train_idx = []

    for i in range(len(labels)):
        label = labels[i]
        if label_counter[label]<limit:
            #add the example to the training data
            train_idx.append(i)
            label_counter[label]+=1

        #exit the loop once we found 20 examples for each class
        if all(count == limit for count in label_counter.values()):
            break

    #get the indices that do not go to traning data
    rest_idx = [x for x in range(len(labels)) if x not in train_idx]
    #get the first val_num
    val_idx = rest_idx[:val_num]
    test_idx = rest_idx[val_num:(val_num+test_num)]
    return train_idx, val_idx,test_idx


train_idx,val_idx,test_idx = sample_data(labels)

#set the mask
train_mask = np.zeros((N,),dtype=bool)
train_mask[train_idx] = True

val_mask = np.zeros((N,),dtype=bool)
val_mask[val_idx] = True

test_mask = np.zeros((N,),dtype=bool)
test_mask[test_idx] = True

print("Training data distribution:\n{}".format(Counter([labels[i] for i in 
 train_idx])))
print("Validation data distribution:\n{}".format(Counter([labels[i] for i 
 in val_idx])))

def encode_label(labels):
    label_encoder = LabelEncoder()
    labels = label_encoder.fit_transform(labels)
    labels = to_categorical(labels)
    return labels, label_encoder.classes_

labels_encoded, classes = encode_label(labels)

G = nx.Graph()               
G.add_nodes_from(nodes)     #3
G.add_edges_from(edge_list)   #3

A = nx.adjacency_matrix(G)    #4
print('Graph info: ', nx.info(G))

# Parameters
channels = 16    #5
dropout = 0.5    #6
l2_reg = 5e-4     #7
learning_rate = 1e-2     #8
epochs = 200     #9
es_patience = 10     #10

# Preprocessing operations
A = GCNConv.preprocess(A).astype('f4')

# Model definition
X_in = Input(shape=(F, ))
fltr_in = Input((N, ), sparse=True)

dropout_1 = Dropout(dropout)(X_in)
graph_conv_1 = GCNConv(channels,
                       activation='relu',
                       kernel_regularizer=l2(l2_reg),
                       use_bias=False)([dropout_1, fltr_in])

dropout_2 = Dropout(dropout)(graph_conv_1)
graph_conv_2 = GCNConv(num_classes,
                       activation='softmax',
                       use_bias=False)([dropout_2, fltr_in])

# Build model
model = Model(inputs=[X_in, fltr_in], outputs=graph_conv_2)
model.compile(optimizer=Adam(learning_rate=learning_rate),
              loss='categorical_crossentropy',
              weighted_metrics=['accuracy'])
model.summary()

# Train model
validation_data = ([X, A], labels_encoded, val_mask)
hist = model.fit([X, A],
          labels_encoded,
          sample_weight=train_mask,
          epochs=epochs,
          batch_size=N,
          validation_data=validation_data,
          shuffle=False,
          callbacks=[
              EarlyStopping(patience=es_patience,  restore_best_weights=True)
          ])     #11

# Evaluate model
X_test = X
A_test = A
y_test = labels_encoded

y_pred = model.predict([X_test, A_test], batch_size=N)    #12
report = classification_report(np.argmax(y_test,axis=1), 
 np.argmax(y_pred,axis=1), target_names=classes)
print('GCN Classification Report: \n {}'.format(report))

layer_outputs = [layer.output for layer in model.layers]
activation_model = Model(inputs=model.input, outputs=layer_outputs)
activations = activation_model.predict([X,A],batch_size=N)

#Get t-SNE Representation
#get the hidden layer representation after the first GCN layer
x_tsne = TSNE(n_components=2).fit_transform(activations[3])

def plot_tSNE(labels_encoded,x_tsne):
    color_map = np.argmax(labels_encoded, axis=1)
    plt.figure(figsize=(10,10))
    for cl in range(num_classes):
        indices = np.where(color_map==cl)
        indices = indices[0]
        plt.scatter(x_tsne[indices,0], x_tsne[indices, 1], label=cl)
    plt.legend()
    plt.show()

plot_tSNE(labels_encoded,x_tsne)

plt.figure()
plt.plot(hist.history['loss'], 'b', lw=2.0, label='train')
plt.plot(hist.history['val_loss'], '--r', lw=2.0, label='val')
plt.title('GNN model')
plt.xlabel('Epochs')
plt.ylabel('Cross-Entropy Loss')
plt.legend(loc='upper right')
plt.show()

plt.figure()
plt.plot(hist.history['accuracy'], 'b', lw=2.0, label='train')
plt.plot(hist.history['val_accuracy'], '--r', lw=2.0, label='val')
plt.title('GNN model')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend(loc='upper left')
plt.show()

Use the implementation listing to inspect a TensorFlow/Keras GCN classifier built with Spektral's GCNConv layer and standard training utilities. section 11.4

How Machine Learning Algorithms in Depth covers it

How deep it goes
Defines graph notation, feature matrices, and a normalized forward equation, then applies the model through an implementation.
Examples
Uses the CORA citation network, binary document-word features, the normalized forward equation, and a TensorFlow implementation.
What it assumes
Assumes familiarity with matrices and neural networks while introducing graph notation, adjacency matrices, node features, and edge features.
What you can do after
Build and apply a GCN classifier for citation graphs using TensorFlow, Keras, and Spektral.
How it connects
Connects graph neural networks, message passing, convolutional neural networks, node classification, and graph classification.

Also explained in

Compare how 4 books explain graph convolutional network →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning Algorithms in Depth. The section links point to the parts of the book it draws on.