How to use edge weighted?

Currently I am using the GAT model that comes with DGL. Now I hope that the parameter ‘g’ of the input model is a weighted graph. During the forward propagation process, the node will take the weight of the edges into account when aggregating neighbor information.

class GAT(nn.Module):

def __init__(self, in_feats, hidden_feats, n_classes, activation,dropout):
    super(GAT, self).__init__()
    self.dropout = nn.Dropout(dropout)
    self.layers = nn.ModuleList()
    self.layers.append(GATConv(in_feats, hidden_feats, num_heads=4, activation=activation))
    self.layers.append(GATConv(hidden_feats * 4, n_classes, num_heads=1, activation=None))  

def forward(self, g, features):
    x = features
    for layer in self.layers[:-1]:
        x = layer(g, x).flatten(1)
        x = self.dropout(x) 
    x = self.layers[-1](g, x).flatten(1)
    return x

In my understanding, I use the parameter ‘g’, the adjacency matrix of the dgl graph, to obtain the graph structure to facilitate subsequent message dissemination. But I currently don’t know how to modify this adjacency matrix into a weighted matrix.

Hi @wryyy1999, you can do this by setting edge attributes:
`g.edata[‘x’] = xxxxx
1.3 Node and Edge Features — DGL 0.8.2post1 documentation.

@peizhou001 Yes, I already know that there is an attribute ‘edata’ in which the edge weights can be stored, but what I am curious about is how to use the weights in ‘endata’ for the message propagation of the model.

Just fetch the attribute and do anything you want, which depends on your specific model.

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