Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
Paper โข 1908.10084 โข Published โข 19
How to use rabit223/vi_MiniLM_v1 with sentence-transformers:
from sentence_transformers import CrossEncoder
model = CrossEncoder("rabit223/vi_MiniLM_v1")
query = "Which planet is known as the Red Planet?"
passages = [
"Venus is often called Earth's twin because of its similar size and proximity.",
"Mars, known for its reddish appearance, is often referred to as the Red Planet.",
"Jupiter, the largest planet in our solar system, has a prominent red spot.",
"Saturn, famous for its rings, is sometimes mistaken for the Red Planet."
]
scores = model.predict([(query, passage) for passage in passages])
print(scores)This is a Cross Encoder model finetuned from cross-encoder/ms-marco-MiniLM-L6-v2 using the sentence-transformers library. It computes scores for pairs of texts, which can be used for text reranking and semantic search.
First install the Sentence Transformers library:
pip install -U sentence-transformers
Then you can load this model and run inference.
from sentence_transformers import CrossEncoder
# Download from the ๐ค Hub
model = CrossEncoder("cross_encoder_model_id")
# Get scores for pairs of texts
pairs = [
['vรฌ sao repository pattern ฤรดi khi lร m code khรณ hiแปu hฦกn', 'Repository pattern cรณ thแป lร m code khรณ hiแปu khi nรณ che giแบฅu quรก nhiแปu ฤแบทc tรญnh truy vแบฅn thแบญt sแปฑ cแปงa data store, khiแบฟn ngฦฐแปi ฤแปc khรดng biแบฟt ฤรขu lร thao tรกc ฤฦกn giแบฃn, ฤรขu lร query ฤแบฏt tiแปn. Nแบฟu thรชm mแปt lแปp repository chแป ฤแป bแปc CRUD mรกy mรณc, bแบกn cรณ thรชm abstraction nhฦฐng khรดng thรชm รฝ nghฤฉa domain. Nรณ hแปฏu รญch khi tแบกo ranh giแปi rรต vร test tแปt hฦกn, nhฦฐng phแบฃn tรกc dแปฅng nแบฟu mแปi logic query phแปฉc tแบกp lแบกi bแป nhแปi vร o interface quรก chung chung.'],
['vรฌ sao repository pattern ฤรดi khi lร m code khรณ hiแปu hฦกn', 'Repository pattern lร m code khรณ hiแปu chแปง yแบฟu vรฌ nรณ buแปc mแปi truy vแบฅn phแบฃi chแบกy ฤแปng bแป, kแป cแบฃ khi database hแป trแปฃ async. ฤiแปu nร y khiแบฟn tแบงng data access luรดn trแป thร nh bottleneck vร lแบญp trรฌnh viรชn phแบฃi lแบงn theo nhiแปu callback.'],
['vรฌ sao repository pattern ฤรดi khi lร m code khรณ hiแปu hฦกn', 'Nguyรชn nhรขn lร repository luรดn nhรขn ฤรดi mรด hรฌnh dแปฏ liแปu: mแปt bแบฃn trong database vร mแปt bแบฃn trong bแป nhแป JVM hay Python. Vรฌ dแปฏ liแปu bแป copy hai lแบงn, lแบญp trรฌnh viรชn khรณ theo dรตi object nร o lร bแบฃn chuแบฉn.'],
['vรฌ sao repository pattern ฤรดi khi lร m code khรณ hiแปu hฦกn', 'Repository thฦฐแปng ฤแบกi diแปn cho tแบญp hแปฃp thao tรกc ฤแปc ghi ฤแปi vแปi entity hoแบทc aggregate. Nรณ giรบp tรกch business logic khแปi chi tiแบฟt persistence nhฦฐ ORM hay SQL cแปฅ thแป.'],
['vรฌ sao repository pattern ฤรดi khi lร m code khรณ hiแปu hฦกn', 'Abstraction tแปt cแบงn cรขn bแบฑng giแปฏa che giแบฅu chi tiแบฟt vร ฤแป lแป cรกc capability quan trแปng. Nแบฟu abstraction quรก chung, ngฦฐแปi dรนng khรณ tแบญn dแปฅng ฤฦฐแปฃc ฤแบทc ฤiแปm hแปฏu รญch cแปงa cรดng cแปฅ bรชn dฦฐแปi.'],
]
scores = model.predict(pairs)
print(scores.shape)
# (5,)
# Or rank different texts based on similarity to a single text
ranks = model.rank(
'vรฌ sao repository pattern ฤรดi khi lร m code khรณ hiแปu hฦกn',
[
'Repository pattern cรณ thแป lร m code khรณ hiแปu khi nรณ che giแบฅu quรก nhiแปu ฤแบทc tรญnh truy vแบฅn thแบญt sแปฑ cแปงa data store, khiแบฟn ngฦฐแปi ฤแปc khรดng biแบฟt ฤรขu lร thao tรกc ฤฦกn giแบฃn, ฤรขu lร query ฤแบฏt tiแปn. Nแบฟu thรชm mแปt lแปp repository chแป ฤแป bแปc CRUD mรกy mรณc, bแบกn cรณ thรชm abstraction nhฦฐng khรดng thรชm รฝ nghฤฉa domain. Nรณ hแปฏu รญch khi tแบกo ranh giแปi rรต vร test tแปt hฦกn, nhฦฐng phแบฃn tรกc dแปฅng nแบฟu mแปi logic query phแปฉc tแบกp lแบกi bแป nhแปi vร o interface quรก chung chung.',
'Repository pattern lร m code khรณ hiแปu chแปง yแบฟu vรฌ nรณ buแปc mแปi truy vแบฅn phแบฃi chแบกy ฤแปng bแป, kแป cแบฃ khi database hแป trแปฃ async. ฤiแปu nร y khiแบฟn tแบงng data access luรดn trแป thร nh bottleneck vร lแบญp trรฌnh viรชn phแบฃi lแบงn theo nhiแปu callback.',
'Nguyรชn nhรขn lร repository luรดn nhรขn ฤรดi mรด hรฌnh dแปฏ liแปu: mแปt bแบฃn trong database vร mแปt bแบฃn trong bแป nhแป JVM hay Python. Vรฌ dแปฏ liแปu bแป copy hai lแบงn, lแบญp trรฌnh viรชn khรณ theo dรตi object nร o lร bแบฃn chuแบฉn.',
'Repository thฦฐแปng ฤแบกi diแปn cho tแบญp hแปฃp thao tรกc ฤแปc ghi ฤแปi vแปi entity hoแบทc aggregate. Nรณ giรบp tรกch business logic khแปi chi tiแบฟt persistence nhฦฐ ORM hay SQL cแปฅ thแป.',
'Abstraction tแปt cแบงn cรขn bแบฑng giแปฏa che giแบฅu chi tiแบฟt vร ฤแป lแป cรกc capability quan trแปng. Nแบฟu abstraction quรก chung, ngฦฐแปi dรนng khรณ tแบญn dแปฅng ฤฦฐแปฃc ฤแบทc ฤiแปm hแปฏu รญch cแปงa cรดng cแปฅ bรชn dฦฐแปi.',
]
)
# [{'corpus_id': ..., 'score': ...}, {'corpus_id': ..., 'score': ...}, ...]
query, passage, and label| query | passage | label | |
|---|---|---|---|
| type | string | string | float |
| details |
|
|
|
| query | passage | label |
|---|---|---|
tแบกi sao Python async chแบกy nhiแปu task nhฦฐng chฦฐฦกng trรฌnh vแบซn chแบญm |
Async trong Python chแป giรบp แบฉn thแปi gian chแป I/O, khรดng tแปฑ lร m nhanh cรกc ฤoแบกn CPU-bound. Nแบฟu task cแปงa bแบกn vแบซn chแปฉa xแปญ lรฝ nแบทng nhฦฐ parse lแปn, mรฃ hรณa hoแบทc vรฒng lแบทp tรญnh toรกn, event loop sแบฝ bแป chiแบฟm vร cรกc coroutine khรกc khรดng ฤฦฐแปฃc xen kแบฝ hแปฃp lรฝ. Cรกch xแปญ lรฝ thฦฐแปng lร tรกch phแบงn CPU-bound sang process pool hoแบทc tแปi ฦฐu thuแบญt toรกn, cรฒn async chแป nรชn dรนng ฤแป phแปi hแปฃp I/O concurrency. |
1.0 |
tแบกi sao Python async chแบกy nhiแปu task nhฦฐng chฦฐฦกng trรฌnh vแบซn chแบญm |
Python async thฦฐแปng chแบญm khi tแบกo quรก nhiแปu coroutine, vรฌ mแปi coroutine tแบกo ra mแปt thread riรชng nรชn hแป ฤiแปu hร nh phแบฃi chuyแปn ngแปฏ cแบฃnh liรชn tแปฅc. Khi giแบฃm sแป coroutine, hiแปu nฤng sแบฝ gแบงn nhฦฐ luรดn tฤng vรฌ chi phรญ thread scheduling biแบฟn mแบฅt. |
0.0 |
tแบกi sao Python async chแบกy nhiแปu task nhฦฐng chฦฐฦกng trรฌnh vแบซn chแบญm |
Nguyรชn nhรขn chรญnh lร await lร m task chแบกy tuแบงn tแปฑ hฦกn so vแปi code ฤแปng bแป, vรฌ mแปi lแบงn await sแบฝ chแป task trฦฐแปc hoร n thร nh rแปi mแปi chuyแปn sang task sau. Do ฤรณ trong ฤa sแป trฦฐแปng hแปฃp, bแป await vร gแปi hร m trแปฑc tiแบฟp sแบฝ giรบp async nhanh hฦกn. |
0.0 |
BinaryCrossEntropyLoss with these parameters:{
"activation_fn": "torch.nn.modules.linear.Identity",
"pos_weight": 4.186915874481201
}
query, passage, and label| query | passage | label | |
|---|---|---|---|
| type | string | string | float |
| details |
|
|
|
| query | passage | label |
|---|---|---|
vรฌ sao repository pattern ฤรดi khi lร m code khรณ hiแปu hฦกn |
Repository pattern cรณ thแป lร m code khรณ hiแปu khi nรณ che giแบฅu quรก nhiแปu ฤแบทc tรญnh truy vแบฅn thแบญt sแปฑ cแปงa data store, khiแบฟn ngฦฐแปi ฤแปc khรดng biแบฟt ฤรขu lร thao tรกc ฤฦกn giแบฃn, ฤรขu lร query ฤแบฏt tiแปn. Nแบฟu thรชm mแปt lแปp repository chแป ฤแป bแปc CRUD mรกy mรณc, bแบกn cรณ thรชm abstraction nhฦฐng khรดng thรชm รฝ nghฤฉa domain. Nรณ hแปฏu รญch khi tแบกo ranh giแปi rรต vร test tแปt hฦกn, nhฦฐng phแบฃn tรกc dแปฅng nแบฟu mแปi logic query phแปฉc tแบกp lแบกi bแป nhแปi vร o interface quรก chung chung. |
1.0 |
vรฌ sao repository pattern ฤรดi khi lร m code khรณ hiแปu hฦกn |
Repository pattern lร m code khรณ hiแปu chแปง yแบฟu vรฌ nรณ buแปc mแปi truy vแบฅn phแบฃi chแบกy ฤแปng bแป, kแป cแบฃ khi database hแป trแปฃ async. ฤiแปu nร y khiแบฟn tแบงng data access luรดn trแป thร nh bottleneck vร lแบญp trรฌnh viรชn phแบฃi lแบงn theo nhiแปu callback. |
0.0 |
vรฌ sao repository pattern ฤรดi khi lร m code khรณ hiแปu hฦกn |
Nguyรชn nhรขn lร repository luรดn nhรขn ฤรดi mรด hรฌnh dแปฏ liแปu: mแปt bแบฃn trong database vร mแปt bแบฃn trong bแป nhแป JVM hay Python. Vรฌ dแปฏ liแปu bแป copy hai lแบงn, lแบญp trรฌnh viรชn khรณ theo dรตi object nร o lร bแบฃn chuแบฉn. |
0.0 |
BinaryCrossEntropyLoss with these parameters:{
"activation_fn": "torch.nn.modules.linear.Identity",
"pos_weight": 4.186915874481201
}
eval_strategy: stepsper_device_eval_batch_size: 32learning_rate: 1e-05weight_decay: 0.01num_train_epochs: 1warmup_steps: 0.1bf16: Truedataloader_num_workers: 2remove_unused_columns: Falseload_best_model_at_end: Truedo_predict: Falseeval_strategy: stepsprediction_loss_only: Trueper_device_train_batch_size: 8per_device_eval_batch_size: 32gradient_accumulation_steps: 1eval_accumulation_steps: Nonetorch_empty_cache_steps: Nonelearning_rate: 1e-05weight_decay: 0.01adam_beta1: 0.9adam_beta2: 0.999adam_epsilon: 1e-08max_grad_norm: 1.0num_train_epochs: 1max_steps: -1lr_scheduler_type: linearlr_scheduler_kwargs: Nonewarmup_ratio: Nonewarmup_steps: 0.1log_level: passivelog_level_replica: warninglog_on_each_node: Truelogging_nan_inf_filter: Trueenable_jit_checkpoint: Falsesave_on_each_node: Falsesave_only_model: Falserestore_callback_states_from_checkpoint: Falseuse_cpu: Falseseed: 42data_seed: Nonebf16: Truefp16: Falsebf16_full_eval: Falsefp16_full_eval: Falsetf32: Nonelocal_rank: -1ddp_backend: Nonedebug: []dataloader_drop_last: Falsedataloader_num_workers: 2dataloader_prefetch_factor: Nonedisable_tqdm: Falseremove_unused_columns: Falselabel_names: Noneload_best_model_at_end: Trueignore_data_skip: Falsefsdp: []fsdp_config: {'min_num_params': 0, 'xla': False, 'xla_fsdp_v2': False, 'xla_fsdp_grad_ckpt': False}accelerator_config: {'split_batches': False, 'dispatch_batches': None, 'even_batches': True, 'use_seedable_sampler': True, 'non_blocking': False, 'gradient_accumulation_kwargs': None}parallelism_config: Nonedeepspeed: Nonelabel_smoothing_factor: 0.0optim: adamw_torch_fusedoptim_args: Nonegroup_by_length: Falselength_column_name: lengthproject: huggingfacetrackio_space_id: trackioddp_find_unused_parameters: Noneddp_bucket_cap_mb: Noneddp_broadcast_buffers: Falsedataloader_pin_memory: Truedataloader_persistent_workers: Falseskip_memory_metrics: Truepush_to_hub: Falseresume_from_checkpoint: Nonehub_model_id: Nonehub_strategy: every_savehub_private_repo: Nonehub_always_push: Falsehub_revision: Nonegradient_checkpointing: Falsegradient_checkpointing_kwargs: Noneinclude_for_metrics: []eval_do_concat_batches: Trueauto_find_batch_size: Falsefull_determinism: Falseddp_timeout: 1800torch_compile: Falsetorch_compile_backend: Nonetorch_compile_mode: Noneinclude_num_input_tokens_seen: noneftune_noise_alpha: Noneoptim_target_modules: Nonebatch_eval_metrics: Falseeval_on_start: Falseuse_liger_kernel: Falseliger_kernel_config: Noneeval_use_gather_object: Falseaverage_tokens_across_devices: Trueuse_cache: Falseprompts: Nonebatch_sampler: batch_samplermulti_dataset_batch_sampler: proportionalrouter_mapping: {}learning_rate_mapping: {}| Epoch | Step | Training Loss | Validation Loss |
|---|---|---|---|
| 0.0011 | 1 | 1.9171 | - |
| 0.0534 | 50 | 1.5105 | - |
| 0.1067 | 100 | 1.2100 | - |
| 0.1601 | 150 | 0.7601 | - |
| 0.2134 | 200 | 0.4333 | 0.3964 |
| 0.2668 | 250 | 0.4411 | - |
| 0.3202 | 300 | 0.3237 | - |
| 0.3735 | 350 | 0.3547 | - |
| 0.4269 | 400 | 0.4869 | 0.2572 |
| 0.4803 | 450 | 0.4513 | - |
| 0.5336 | 500 | 0.6128 | - |
| 0.5870 | 550 | 0.6998 | - |
| 0.6403 | 600 | 0.2516 | 0.1479 |
| 0.6937 | 650 | 0.6151 | - |
| 0.7471 | 700 | 0.1841 | - |
| 0.8004 | 750 | 0.0197 | - |
| 0.8538 | 800 | 0.2113 | 0.1038 |
| 0.9072 | 850 | 0.2645 | - |
| 0.9605 | 900 | 0.2026 | - |
| 1.0 | 937 | - | 0.1042 |
@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",
}
Base model
microsoft/MiniLM-L12-H384-uncased