MCPcopy Create free account
hub / github.com/NineAbyss/ZeroG / normalize_adjacency_matrix

Function normalize_adjacency_matrix

code/utils.py:450–471  ·  view source on GitHub ↗
(data,args)

Source from the content-addressed store, hash-verified

448
449
450def normalize_adjacency_matrix(data,args):
451 edge_index = data.data.edge_index
452 num_nodes = data.data.x.shape[0]
453
454
455 edge_index_self_loops = torch.stack([torch.arange(num_nodes), torch.arange(num_nodes)], dim=0)
456 edge_index = torch.cat([edge_index, edge_index_self_loops], dim=1)
457
458
459 adj = torch.sparse_coo_tensor(edge_index, torch.ones(edge_index.shape[1]), (num_nodes, num_nodes))
460
461
462 deg = torch.sparse.sum(adj, dim=1).to_dense()
463 deg_inv_sqrt = deg.pow(-0.5)
464 deg_inv_sqrt[deg_inv_sqrt == float('inf')] = 0
465
466
467 adj_normalized = adj.coalesce()
468 deg_inv_sqrt_mat = torch.sparse_coo_tensor(torch.arange(num_nodes).unsqueeze(0).repeat(2, 1), deg_inv_sqrt, (num_nodes, num_nodes))
469 adj_normalized = torch.sparse.mm(deg_inv_sqrt_mat, torch.sparse.mm(adj_normalized, deg_inv_sqrt_mat))
470
471 return adj_normalized

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected