Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,96 @@
|
|
1 |
---
|
2 |
license: mit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: mit
|
3 |
+
datasets:
|
4 |
+
- MedRAG/textbooks
|
5 |
+
- MedRAG/pubmed
|
6 |
+
- MedRAG/statpearls
|
7 |
+
- mteb/raw_biorxiv
|
8 |
+
- mteb/raw_medrxiv
|
9 |
+
- ms_marco
|
10 |
+
- BMRetriever/biomed_retrieval_dataset
|
11 |
+
language:
|
12 |
+
- en
|
13 |
+
tags:
|
14 |
+
- medical
|
15 |
+
- biology
|
16 |
+
- retrieval
|
17 |
+
- LLM
|
18 |
---
|
19 |
+
|
20 |
+
This model has been finetuned following the approach described in the paper **BMRetriever: Tuning Large Language Models as Better Biomedical Text Retrievers**. The associated GitHub repository is available here https://github.com/ritaranx/BMRetriever.
|
21 |
+
|
22 |
+
This model has 7B parameters. See the paper [link]() for details.
|
23 |
+
|
24 |
+
|
25 |
+
## Usage
|
26 |
+
|
27 |
+
Pre-trained models can be loaded through the HuggingFace transformers library:
|
28 |
+
|
29 |
+
```python
|
30 |
+
from transformers import AutoModel, AutoTokenizer
|
31 |
+
|
32 |
+
model = AutoModel.from_pretrained("BMRetriever/BMRetriever-7B")
|
33 |
+
tokenizer = AutoTokenizer.from_pretrained("BMRetriever/BMRetriever-7B")
|
34 |
+
```
|
35 |
+
|
36 |
+
Then embeddings for different sentences can be obtained by doing the following:
|
37 |
+
|
38 |
+
```python
|
39 |
+
import torch
|
40 |
+
import torch.nn.functional as F
|
41 |
+
|
42 |
+
from torch import Tensor
|
43 |
+
from transformers import AutoTokenizer, AutoModel
|
44 |
+
|
45 |
+
|
46 |
+
def last_token_pool(last_hidden_states: Tensor,
|
47 |
+
attention_mask: Tensor) -> Tensor:
|
48 |
+
left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
|
49 |
+
if left_padding:
|
50 |
+
return last_hidden_states[:, -1]
|
51 |
+
else:
|
52 |
+
sequence_lengths = attention_mask.sum(dim=1) - 1
|
53 |
+
batch_size = last_hidden_states.shape[0]
|
54 |
+
return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]
|
55 |
+
|
56 |
+
|
57 |
+
def get_detailed_instruct_query(task_description: str, query: str) -> str:
|
58 |
+
return f'Instruct: {task_description}\nQuery: {query}'
|
59 |
+
|
60 |
+
def get_detailed_instruct_passage(passage: str) -> str:
|
61 |
+
return f'Represent this passage\npassage: {passage}'
|
62 |
+
|
63 |
+
# Each query must come with a one-sentence instruction that describes the task
|
64 |
+
task = 'Given a scientific claim, retrieve documents that support or refute the claim'
|
65 |
+
queries = [
|
66 |
+
get_detailed_instruct_query(task, 'Cis-acting lncRNAs control the expression of genes that are positioned in the vicinity of their transcription sites.'),
|
67 |
+
get_detailed_instruct_query(task, 'Forkhead 0 (fox0) transcription factors are involved in apoptosis.')
|
68 |
+
]
|
69 |
+
|
70 |
+
# No need to add instruction for retrieval documents
|
71 |
+
documents = [
|
72 |
+
get_detailed_instruct_passage("Gene regulation by the act of long non-coding RNA transcription Long non-protein-coding RNAs (lncRNAs) are proposed to be the largest transcript class in the mouse and human transcriptomes. Two important questions are whether all lncRNAs are functional and how they could exert a function. Several lncRNAs have been shown to function through their product, but this is not the only possible mode of action. In this review we focus on a role for the process of lncRNA transcription, independent of the lncRNA product, in regulating protein-coding-gene activity in cis. We discuss examples where lncRNA transcription leads to gene silencing or activation, and describe strategies to determine if the lncRNA product or its transcription causes the regulatory effect."),
|
73 |
+
get_detailed_instruct_passage("Noncoding transcription at enhancers: general principles and functional models. Mammalian genomes are extensively transcribed outside the borders of protein-coding genes. Genome-wide studies recently demonstrated that cis-regulatory genomic elements implicated in transcriptional control, such as enhancers and locus-control regions, represent major sites of extragenic noncoding transcription. Enhancer-templated transcripts provide a quantitatively small contribution to the total amount of cellular nonribosomal RNA; nevertheless, the possibility that enhancer transcription and the resulting enhancer RNAs may, in some cases, have functional roles, rather than represent mere transcriptional noise at accessible genomic regions, is supported by an increasing amount of experimental data. In this article we review the current knowledge on enhancer transcription and its functional implications.")
|
74 |
+
]
|
75 |
+
input_texts = queries + documents
|
76 |
+
|
77 |
+
max_length = 512
|
78 |
+
|
79 |
+
# Tokenize the input texts
|
80 |
+
batch_dict = tokenizer(input_texts, max_length=max_length, padding=True, truncation=True, return_tensors='pt')
|
81 |
+
|
82 |
+
model.eval()
|
83 |
+
with torch.no_grad():
|
84 |
+
outputs = model(**batch_dict)
|
85 |
+
embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
|
86 |
+
```
|
87 |
+
|
88 |
+
Then similarity scores between the different sentences are obtained with a dot product between the embeddings:
|
89 |
+
|
90 |
+
```python
|
91 |
+
scores = (embeddings[:2] @ embeddings[2:].T)
|
92 |
+
print(scores.tolist())
|
93 |
+
```
|
94 |
+
|
95 |
+
## Citation
|
96 |
+
Coming Soon!
|