【问题标题】:How to get BioBERT embeddings如何获得 BioBERT 嵌入
【发布时间】:2021-02-19 20:09:03
【问题描述】:

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

这是数据框的假设样本

Visit Code Problem Assessment
1234 ge reflux working diagnosis well
4567 medication refill order working diagnosis note called in brand benicar 5mg qd 30 prn refill

我试过这个包,但安装时收到错误 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)

非常感谢任何帮助!

【问题讨论】:

    标签: python nlp data-science biopython bert-language-model


    【解决方案1】:

    尝试如下安装:

    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
    
    Visit Code Problem Assessment
    0 1234 ge reflux working diagnosis well
    1 1234 other reflux diagnosis poor
    2 4567 medication refill order working diagnosis note called in brand benicar 5mg qd 30 prn refill
    3 4567 medication must be refilled diagnosis note called in brand Olmesartan 10mg qd 40 prn refill
    >>> biobert = BiobertEmbedding()
    >>> df['sentence embedding'] = df['Problem Assessment'].apply(lambda sentence: biobert.sentence_vector(sentence))
    >>> df
    
    Visit Code Problem Assessment sentence embedding
    0 1234 ge reflux working diagnosis well tensor([ 2.7189e-01, -1.6195e-01, 5.8270e-02, -3.2730e-01, 7.5583e-02, ...
    1 1234 other reflux diagnosis poor tensor([ 1.6971e-01, -2.1405e-01, 3.4427e-02, -2.3090e-01, 1.6007e-02, ...
    2 4567 medication refill order working diagnosis note called in brand benicar 5mg qd 30 prn refill tensor([ 1.5370e-01, -3.9875e-01, 2.0089e-01, 4.1506e-02, 6.9854e-02, ...
    3 4567 medication must be refilled diagnosis note called in brand Olmesartan 10mg qd 40 prn refill tensor([ 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
    

    我们可以看到,不出所料,相似的句子非常接近

    【讨论】:

    • 使用该安装方法我收到此错误消息:错误:找不到匹配的分发为 torch==1.2.0
    • 完整错误消息:查看链接:download.pytorch.org/whl/torch_stable.html 收集 biobert-embedding==0.1.2 使用缓存的 biobert-embedding-0.1.2.tar.gz (4.8 kB) 错误:找不到满足要求的版本torch==1.2.0(来自版本:0.1.2、0.1.2.post1、0.1.2.post2、1.4.0、1.4.0+cpu、1.4.0+cu92、1.5。 0, 1.5.0+cpu, 1.5.0+cu101, 1.5.0+cu92, 1.5.1, 1.5.1+cpu, 1.5.1+cu101, 1.5.1+cu92, 1.6.0, 1.6.0+ cpu, 1.6.0+cu101, 1.7.0, 1.7.0+cpu, 1.7.0+cu101, 1.7.0+cu110, 1.7.1, 1.7.1+cpu, 1.7.1+cu101, 1.7.1+ cu110) 错误:找不到匹配的分布 fortor==1.2.0
    • 再次感谢您的帮助!
    • 你有 32 位 Python 吗? PyTorch 依赖于 64 位,请参阅stackoverflow.com/questions/57499002/… 了解更多信息。
    猜你喜欢
    • 2020-07-04
    • 1970-01-01
    • 2020-04-07
    • 2013-05-11
    • 2021-11-09
    • 2021-03-04
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多