def get_index_from_counts(counts): """Return index generated from counts This function return the index from given counts. For example, when counts = [ 2, 3, 4], return [0, 2, 5, 9] Args: counts: numpy.ndarray of paddle.Tensor Return: Return idnex of the counts """ if check_is_tensor(counts): index = paddle.concat( [paddle.zeros(shape=[ 1, ], dtype="int64"), paddle.cumsum(counts)], axis=-1) else: index = np.cumsum(counts, dtype="int64") index = np.insert(index, 0, 0) return index
def from_index(cls, sorted_v, sorted_u, sorted_eid, degree, indptr): self = cls() self._degree = degree self._sorted_v = sorted_v self._sorted_u = sorted_u self._sorted_eid = sorted_eid self._indptr = indptr self._is_tensor = check_is_tensor(sorted_v, sorted_u, sorted_eid, degree, indptr) return self
def from_edges(cls, u, v, num_nodes): self = cls() self._is_tensor = check_is_tensor(u, v, num_nodes) if self._is_tensor: self._degree = paddle.zeros(shape=[num_nodes], dtype="int64") self._degree = scatter(x=self._degree, overwrite=False, index=u, updates=paddle.ones_like(u, dtype="int64")) self._sorted_eid = paddle.argsort(u) self._sorted_u = paddle.gather(u, self._sorted_eid) self._sorted_v = paddle.gather(v, self._sorted_eid) self._indptr = op.get_index_from_counts(self._degree) else: self._degree, self._sorted_v, self._sorted_u, \ self._sorted_eid, self._indptr = graph_kernel.build_index(u, v, num_nodes) return self
def __init__(self, edges, num_nodes=None, node_feat=None, edge_feat=None, **kwargs): if node_feat is not None: self._node_feat = node_feat else: self._node_feat = {} if edge_feat is not None: self._edge_feat = edge_feat else: self._edge_feat = {} if not check_is_tensor(edges): if isinstance(edges, np.ndarray): if edges.dtype != "int64": edges = edges.astype("int64") else: edges = np.array(edges, dtype="int64") self._edges = edges if num_nodes is None: self._num_nodes = maybe_num_nodes(self._edges) else: self._num_nodes = num_nodes max_edge_id = maybe_num_nodes(self._edges) if not isinstance(max_edge_id, paddle.fluid.framework.Variable ) and self._num_nodes < max_edge_id: raise ValueError("The max edge ID should be less than the number of nodes. " "But got max edge ID [%s] >= num_nodes [%s]" \ % (max_edge_id-1, self._num_nodes)) self._adj_src_index = kwargs.get("adj_src_index", None) self._adj_dst_index = kwargs.get("adj_dst_index", None) if check_is_tensor(self._num_nodes, self._edges, *list(self._node_feat.values()), *list(self._edge_feat.values())): self._is_tensor = True elif self._adj_src_index is not None and self._adj_src_index.is_tensor( ): self._is_tensor = True elif self._adj_dst_index is not None and self._adj_dst_index.is_tensor( ): self._is_tensor = True else: self._is_tensor = False if self._is_tensor: # ensure all variable is tenosr if not check_is_tensor(self._num_nodes): self._num_nodes = paddle.to_tensor(self._num_nodes) if not check_is_tensor(self._edges): self._edges = paddle.to_tensor(self._edges) for key in self._node_feat: if not check_is_tensor(self._node_feat[key]): self._node_feat[key] = paddle.to_tensor( self._node_feat[key]) for key in self._edge_feat: if not check_is_tensor(self._edge_feat[key]): self._edge_feat[key] = paddle.to_tensor( self._edge_feat[key]) if self._adj_src_index is not None: if not self._adj_src_index.is_tensor(): self._adj_src_index.tensor(inplace=True) if self._adj_dst_index is not None: if not self._adj_dst_index.is_tensor(): self._adj_dst_index.tensor(inplace=True) # preprocess graph level informations self._process_graph_info(**kwargs) self._nodes = None
def __init__(self, edges, num_nodes=None, node_feat=None, edge_feat=None, **kwargs): if node_feat is not None: self._node_feat = node_feat else: self._node_feat = {} if edge_feat is not None: self._edge_feat = edge_feat else: self._edge_feat = {} if not check_is_tensor(edges): if isinstance(edges, np.ndarray): if edges.dtype != "int64": edges = edges.astype("int64") else: edges = np.array(edges, dtype="int64") self._edges = edges if num_nodes is None: self._num_nodes = maybe_num_nodes(self._edges) else: self._num_nodes = num_nodes self._adj_src_index = kwargs.get("adj_src_index", None) self._adj_dst_index = kwargs.get("adj_dst_index", None) if check_is_tensor(self._num_nodes, self._edges, *list(self._node_feat.values()), *list(self._edge_feat.values())): self._is_tensor = True elif self._adj_src_index is not None and self._adj_src_index.is_tensor( ): self._is_tensor = True elif self._adj_dst_index is not None and self._adj_dst_index.is_tensor( ): self._is_tensor = True else: self._is_tensor = False if self._is_tensor: # ensure all variable is tenosr if not check_is_tensor(self._num_nodes): self._num_nodes = paddle.to_tensor(self._num_nodes) if not check_is_tensor(self._edges): self._edges = paddle.to_tensor(self._edges) for key in self._node_feat: if not check_is_tensor(self._node_feat[key]): self._node_feat[key] = paddle.to_tensor( self._node_feat[key]) for key in self._edge_feat: if not check_is_tensor(self._edge_feat[key]): self._edge_feat[key] = paddle.to_tensor( self._edge_feat[key]) if self._adj_src_index is not None: if not self._adj_src_index.is_tensor(): self._adj_src_index.tensor(inplace=True) if self._adj_dst_index is not None: if not self._adj_dst_index.is_tensor(): self._adj_dst_index.tensor(inplace=True) # preprocess graph level informations self._process_graph_info(**kwargs) self._nodes = None