DGLError: Expected data to have 220 rows, got 222

I am trying to implement attentivefp to a set of graphs of mine. I keep getting the error above when executing the apply_edges1 function in the GetContext Class.

def apply_edges1(edges):
    """Edge feature update."""
    return {'he1': torch.cat([edges.src['h'][0], edges.data['e']], dim=1)}

Every time I get the error that 2 extra rows exist in the edge update. For example, I tried this function on one sample graph in my dataset, with 220 edges and 55 nodes. The error says: DGLError: Expected data to have 220 rows, got 222. If I try it on another sample with 100 edges it would say DGLError: Expected data to have 100 rows, got 102. In case you are wondering why I added [0] to edges.src[‘h’] it’s because of the dimensional different between the edge feature (1) and node feature size (2), so other I would get a concatenation error due to dimension difference.

Any help is appreciated. Where are the 2 extra rows coming from ?
Best regards
Ali

@aah71 Thanks for letting us know, could you please share us the structure of your second graph (e.g. in the form of edge list) so that we could reproduce your bug?

@zihao Thank you for replying. There are hundreds of edges for each graph.If it makes it easier, I made 3 sample graphs and put them in this file in dropbox. To run it you just run the following:

from dgl.data.utils import load_graphs
from dgl.data.utils import load_labels
sample=load_graphs('sample.bin') 

Thanks a lot in advance ! :slight_smile:
Regards
Ali

Dear @aah71, the reason you have such problem is the dimensionality of node feature does not equal the dimensionality of edge feature.
I have checked the features of your data and found out the node feature has shape (#nodes, 2) while the edge feature has shape (#edges).

For example, I debug with the first graph in your dataset (72 nodes, 300 edges):

def apply_edges1(edges):
    """Edge feature update."""
    print(edges.src['h'].shape) # (300, 2), the node features are broadcast to edges
    print(edges.data['h'].shape) # (300)
    print(torch.cat([edges.src['h'][0], edges.data['e']], dim=1)) # (302), however, the size of first dimension in  the returned tensor should equal #edges
    return {'he1': torch.cat([edges.src['h'][0], edges.data['e']], dim=1)}

Solution is quite easy, add an extra dimension to the edge feature:

g.edata['e'] = g.edata['e'].unsqueeze(-1) # (300) -> (300, 1)
g.apply_edges(
    lambda edges: {'he1': torch.cat([edges.src['h'], edges.data['e']], dim=1)}
)
print(g.edata['he1'].shape) # (300, 3)