def _gen_sampling_table(self): # create sampling table for vertex power = 0.75 numNodes = self.node_size node_degree = np.zeros(numNodes) # out degree node2idx = self.node2idx for edge in self.graph.edges(): node_degree[node2idx[edge[0]]] += self.graph[edge[0]][edge[1]].get( 'weight', 1.0) total_sum = sum( [math.pow(node_degree[i], power) for i in range(numNodes)]) norm_prob = [ float(math.pow(node_degree[j], power)) / total_sum for j in range(numNodes) ] self.node_accept, self.node_alias = create_alias_table(norm_prob) # create sampling table for edge numEdges = self.graph.number_of_edges() total_sum = sum([ self.graph[edge[0]][edge[1]].get('weight', 1.0) for edge in self.graph.edges() ]) norm_prob = [ self.graph[edge[0]][edge[1]].get('weight', 1.0) * numEdges / total_sum for edge in self.graph.edges() ] self.edge_accept, self.edge_alias = create_alias_table(norm_prob)
def preprocess_transition_probs(self): """ Preprocessing of transition probabilities for guiding the random walks. """ G = self.G alias_nodes = {} for node in G.nodes(): unnormalized_probs = [ G[node][nbr].get('weight', 1.0) for nbr in G.neighbors(node) ] norm_const = sum(unnormalized_probs) normalized_probs = [ float(u_prob) / norm_const for u_prob in unnormalized_probs ] alias_nodes[node] = create_alias_table(normalized_probs) alias_edges = {} for edge in G.edges(): alias_edges[edge] = self.get_alias_edge(edge[0], edge[1]) self.alias_nodes = alias_nodes self.alias_edges = alias_edges return
def get_alias_edge(self, t, v): """ compute unnormalized transition probability between nodes v and its neighbors give the previous visited node t. :param t: :param v: :return: """ G = self.G p = self.p q = self.q unnormalized_probs = [] for x in G.neighbors(v): weight = G[v][x].get('weight', 1.0) # w_vx if x == t: # d_tx == 0 unnormalized_probs.append(weight / p) elif G.has_edge(x, t): # d_tx == 1 unnormalized_probs.append(weight) else: # d_tx > 1 unnormalized_probs.append(weight / q) norm_const = sum(unnormalized_probs) normalized_probs = [ float(u_prob) / norm_const for u_prob in unnormalized_probs ] return create_alias_table(normalized_probs)
def preprocess_motif_probs(): # creat alias table motif_pro = [ float(value) / sum(motif_length_dict.values()) for value in motif_length_dict.values() ] accept, alias = create_alias_table(motif_pro) return accept, alias
def preprocessing_transition_probs(self): G = self.G alias_nodes = {} alias_edges = {} for node in G.nodes(): unnormalized_probs = [G[node][nbs].get('weight',1.0) for nbs in G.neighbors(node)] norm_const = sum(unnormalized_probs) normalized_probs = [float(un_prob/norm_const) for un_prob in unnormalized_probs] alias_nodes[node] = create_alias_table(normalized_probs) for edge in G.edges(): alias_edges[edge] = self.get_alias_edge(edge[0],edge[1]) self.alias_nodes = alias_nodes self.alias_edges = alias_edges
def get_alias_edge(self,pre_node,_node): G = self.G p = self.p q = self.q unnormalized_probs = [] for nbs in G.neighbors(_node): weight = G[_node][nbs].get('weight', 1.0) if nbs == pre_node: unnormalized_probs.append(weight/p) elif G.has_edge(pre_node,nbs): unnormalized_probs.append(weight) else:unnormalized_probs.append(weight/q) norm_const = sum(unnormalized_probs) normalized_probs = [float(un_probs/norm_const) for un_probs in unnormalized_probs] return create_alias_table(normalized_probs)
def preprocess_transition_probs(self, G): """ Preprocessing of transition probabilities for guiding the random walks. """ print("Preprocess node alias") alias_nodes = {} L = len(G.nodes()) / 100 for i, node in enumerate(G.nodes()): if (self.verbose != 0 and i % L == 0): print(i / len(G.nodes())) unnormalized_probs = [ G[node][nbr].get('weight', 1.0) for nbr in G.neighbors(node) ] norm_const = sum(unnormalized_probs) normalized_probs = [ float(u_prob) / norm_const for u_prob in unnormalized_probs ] alias_nodes[node] = create_alias_table(normalized_probs) self.alias_nodes.append(alias_nodes)
def _get_transition_probs(self, layers_adj, layers_distances): layers_alias = {} layers_accept = {} for layer in layers_adj: neighbors = layers_adj[layer] layer_distances = layers_distances[layer] node_alias_dict = {} node_accept_dict = {} norm_weights = {} for v, neighbors in neighbors.items(): e_list = [] sum_w = 0.0 for n in neighbors: if (v, n) in layer_distances: wd = layer_distances[v, n] else: wd = layer_distances[n, v] w = np.exp(-float(wd)) e_list.append(w) sum_w += w # 对每个节点的邻居节点权值归一化 e_list = [x / sum_w for x in e_list] norm_weights[v] = e_list accept, alias = create_alias_table(e_list) node_alias_dict[v] = alias node_accept_dict[v] = accept pd.to_pickle( norm_weights, self.temp_path + "norm_weights_distance-layer-" + str(layer) + '.pkl') layers_alias[layer] = node_alias_dict layers_accept[layer] = node_accept_dict return layers_accept, layers_alias