Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
Paper • 1908.10084 • Published • 17
How to use cafierom/smiles_embedding_gemma_FT with sentence-transformers:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("cafierom/smiles_embedding_gemma_FT")
sentences = [
"C[C@H](CCCC(C)(C)O)[C@H]1CC[C@H]2[C@@H]3CC=C4C[C@@H](O)CC[C@]4(C)[C@H]3CC[C@]12C",
"CC(C)n1c(CC[C@@H](O)C[C@@H](O)CC([O-])=O)c(c(c1C(=O)NCc1ccccn1)-c1ccccc1)-c1ccc(F)cc1",
"Cc1cc(OCc2ccccc2)cc(C)c1\\C=C\\[C@@H]1C[C@@H](O)CC(=O)O1",
"Cc1ccc(C2CC3CCC2C=C3)n1CC[C@@H]1C[C@@H](O)CC(=O)O1"
]
embeddings = model.encode(sentences)
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [4, 4]This is a sentence-transformers model finetuned from google/embeddinggemma-300m. It maps sentences & paragraphs to a 768-dimensional dense vector space and can be used for retrieval.
SentenceTransformer(
(0): Transformer({'transformer_task': 'feature-extraction', 'modality_config': {'text': {'method': 'forward', 'method_output_name': 'last_hidden_state'}}, 'module_output_name': 'token_embeddings', 'architecture': 'Gemma3TextModel'})
(1): Pooling({'embedding_dimension': 768, 'pooling_mode': 'mean', 'include_prompt': True})
(2): Dense({'in_features': 768, 'out_features': 3072, 'bias': False, 'activation_function': 'torch.nn.modules.linear.Identity', 'module_input_name': 'sentence_embedding', 'module_output_name': 'sentence_embedding'})
(3): Dense({'in_features': 3072, 'out_features': 768, 'bias': False, 'activation_function': 'torch.nn.modules.linear.Identity', 'module_input_name': 'sentence_embedding', 'module_output_name': 'sentence_embedding'})
(4): 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("cafierom/smiles_embedding_gemma_FT")
# Run inference
queries = [
'CCC(CC)(c1ccccc1)c1ccc(\\C=C\\[C@@H](O)C[C@@H](O)CC([O-])=O)c(c1)-c1ccccc1F',
]
documents = [
'Cc1ccc(C(=C(\\C=C\\[C@@H](O)C[C@@H](O)CC([O-])=O)c2nnnn2C)c2ccc(C)cc2C)c(C)c1',
'O[C@H](CCn1c(nc(c1-c1ccc(F)cc1)-c1ccc(F)cc1)C(F)(F)F)C[C@@H](O)CC([O-])=O',
'CC(C)c1c(c(c(-c2ccc(F)cc2)n1CC[C@@H](O)C[C@@H](O)CC([O-])=O)-c1ccccc1)S(=O)(=O)N(C)C',
]
query_embeddings = model.encode_query(queries)
document_embeddings = model.encode_document(documents)
print(query_embeddings.shape, document_embeddings.shape)
# [1, 768] [3, 768]
# Get the similarity scores for the embeddings
similarities = model.similarity(query_embeddings, document_embeddings)
print(similarities)
# tensor([[0.9356, 0.9019, 0.5159]])
premise, hypothesis, and label| premise | hypothesis | label | |
|---|---|---|---|
| type | string | string | int |
| modality | text | text | |
| details |
|
|
|
| premise | hypothesis | label |
|---|---|---|
CCC@HC(=O)O[C@H]1CC@H\C=C\Cc1ccccc1 |
Cc1cc(-c2ccc(Cl)cc2)c(\C=C[C@@H]2CC@@HCC(=O)O2)c(C)n1 |
2 |
CC(C)n1c(CCC@@HCC@@HCC([O-])=O)c(c(c1C(=O)Nc1ccccc1)-c1ccccc1)-c1ccc(F)cc1 |
CC(C)n1c(CCC@@HCC@@HCC([O-])=O)c(c(c1C(=O)NCc1ccccn1)-c1ccccc1)-c1ccc(F)cc1 |
0 |
CC(C)n1c(CCC@@HCC@@HCC([O-])=O)c(c(c1C(=O)N(C)C)-c1ccccc1)-c1ccc(F)cc1 |
COc1ccc2ccccc2c1-c1ccc(C(C)C)n1CC[C@@H]1CC@@HCC(=O)O1 |
2 |
SoftmaxLoss with these parameters:{
"num_labels": 3,
"concatenation_sent_rep": true,
"concatenation_sent_difference": true,
"concatenation_sent_multiplication": false
}
premise, hypothesis, and label| premise | hypothesis | label | |
|---|---|---|---|
| type | string | string | int |
| modality | text | text | |
| details |
|
|
|
| premise | hypothesis | label |
|---|---|---|
CNc1cccc(c1)-c1nc(C(C)C)n(\C=C[C@@H](O)CC@@HCC([O-])=O)c1-c1ccc(F)cc1 |
CC(C)c1c(CCC@@HCC@@HCC([O-])=O)c(cn1-c1ccccc1)-c1ccc(F)cc1 |
0 |
CC(C)c1nc(nc(-c2ccc(F)cc2)c1\C=C[C@@H]1CC@@HCC(OCC(Cl)(Cl)Cl)O1)N(C)S(C)(=O)=O |
CC(C)c1nn(-c2nc3ccccc3s2)c(c1\C=C[C@@H](O)CC@@HCC([O-])=O)-c1ccc(F)cc1 |
2 |
Cc1c(\C=C[C@H](O)CC@@HCC([O-])=O)c(cn1-c1ccccc1)-c1ccc(F)cc1 |
CC@H[C@H]1CC(=O)[C@@]2(C)C3=C(C(=O)C[C@]12C)[C@@]1(C)CCC(=O)C@@(COC(=O)CC@@(O)CC(O)=O)[C@@H]1CC3=O |
2 |
SoftmaxLoss with these parameters:{
"num_labels": 3,
"concatenation_sent_rep": true,
"concatenation_sent_difference": true,
"concatenation_sent_multiplication": false
}
warmup_steps: 10optim: adafactorweight_decay: 0.01bf16: Trueload_best_model_at_end: Truedataloader_pin_memory: Falseper_device_train_batch_size: 8num_train_epochs: 3max_steps: -1learning_rate: 5e-05lr_scheduler_type: linearlr_scheduler_kwargs: Nonewarmup_steps: 10optim: adafactoroptim_args: Noneweight_decay: 0.01adam_beta1: 0.9adam_beta2: 0.999adam_epsilon: 1e-08optim_target_modules: Nonegradient_accumulation_steps: 1average_tokens_across_devices: Truemax_grad_norm: 1.0label_smoothing_factor: 0.0bf16: Truefp16: Falsebf16_full_eval: Falsefp16_full_eval: Falsetf32: Nonegradient_checkpointing: Falsegradient_checkpointing_kwargs: Nonetorch_compile: Falsetorch_compile_backend: Nonetorch_compile_mode: Noneuse_liger_kernel: Falseliger_kernel_config: Noneuse_cache: Falseneftune_noise_alpha: Nonetorch_empty_cache_steps: Noneauto_find_batch_size: Falselog_on_each_node: Truelogging_nan_inf_filter: Trueinclude_num_input_tokens_seen: nolog_level: passivelog_level_replica: warningdisable_tqdm: Falseproject: huggingfacetrackio_space_id: Nonetrackio_bucket_id: Nonetrackio_static_space_id: Noneper_device_eval_batch_size: 8prediction_loss_only: Trueeval_on_start: Falseeval_do_concat_batches: Trueeval_use_gather_object: Falseeval_accumulation_steps: Noneinclude_for_metrics: []batch_eval_metrics: Falsesave_only_model: Falsesave_on_each_node: Falseenable_jit_checkpoint: Falsepush_to_hub: Falsehub_private_repo: Nonehub_model_id: Nonehub_strategy: every_savehub_always_push: Falsehub_revision: Noneload_best_model_at_end: Trueignore_data_skip: Falserestore_callback_states_from_checkpoint: Falsefull_determinism: Falseseed: 42data_seed: Noneuse_cpu: Falseaccelerator_config: {'split_batches': False, 'dispatch_batches': None, 'even_batches': True, 'use_seedable_sampler': True, 'non_blocking': False, 'gradient_accumulation_kwargs': None}parallelism_config: Nonedataloader_drop_last: Falsedataloader_num_workers: 0dataloader_pin_memory: Falsedataloader_persistent_workers: Falsedataloader_prefetch_factor: Noneremove_unused_columns: Truelabel_names: Nonetrain_sampling_strategy: randomlength_column_name: lengthddp_find_unused_parameters: Noneddp_bucket_cap_mb: Noneddp_broadcast_buffers: Falseddp_static_graph: Noneddp_backend: Noneddp_timeout: 1800fsdp: Nonefsdp_config: Nonedeepspeed: Nonedebug: []skip_memory_metrics: Truedo_predict: Falseresume_from_checkpoint: Nonewarmup_ratio: Nonelocal_rank: -1prompts: Nonebatch_sampler: batch_samplermulti_dataset_batch_sampler: proportionalrouter_mapping: {}learning_rate_mapping: {}| Epoch | Step | Training Loss | Validation Loss |
|---|---|---|---|
| 0.0471 | 20 | 0.9762 | - |
| 0.0941 | 40 | 0.8931 | - |
| 0.1176 | 50 | - | 0.8680 |
| 0.1412 | 60 | 0.8805 | - |
| 0.1882 | 80 | 0.8463 | - |
| 0.2353 | 100 | 0.8186 | 0.8070 |
| 0.2824 | 120 | 0.8125 | - |
| 0.3294 | 140 | 0.7831 | - |
| 0.3529 | 150 | - | 0.7607 |
| 0.3765 | 160 | 0.7789 | - |
| 0.4235 | 180 | 0.7543 | - |
| 0.4706 | 200 | 0.7119 | 0.7341 |
| 0.5176 | 220 | 0.7230 | - |
| 0.5647 | 240 | 0.7006 | - |
| 0.5882 | 250 | - | 0.6976 |
| 0.6118 | 260 | 0.6999 | - |
| 0.6588 | 280 | 0.6649 | - |
| 0.7059 | 300 | 0.6693 | 0.6542 |
| 0.7529 | 320 | 0.6768 | - |
| 0.8 | 340 | 0.6203 | - |
| 0.8235 | 350 | - | 0.6334 |
| 0.8471 | 360 | 0.6820 | - |
| 0.8941 | 380 | 0.6402 | - |
| 0.9412 | 400 | 0.6327 | 0.6568 |
| 0.9882 | 420 | 0.5882 | - |
| 1.0353 | 440 | 0.6258 | - |
| 1.0588 | 450 | - | 0.6009 |
| 1.0824 | 460 | 0.5778 | - |
| 1.1294 | 480 | 0.6016 | - |
| 1.1765 | 500 | 0.5251 | 0.5823 |
| 1.2235 | 520 | 0.5810 | - |
| 1.2706 | 540 | 0.5700 | - |
| 1.2941 | 550 | - | 0.5618 |
| 1.3176 | 560 | 0.5384 | - |
| 1.3647 | 580 | 0.6171 | - |
| 1.4118 | 600 | 0.5861 | 0.5493 |
| 1.4588 | 620 | 0.5898 | - |
| 1.5059 | 640 | 0.5139 | - |
| 1.5294 | 650 | - | 0.5250 |
| 1.5529 | 660 | 0.5235 | - |
| 1.6 | 680 | 0.4972 | - |
| 1.6471 | 700 | 0.5056 | 0.5538 |
| 1.6941 | 720 | 0.5550 | - |
| 1.7412 | 740 | 0.5275 | - |
| 1.7647 | 750 | - | 0.5268 |
| 1.7882 | 760 | 0.5224 | - |
| 1.8353 | 780 | 0.4965 | - |
| 1.8824 | 800 | 0.4768 | 0.5072 |
| 1.9294 | 820 | 0.5545 | - |
| 1.9765 | 840 | 0.5012 | - |
| 2.0 | 850 | - | 0.5041 |
| 2.0235 | 860 | 0.5343 | - |
| 2.0706 | 880 | 0.4404 | - |
| 2.1176 | 900 | 0.4679 | 0.4866 |
| 2.1647 | 920 | 0.5153 | - |
| 2.2118 | 940 | 0.4592 | - |
| 2.2353 | 950 | - | 0.4959 |
| 2.2588 | 960 | 0.4473 | - |
| 2.3059 | 980 | 0.5223 | - |
| 2.3529 | 1000 | 0.4917 | 0.4882 |
| 2.4 | 1020 | 0.4113 | - |
| 2.4471 | 1040 | 0.5266 | - |
| 2.4706 | 1050 | - | 0.4844 |
| 2.4941 | 1060 | 0.4752 | - |
| 2.5412 | 1080 | 0.4672 | - |
| 2.5882 | 1100 | 0.5019 | 0.4730 |
| 2.6353 | 1120 | 0.4282 | - |
| 2.6824 | 1140 | 0.3954 | - |
| 2.7059 | 1150 | - | 0.4660 |
| 2.7294 | 1160 | 0.4608 | - |
| 2.7765 | 1180 | 0.4770 | - |
| 2.8235 | 1200 | 0.4747 | 0.4630 |
| 2.8706 | 1220 | 0.5013 | - |
| 2.9176 | 1240 | 0.3948 | - |
| 2.9412 | 1250 | - | 0.4606 |
| 2.9647 | 1260 | 0.4259 | - |
| 3.0 | 1275 | - | 0.4595 |
@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
google/embeddinggemma-300m