Hi, how to input batch of data into the GCN?

class GCNLayer(nn.Module):
    def __init__(self, in_feats, out_feats):
        super(GCNLayer, self).__init__()
        self.linear = nn.Linear(in_feats, out_feats)

    def forward(self, g, feature):
        # Creating a local scope so that all the stored ndata and edata
        # (such as the `'h'` ndata below) are automatically popped out
        # when the scope exits.
        with g.local_scope():
            g.ndata['h'] = feature
            g.update_all(gcn_msg, gcn_reduce)
            h = g.ndata['h']
            return self.linear(h)
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.layer1 = GCNLayer(6, 16)
        self.layer2 = GCNLayer(16, 1)

    def forward(self, g, features):
        x = F.relu(self.layer1(g, features))
        x = self.layer2(g, x)
        return x
net = Net()
x = torch.randn(32, 307, 6)# batch_size, node_count, feature_dim
G = dgl.graph((df_data[:,0],df_data[:,1]))#num_nodes=307, num_edges=340

output = net(G,x)#<-----it can not work

the above is the GCN.
output = net(G,x) can not work. I get this:

DGLError: Expect number of features to match number of nodes (len(u)). Got 32 and 307 instead.

So how to input batch of data (like x) into the GCN? Thank you guys!

Do you have a batch of 32 graphs, all with a same graph structure?

Yeah, all with the same graph structure.

Either you can create 32 graphs with a same graph structure or you can assign a node feature of shape (307, 32, 6) and try adapting your model to multi-dimensional input node features.

@mufeili Thanks for your help!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.