搜这个转化实在难找,在此记录一下!
**pyG的edge_index 转 COO
同质图**
import torch_geometric
torch_geometric.utils.to_scipy_sparse_matrix(data.edge_index)
异质图文章来源:https://www.toymoban.com/news/detail-556042.html
import numpy as np
# 假如异质图size:N*M
from scipy.sparse import coo_matrix
row = (hetedata['节点1','边', '节点2'].edge_index)[0]
col = (hetedata['节点1','边', '节点2'].edge_index)[1]
value = torch.ones(hetedata['节点1','边', '节点2'].num_edges)
adj = coo_matrix((value, (row, col)), shape=(hetedata['节点1'].num_nodes, hetedata['节点2'].num_nodes))
下面是pytorch版本文章来源地址https://www.toymoban.com/news/detail-556042.html
edge_shape= torch.zeros((N,M)).shape
value = torch.ones(hetedata['节点1','边', '节点2'].num_edges))
adj = torch.sparse_coo_tensor(hetedata['节点1','边', '节点2'].edge_index,value,edge_shape)
**pyG的edge_index 转 coo 再转普通矩阵
python
adj = adj.toarray()
pytorch
adj = adj.to_dense()
到了这里,关于pyG edge_index矩阵 转 普通邻接矩阵,COO稀疏矩阵,包含同质图和异质图的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!