[英]Use fasttext model (gensim) with threading
是否可以使用多線程訪問快速文本模型(gensim)?
目前,我正在嘗試加載一次模型(由於大小和加載時間),因此它會保留在內存中並連續數千次訪問其相似性函數。 我想並行執行此操作,而我當前的方法使用一個包裝類來加載模型,然后將其傳遞給工作人員。 但看起來它沒有返回任何結果。
包裝類。 發起一次。
from gensim.models.fasttext import load_facebook_model
class FastTextLocalModel:
def __init__(self):
self.model_name = "cc.de.300.bin"
self.model_path = path.join("data", "models", self.model_name)
self.fast_text = None
def load_model(self):
self.fast_text = load_facebook_model(self.model_path)
def similarity(self, word1: str = None, word2: str = None):
return self.fast_text.wv.similarity(word1, word2)
Processor 類使用了上面的FastTextLocalModel
方法:
fast_text_instance = FastTextLocalModel()
fast_text_instance.load_model()
with concurrent.futures.ThreadPoolExecutor(max_workers=multiprocessing.cpu_count()) as executor:
docs = corpus.get_documents() # docs is iterable
processor = ProcessorClass(model=fast_text_instance)
executor.map(processor.process, docs)
使用max_workers=1
似乎有效。
我不得不提一下,我沒有 Python 多線程方面的專業知識。
在這個先前的答案中可能有對您有用的想法,可能需要針對FastText
和最新版本的gensim
:
https://stackoverflow.com/a/43067907/130288
(關鍵是...
即使在不同進程中冗余加載也可能不會使用冗余內存,如果關鍵內存消耗數組是mmap
並因此在 OS 級別自動共享; 和
你必須做一些額外的技巧來防止通常的重新計算加載后和歸一化向量之前的相似性操作,這會破壞共享
FastText
但是FastText
代碼中的混亂可能會使這些代碼變得更難。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.