Source code for torch_geometric.utils.sparse

[docs]def dense_to_sparse(tensor): r"""Converts a dense adjacency matrix to a sparse adjacency matrix defined by edge indices and edge attributes. Args: tensor (Tensor): The dense adjacency matrix. :rtype: (:class:`LongTensor`, :class:`Tensor`) """ assert tensor.dim() == 2 index = tensor.nonzero(as_tuple=False).t().contiguous() value = tensor[index[0], index[1]] return index, value