ritaranx commited on
Commit
8d037c7
1 Parent(s): 947cb2d

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +81 -0
README.md CHANGED
@@ -1,3 +1,84 @@
1
  ---
2
  license: mit
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: mit
3
  ---
4
+
5
+ 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.
6
+
7
+ This model has 410M parameters. See the paper [link]() for details.
8
+
9
+
10
+ ## Usage
11
+
12
+ Pre-trained models can be loaded through the HuggingFace transformers library:
13
+
14
+ ```python
15
+ from transformers import AutoModel, AutoTokenizer
16
+
17
+ model = AutoModel.from_pretrained("BMRetriever/BMRetriever-410m")
18
+ tokenizer = AutoTokenizer.from_pretrained("BMRetriever/BMRetriever-410m")
19
+ ```
20
+
21
+ Then embeddings for different sentences can be obtained by doing the following:
22
+
23
+ ```python
24
+
25
+ import torch
26
+ import torch.nn.functional as F
27
+
28
+ from torch import Tensor
29
+ from transformers import AutoTokenizer, AutoModel
30
+
31
+
32
+ def last_token_pool(last_hidden_states: Tensor,
33
+ attention_mask: Tensor) -> Tensor:
34
+ left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
35
+ if left_padding:
36
+ return last_hidden_states[:, -1]
37
+ else:
38
+ sequence_lengths = attention_mask.sum(dim=1) - 1
39
+ batch_size = last_hidden_states.shape[0]
40
+ return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]
41
+
42
+
43
+ def get_detailed_instruct_query(task_description: str, query: str) -> str:
44
+ return f'Instruct: {task_description}\nQuery: {query}'
45
+
46
+ def get_detailed_instruct_passage(passage: str) -> str:
47
+ return f'Represent this passage\npassage: {passage}'
48
+
49
+ # Each query must come with a one-sentence instruction that describes the task
50
+ task = 'Given a scientific claim, retrieve documents that support or refute the claim'
51
+ queries = [
52
+ get_detailed_instruct(task, 'Cis-acting lncRNAs control the expression of genes that are positioned in the vicinity of their transcription sites.'),
53
+ get_detailed_instruct(task, 'Forkhead 0 (fox0) transcription factors are involved in apoptosis.')
54
+ ]
55
+ # No need to add instruction for retrieval documents
56
+ documents = [
57
+ "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.",
58
+ "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."
59
+ ]
60
+ input_texts = queries + documents
61
+
62
+
63
+ max_length = 512
64
+ # Tokenize the input texts
65
+ batch_dict = tokenizer(input_texts, max_length=max_length, padding=True, truncation=True, return_tensors='pt')
66
+
67
+ model.eval()
68
+ with torch.no_grad():
69
+ outputs = model(**batch_dict)
70
+ embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
71
+
72
+ ```
73
+
74
+ Then similarity scores between the different sentences are obtained with a dot product between the embeddings:
75
+ ```python
76
+
77
+ scores = (embeddings[:2] @ embeddings[2:].T)
78
+ print(scores.tolist())
79
+ ```
80
+
81
+ ## Citation
82
+ Coming Soon!
83
+
84
+