不能有效地使用多核 CPU 用 gensim 训练 Doc2vec
Not efficiently to use multi-Core CPU for training Doc2vec with gensim
我正在使用 24 个虚拟内核 CPU 和 100G 内存来使用 Gensim 训练 Doc2Vec,但是 CPU 的使用率总是在 200% 左右以修改内核数量。
top
htop
以上两张图显示了cpu的使用百分比,这表明cpu没有被有效使用。
cores = multiprocessing.cpu_count()
assert gensim.models.doc2vec.FAST_VERSION > -1, "This will be painfully slow otherwise"
simple_models = [
# PV-DBOW plain
Doc2Vec(dm=0, vector_size=100, negative=5, hs=0, min_count=2, sample=0,
epochs=20, workers=cores),
# PV-DM w/ default averaging; a higher starting alpha may improve CBOW/PV-DM modes
Doc2Vec(dm=1, vector_size=100, window=10, negative=5, hs=0, min_count=2, sample=0,
epochs=20, workers=cores, alpha=0.05, comment='alpha=0.05'),
# PV-DM w/ concatenation - big, slow, experimental mode
# window=5 (both sides) approximates paper's apparent 10-word total window size
Doc2Vec(dm=1, dm_concat=1, vector_size=100, window=5, negative=5, hs=0, min_count=2, sample=0,
epochs=20, workers=cores),
]
for model in simple_models:
model.build_vocab(all_x_w2v)
print("%s vocabulary scanned & state initialized" % model)
models_by_name = OrderedDict((str(model), model) for model in simple_models)
编辑:
我尝试使用参数 corpus_file 代替文件,并解决了上述问题。但是,我需要调整代码并将all_x_w2v转换为文件,而all_x_w2v并没有直接这样做。
Python 全局解释器锁("GIL")和其他线程间瓶颈阻止其代码使用经典的 gensim Word2Vec
/[= 使所有 CPU 核心饱和11=]/etc 灵活的语料库迭代器——你可以在其中提供任何可重复的文本序列。
您可以通过以下步骤稍微提高吞吐量:
negative
、size
和 window
的较大值
避免迭代器中的任何复杂步骤(如标记化)——理想情况下它只是从简单的磁盘格式流式传输
尝试不同的 worker
计数 – 最佳计数会根据您的其他参数和系统详细信息而有所不同,但通常在 3-12 范围内(无论多多少个内核你有)
此外,gensim
的最新版本提供了另一种语料库规范方法:corpus_file
指向已经 space 分隔的每行文本文件的指针。如果您以这种方式提供文本,多个线程将各自读取优化代码中的原始文件——并且有可能实现更高的 CPU 利用率。但是,在这种模式下,您无法指定自己的文档 tags
,或每个文档指定多个 tag
。 (文件将根据文件中的行号获得唯一的 ID。)
请参阅 Doc2Vec
的文档及其参数 corpus_file
:
https://radimrehurek.com/gensim/models/doc2vec.html#gensim.models.doc2vec.Doc2Vec
我正在使用 24 个虚拟内核 CPU 和 100G 内存来使用 Gensim 训练 Doc2Vec,但是 CPU 的使用率总是在 200% 左右以修改内核数量。
top
htop
以上两张图显示了cpu的使用百分比,这表明cpu没有被有效使用。
cores = multiprocessing.cpu_count()
assert gensim.models.doc2vec.FAST_VERSION > -1, "This will be painfully slow otherwise"
simple_models = [
# PV-DBOW plain
Doc2Vec(dm=0, vector_size=100, negative=5, hs=0, min_count=2, sample=0,
epochs=20, workers=cores),
# PV-DM w/ default averaging; a higher starting alpha may improve CBOW/PV-DM modes
Doc2Vec(dm=1, vector_size=100, window=10, negative=5, hs=0, min_count=2, sample=0,
epochs=20, workers=cores, alpha=0.05, comment='alpha=0.05'),
# PV-DM w/ concatenation - big, slow, experimental mode
# window=5 (both sides) approximates paper's apparent 10-word total window size
Doc2Vec(dm=1, dm_concat=1, vector_size=100, window=5, negative=5, hs=0, min_count=2, sample=0,
epochs=20, workers=cores),
]
for model in simple_models:
model.build_vocab(all_x_w2v)
print("%s vocabulary scanned & state initialized" % model)
models_by_name = OrderedDict((str(model), model) for model in simple_models)
编辑:
我尝试使用参数 corpus_file 代替文件,并解决了上述问题。但是,我需要调整代码并将all_x_w2v转换为文件,而all_x_w2v并没有直接这样做。
Python 全局解释器锁("GIL")和其他线程间瓶颈阻止其代码使用经典的 gensim Word2Vec
/[= 使所有 CPU 核心饱和11=]/etc 灵活的语料库迭代器——你可以在其中提供任何可重复的文本序列。
您可以通过以下步骤稍微提高吞吐量:
negative
、size
和window
的较大值
避免迭代器中的任何复杂步骤(如标记化)——理想情况下它只是从简单的磁盘格式流式传输
尝试不同的
worker
计数 – 最佳计数会根据您的其他参数和系统详细信息而有所不同,但通常在 3-12 范围内(无论多多少个内核你有)
此外,gensim
的最新版本提供了另一种语料库规范方法:corpus_file
指向已经 space 分隔的每行文本文件的指针。如果您以这种方式提供文本,多个线程将各自读取优化代码中的原始文件——并且有可能实现更高的 CPU 利用率。但是,在这种模式下,您无法指定自己的文档 tags
,或每个文档指定多个 tag
。 (文件将根据文件中的行号获得唯一的 ID。)
请参阅 Doc2Vec
的文档及其参数 corpus_file
:
https://radimrehurek.com/gensim/models/doc2vec.html#gensim.models.doc2vec.Doc2Vec