普通矩阵:
adj <class 'torch.Tensor'> tensor([[10.0527, 0.7901, 4.7365, ..., -2.9740, -1.2431, 2.6618],
[ 0.7901, 7.8155, 2.0635, ..., -0.7821, 0.8580, -1.8541],
[ 4.7365, 2.0635, 6.2384, ..., -2.2005, -0.2560, 2.0816],
...,
[-2.9740, -0.7821, -2.2005, ..., 18.2876, -2.9486, 0.2639],
[-1.2431, 0.8580, -0.2560, ..., -2.9486, 16.9452, -3.5839],
[ 2.6618, -1.8541, 2.0816, ..., 0.2639, -3.5839, 17.8889]],
grad_fn=<MmBackward>)
变为COO稀疏矩阵:
idx = torch.nonzero(adj).T
data = adj[idx[0],idx[1]]
adj_coo = torch.sparse_coo_tensor(idx, data, adj.shape) # 转换成COO矩阵
coo矩阵:
adj_coo <class 'torch.Tensor'> tensor(indices=tensor([[ 0, 0, 0, ..., 2707, 2707, 2707],
[ 0, 1, 2, ..., 2705, 2706, 2707]]),
values=tensor([10.0527, 0.7901, 4.7365, ..., 0.2639, -3.5839,
17.8889]),
size=(2708, 2708), nnz=7333264, layout=torch.sparse_coo,
grad_fn=<SparseCooTensorWithDimsAndTensorsBackward>)