Is it possible to train a graph convolutional network on graphs with a varying number of nodes? I have a dataset of graphs with a range of 400-1000 nodes, though I could see a higher number of nodes when it's deployed in the real world. I'm using dgl to try and classify each node as one of k classes with a graph convolutional network. However, I can't find a way to do it when the size of my graph varies across my training set. Is there a simple way to do this?

So far I've tried a GCN that looks something like the one from the tutorial

class GCN(nn.Module):
    def __init__(self, in_feats, hidden_size, num_classes):
        super(GCN, self).__init__()
        self.conv1 = GraphConv(in_feats, hidden_size)
        self.conv2 = GraphConv(hidden_size, num_classes)

    def forward(self, g, inputs):
        h = self.conv1(g, inputs)
        h = torch.relu(h)
        h = self.conv2(g, h)
        return h

Graph convolution of the spatial variety can be applied to arbitrary sized graphs, the same way that normal "grid" convolution can be run on arbitrary sized raster inputs. The same can also be said of most (all?) spectral versions of graph convolution. In both cases, you can see that the size of the weights are independent from the size of the graph (just like in normal convolution). So tl;dr everything should already work with variable size inputs.

There are some additional practical considerations -- to improve computation performance, it's common practice to group, or "batch" a number of inputs together and parallelize any operation (such as convolution) across all examples. This poses problems when the inputs are of variable size, and this is typically solved by padding all inputs until they are the same size.

Of course, this only works if it's possible to find a padding function $p$ such that for the given operation $f$, $p^{-1}(f(p(x))) = f(x)$ modulo some unimportant details. Luckily this is roughly true for almost any operation used in a NN, and it seems to hold true for spatial graph conv as well.

Depending on the software you're using, this padding may or may not be already implemented for you, but it typically consists of just expanding any relevant array and filling in new values with 0.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.