我正在尝试使用microsoft/pubmedbert获取临床数据的word嵌入。我有360万行文本。将文本转换为10k行的向量大约需要30分钟。因此,对于360万行,大约需要180小时(约8天)。
有什么方法可以加快这个过程吗?
我的密码-
from transformers import AutoTokenizer
from transformers import pipeline
model_name = "microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract-fulltext"
tokenizer = AutoTokenizer.from_pretrained(model_name)
classifier = pipeline('feature-extraction',model=model_name, tokenizer=tokenizer)
def lambda_func(row):
tokens = tokenizer(row['notetext'])
if len(tokens['input_ids'])>512:
tokens = re.split(r'\b', row['notetext'])
tokens= [t for t in tokens if len(t) > 0 ]
row['notetext'] = ''.join(tokens[:512])
row['vectors'] = classifier(row['notetext'])[0][0]
return row
def process(progress_notes):
progress_notes = progress_notes.apply(lambda_func, axis=1)
return progress_notes
progress_notes = process(progress_notes)
vectors_2d = np.reshape(progress_notes['vectors'].to_list(), (vectors_length, vectors_breadth))
vectors_df = pd.DataFrame(vectors_2d)我的progress_notes数据就像-
progress_notes = pd.DataFrame({'id':[1,2,3],'progressnotetype':['Nursing Note', 'Nursing Note', 'Administration Note'], 'notetext': ['Patient\'s skin is grossly intact with exception of skin tear to r inner elbow and r lateral lower leg','Patient with history of Afib with RVR. Patient is incontinent of bowel and bladder.','Give 2 tablet by mouth every 4 hours as needed for Mild to moderate Pain Not to exceed 3 grams in 24 hours']})注- 1)我在aws ec2实例r5.8x大型(32个cpu )上运行代码--我尝试使用多处理,但代码陷入死锁,因为伯特占用了我的所有cpu核心。
发布于 2020-12-30 16:16:51
我认为主要的问题是你如何使用伯特,因为你正在逐句处理你的文本。相反,您应该将输入以小型批次的形式输入:
神经网络对于NLP来说不仅仅是一句一句,而且是多句话。这些句子以整数(令牌ID)的单个张量(令牌ID)与维数、句子数、\times序列长度叠加在一起。由于句子有不同的长度,通常批处理中最长序列的长度作为序列长度,并且所有短于此长度的句子都填充填充标记。您可以在huggingface的库这里中查看批处理。
使用面向批处理的处理方式可以使您从批处理中的所有句子的并行处理中获益。
问题是,虽然HuggingFace变压器支持面向批处理的训练,但在某些情况下它不支持面向批处理的推理。例如,提取令牌嵌入的FeatureExtractionPipeline不支持批处理(与具有sequential参数的TableQuestionAnsweringPipeline不同)。
这样,为了进行面向批处理的推理,您需要手动输入数据,而不是依赖于管道API。您可以在这条线中找到有关如何做到这一点的示例。
如果您尝试使用GPU而不是CPU,那么使用面向批处理的处理也是实现性能提高的关键。
如果您决定继续使用CPU,请确保您的Pytorch构建使用的是MKL,这是CPU中的一个主要性能提升器。您可以检查这条线如何做到这一点。如果您没有使用它,请安装包含它的更新版本。
发布于 2020-12-30 16:01:45
https://datascience.stackexchange.com/questions/87244
复制相似问题