Edge classification in weighted directed graphs

Hello,

I’m still very new to the topic of deep learning on graphs. Given a directed weighted graph, the objective is to perform binary classification of the graph’s edges.

Any pointers to resolve the following issues are much appreciated.

  • I already implemented a function which loads the graphs into a batch. Is the implementation correct or am I missing some details. The constructs each graph with each edge weight and edge label. There are no known node features. Finally, all graphs are added to a graph batch
def read_graphs(path):
    graphs = []
    
    for file_path in glob.iglob(path):
        g = dgl.DGLGraph()
        with open(file_path) as file:
            u, v, f = [], [], []
            w, labels,  = [], []
            labels_2d = [line.split() for line in open(label_file_path)]
            for line in file:
                l = line.split()
                if l[0] != l[1]:
                    u.append(int(l[0]))
                    v.append(int(l[1]))
                    w.append(float(l[2]))
                    labels.append(int(labels_2d[int(l[0])][int(l[1])]))
        g.add_edges(torch.tensor(u), torch.tensor(v), 
                    {'weight': torch.tensor(w), 'label': torch.tensor(labels)})
        graphs.append(g)
        
    return graphs

graphs = read_graphs(path)
batch_graph = dgl.batch(graphs)
  • Is it possible to perform training on several batches instead of a single bath which contains all graphs?

  • Which model should I be using for the problem described, and how do I use the dataset to be fed into a model?

Thanks in advance

(1) This looks fine to me. Although you can create a graph with g = dgl.graph((torch.tensor(u), torch.tensor(v))) and update the edge weights with g.edata.update(...).
(2) You can give dgl.dataloading.GraphDataLoader a try. It iterates over a list of graphs in minibatches.
(3) Is your problem “train on multiple graphs, and then test on a new graph with no edge labels”? If so, we don’t have a ready example for this scenario. However, you could in general follow the edge classification user guide to build your own. Although the user guide talks about a single graph, the same idea applies to a dataset of graphs as well.

Thank you so much for your answer. That was really helpful in solving the first two points.

My problem is to train on multiple graphs that are complete with known edge labels, and then test on a new graph that is also complete and classify each edge to either class 0 or 1. The problem also has a high class imbalance, most edges will be classified as class 0.

I have another question, how exactly does the model in the link provided (link) takes edge weights into account for the classification task?

The example itself doesn’t readily provide examples handling edge weights. However, you can change the GNN modules with your own that considers the edge weights. For instance you can see the 9th and 13th question in Frequently Asked Questions (FAQ).

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