Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
Paper
•
1908.10084
•
Published
•
10
This is a sentence-transformers model finetuned from intfloat/multilingual-e5-base. It maps sentences & paragraphs to a 768-dimensional dense vector space and can be used for semantic textual similarity, semantic search, paraphrase mining, text classification, clustering, and more.
SentenceTransformer(
(0): Transformer({'max_seq_length': 256, 'do_lower_case': False}) with Transformer model: XLMRobertaModel
(1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False, 'pooling_mode_weightedmean_tokens': False, 'pooling_mode_lasttoken': False, 'include_prompt': True})
(2): Normalize()
)
First install the Sentence Transformers library:
pip install -U sentence-transformers
Then you can load this model and run inference.
from sentence_transformers import SentenceTransformer
# Download from the 🤗 Hub
model = SentenceTransformer("ThanhLe0125/e5-math-ebd-01")
# Run inference
sentences = [
"query: Ý nghĩa của việc tìm nghiệm của phương trình f'(x) = 0?",
"passage: # Phương pháp tìm cực trị của hàm số\n\n## Quy trình tìm cực trị\n**Bước 1:** Tìm tập xác định của hàm số\n**Bước 2:** Tính đạo hàm $f'(x)$\n**Bước 3:** Tìm nghiệm của phương trình $f'(x) = 0$ và các điểm mà $f'(x)$ không xác định\n**Bước 4:** Lập bảng xét dấu $f'(x)$\n**Bước 5:** Kết luận về cực trị\n\n## Ví dụ minh họa\nTìm cực trị của hàm số $f(x) = x^4 - 2x^2 + 1$.\n\n**Lời giải:**\nTập xác định: $D = \\mathbb{R}$\n$f'(x) = 4x^3 - 4x = 4x(x^2 - 1) = 4x(x-1)(x+1)$\n$f'(x) = 0 \\Leftrightarrow x = -1, x = 0, x = 1$\n\nBảng xét dấu:\n- $x \\in (-\\infty; -1)$: $f'(x) < 0$\n- $x \\in (-1; 0)$: $f'(x) > 0$ \n- $x \\in (0; 1)$: $f'(x) < 0$\n- $x \\in (1; +\\infty)$: $f'(x) > 0$\n\n**Kết luận:**\n- $x = -1$: cực tiểu, $f(-1) = 0$\n- $x = 0$: cực đại, $f(0) = 1$\n- $x = 1$: cực tiểu, $f(1) = 0$",
'passage: # Tổ hợp\n\n## Định nghĩa\n**Tổ hợp chập $k$ của $n$ phần tử** là cách chọn $k$ phần tử từ $n$ phần tử mà không quan tâm đến thứ tự.\n\n**Ký hiệu:** $C_n^k$ hoặc $\\binom{n}{k}$\n\n**Điều kiện:** $0 \\leq k \\leq n$\n\n## Công thức\n$$C_n^k = \\frac{A_n^k}{k!} = \\frac{n!}{k!(n-k)!}$$\n\n**Công thức Pascal:** $C_n^k = C_{n-1}^{k-1} + C_{n-1}^k$\n\n## Tính chất cơ bản\n1. $C_n^0 = C_n^n = 1$\n2. $C_n^k = C_n^{n-k}$\n3. $C_n^1 = n$\n4. $C_n^2 = \\frac{n(n-1)}{2}$\n\n## Ví dụ tính toán\n**Ví dụ 1:** Từ 10 học sinh, chọn 3 học sinh tham gia đội tuyển. Có bao nhiêu cách?\n\n**Lời giải:** $C_{10}^3 = \\frac{10!}{3! \\times 7!} = \\frac{10 \\times 9 \\times 8}{3 \\times 2 \\times 1} = 120$ cách.\n\n**Ví dụ 2:** Trong một lớp có 15 nam và 10 nữ. Chọn 5 người sao cho có đúng 3 nam và 2 nữ.\n\n**Lời giải:** \n- Chọn 3 nam từ 15 nam: $C_{15}^3 = 455$ cách\n- Chọn 2 nữ từ 10 nữ: $C_{10}^2 = 45$ cách\n- Tổng cộng: $455 \\times 45 = 20,475$ cách\n\n## Nhị thức Newton\n$$(a + b)^n = \\sum_{k=0}^{n} C_n^k a^{n-k} b^k$$\n\n**Ví dụ:** $(x + 2)^4 = C_4^0 x^4 + C_4^1 x^3 \\cdot 2 + C_4^2 x^2 \\cdot 2^2 + C_4^3 x \\cdot 2^3 + C_4^4 \\cdot 2^4$\n$= x^4 + 8x^3 + 24x^2 + 32x + 16$\n\n## Phân biệt chỉnh hợp và tổ hợp\n- **Chỉnh hợp:** Quan tâm thứ tự (Ví dụ: Chọn và xếp hạng)\n- **Tổ hợp:** Không quan tâm thứ tự (Ví dụ: Chọn thành viên nhóm)\n\n**Mối quan hệ:** $A_n^k = k! \\times C_n^k$',
]
embeddings = model.encode(sentences)
print(embeddings.shape)
# [3, 768]
# Get the similarity scores for the embeddings
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [3, 3]
ranking_evaluatorBinaryClassificationEvaluator| Metric | Value |
|---|---|
| cosine_accuracy | 0.95 |
| cosine_accuracy_threshold | 0.3498 |
| cosine_f1 | 0.9231 |
| cosine_f1_threshold | 0.2493 |
| cosine_precision | 0.8684 |
| cosine_recall | 0.9851 |
| cosine_ap | 0.9737 |
| cosine_mcc | 0.8848 |
sentence_0, sentence_1, and sentence_2| sentence_0 | sentence_1 | sentence_2 | |
|---|---|---|---|
| type | string | string | string |
| details |
|
|
|
| sentence_0 | sentence_1 | sentence_2 |
|---|---|---|
query: Giải thích ý nghĩa của ký hiệu C(n, k) và A(n, k) trong toán tổ hợp. |
passage: # Bài toán tổ hợp - chỉnh hợp thực tế |
passage: # Tiệm cận đứng của đồ thị hàm số |
query: Trong một hộp có 7 viên bi khác nhau. Lấy ngẫu nhiên ra 2 viên. Hỏi có bao nhiêu cách lấy? |
passage: # Bài toán tổ hợp - chỉnh hợp thực tế |
passage: # Tính toán phương sai và độ lệch chuẩn |
query: So sánh sự khác biệt giữa khảo sát hàm phân thức bậc nhất trên bậc nhất với khảo sát hàm số bậc hai. |
passage: # Khảo sát hàm phân thức bậc nhất trên bậc nhất: $f(x) = \frac{ax + b}{cx + d}$ |
passage: # Giá trị lớn nhất và nhỏ nhất của hàm số |
MultipleNegativesRankingLoss with these parameters:{
"scale": 20.0,
"similarity_fct": "cos_sim"
}
eval_strategy: stepsper_device_train_batch_size: 4per_device_eval_batch_size: 4num_train_epochs: 20multi_dataset_batch_sampler: round_robinoverwrite_output_dir: Falsedo_predict: Falseeval_strategy: stepsprediction_loss_only: Trueper_device_train_batch_size: 4per_device_eval_batch_size: 4per_gpu_train_batch_size: Noneper_gpu_eval_batch_size: Nonegradient_accumulation_steps: 1eval_accumulation_steps: Nonetorch_empty_cache_steps: Nonelearning_rate: 5e-05weight_decay: 0.0adam_beta1: 0.9adam_beta2: 0.999adam_epsilon: 1e-08max_grad_norm: 1num_train_epochs: 20max_steps: -1lr_scheduler_type: linearlr_scheduler_kwargs: {}warmup_ratio: 0.0warmup_steps: 0log_level: passivelog_level_replica: warninglog_on_each_node: Truelogging_nan_inf_filter: Truesave_safetensors: Truesave_on_each_node: Falsesave_only_model: Falserestore_callback_states_from_checkpoint: Falseno_cuda: Falseuse_cpu: Falseuse_mps_device: Falseseed: 42data_seed: Nonejit_mode_eval: Falseuse_ipex: Falsebf16: Falsefp16: Falsefp16_opt_level: O1half_precision_backend: autobf16_full_eval: Falsefp16_full_eval: Falsetf32: Nonelocal_rank: 0ddp_backend: Nonetpu_num_cores: Nonetpu_metrics_debug: Falsedebug: []dataloader_drop_last: Falsedataloader_num_workers: 0dataloader_prefetch_factor: Nonepast_index: -1disable_tqdm: Falseremove_unused_columns: Truelabel_names: Noneload_best_model_at_end: Falseignore_data_skip: Falsefsdp: []fsdp_min_num_params: 0fsdp_config: {'min_num_params': 0, 'xla': False, 'xla_fsdp_v2': False, 'xla_fsdp_grad_ckpt': False}tp_size: 0fsdp_transformer_layer_cls_to_wrap: Noneaccelerator_config: {'split_batches': False, 'dispatch_batches': None, 'even_batches': True, 'use_seedable_sampler': True, 'non_blocking': False, 'gradient_accumulation_kwargs': None}deepspeed: Nonelabel_smoothing_factor: 0.0optim: adamw_torchoptim_args: Noneadafactor: Falsegroup_by_length: Falselength_column_name: lengthddp_find_unused_parameters: Noneddp_bucket_cap_mb: Noneddp_broadcast_buffers: Falsedataloader_pin_memory: Truedataloader_persistent_workers: Falseskip_memory_metrics: Trueuse_legacy_prediction_loop: Falsepush_to_hub: Falseresume_from_checkpoint: Nonehub_model_id: Nonehub_strategy: every_savehub_private_repo: Nonehub_always_push: Falsegradient_checkpointing: Falsegradient_checkpointing_kwargs: Noneinclude_inputs_for_metrics: Falseinclude_for_metrics: []eval_do_concat_batches: Truefp16_backend: autopush_to_hub_model_id: Nonepush_to_hub_organization: Nonemp_parameters: auto_find_batch_size: Falsefull_determinism: Falsetorchdynamo: Noneray_scope: lastddp_timeout: 1800torch_compile: Falsetorch_compile_backend: Nonetorch_compile_mode: Noneinclude_tokens_per_second: Falseinclude_num_input_tokens_seen: Falseneftune_noise_alpha: Noneoptim_target_modules: Nonebatch_eval_metrics: Falseeval_on_start: Falseuse_liger_kernel: Falseeval_use_gather_object: Falseaverage_tokens_across_devices: Falseprompts: Nonebatch_sampler: batch_samplermulti_dataset_batch_sampler: round_robin| Epoch | Step | Training Loss | ranking_evaluator_cosine_ap |
|---|---|---|---|
| 0.8299 | 200 | - | 0.9308 |
| 1.0 | 241 | - | 0.9113 |
| 1.6598 | 400 | - | 0.9397 |
| 2.0 | 482 | - | 0.9254 |
| 2.0747 | 500 | 0.7221 | - |
| 2.4896 | 600 | - | 0.9335 |
| 3.0 | 723 | - | 0.9318 |
| 3.3195 | 800 | - | 0.9493 |
| 4.0 | 964 | - | 0.9419 |
| 4.1494 | 1000 | 0.4382 | 0.9440 |
| 4.9793 | 1200 | - | 0.9523 |
| 5.0 | 1205 | - | 0.9505 |
| 5.8091 | 1400 | - | 0.9629 |
| 6.0 | 1446 | - | 0.9639 |
| 6.2241 | 1500 | 0.402 | - |
| 6.6390 | 1600 | - | 0.9537 |
| 7.0 | 1687 | - | 0.9532 |
| 7.4689 | 1800 | - | 0.9737 |
@inproceedings{reimers-2019-sentence-bert,
title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
author = "Reimers, Nils and Gurevych, Iryna",
booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
month = "11",
year = "2019",
publisher = "Association for Computational Linguistics",
url = "https://arxiv.org/abs/1908.10084",
}
@misc{henderson2017efficient,
title={Efficient Natural Language Response Suggestion for Smart Reply},
author={Matthew Henderson and Rami Al-Rfou and Brian Strope and Yun-hsuan Sung and Laszlo Lukacs and Ruiqi Guo and Sanjiv Kumar and Balint Miklos and Ray Kurzweil},
year={2017},
eprint={1705.00652},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
Base model
intfloat/multilingual-e5-base