首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用伯特的预先训练过的模型“更快”将文本转换为文字嵌入?

如何使用伯特的预先训练过的模型“更快”将文本转换为文字嵌入?
EN

Stack Overflow用户
提问于 2020-12-29 15:48:40
回答 1查看 316关注 0票数 0

我正在尝试使用microsoft/pubmedbert获取临床数据的word嵌入。我有360万行文本。将文本转换为10k行的向量大约需要30分钟。因此,对于360万行,大约需要180小时(约8天)。

,有什么方法可以加快这个过程吗?

我的密码-

代码语言:javascript
复制
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_breadth = 768
vectors_length = len(progress_notes)
vectors_2d = np.reshape(progress_notes['vectors'].to_list(), (vectors_length, vectors_breadth))
vectors_df = pd.DataFrame(vectors_2d)

我的progress_notes数据就像-

代码语言:javascript
复制
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 )上运行代码--我尝试使用多处理,但代码陷入死锁,因为ec2占用了我的所有cpu核心。

EN

回答 1

Stack Overflow用户

发布于 2022-12-02 22:02:10

不只是?应用不是最快的方法。

代码语言:javascript
复制
from sentence_transformers import SentenceTransformer
sbert_model = SentenceTransformer('microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract-fulltext')
document_embeddings = sbert_model.encode(pd.Series(['hello', 'cell type', 'protein']))
document_embeddings

您将得到如下的输出

代码语言:javascript
复制
array([[ 0.06255245,  0.14945783, -0.06224129, ..., -0.11892398,
        -0.0507343 ,  0.0153866 ],
       [-0.17571464,  0.03554079, -0.04899959, ..., -0.24369009,
        -0.00672011,  0.04914075],
       [-0.22093703, -0.03271236, -0.08943298, ..., -0.21335356,
         0.11418738, -0.09207606]], dtype=float32)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65494850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档