Hi.
I’m follwing this 6.3 Training GNN for Link Prediction with Neighborhood Sampling — DGL 0.6.1 documentation
I have a heterograph with this shape.
Graph(num_nodes={'post': 11911, 'user': 9482},
num_edges={('user', 'like', 'post'): 363984},
metagraph=[('user', 'post', 'like')])
When trying the training loop, I get this error:
KeyError: 'x'
I got the intuition it is because I have just one edge type, because this same architecture used to work with another graph with two edge types.
What should I change? For now I fixed it by adding the reverse edges, but I assume the is a proper way to do it.
Thanks!!
Predictor, model:
class ScorePredictor(nn.Module):
def forward(self, edge_subgraph, x):
with edge_subgraph.local_scope():
edge_subgraph.ndata['x'] = x
for etype in edge_subgraph.canonical_etypes:
edge_subgraph.apply_edges(
dgl.function.u_dot_v('x', 'x', 'score'), etype=etype)
return edge_subgraph.edata['score']
class Model(nn.Module):
def __init__(self, in_features, hidden_features, out_features, num_classes,
etypes):
super().__init__()
self.rgcn = StochasticTwoLayerRGCN(
in_features, hidden_features, out_features, etypes)
self.pred = ScorePredictor()
def forward(self, positive_graph, negative_graph, blocks, x):
x = self.rgcn(blocks, x)
pos_score = self.pred(positive_graph, x)
neg_score = self.pred(negative_graph, x)
return pos_score, neg_score
Training loop:
for epoch in range(n_epochs):
for input_nodes, positive_graph, negative_graph, blocks in dataloader:
blocks = [b for b in blocks]
input_features = blocks[0].srcdata['feats']
pos_score, neg_score = model(positive_graph, negative_graph, blocks, input_features)
loss = compute_loss(pos_score, neg_score, g.canonical_etypes)
opt.zero_grad()
loss.backward()
opt.step()
print(loss.item())