Open shakeel608 opened 1 year ago
could you share a minimum reproducible code?
@Rhett-Ying The sample code is as follows. I am loading a graph from JSON file. ` def load_graph(self, path):
with open(path, 'r') as f:
graph_load = json.loads(f.read())
return nx.readwrite.json_graph.node_link_graph(graph_load)
graph_nx = load_graph(graphs_path) print("Number of Edges with Nx===>\n",graph_nx.number_of_edges()) dgl_graph = dgl.from_networkx(graph_nx, node_attrs=['features', 'label']) print("Number of Edges with DGL===>\n", dgl_graph.number_of_edges())
`
I think, DGL counts Non-directed edges twice here which is not the case with NetworkX Package
Say if adjacent_matrix[i,j] = 1, In Non directed Edges adjacent_matrix[j,i] is also 1.
Can you please let me know how to fix this. I tried to dig into the code, but I am yet to figure it out
DGLGraph
is always directed. You could use dgl.add_reverse_edges()
or dgl.to_bidirected()
to see if any new edges are added. If not, then the DGLGraph is already bi-directed. Or try with directed=True
in node_link_graph
of nx.
Thank you so much But I have another questions I tried a sample code Suppose I have a graph with adj matrix as given below
graph.adjacency_matrix().to_dense()
tensor([[0., 1.], [1., 0.]])
When I just print the properties of variable 'graph', it gives me following
Graph(num_nodes=2, num_edges=4, ndata_schemes={'h': Scheme(shape=(), dtype=torch.float32)} edata_schemes={})
Why num_edges is 4. Shouldn't it be only two even if the DGL graphs are directed
that's weird. could you share the minimum reproducible code for above case? Below is the one for illustration.
import dgl
import torch
g = dgl.graph(([0, 1], [1, 0]))
print(g.adjacency_matrix().to_dense())
print(g)
tensor([[0., 1.],
[1., 0.]])
Graph(num_nodes=2, num_edges=2,
ndata_schemes={}
edata_schemes={})
This issue has been automatically marked as stale due to lack of activity. It will be closed if no further activity occurs. Thank you
📚 Documentation
When I convert my networkX graph to DGL graph, why I get double the number of edges in DGL object.
How to fix it ?
Any help would be appreciated please ?