仅供个人学习之用,如有纰漏,敬请指正。原文地址
阅读《分布式计算》教程来了解gensim中的分布式计算。
设置一个集群
我们将会通过一个案例展示如何运行分布式潜在语义分析。让我们假设我们有5个计算机,所有的电脑都在一个网段(网络广播可达)。为了开始,首先安装gensim并在每台电脑上设置Pyro():
- 1
- 2
- 3
接下来,在某一个计算机上运行Pyro的名称服务(无论是哪一台):
- 1
假设我们的集群中的电脑都是有带有内存负载的双核电脑,我们可以在其中4台上运行2个工作者脚本,共创建8个逻辑工作节点:
- 1
这将会运行gensim的lsi_worker.py
脚本(在4台电脑上运行2次)。这让gensim知道它可以在这四台电脑上每台并行运行两个工作,以便计算可以更快,当然也会消耗双倍的内存。
再下一步,选择一台计算机将其作为一个作业调度程序,负责工作者同步,并在其上运行LSA调度器。
在我们的例子中,我们将会使用第5台电脑来作为调度器,并在那里运行:
- 1
一般来说,调度器可以运行在与其中一个工作者节点上,或者也可以是另一个不同的电脑(在相同的广播域)。调度器大多数时间不会占用太多CPU,但是请选择一个有足够内存的计算机。
就是这样!集群已经被建立起来了,并且可以用来接受工作了。后来需要移除工作者节点时,只要结束其lsi_worker
进程即可(不会影响正在运行的计算,节点的添加和删除都是动态的)。如果结束了lsi_dispatcher
,在你重启它之前将不能运行计算(虽然已经存在的工作者进程能重新启用)。
运行LSA
让我们测试一下我们的设置,运行一个分布式LSA计算。在五台计算机中的任意一台打开一个Python shell(再说一遍,可以是任意一台在同一广播域的计算机,我们的选择都是偶然的)并尝试运行:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
这里使用到了《语料库与向量空间》教程中创建的语料库及属性记号映射。如果你查找Python会话的日志,你应该会找到类似这样的一行:
- 1
这意味着一切都在顺利进行。你也可以检查来自工作者和调度器进程的日志文件——这对于防止问题特别有用。检查一下LSA的结果,打印前两个潜在主题:
- 1
- 2
成功了!但是这种规模的语料库对于我们强大的集群来说没有什么挑战性……实际上,我们故意降低了单个文档一次工作的大小(chunksize
参数),否则所有的文档将会被一个工作者一次处理完。
所以,让我们在百万文档上试验一下LSA:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
其日志文件应该类似于:
2010-08-10 02:46:35,087 : INFO : using distributed version with 8 workers
2010-08-10 02:46:35,087 : INFO : updating SVD with new documents
2010-08-10 02:46:35,202 : INFO : dispatched documents up to #10000
2010-08-10 02:46:35,296 : INFO : dispatched documents up to #20000
…
2010-08-10 02:46:46,524 : INFO : dispatched documents up to #990000
2010-08-10 02:46:46,694 : INFO : dispatched documents up to #1000000
2010-08-10 02:46:46,694 : INFO : reached the end of input; now waiting for all remaining jobs to finish
2010-08-10 02:46:47,195 : INFO : all jobs finished, downloading final projection
2010-08-10 02:46:47,200 : INFO : decomposition complete
因为我们的“一百万语料库”词汇量太小、结构太一般,LSA的计算啊仅仅消耗了12秒。-_-!为了真实的压力测试,让我们在英文维基百科语料库上做一个LSA。
维基百科上的分布式LSA
首先,像之前的《英文维基百科的实验》一样下载并准备维基百科语料库,然后加载语料库迭代器:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
现在我们已经准备好在英文维基百科上运行分布式LSA了:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
在串行模式,使用单程算法(one-pass algorithm)创建维基百科的LSI模型,在我的笔记本上消耗了5.25小时(OS X, C2D 2.53GHz, 4GB RAM with libVec)。使用了有4个工作者(Linux, dual-core Xeons of 2Ghz, 4GB RAM with ATLAS)的分布式模式,消耗时间下降至1小时41分。你可以在我的研究论文中阅读到更多的关于内部设定和实验的内容。