Tutorial of Graph Classification by DGL

The official tutorial

Comment about the tutorial

Graph classification process from Here

What are the details before g and after g

class Classifier(nn.Module):
def __init__(self, in_dim, hidden_dim, n_classes):
super(Classifier, self).__init__()
self.conv1 = GraphConv(in_dim, hidden_dim)
self.conv2 = GraphConv(hidden_dim, hidden_dim)
self.classify = nn.Linear(hidden_dim, n_classes)

def forward(self, g):
# Use node degree as the initial node feature. For undirected graphs, the in-degree
# is the same as the out_degree.
h = g.in_degrees().view(-1, 1).float()
# Perform graph convolution and activation function.
h = F.relu(self.conv1(g, h))
h = F.relu(self.conv2(g, h))
g.ndata['h'] = h
# Calculate graph representation by averaging all the node representations.
hg = dgl.mean_nodes(g, 'h')
return self.classify(hg)
class Classifier(nn.Module):
def __init__(self, in_dim, hidden_dim, n_classes):
super(Classifier, self).__init__()
self.conv1 = GraphConv(in_dim, hidden_dim)
self.conv2 = GraphConv(hidden_dim, hidden_dim)
self.classify = nn.Linear(hidden_dim, n_classes)
print(f"n_classes, {n_classes}")
def forward(self, g):
# Use node degree as the initial node feature. For undirected graphs, the in-degree
# is the same as the out_degree.
h = g.in_degrees().view(-1, 1).float()
# Perform graph convolution and activation function.
h = F.relu(self.conv1(g, h))
h = F.relu(self.conv2(g, h))
g.ndata['h'] = h
print(f"h.size, {h.size()}")
# Calculate graph representation by averaging all the node representations.
hg = dgl.mean_nodes(g, 'h')
print(f"hg.size, {hg.size()}")
return self.classify(hg)
n_classes, 8
h.size, torch.Size([476, 256])
hg.size, torch.Size([32, 256])
h.size, torch.Size([420, 256])
hg.size, torch.Size([32, 256])
h.size, torch.Size([451, 256])
hg.size, torch.Size([32, 256])
h.size, torch.Size([463, 256])
hg.size, torch.Size([32, 256])
h.size, torch.Size([464, 256])
hg.size, torch.Size([32, 256])
h.size, torch.Size([450, 256])
hg.size, torch.Size([32, 256])
h.size, torch.Size([463, 256])
hg.size, torch.Size([32, 256])
h.size, torch.Size([448, 256])
hg.size, torch.Size([32, 256])
h.size, torch.Size([426, 256])
hg.size, torch.Size([32, 256])
h.size, torch.Size([452, 256])
hg.size, torch.Size([32, 256])
Epoch 0, loss 2.0047

What we can get from the output?

What is the output of the classifier g in the official tutorial?

self.classify = nn.Linear(hidden_dim, n_classes)
loss_func = nn.CrossEntropyLoss()
prediction = model(bg)
loss = loss_func(prediction, label)
probs_Y = torch.softmax(model(test_bg), 1)

The Pytorch nn.CrossEntropyLoss formular

Pytorch nn.CrossEntropyLoss formular

The readout process

hg = dgl.mean_nodes(g, 'h')

Source code of a toy example

Graph classification source code

Data Scientist/MLE/SWE @takemobi