Question about Edge Feature

Hi, Thank you very much for your time in reading my question .

if I have a bunch of directed graphs with varying number of nodes and edges,

and individually I create some subgraphs and want edge feature in this case [0,3] to be concat of the edges in between its start and end and pass through a nn.Linear() layer,

if the number of nodes and edges vary from cases to cases, some are five, some are ten or any number etc. how should I approach, by padding them all to the same size ? , or engineers using graphs dont do things like this ?

Thank you very much for your time in reading my question.

Do you mean for edge from node 0 to node 3, you want its feature to capture the information of the path 0->1->2->3?

1 Like

Hi, Firstly, thank you very much for your reply ! may I please describe a little more clearly

src = np.arange(9)
dst = src + 1 
src = np.append(src, [0,2,5])
dst = np.append(dst, [2,5,9])
g = dgl.graph((src,dst))
g.edges()

(tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 2, 5]),
 tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 5, 9]))

tt = torch.arange(12).repeat(3,1)
g.edata['x'] = torch.permute(tt, (1,0))
nodes = g.nodes()
g0 = dgl.node_subgraph(g, nodes[0:3])
g1 = dgl.node_subgraph(g, nodes[2:6])
g2 = dgl.node_subgraph(g, nodes[5:])
print(g0.edata['x'].shape)
print(g1.edata['x'].shape)
print(g2.edata['x'].shape)
torch.Size([3, 3])
torch.Size([4, 3])
torch.Size([5, 3]) 

if I want any edge between arbitrary distance [ src and dst ] in directed graphs to :

  • find the path between src and dst and concat every edges features and pass through the same linear layer to have shape torch.Size([1, 3]) to update the big edge. can I pad all of them to some same length ?
def Pad(ii):
    ee = ii.edata['x']
    ee = F.pad(input=ee, pad=(0,0,0,7 - ee.shape[0]), mode='constant', value=0)
    return ee 
print(Pad(g0).shape)
print(Pad(g1).shape)
print(Pad(g2).shape)
torch.Size([7, 3])
torch.Size([7, 3])
torch.Size([7, 3])   ->>  self.Linear = nn.Linear(7,1)

then can update the big edges with it … ?
or something like

class CClass(nn.Module):
    def __init__(self):
        super(CClass, self).__init__()
        self.weight = nn.ModuleDict()
        for _ in range(10):
            self.weight[str(_)] = nn.Linear(_,1)

    def forward(self,G, edata):
        num_edges = G.num_edges()
        oo = self.weight[str(num_edges)](edata)
        return oo

or does this kind of operation make sense to graphs engineers…,…

Thank you very much for your time in reading and assisting my question

In theory, you can do padding or take the average/sum of the path edge features. Whether this will help depends on your particular task and dataset.

1 Like

Thank you very much for your time in replying my every question that is so kind of you and the community !

1 Like

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