Looking for a bit of direction and understanding here. I’ve spent a few nights comparing various PyTorch examples to the various DGL examples. I have not been able to dissect meaning from the Hetero example in the docs.
Here is the ndata
of a basic 3 node graph with 2 features. I am using this simple graph to feel out the library.
Features in ndata
g.ndata = {
# continuous feature
'n_weight': tensor([
[1.11],
[2.22],
[3.33]
]),
# categorical/ discrete feature
'n_community': tensor([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
])
}
Defining NN that will take both features into account
(I know it’s heinous, I’m just trying to understand)
class GraphClassifier(nn.Module):
def __init__(self, in_dim, hidden_dim, n_classes):
super(GraphClassifier, self).__init__()
self.conv1 = GraphConv(in_dim, hidden_dim)
self.conv2 = GraphConv(hidden_dim, hidden_dim)
# flatten into linear so we can crossentropy/ softmax it.
self.classify = nn.Linear(hidden_dim, n_classes)
def forward(self, g):
# run the weight feature through the net
w = g.ndata['n_weight']
w = F.relu(self.conv1(g, w))
w = F.relu(self.conv2(g, w))
g.ndata['n_weight'] = w
# run the community feature through the net
c = g.ndata['n_community']
c = F.relu(self.conv1(g, c))
c = F.relu(self.conv1(g, c))
g.ndata['n_community'] = c
# combine both features into one tensor
wc = torch.cat((w, c), 1)
return self.classify(wc)
Questions
-
Q1: mapping the features
– In myforward
, is it possible to access the features as attributes ofg
like sog.ndata['key']
?
– Or should I do I need to do aself.features = nn.Sequential(...layers...)
and pass them into forward likedef forward(self, g, n_weight, n_community)
?
– Or could I usenn.Parameters('ndata')
to get it into forward?
– Just need a standard approach. -
Q2: reducing to an output
In thewc
above, I am trying to combine the different features in the forward pass. If I understand correctly, I need to feed each feature through the network and then flatten them somehow so Linear can run on them?