[英]Speeding up sampling in a graph
I've been recently working with graph sampling in Python. 我最近一直在使用Python中的图形采样。 My working example reads as:
我的工作示例如下:
for enx, wlen in enumerate(wlen_dist):
for j in range(wlen):
node_container = queue.Queue(maxsize=200000000)
node_container.put(node_name)
tmp_walk = [] # [node_name]
while not node_container.empty():
nod = node_container.get()
neighs = list(network.neighbors(nod))
tar = random.choice(neighs)
node_container.put(tar)
if len(tmp_walk) > enx+1:
break
tmp_walk.append(tar)
some_container.append(tmp_walk)
where wlen is the number of samples of path of length enx, and I am simply saving the walks to some_container (not really important here). 其中wlen是长度为enx的路径样本数,我只是将遍历保存到some_container(这里不是很重要)。 The wlen_dist is for example:
wlen_dist例如:
[1000,500,100]
and here, 1000 samples of a walk of length two, 500 of length 3 and 100 of length 4 are obtained. 这里,获得1000个长度为2的步行,500个长度为3和100个长度为4的样本。 The networkx is a networkX graph.
networkx是networkX图。 I was wondering, how does one speed up code like this (I am new to this part).
我想知道,如何加速这样的代码(我是这部分的新手)。
My ideas: 我的想法:
Use Numba and wrap individual walks into a method 使用Numba并将单独的步行包装到方法中
Use Cython somehow 以某种方式使用Cython
Rewrite it alltogether in C++ and call it somehow 用C ++重写它并以某种方式调用它
I would be glad for any ideas and feedback, thanks! 我很高兴有任何想法和反馈,谢谢!
One idea often used in graph embedding is the idea of reusing parts of random walks: 图形嵌入中经常使用的一个想法是重用随机游走的部分:
If you have a random walk visiting the nodes a_1, a_2, a_3
, you can regard this as one random walk of length 3 and 2 random walks of length 2 ( a_1, a_2
and a_2, a_3
). 如果您有随机游走访问节点
a_1, a_2, a_3
,您可以将其视为长度为3的随机游走和长度为2的随机游走( a_1, a_2
和a_2, a_3
)。
This can be generalised to longer walks, so your random walks of length 4 contain 2 random walks of length 3 and 3 random walks of length 2. 这可以推广到更长的步行,因此随机游走的长度4包含2个长度为3的随机游走和3个长度为2的随机游走。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.