Exemplo n.º 1
0
    def get(self, ids):
        """ Get batched samples.

    Args:
      ids (numpy.array): A 1d numpy array of whose negative dst nodes
        will be sampled.

    Return:
      A `Nodes` object, shape=[ids.shape, `expand_factor`].
    """
        if not isinstance(ids, np.ndarray):
            raise ValueError("ids must be a numpy array, got {}.".format(
                type(ids)))
        ids = ids.flatten()

        req = self._make_req(ids)
        res = pywrap.new_sampling_response()
        status = self._client.sample_neighbor(req, res)
        if status.ok():
            nbrs = pywrap.get_sampling_node_ids(res)
            neg_nbrs = self._graph.get_nodes(self._dst_type,
                                             nbrs,
                                             shape=(ids.shape[0],
                                                    self._expand_factor))

        pywrap.del_op_response(res)
        pywrap.del_op_request(req)
        raise_exception_on_not_ok_status(status)
        return neg_nbrs
Exemplo n.º 2
0
    def get(self, src_ids, dst_ids):
        """ Get batched negative samples.

    Args:
      src_ids (numpy.array): A 1d numpy array of whose negative dst nodes
        will be sampled.
      dst_ids (numpy.array): A 1d numpy array of positive dst nodes.

    Return:
      A `Nodes` object, shape=[ids.shape, `expand_factor`].
    """
        src_ids = np.array(src_ids).flatten()
        dst_ids = np.array(dst_ids).flatten()

        req = self._make_req(src_ids, dst_ids)
        res = pywrap.new_sampling_response()
        status = self._client.cond_neg_sample(req, res)
        if status.ok():
            nbrs = pywrap.get_sampling_node_ids(res)
            neg_nbrs = self._graph.get_nodes(self._dst_type,
                                             nbrs,
                                             shape=(dst_ids.shape[0],
                                                    self._expand_factor))

        pywrap.del_op_response(res)
        pywrap.del_op_request(req)
        errors.raise_exception_on_not_ok_status(status)
        return neg_nbrs
Exemplo n.º 3
0
    def get(self, ids):  # pylint: disable=unused-argument
        """ Get batched samples.

    Args:
      ids: A 1d numpy array, the input ids whose neighbors will be returned,
        type=np.int64.

    Return:
      A `Layers` object.
    """

        if len(self._meta_path) != len(self._expand_factor):
            raise ValueError("The meta_path must have the same number"
                             "of elements as num_at_each_hop")

        src_ids = ids
        current_batch_size = ids.size
        layers = Layers()
        for i in xrange(len(self._meta_path)):
            req = self._make_req(i, src_ids)
            res = pywrap.new_sampling_response()
            status = self._client.sample_neighbor(req, res)
            if status.ok():
                nbr_ids = pywrap.get_sampling_node_ids(res)
                edge_ids = pywrap.get_sampling_edge_ids(res)

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

            dst_type = self._dst_types[i]
            layer_nodes = self._graph.get_nodes(dst_type,
                                                nbr_ids,
                                                shape=(current_batch_size,
                                                       self._expand_factor[i]))

            ids = src_ids.repeat(self._expand_factor[i]).flatten()
            nbr_ids_flat = nbr_ids.flatten()
            layer_edges = \
              self._graph.get_edges(self._meta_path[i],
                                    ids,
                                    nbr_ids_flat,
                                    shape=(current_batch_size,
                                           self._expand_factor[i]))
            layer_edges.edge_ids = edge_ids
            layers.append_layer(Layer(layer_nodes, layer_edges))
            current_batch_size = nbr_ids_flat.size

            src_ids = nbr_ids
        return layers
Exemplo n.º 4
0
  def get(self, ids):  # pylint: disable=unused-argument
    if len(self._meta_path) != len(self._expand_factor):
      raise ValueError("The length of meta_path must be same with hop count.")

    ids = np.array(ids).flatten()
    src_ids = ids
    current_batch_size = ids.size
    layers = Layers()
    for i in range(len(self._meta_path)):
      # req, res & call method.
      req = self._make_req(i, src_ids)
      res = pywrap.new_sampling_response()
      status = self._client.sample_neighbor(req, res)
      if status.ok():
        src_degrees = pywrap.get_sampling_node_degrees(res)
        dense_shape = (current_batch_size, max(src_degrees))

        nbr_ids = pywrap.get_sampling_node_ids(res)
        edge_ids = pywrap.get_sampling_edge_ids(res)

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

      dst_type = self._dst_types[i]
      layer_nodes = self._graph.get_nodes(
          dst_type,
          nbr_ids,
          offsets=src_degrees,
          shape=dense_shape)

      ids = np.concatenate([src_ids[idx].repeat(d) for \
                              idx, d in enumerate(src_degrees)])
      nbr_ids_flat = nbr_ids.flatten()
      layer_edges = self._graph.get_edges(
          self._meta_path[i],
          ids,
          nbr_ids_flat,
          offsets=src_degrees,
          shape=dense_shape)
      layer_edges.edge_ids = edge_ids
      layers.append_layer(Layer(layer_nodes, layer_edges))
      current_batch_size = nbr_ids_flat.size

      src_ids = nbr_ids

    return layers