Upload folder using huggingface_hub
Browse files- README.md +53 -64
- config.json +1 -0
- data_collator.py +13 -0
- modeling.py +35 -0
- pytorch_model.bin +3 -0
- special_tokens_map.json +7 -0
- tokenizer.json +0 -0
- tokenizer_config.json +55 -0
- vocab.txt +0 -0
README.md
CHANGED
@@ -1,68 +1,57 @@
|
|
1 |
---
|
2 |
-
|
3 |
tags:
|
4 |
-
-
|
5 |
-
|
6 |
-
-
|
7 |
-
|
|
|
8 |
---
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
-
|
18 |
-
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
| 0.0569 | 4.0 | 84 | 0.0806 | 0.1047 | 0.2814 | 0.1261 | 0.1386 | 0.8897 |
|
58 |
-
| 0.0524 | 5.0 | 105 | 0.0801 | 0.1114 | 0.2855 | 0.1103 | 0.1033 | 0.8819 |
|
59 |
-
| 0.0487 | 6.0 | 126 | 0.0808 | 0.1117 | 0.2871 | 0.1187 | 0.0951 | 0.8806 |
|
60 |
-
| 0.0451 | 7.0 | 147 | 0.0810 | 0.1141 | 0.2911 | 0.1024 | 0.0843 | 0.8774 |
|
61 |
-
|
62 |
-
|
63 |
-
### Framework versions
|
64 |
-
|
65 |
-
- Transformers 4.45.1
|
66 |
-
- Pytorch 2.4.0
|
67 |
-
- Datasets 3.0.1
|
68 |
-
- Tokenizers 0.20.0
|
|
|
1 |
---
|
2 |
+
language: en
|
3 |
tags:
|
4 |
+
- bert
|
5 |
+
- regression
|
6 |
+
- biencoder
|
7 |
+
- similarity
|
8 |
+
pipeline_tag: text-similarity
|
9 |
---
|
10 |
|
11 |
+
# BiEncoder Regression Model
|
12 |
+
|
13 |
+
This model is a BiEncoder architecture that outputs similarity scores between text pairs.
|
14 |
+
|
15 |
+
## Model Details
|
16 |
+
- Base Model: bert-base-uncased
|
17 |
+
- Task: Regression
|
18 |
+
- Architecture: BiEncoder with cosine similarity
|
19 |
+
- Loss Function: contrastive
|
20 |
+
|
21 |
+
## Usage
|
22 |
+
|
23 |
+
```python
|
24 |
+
from transformers import AutoTokenizer, AutoModel
|
25 |
+
from modeling import BiEncoderModelRegression
|
26 |
+
|
27 |
+
# Load model components
|
28 |
+
tokenizer = AutoTokenizer.from_pretrained("minoosh/bert-reg-biencoder-contrastive")
|
29 |
+
base_model = AutoModel.from_pretrained("bert-base-uncased")
|
30 |
+
model = BiEncoderModelRegression(base_model, loss_fn="contrastive")
|
31 |
+
|
32 |
+
# Load weights
|
33 |
+
state_dict = torch.load("pytorch_model.bin")
|
34 |
+
model.load_state_dict(state_dict)
|
35 |
+
|
36 |
+
# Prepare inputs
|
37 |
+
texts1 = ["first text"]
|
38 |
+
texts2 = ["second text"]
|
39 |
+
inputs = tokenizer(
|
40 |
+
texts1, texts2,
|
41 |
+
padding=True,
|
42 |
+
truncation=True,
|
43 |
+
return_tensors="pt"
|
44 |
+
)
|
45 |
+
|
46 |
+
# Get similarity scores
|
47 |
+
outputs = model(**inputs)
|
48 |
+
similarity_scores = outputs["logits"]
|
49 |
+
```
|
50 |
+
|
51 |
+
## Metrics
|
52 |
+
The model was trained using contrastive loss and evaluated using:
|
53 |
+
- Mean Squared Error (MSE)
|
54 |
+
- Mean Absolute Error (MAE)
|
55 |
+
- Pearson Correlation
|
56 |
+
- Spearman Correlation
|
57 |
+
- Cosine Similarity
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
config.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"return_dict": true, "output_hidden_states": false, "output_attentions": false, "torchscript": false, "torch_dtype": null, "use_bfloat16": false, "tf_legacy_loss": false, "pruned_heads": {}, "tie_word_embeddings": true, "chunk_size_feed_forward": 0, "is_encoder_decoder": false, "is_decoder": false, "cross_attention_hidden_size": null, "add_cross_attention": false, "tie_encoder_decoder": false, "max_length": 20, "min_length": 0, "do_sample": false, "early_stopping": false, "num_beams": 1, "num_beam_groups": 1, "diversity_penalty": 0.0, "temperature": 1.0, "top_k": 50, "top_p": 1.0, "typical_p": 1.0, "repetition_penalty": 1.0, "length_penalty": 1.0, "no_repeat_ngram_size": 0, "encoder_no_repeat_ngram_size": 0, "bad_words_ids": null, "num_return_sequences": 1, "output_scores": false, "return_dict_in_generate": false, "forced_bos_token_id": null, "forced_eos_token_id": null, "remove_invalid_values": false, "exponential_decay_length_penalty": null, "suppress_tokens": null, "begin_suppress_tokens": null, "architectures": ["BiEncoderModelRegression"], "finetuning_task": null, "id2label": {"0": "LABEL_0", "1": "LABEL_1"}, "label2id": {"LABEL_0": 0, "LABEL_1": 1}, "tokenizer_class": null, "prefix": null, "bos_token_id": null, "pad_token_id": 0, "eos_token_id": null, "sep_token_id": null, "decoder_start_token_id": null, "task_specific_params": null, "problem_type": null, "_name_or_path": "bert-base-uncased", "transformers_version": "4.45.1", "gradient_checkpointing": false, "model_type": "bert", "vocab_size": 30522, "hidden_size": 768, "num_hidden_layers": 12, "num_attention_heads": 12, "hidden_act": "gelu", "intermediate_size": 3072, "hidden_dropout_prob": 0.1, "attention_probs_dropout_prob": 0.1, "max_position_embeddings": 512, "type_vocab_size": 2, "initializer_range": 0.02, "layer_norm_eps": 1e-12, "position_embedding_type": "absolute", "use_cache": true, "classifier_dropout": null, "loss_fn": "contrastive", "task_type": "regression", "is_regression": true}
|
data_collator.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
|
4 |
+
class BiEncoderCollator:
|
5 |
+
def __call__(self, features):
|
6 |
+
batch = {
|
7 |
+
'input_ids_text1': torch.stack([f['input_ids_text1'] for f in features]),
|
8 |
+
'attention_mask_text1': torch.stack([f['attention_mask_text1'] for f in features]),
|
9 |
+
'input_ids_text2': torch.stack([f['input_ids_text2'] for f in features]),
|
10 |
+
'attention_mask_text2': torch.stack([f['attention_mask_text2'] for f in features]),
|
11 |
+
'labels': torch.tensor([f['labels'] for f in features], dtype=torch.float)
|
12 |
+
}
|
13 |
+
return batch
|
modeling.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
from transformers import PreTrainedModel
|
4 |
+
|
5 |
+
class BiEncoderModelRegression(torch.nn.Module):
|
6 |
+
def __init__(self, base_model, config=None, loss_fn="mse"):
|
7 |
+
super().__init__()
|
8 |
+
self.base_model = base_model
|
9 |
+
self.cos = torch.nn.CosineSimilarity(dim=1)
|
10 |
+
self.loss_fn = loss_fn
|
11 |
+
self.config = config
|
12 |
+
|
13 |
+
def forward(self, input_ids_text1, attention_mask_text1, input_ids_text2, attention_mask_text2, labels=None):
|
14 |
+
outputs_text1 = self.base_model(input_ids_text1, attention_mask=attention_mask_text1)
|
15 |
+
outputs_text2 = self.base_model(input_ids_text2, attention_mask=attention_mask_text2)
|
16 |
+
|
17 |
+
cls_embedding_text1 = outputs_text1.last_hidden_state[:, 0, :]
|
18 |
+
cls_embedding_text2 = outputs_text2.last_hidden_state[:, 0, :]
|
19 |
+
|
20 |
+
cos_sim = self.cos(cls_embedding_text1, cls_embedding_text2)
|
21 |
+
|
22 |
+
loss = None
|
23 |
+
if labels is not None:
|
24 |
+
if self.loss_fn == "mse":
|
25 |
+
loss_fct = torch.nn.MSELoss()
|
26 |
+
elif self.loss_fn == "mae":
|
27 |
+
loss_fct = torch.nn.L1Loss()
|
28 |
+
elif self.loss_fn == "cosine_embedding":
|
29 |
+
loss_fct = torch.nn.CosineEmbeddingLoss()
|
30 |
+
labels_cosine = 2 * (labels > 0.5).float() - 1
|
31 |
+
return {"loss": loss_fct(cls_embedding_text1, cls_embedding_text2, labels_cosine), "logits": cos_sim}
|
32 |
+
|
33 |
+
loss = loss_fct(cos_sim, labels)
|
34 |
+
|
35 |
+
return {"loss": loss, "logits": cos_sim}
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d9fad9a28aef671e03942e04d5fa22f80e35bf98421fa4dd6a032ea2f0c7369e
|
3 |
+
size 438013734
|
special_tokens_map.json
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cls_token": "[CLS]",
|
3 |
+
"mask_token": "[MASK]",
|
4 |
+
"pad_token": "[PAD]",
|
5 |
+
"sep_token": "[SEP]",
|
6 |
+
"unk_token": "[UNK]"
|
7 |
+
}
|
tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tokenizer_config.json
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"added_tokens_decoder": {
|
3 |
+
"0": {
|
4 |
+
"content": "[PAD]",
|
5 |
+
"lstrip": false,
|
6 |
+
"normalized": false,
|
7 |
+
"rstrip": false,
|
8 |
+
"single_word": false,
|
9 |
+
"special": true
|
10 |
+
},
|
11 |
+
"100": {
|
12 |
+
"content": "[UNK]",
|
13 |
+
"lstrip": false,
|
14 |
+
"normalized": false,
|
15 |
+
"rstrip": false,
|
16 |
+
"single_word": false,
|
17 |
+
"special": true
|
18 |
+
},
|
19 |
+
"101": {
|
20 |
+
"content": "[CLS]",
|
21 |
+
"lstrip": false,
|
22 |
+
"normalized": false,
|
23 |
+
"rstrip": false,
|
24 |
+
"single_word": false,
|
25 |
+
"special": true
|
26 |
+
},
|
27 |
+
"102": {
|
28 |
+
"content": "[SEP]",
|
29 |
+
"lstrip": false,
|
30 |
+
"normalized": false,
|
31 |
+
"rstrip": false,
|
32 |
+
"single_word": false,
|
33 |
+
"special": true
|
34 |
+
},
|
35 |
+
"103": {
|
36 |
+
"content": "[MASK]",
|
37 |
+
"lstrip": false,
|
38 |
+
"normalized": false,
|
39 |
+
"rstrip": false,
|
40 |
+
"single_word": false,
|
41 |
+
"special": true
|
42 |
+
}
|
43 |
+
},
|
44 |
+
"clean_up_tokenization_spaces": false,
|
45 |
+
"cls_token": "[CLS]",
|
46 |
+
"do_lower_case": true,
|
47 |
+
"mask_token": "[MASK]",
|
48 |
+
"model_max_length": 512,
|
49 |
+
"pad_token": "[PAD]",
|
50 |
+
"sep_token": "[SEP]",
|
51 |
+
"strip_accents": null,
|
52 |
+
"tokenize_chinese_chars": true,
|
53 |
+
"tokenizer_class": "BertTokenizer",
|
54 |
+
"unk_token": "[UNK]"
|
55 |
+
}
|
vocab.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|