首页 > 解决方案 > 如何获得 BioBERT 嵌入

问题描述

我在 pandas 数据框中有一个字段,其中有一个文本字段,我想为其生成 BioBERT 嵌入。有没有一种简单的方法可以生成向量嵌入?我想在另一个模型中使用它们。

这是数据框的假设样本

访问代码 问题评估
1234 ge 反流工作诊断良好
4567 药物补充订单工作诊断说明称为品牌 benicar 5mg qd 30 prn 补充

我试过这个包,但安装时收到错误 https://pypi.org/project/biobert-embedding

错误:

Collecting biobert-embedding
  Using cached biobert-embedding-0.1.2.tar.gz (4.8 kB)
ERROR: Could not find a version that satisfies the requirement torch==1.2.0 (from biobert-embedding) (from versions: 0.1.2, 0.1.2.post1, 0.1.2.post2, 1.7.1)
ERROR: No matching distribution found for torch==1.2.0 (from biobert-embedding)

任何帮助是极大的赞赏!

标签: pythonnlpdata-sciencebiopythonbert-language-model

解决方案


尝试如下安装:

pip install biobert-embedding==0.1.2 torch==1.2.0 -f https://download.pytorch.org/whl/torch_stable.html

我扩展了您的示例数据框以说明您现在如何为您计算句子向量problem assessments并使用它们来计算例如相似之间的余弦相似度visit codes

>>> from biobert_embedding.embedding import BiobertEmbedding
>>> from scipy.spatial import distance
>>> import pandas as pd

>>> data = {'Visit Code': [1234, 1235, 4567, 4568], 
        'Problem Assessment': ['ge reflux working diagnosis well', 
                               'other reflux diagnosis poor', 
                               'medication refill order working diagnosis note called in brand benicar 5mg qd 30 prn refill',
                               'medication must be refilled diagnosis note called in brand Olmesartan 10mg qd 40 prn refill']}

>>> df = pd.DataFrame(data)
>>> df
访问代码 问题评估
0 1234 ge 反流工作诊断良好
1 1234 其他反流诊断差
2 4567 药物补充订单工作诊断说明称为品牌 benicar 5mg qd 30 prn 补充
3 4567 药物必须重新填充诊断说明称为奥美沙坦 10mg qd 40 prn 补充
>>> biobert = BiobertEmbedding()
>>> df['sentence embedding'] = df['Problem Assessment'].apply(lambda sentence: biobert.sentence_vector(sentence))
>>> df
访问代码 问题评估 句子嵌入
0 1234 ge 反流工作诊断良好 张量([ 2.7189e-01, -1.6195e-01, 5.8270e-02, -3.2730e-01, 7.5583e-02, ...
1 1234 其他反流诊断差 张量([ 1.6971e-01, -2.1405e-01, 3.4427e-02, -2.3090e-01, 1.6007e-02, ...
2 4567 药物补充订单工作诊断说明称为品牌 benicar 5mg qd 30 prn 补充 张量([ 1.5370e-01, -3.9875e-01, 2.0089e-01, 4.1506e-02, 6.9854e-02, ...
3 4567 药物必须重新填充诊断说明称为奥美沙坦 10mg qd 40 prn 补充 张量([ 2.2128e-01, -2.0283e-01, 2.2194e-01, 9.1156e-02, 1.1620e-01, ...
>>> df.groupby('Visit Code')['sentence embedding'].apply(lambda sentences: 1 - distance.cosine(sentences.values) )


Visit Code
1234    0.950492
4567    0.969715
Name: sentence embedding, dtype: float64

我们可以看到,正如预期的那样,相似的句子非常接近


推荐阅读