def sparse_adj_to_sparse_tensor(x, dtype=None): """Converts a Scipy sparse matrix to a PyTorch SparseTensor. Parameters ---------- x: Scipy sparse matrix Matrix in Scipy sparse format. Returns ------- S: SparseTensor Matrix as a pytorch sparse tensor. """ if dtype is None: dtype = infer_type(x) elif isinstance(dtype, torch.dtype): dtype = str(dtype).split('.')[-1] if not isinstance(dtype, str): raise TypeError(dtype) edge_index, edge_weight = gf.sparse_adj_to_edge(x) edge_weight = edge_weight.astype(dtype, copy=False) return sparse_edge_to_sparse_tensor(edge_index, edge_weight, x.shape)
def process_step(self): graph = self.graph adj_matrix = self.adj_transform(graph.adj_matrix) node_attr = self.attr_transform(graph.node_attr) edge_index, edge_weight = gf.sparse_adj_to_edge(adj_matrix) self.feature_inputs, self.structure_inputs = gf.astensors( node_attr, (edge_index.T, edge_weight), device=self.device)
def data_step(self, adj_transform="normalize_adj", attr_transform=None): graph = self.graph adj_matrix = gf.get(adj_transform)(graph.adj_matrix) node_attr = gf.get(attr_transform)(graph.node_attr) edge_index, edge_weight = gf.sparse_adj_to_edge(adj_matrix) X, E = gf.astensors(node_attr, (edge_index.T, edge_weight), device=self.data_device) # ``E`` and ``X`` are cached for later use self.register_cache(E=E, X=X)
def sparse_adj_to_sparse_tensor(x: sp.csr_matrix, dtype=None): if dtype is None: dtype = gf.infer_type(x) elif isinstance(dtype, tf.dtypes.DType): dtype = dtype.name if not isinstance(dtype, str): raise TypeError(dtype) edge_index, edge_weight = gf.sparse_adj_to_edge(x) edge_weight = edge_weight.astype(dtype, copy=False) return sparse_edge_to_sparse_tensor(edge_index, edge_weight, x.shape)
def sparse_adj_to_sparse_tensor(x, dtype=None): if dtype is None: dtype = gf.infer_type(x) elif isinstance(dtype, torch.dtype): dtype = str(dtype).split('.')[-1] if not isinstance(dtype, str): raise TypeError(dtype) edge_index, edge_weight = gf.sparse_adj_to_edge(x) edge_weight = edge_weight.astype(dtype, copy=False) return sparse_edge_to_sparse_tensor(edge_index, edge_weight, x.shape)
def astensor(x, *, dtype=None, device=None, escape=None): try: if x is None or (escape is not None and isinstance(x, escape)): return x except TypeError: raise TypeError(f"argument 'escape' must be a type or tuple of types.") if dtype is None: dtype = gf.infer_type(x) if isinstance(dtype, (np.dtype, str)): dtype = data_type_dict().get(str(dtype), dtype) elif not isinstance(dtype, torch.dtype): raise TypeError( f"argument 'dtype' must be torch.dtype, np.dtype or str, but got {type(dtype)}." ) if is_tensor(x): tensor = x.to(dtype) elif gf.is_tensor(x, backend='tensorflow'): return astensor(gf.tensoras(x), dtype=dtype, device=device, escape=escape) elif sp.isspmatrix(x): if gg.backend() == "dgl_torch": import dgl tensor = dgl.from_scipy(x, idtype=getattr(torch, gg.intx())) elif gg.backend() == "pyg": edge_index, edge_weight = gf.sparse_adj_to_edge(x) return (astensor(edge_index, dtype=gg.intx(), device=device, escape=escape), astensor(edge_weight, dtype=gg.floatx(), device=device, escape=escape)) else: tensor = sparse_adj_to_sparse_tensor(x, dtype=dtype) elif any((isinstance(x, (np.ndarray, np.matrix)), gg.is_listlike(x), gg.is_scalar(x))): tensor = torch.tensor(x, dtype=dtype, device=device) else: raise TypeError( f"Invalid type of inputs. Allowed data type (Tensor, SparseTensor, Numpy array, Scipy sparse tensor, None), but got {type(x)}." ) return tensor.to(device)
def edge_index(self): edge_index, edge_weight = gf.sparse_adj_to_edge(self.adj_matrix) return edge_index
def astensor(x, *, dtype=None, device=None, escape=None) -> torch.Tensor: try: if x is None or (escape is not None and isinstance(x, escape)): return x except TypeError: raise TypeError(f"argument 'escape' must be a type or tuple of types.") device = torch.device(device) if device is not None else torch.device( "cpu") # update: accept `dict` instance if isinstance(x, dict): for k, v in x.items(): try: x[k] = astensor(v, dtype=dtype, device=device, escape=escape) except TypeError: pass return x if dtype is None: dtype = gf.infer_type(x) if isinstance(dtype, (np.dtype, str)): dtype = data_type_dict().get(str(dtype), dtype) elif not isinstance(dtype, torch.dtype): raise TypeError( f"argument 'dtype' must be torch.dtype, np.dtype or str, but got {type(dtype)}." ) if is_tensor(x): tensor = x.to(dtype) elif sp.isspmatrix(x): if gg.backend() == "dgl": import dgl if x.sum() != x.nnz: warnings.warn( "Got a weighted sparse matrix with elements not equal to 1. " "The element weights can be accessed by `g.edata['_edge_weight'].`" ) tensor = dgl.from_scipy(x, idtype=torch.int64, eweight_name="_edge_weight") else: tensor = dgl.from_scipy(x, idtype=torch.int64) elif gg.backend() == "pyg": edge_index, edge_weight = gf.sparse_adj_to_edge(x) return (astensor(edge_index, dtype=torch.int64, device=device, escape=escape), astensor(edge_weight, dtype=torch.float32, device=device, escape=escape)) else: tensor = sparse_adj_to_sparse_tensor(x, dtype=dtype) elif any((isinstance(x, (np.ndarray, np.matrix)), gg.is_listlike(x), gg.is_scalar(x))): tensor = torch.tensor(x, dtype=dtype, device=device) else: raise TypeError( f"Invalid type of inputs. Allowed data type (Tensor, SparseTensor, Numpy array, Scipy sparse tensor, None), but got {type(x)}." ) return tensor.to(device)