shreyansh26 commited on
Commit
f260bdd
1 Parent(s): e23603a

Add model card

Browse files
Files changed (1) hide show
  1. README.md +83 -0
README.md ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ datasets:
3
+ - sentence-transformers/embedding-training-data
4
+ - flax-sentence-embeddings/stackexchange_xml
5
+ - snli
6
+ - eli5
7
+ - search_qa
8
+ - multi_nli
9
+ - wikihow
10
+ - natural_questions
11
+ - trivia_qa
12
+ - ms_marco
13
+ - gooaq
14
+ - yahoo_answers_topics
15
+ language:
16
+ - en
17
+ ---
18
+
19
+ # bert-base-1024-biencoder-6M-pairs
20
+
21
+ A long context biencoder based on [MosaicML's BERT pretrained on 1024 sequence length](https://huggingface.co/mosaicml/mosaic-bert-base-seqlen-1024). This model maps sentences & paragraphs to a 768 dimensional dense vector space
22
+ and can be used for tasks like clustering or semantic search.
23
+
24
+ ## Usage
25
+
26
+ ### Download the model and related scripts
27
+ ```git clone https://huggingface.co/shreyansh26/bert-base-1024-biencoder-6M-pairs```
28
+
29
+ ### Inference
30
+ ```python
31
+ import torch
32
+ from torch import nn
33
+ from transformers import AutoModelForMaskedLM, AutoTokenizer, pipeline, AutoModel
34
+ from mosaic_bert import BertModel
35
+
36
+ # pip install triton==2.0.0.dev20221202 --no-deps if using Pytorch 2.0
37
+
38
+ class AutoModelForSentenceEmbedding(nn.Module):
39
+ def __init__(self, model, tokenizer, normalize=True):
40
+ super(AutoModelForSentenceEmbedding, self).__init__()
41
+
42
+ self.model = model.to("cuda")
43
+ self.normalize = normalize
44
+ self.tokenizer = tokenizer
45
+
46
+ def forward(self, **kwargs):
47
+ model_output = self.model(**kwargs)
48
+ embeddings = self.mean_pooling(model_output, kwargs['attention_mask'])
49
+ if self.normalize:
50
+ embeddings = torch.nn.functional.normalize(embeddings, p=2, dim=1)
51
+
52
+ return embeddings
53
+
54
+ def mean_pooling(self, model_output, attention_mask):
55
+ token_embeddings = model_output[0] # First element of model_output contains all token embeddings
56
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
57
+ return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
58
+
59
+ model = AutoModel.from_pretrained("<path-to-model>", trust_remote_code=True).to("cuda")
60
+ model = AutoModelForSentenceEmbedding(model, tokenizer)
61
+ tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
62
+
63
+ sentences = ["This is an example sentence", "Each sentence is converted"]
64
+
65
+ encoded_input = tokenizer(sentences, padding=True, truncation=True, max_length=1024, return_tensors='pt').to("cuda")
66
+ embeddings = model(**encoded_input)
67
+
68
+ print(embeddings)
69
+ print(embeddings.shape)
70
+ ```
71
+
72
+ ## Other details
73
+
74
+ ### Training
75
+
76
+ This model has been trained on 6.4M randomly sampled pairs of sentences/paragraphs from the same training set that Sentence Transformers models use. Details of the
77
+ training set [here](https://huggingface.co/sentence-transformers/all-mpnet-base-v2#training-data).
78
+
79
+ The training (along with hyperparameters), inference and data loading scripts can all be found in [this Github repository](https://github.com/shreyansh26/Long-Context-Biencoder).
80
+
81
+ ### Evaluations
82
+
83
+ We ran the model on a few retrieval based benchmarks (CQADupstackEnglishRetrieval, DBPedia, MSMARCO, QuoraRetrieval) and the results are [here](https://github.com/shreyansh26/Long-Context-Biencoder/tree/master/models/results/6M_results).