MCPcopy
hub / github.com/THUDM/CogDL / adj_to_tensor

Function adj_to_tensor

cogdl/utils/grb_utils.py:70–97  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

68
69
70def 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
100def adj_preprocess(adj, adj_norm_func=None, mask=None, device="cpu"):

Callers 4

modificationMethod · 0.90
trainMethod · 0.90
read_grb_dataMethod · 0.90
adj_preprocessFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected