r""" Description ----------- Convert adjacency matrix in scipy sparse format to torch sparse tensor. Parameters ---------- adj : scipy.sparse.csr.csr_matrix Adjacency matrix in form of ``N * N`` sparse matrix. Returns ------- adj_tensor : torch.Tensor
(adj)
| 68 | |
| 69 | |
| 70 | def adj_to_tensor(adj): |
| 71 | r""" |
| 72 | |
| 73 | Description |
| 74 | ----------- |
| 75 | Convert adjacency matrix in scipy sparse format to torch sparse tensor. |
| 76 | |
| 77 | Parameters |
| 78 | ---------- |
| 79 | adj : scipy.sparse.csr.csr_matrix |
| 80 | Adjacency matrix in form of ``N * N`` sparse matrix. |
| 81 | Returns |
| 82 | ------- |
| 83 | adj_tensor : torch.Tensor |
| 84 | Adjacency matrix in form of ``N * N`` sparse tensor. |
| 85 | |
| 86 | """ |
| 87 | if type(adj) == torch.Tensor: |
| 88 | return adj |
| 89 | if type(adj) != scipy.sparse.coo.coo_matrix: |
| 90 | adj = adj.tocoo() |
| 91 | sparse_row = torch.LongTensor(adj.row).unsqueeze(1) |
| 92 | sparse_col = torch.LongTensor(adj.col).unsqueeze(1) |
| 93 | sparse_concat = torch.cat((sparse_row, sparse_col), 1) |
| 94 | sparse_data = torch.FloatTensor(adj.data) |
| 95 | adj_tensor = torch.sparse.FloatTensor(sparse_concat.t(), sparse_data, torch.Size(adj.shape)) |
| 96 | |
| 97 | return adj_tensor |
| 98 | |
| 99 | |
| 100 | def adj_preprocess(adj, adj_norm_func=None, mask=None, device="cpu"): |
no outgoing calls
no test coverage detected