我有一个列表,其中包含了数以百万计的句子,我需要嵌入。我正为此目的使用天资。这个问题似乎应该是令人尴尬的平行问题。但是当我试图优化的时候,我要么没有提高性能,要么它就停下来了。
我将我的句子定义为一个简单的字符串列表:
texts = [
"this is a test",
"to see how well",
"this system works",
"here are alot of words",
"many of them",
"they keep comming",
"many more sentences",
"so many",
"some might even say",
"there are 10 of them",
]我使用Flair创建嵌入:
from flair.embeddings import SentenceTransformerDocumentEmbeddings
from flair.data import Sentence
sentence_embedding = SentenceTransformerDocumentEmbeddings("bert-base-nli-mean-tokens")
def sentence_to_vector(sentence):
sentence_tokens = Sentence(sentence)
sentence_embedding.embed(sentence_tokens)
return sentence_tokens.get_embedding().tolist()import time
from joblib import Parallel, delayed
import concurrent.futures
def parallelize(iterable, func):
return Parallel(n_jobs=4, prefer="threads")(delayed(func)(i) for i in iterable)
print("start embedding sequentially")
tic = time.perf_counter()
embeddings = [sentence_to_vector(text) for text in texts]
toc = time.perf_counter()
print(toc - tic)
print("start embedding parallel, w. joblib")
tic = time.perf_counter()
embeddings = parallelize(texts, sentence_to_vector)
toc = time.perf_counter()
print(toc - tic)
print("start embedding parallel w. concurrent.futures")
tic = time.perf_counter()
with concurrent.futures.ProcessPoolExecutor() as executor:
embeddings = [executor.submit(sentence_to_vector, text) for text in texts]
toc = time.perf_counter()
print(toc - tic)Joblib函数正在运行,但它比按顺序执行要慢。concurrent.futures函数旋转了一堆线程,但却无限期地挂起。
任何正确方向的解决方案或暗示都将不胜感激。
发布于 2022-09-05 09:50:38
使用一个经过训练的模型的类比-看来,经过训练的模型一次只能识别一个项目。
通过复制文件,并运行all -应该没有问题的并行处理,例如Prog1.py,pr2.py.是相同代码的副本--当运行时,它们会得到不同的数据以进行处理。要手动并行运行,请打开多个命令窗口,并在每个命令窗口中运行不同的文件。
要以编程方式运行,主程序可以创建子进程并向每个进程发送不同的数据。或者批处理文件可以启动程序。运行10份你的剧本,把你的句子的1/10发给每个人。
然后把结果结合起来。
注意CPU和内存,避免机器占用100 pc CPU。(在计算出计算机可以处理多少并行程序时,慢慢增加程序和数据的数量)
https://stackoverflow.com/questions/73546129
复制相似问题