如何解决如何获得 BioBERT 嵌入
我在 Pandas 数据框中有一个字段,其中有一个文本字段,我想为其生成 BioBERT 嵌入。有没有一种简单的方法可以生成向量嵌入?我想在另一个模型中使用它们。
这是数据框的假设样本
访问代码 | 问题评估 |
---|---|
1234 | 胃食管反流工作诊断良好 |
4567 | 品牌 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)
非常感谢任何帮助!
解决方法
尝试如下安装:
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 | 胃食管反流工作诊断良好 |
1 | 1234 | 其他反流诊断不佳 |
2 | 4567 | 品牌 benicar 5mg qd 30 prn refill 中调用的药物补充订单工作诊断说明 |
3 | 4567 | 必须重新填充药物诊断说明,称为奥美沙坦 10mg qd 40 prn refill |
>>> biobert = BiobertEmbedding()
>>> df['sentence embedding'] = df['Problem Assessment'].apply(lambda sentence: biobert.sentence_vector(sentence))
>>> df
访问代码 | 问题评估 | 句子嵌入 | |
---|---|---|---|
0 | 1234 | 胃食管反流工作诊断良好 | 张量([ 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 refill 中调用的药物补充订单工作诊断说明 | 张量([ 1.5370e-01,-3.9875e-01,2.0089e-01,4.1506e-02,6.9854e-02,... |
3 | 4567 | 必须重新填充药物诊断说明,称为奥美沙坦 10mg qd 40 prn refill | 张量([ 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
我们可以看到,正如预期的那样,相似的句子靠得很近
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。