示例#1
0
    def node2vec_walk(self, walk_length, start_node):

        G = self.G
        alias_nodes = self.alias_nodes
        alias_edges = self.alias_edges

        walk = [start_node]

        while len(walk) < walk_length:
            cur = walk[-1]
            cur_nbrs = list(G.neighbors(cur))
            if len(cur_nbrs) > 0:
                if len(walk) == 1:
                    walk.append(cur_nbrs[alias_sample(alias_nodes[cur][0],
                                                      alias_nodes[cur][1])])
                else:
                    prev = walk[-2]
                    # if cur<prev:
                    #     edge = (cur, prev)
                    # else:
                    #     edge = (prev, cur)
                    edge = (prev, cur)
                    # print("edge:", edge)
                    # print("test:", len(alias_edges[edge]))
                    # if edge not in G.edges():
                    #     edge = (cur, prev)
                    #     print("afteredge:",edge)
                    next_node = cur_nbrs[alias_sample(alias_edges[edge][0],
                                                      alias_edges[edge][1])]
                    walk.append(next_node)
            else:
                break

        return walk
示例#2
0
    def node2vec_walk(self, walk_length, start_node):

        G = self.G
        alias_nodes = self.alias_nodes
        alias_edges = self.alias_edges

        walk = [start_node]

        while len(walk) < walk_length:
            cur = walk[-1]
            cur_nbrs = list(G.neighbors(cur))
            if len(cur_nbrs) > 0:
                if len(walk) == 1:
                    walk.append(cur_nbrs[alias_sample(alias_nodes[cur][0],
                                                      alias_nodes[cur][1])])
                else:
                    prev = walk[-2]
                    edge = (prev, cur)
                    next_node = cur_nbrs[alias_sample(alias_edges[edge][0],
                                                      alias_edges[edge][1])]
                    walk.append(next_node)
            else:
                break

        return walk
示例#3
0
            def motif_walk(index):
                start0 = time.process_time()
                walk = []  # motif sequence
                walk.append(tuple(mot_np[index]))
                # esxit_mot_index_dict {'motif type' : [index of exsit motif in walk]}
                exsit_mot_index_dict = defaultdict(list)
                exsit_mot_index_dict[mot_type].append(
                    motif_index_dict[mot_type][walk[-1]])
                while len(walk) < self.walk_length:
                    # use Alias sample to choose next motif type
                    next_mot_type = motype_list[alias_sample(accept, alias)]
                    next_mot_np = motif_dict[next_mot_type]
                    np_list = [
                        lud[i][next_mot_type].toarray() for i in walk[-1]
                        if i in lud.keys() and next_mot_type in lud[i].keys()
                    ]
                    if np_list:
                        tmp_zero_np = sum(np_list).sum(axis=1)
                        if next_mot_type in exsit_mot_index_dict.keys():
                            # set the index of motif which has been in walk weight to 0
                            tmp_zero_np[
                                exsit_mot_index_dict[next_mot_type]] = 0

                        max_count = tmp_zero_np.max()
                        index_for_choice = np.where(
                            tmp_zero_np == max_count)[0]
                        index_seleced = random.choice(index_for_choice)
                        next_mot = tuple(next_mot_np[index_seleced])
                        walk.append(next_mot)
                        exsit_mot_index_dict[next_mot_type].append(
                            motif_index_dict[next_mot_type][walk[-1]])
                print('\r' + 'each walk needs:',
                      time.process_time() - start0,
                      end='')
                return walk
示例#4
0
def chooseNeighbor(v, graphs, layers_alias, layers_accept, layer):

    v_list = graphs[layer][v]
    idx = alias_sample(layers_accept[layer][v], layers_alias[layer][v])
    v = v_list[idx]

    return v
示例#5
0
    def node2vec_walk(self, walk_length):

        G = self.g
        start_node = np.random.choice(G.nodes())
        alias_nodes = self.alias_nodes
        alias_edges = self.alias_edges

        walk = [start_node]
        walk_index = [self.node_index[start_node]]

        while len(walk) < walk_length:
            cur = walk[-1]
            cur_nbrs = list(G.neighbors(cur))
            if len(cur_nbrs) > 0:
                if len(walk) == 1:
                    walk_node = cur_nbrs[alias_sample(alias_nodes[cur][0],
                                                      alias_nodes[cur][1])]
                    walk.append(walk_node)
                    walk_index.append(self.node_index[walk_node])
                else:

                    prev = walk[-2]
                    edge = (prev, cur)
                    if alias_edges.__contains__(edge):

                        next_node = cur_nbrs[alias_sample(
                            alias_edges[edge][0], alias_edges[edge][1])]
                        walk.append(next_node)
                        walk_index.append(self.node_index[next_node])
                    else:
                        edge = (cur, prev)
                        next_node = cur_nbrs[alias_sample(
                            alias_edges[edge][0], alias_edges[edge][1])]
                        walk.append(next_node)
                        walk_index.append(self.node_index[next_node])

                    # next_node = cur_nbrs[alias_sample(prob = alias_edges[edge][0], alias=alias_edges[edge][1])]
                    # walk.append(next_node)
                    # walk_index.append(self.node_index[next_node])

            else:
                break

        return walk_index
示例#6
0
文件: line.py 项目: TIXhjq/GNN
    def batch_iter(self):

        edges = [(self.node2idx[x[0]], self.node2idx[x[1]])
                 for x in self.graph.edges()]

        data_size = self.graph.number_of_edges()
        shuffle_indices = np.random.permutation(np.arange(data_size))
        # positive or negative mod
        mod = 0
        # mod_size = 1 + self.negative_ratio
        mod_size = 10
        h = []
        t = []
        sign = 0
        count = 0
        start_index = 0
        end_index = min(start_index + self.batch_size, data_size)
        while True:
            if mod == 0:

                h = []
                t = []
                for i in range(start_index, end_index):
                    if random.random() >= self.edge_accept[shuffle_indices[i]]:
                        shuffle_indices[i] = self.edge_alias[
                            shuffle_indices[i]]
                    cur_h = edges[shuffle_indices[i]][0]
                    cur_t = edges[shuffle_indices[i]][1]
                    h.append(cur_h)
                    t.append(cur_t)
                sign = np.ones(len(h))
            else:
                sign = np.ones(len(h)) * -1
                t = []
                for i in range(len(h)):

                    t.append(alias_sample(self.node_accept, self.node_alias))

            if self.order == 'all':
                yield ([np.array(h), np.array(t)], [sign, sign])
            else:
                yield ([np.array(h), np.array(t)], [sign])

            mod += 1
            mod %= mod_size
            if mod == 0:
                start_index = end_index
                end_index = min(start_index + self.batch_size, data_size)

            if start_index >= data_size:
                count += 1
                mod = 0
                h = []
                shuffle_indices = np.random.permutation(np.arange(data_size))
                start_index = 0
                end_index = min(start_index + self.batch_size, data_size)
示例#7
0
 def motif_walk(index):
     start0 = time.process_time()
     walk = []  # motif sequence
     walk.append(tuple(mot_np[index]))
     while len(walk) < self.walk_length:
         # use Alias sample to choose next motif type
         next_mot_type = motype_list[alias_sample(accept, alias)]
         next_mot_np = motif_dict[next_mot_type]
         index_seleced = random.choice(
             motif_index_dict[next_mot_type])
         next_mot = tuple(next_mot_np[index_seleced])
         walk.append(next_mot)
     print('\r' + 'each walk needs:',
           time.process_time() - start0,
           end='')
     return walk
示例#8
0
文件: walker.py 项目: xuxiaohan/MSNE
 def walk(self, walk_length, start_node):
     Graphs = self.Graphs
     alias_nodes = self.alias_nodes
     walk = [start_node]
     while len(walk) < walk_length:
         cur = walk[-1]
         cur_nbrs = [
             list(G.neighbors(cur)) if (G.has_node(cur)) else []
             for G in Graphs
         ]
         cand = [i for i, e in enumerate(cur_nbrs) if len(e) != 0]
         if (len(cand) > 0):
             select = random.choice(cand)
             walk.append(
                 cur_nbrs[select][alias_sample(*alias_nodes[select][cur])])
         else:
             break
     return walk