コード例 #1
0
  def get(self):
    """ Get sampled `SubGraph`.

    Return:
      An `SubGraph` object.
    Raise:
      `graphlearn.OutOfRangeError`
    """
    state = self._graph.node_state.get(self._seed_type)
    req = pywrap.new_subgraph_request(
        self._seed_type,
        self._nbr_type,
        strategy2op(self._strategy, "SubGraphSampler"),
        self._batch_size,
        state)
    res = pywrap.new_subgraph_response()

    status = self._client.sample_subgraph(req, res)
    if not status.ok():
      self._graph.node_state.inc(self._seed_type)
    else:
      node_ids = pywrap.get_node_set(res)
      row_idx = pywrap.get_row_idx(res)
      col_idx = pywrap.get_col_idx(res)
      edge_ids = pywrap.get_edge_set(res)

    pywrap.del_op_response(res)
    pywrap.del_op_request(req)
    errors.raise_exception_on_not_ok_status(status)

    nodes = self._graph.get_nodes(self._node_type, node_ids)
    return SubGraph(np.stack([row_idx, col_idx], axis=0), nodes, Edges(edge_ids=edge_ids))
コード例 #2
0
ファイル: graph.py プロジェクト: Seventeen17/graph-learn
    def negative_sampler(self,
                         object_type,
                         expand_factor,
                         strategy="random",
                         conditional=False,
                         **kwargs):
        """Sampler for sampling negative dst nodes of the given src nodes
    with edge_type.

    Args:
      edge_type (string): Sample negative nodes of the source node with
        specified edge_type.
      strategy (string or list): Indicates how to sample negative edges,
        "random" and "in_degree" are supported.
        "random": randomly sample negative nodes.
        "in_degree": sample negative nodes by the in degree of the target nodes.
      expand_factor (int): Indicates how many negatives to sample for one node.
      conditional(bool): Indicates whether sample under condition.
    Return:
      A 'NegativeSampler' object.
    """
        if not conditional:
            sampler = utils.strategy2op(strategy, "NegativeSampler")
            return getattr(samplers, sampler)(self,
                                              object_type,
                                              expand_factor,
                                              strategy=strategy)
        else:
            return getattr(samplers,
                           "ConditionalNegativeSampler")(self,
                                                         object_type,
                                                         expand_factor,
                                                         strategy=strategy,
                                                         **kwargs)
コード例 #3
0
ファイル: graph.py プロジェクト: Seventeen17/graph-learn
    def edge_sampler(self,
                     edge_type,
                     batch_size=64,
                     strategy="by_order",
                     mask=utils.Mask.NONE):
        """Sampler for sample one type of edges.

    Args:
      edge_type (string): Sample edges of the edge_type.
      batch_size (int, Optional): How many edges will be returned for get().
      strategy (string, Optional):Indicates how to sample edges,
        "by_order", "random" and "shuffle" are supported.
        "by_order": Get edge by order. Raise `graphlearn.OutOfRangeError` when
          all the edges are visited. Each edge will be visited and only be
          visited once.
        "random": Randomly get edges. No visting state will be hold, so out of
          range will not happened.
        "shuffle": Get edges with shuffle. Raise `graphlearn.OutOfRangeError`
          when all the edges are visited. Each edge will be visited and only
          be visited once.

    Return:
      An `EdgeSampler` object.

    """
        sampler = utils.strategy2op(strategy, "EdgeSampler")
        return getattr(samplers, sampler)(self,
                                          edge_type,
                                          batch_size=batch_size,
                                          strategy=strategy,
                                          mask=mask)
コード例 #4
0
ファイル: graph.py プロジェクト: Seventeen17/graph-learn
    def neighbor_sampler(self, meta_path, expand_factor, strategy="random"):
        """ Sampler for sample neighbors and edges along a meta path.
    The neighbors of the seed nodes and edges between seed nodes and
    neighbors are called layer.

    Args:
      meta_path (list): A list of edge_type.
      expand_factor (int | list): Number of neighbors sampled from all
        the dst nodes of the node along the corresponding meta_path.
        int: indicates the number of neighbors sampled for each node.
        list of int: indicates the number of neighbors sampled for each node of
          each hop, and length of expand_factor is same with length of meta_path.
      strategy (string): Indicates how to sample meta paths,
        "edge_weight", "random", "in_degree" are supported.
        "edge_weight": sample neighbor nodes by the edge weight between seed
          node and dst node.
        "random": randomly sample neighbor nodes.
        "in_degree": sample neighbor nodes by the in degree of the neighbors.

    Return:
      A 'NeighborSampler' object.
    """
        sampler = utils.strategy2op(strategy, "NeighborSampler")
        return getattr(samplers, sampler)(self,
                                          meta_path,
                                          expand_factor,
                                          strategy=strategy)
コード例 #5
0
  def _make_req(self, index, src_ids):
    """ Make rpc request.
    """
    if len(self._meta_path) > len(self._expand_factor):
      raise ValueError("The length of meta_path must be same with hop count.")

    sampler = strategy2op(self._strategy[index], "Sampler")
    req = pywrap.new_sampling_request(
        self._meta_path[index], sampler, self._expand_factor[index], 0)
    pywrap.set_sampling_request(req, src_ids)
    return req
コード例 #6
0
  def _make_req(self, index, src_ids):
    """ Make rpc request.
    """
    if len(self._meta_path) > len(self._expand_factor):
      raise ValueError("input too many meta_path, and can not decide each hops")

    sampler = strategy2op(self._strategy[index], "Sampler")
    req = pywrap.new_sampling_request(
        self._meta_path[index], sampler, self._expand_factor[index])
    pywrap.set_sampling_request(req, src_ids)
    return req
コード例 #7
0
ファイル: values.py プロジェクト: zwcdp/graph-learn
 def _agg(self, func, segment_ids, num_segments):
     req = pywrap.new_agg_nodes_req(self._type,
                                    utils.strategy2op(func, "Aggregator"))
     pywrap.set_agg_nodes_req(req, self._ids.flatten(),
                              np.array(segment_ids, dtype=np.int32),
                              num_segments)
     res = pywrap.new_agg_nodes_res()
     raise_exception_on_not_ok_status(self.graph.get_client().agg_nodes(
         req, res))
     agged = pywrap.get_node_agg_res(res)
     pywrap.del_agg_nodes_res(res)
     pywrap.del_agg_nodes_req(req)
     return agged
コード例 #8
0
ファイル: values.py プロジェクト: Seventeen17/graph-learn
 def _agg(self, func, segment_ids, num_segments):
     req = pywrap.new_aggregating_request(
         self._type, utils.strategy2op(func, "Aggregator"))
     pywrap.set_aggregating_request(req, self._ids.flatten(),
                                    np.array(segment_ids, dtype=np.int32),
                                    num_segments)
     res = pywrap.new_aggregating_response()
     status = self.graph.get_client().agg_nodes(req, res)
     if status.ok():
         agged = pywrap.get_aggregating_nodes(res)
     pywrap.del_op_response(res)
     pywrap.del_op_request(req)
     errors.raise_exception_on_not_ok_status(status)
     return agged
コード例 #9
0
ファイル: dag_node.py プロジェクト: knut0815/graph-learn
    def by(self, strategy):
        self._strategy = strategy
        if self._op_name == "NegativeSampler":
            assert strategy in [
                "random", "in_degree", "conditional", "node_weight"
            ]
        elif self._op_name == "Sampler":
            assert strategy in \
              ["random", "topk", "in_degree", "edge_weight", "full"]
        else:
            raise ValueError(
                "`by(strategy)` can only be used after`sample(count)`")

        self._sparse = (strategy == "full")
        self._op_name = strategy2op(self._strategy, self._op_name)
        self._add_param(pywrap.kStrategy, self._op_name)
        return self
コード例 #10
0
ファイル: graph.py プロジェクト: Seventeen17/graph-learn
    def node_sampler(self,
                     t,
                     batch_size=64,
                     strategy="by_order",
                     node_from=pywrap.NodeFrom.NODE,
                     mask=utils.Mask.NONE):
        """ Sampler for sample one type of nodes.

    Args:
      t (string): Sample nodes of the given type `t`. `t` can be node type
        which indicates that sampling node dat, otherwise, `t` can be
        edge type which indicate that sampling source node or dst node of
        edge data.
      batch_size (int, Optional): How many nodes will be returned for get().
      strategy (string, Optional): Indicates how to sample edges,
        "by_order", "random" and "shuffle" are supported.
        "by_order": Get node by order. Raise `graphlearn.OutOfRangeError` when
          all the nodes are visited. Each node will be visited and only be
          visited once.
        "random": Randomly get nodes. No visting state will be hold, so out of
          range will not happened.
        "shuffle": Get nodes with shuffle. Raise `graphlearn.OutOfRangeError`
          when all the nodes are visited. Each node will be visited and only
          be visited once.
      node_from (graphlearn.NODE | graphlearn.EDGE_SRC | graphlearn.EDGE_DST):
        `graphlearn.NODE`: get node from node data, and `t` must be a node
          type.
        `graphlearn.EDGE_SRC`: get node from source node of edge data, and `t`
          must be an edge type.
        `graphlearn.EDGE_DST`: get node from destination node of edge data, and
          `t` must be an edge type.

    Return:
      A `NodeSampler` object.
    """
        sampler = utils.strategy2op(strategy, "NodeSampler")
        return getattr(samplers, sampler)(self,
                                          t,
                                          batch_size=batch_size,
                                          strategy=strategy,
                                          node_from=node_from,
                                          mask=mask)
コード例 #11
0
ファイル: graph.py プロジェクト: zeta1999/graph-learn
    def negative_sampler(self, object_type, expand_factor, strategy="random"):
        """Sampler for sample negative dst nodes of the given src nodes
    with edge_type.

    Args:
      edge_type (string): Sample negative nodes of the source node with
        specified edge_type.
      strategy (string or list): Indicates how to sample negative edges,
        "random" and "in_degree" are supported.
        "random": randomly sample negative nodes.
        "in_degree": sample negative nodes by the in degree of the target nodes.
      expand_factor (int): Indicates how many negatives to sample for one node.

    Return:
      A 'NegativeSampler' object.
    """
        sampler = strategy2op(strategy, "NegativeSampler")
        return getattr(samplers, sampler)(self,
                                          object_type,
                                          expand_factor,
                                          strategy=strategy)
コード例 #12
0
ファイル: graph.py プロジェクト: Seventeen17/graph-learn
    def subgraph_sampler(self,
                         seed_type,
                         nbr_type,
                         batch_size=64,
                         strategy="random_node"):
        """Sampler for sample SubGraph.

    Args:
      graph (`Graph` object): The graph which sample from.
      seed_type (string): Sample seed type, either node type or edge type.
      nbr_type (string): Neighbor type of seeds nodes/edges.
      batch_size (int): How many nodes will be returned for `get()`.
      strategy (string, Optional): Sampling strategy. "random_node".
    Return:
      An `SubGraphSampler` object.

    """
        sampler = utils.strategy2op(strategy, "SubGraphSampler")
        return getattr(samplers, sampler)(self,
                                          seed_type,
                                          nbr_type,
                                          batch_size=batch_size,
                                          strategy=strategy)
コード例 #13
0
 def _make_req(self, ids):
     sampler = strategy2op(self._strategy, "NegativeSampler")
     req = pywrap.new_sampling_request(self._object_type, sampler,
                                       self._expand_factor)
     pywrap.set_sampling_request(req, ids)
     return req
コード例 #14
0
 def _make_req(self, ids):
     sampler = strategy2op(self._strategy, "NegativeSampler")
     req = pywrap.new_nbr_req(self._edge_type, sampler, self._expand_factor)
     pywrap.set_nbr_req(req, ids)
     return req