예제 #1
0
    def _init_sampling(self) -> None:
        node_degrees = [self._graph.degree(v) for v in self._graph.nodes]
        node_degrees = np.array(node_degrees)
        self._node_sampling = als.AliasSampling(np.array(node_degrees), np.array(self._graph.nodes))

        edge_weights = [[u, v, w] for u, v, w in self._graph.edges.data("weight", default=1)]
        edge_weights = np.array(edge_weights)
        self._edge_sampling = als.AliasSampling(edge_weights[:, 2], edge_weights[:, :2])
예제 #2
0
파일: node2vec.py 프로젝트: vvancak/gem
    def _precompute_edges(self):
        edge_probs = {}

        for u, v, w in self._graph.edges.data("weight", default=1):
            # === DIRECTION U -> V ===
            next_nodes = []
            next_weights = []
            for t, d in self._graph[v].items():
                w = d.get('weight', 1)
                next_nodes.append(t)

                # BFS - DFS node2vec sampling strategy
                if t == u:
                    prob = 1.0 / self._p
                elif self._graph.has_edge(t, u):
                    prob = 1.0
                else:
                    prob = 1.0 / self._q

                # Append to unnormalized probs
                next_weights.append(prob * w)

            # Generate alias sampling for edge
            next_weights = np.array(next_weights)
            if len(next_weights > 0):
                edge_probs[(u, v)] = als.AliasSampling(
                    next_weights / sum(next_weights), np.array(next_nodes))

            # === DIRECTION V -> U ===
            next_nodes = []
            next_weights = []
            for t, d in self._graph[u].items():
                w = d.get('weight', 1)
                next_nodes.append(t)

                # BFS - DFS node2vec sampling strategy
                if t == v:
                    prob = 1.0 / self._p
                elif self._graph.has_edge(t, v):
                    prob = 1.0
                else:
                    prob = 1.0 / self._q

                # Append to unnormalized probs
                next_weights.append(prob * w)

            # Generate alias sampling for edge
            next_weights = np.array(next_weights)
            if len(next_weights > 0):
                edge_probs[(u, v)] = als.AliasSampling(
                    next_weights / sum(next_weights), np.array(next_nodes))

        # Store the sampled results
        self._edge_probs = edge_probs
예제 #3
0
    def _precompute_nodes(self):
        node_probs = {}

        for u in self._graph.nodes:
            nbr_nodes = []
            nbr_weights = []
            for v, d in self._graph[u].items():
                nbr_nodes.append(v)
                nbr_weights.append(d.get('weight', 1))

            nbr_weights = np.array(nbr_weights)
            if len(nbr_weights > 0):
                node_probs[u] = als.AliasSampling(nbr_weights / sum(nbr_weights), np.array(nbr_nodes))

        self._node_probs = node_probs
예제 #4
0
    np_results = []
    als_results = []

    for d in CLASSES:
        np_class_results = []
        als_class_results = []
        preproc_times = []

        for s in SEEDS:
            # Generate probbilities
            np.random.seed(s)
            probs = np.random.random_sample(d)
            probs = probs / np.sum(probs)

            # Alias Sampling Preprocessing
            preproc_als = als.AliasSampling(probs, probs)

            # Numpy Random Choice
            list = []
            start = time.time()
            for i in range(DRAWS):
                sample = np.random.choice(probs, 1, p=probs)
                list.append(sample)
            np_class_results.append((time.time() - start))
            print(list)

            # Alias Sampling Choice
            list = []
            start = time.time()
            for i in range(DRAWS):
                sample = preproc_als.sample(1)