def torch_coo_to_scipy_coo(m: torch.sparse.FloatTensor) -> coo_matrix: """Convert torch :class:`torch.sparse.FloatTensor` tensor to. :class:`scipy.sparse.coo_matrix` """ data = m.values().numpy() indices = m.indices() return coo_matrix((data, (indices[0], indices[1])), tuple(m.size()))
def get_nonzero_idx(sp: torch.sparse.FloatTensor) -> torch.LongTensor: """Get indices of nonzero elements of a sparse tensor Args: sp (torch.sparse.FloatTensor): a sparse tensor Returns: torch.LongTensor: indices of nonzero elements """ sp = sp.coalesce() return sp.indices()[:, sp.values() > 0]