### Install sentence-transformers Source: https://github.com/fremycompany/biolord-2023/blob/main/README.md Install the sentence-transformers library using pip. This is required for the simplified usage of the BioLORD-2023 model. ```bash pip install -U sentence-transformers ``` -------------------------------- ### Encode sentences with sentence-transformers Source: https://github.com/fremycompany/biolord-2023/blob/main/README.md Use the SentenceTransformer class to load the BioLORD-2023 model and encode a list of sentences into embeddings. This is the simplest way to get sentence embeddings. ```python from sentence_transformers import SentenceTransformer sentences = ["Cat scratch injury", "Cat scratch disease", "Bartonellosis"] model = SentenceTransformer('FremyCompany/BioLORD-2023') embeddings = model.encode(sentences) print(embeddings) ``` -------------------------------- ### Encode sentences with HuggingFace Transformers Source: https://github.com/fremycompany/biolord-2023/blob/main/README.md Load the BioLORD-2023 model and tokenizer using HuggingFace Transformers. Tokenize sentences, compute token embeddings, apply mean pooling, and normalize the resulting sentence embeddings. ```python # Sentences we want sentence embeddings for sentences = ["Cat scratch injury", "Cat scratch disease", "Bartonellosis"] # Load model from HuggingFace Hub tokenizer = AutoTokenizer.from_pretrained('FremyCompany/BioLORD-2023') model = AutoModel.from_pretrained('FremyCompany/BioLORD-2023') # Tokenize sentences encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt') # Compute token embeddings with torch.no_grad(): model_output = model(**encoded_input) # Perform pooling sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask']) # Normalize embeddings sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1) print("Sentence embeddings:") print(sentence_embeddings) ``` -------------------------------- ### Mean Pooling for Sentence Embeddings with HuggingFace Transformers Source: https://github.com/fremycompany/biolord-2023/blob/main/README.md A utility function for mean pooling token embeddings, considering the attention mask. This is used to compute sentence embeddings from token embeddings generated by the HuggingFace Transformers model. ```python from transformers import AutoTokenizer, AutoModel import torch import torch.nn.functional as F #Mean Pooling - Take attention mask into account for correct averaging def mean_pooling(model_output, attention_mask): token_embeddings = model_output[0] #First element of model_output contains all token embeddings input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float() return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9) ``` -------------------------------- ### Citation for BioLORD-2023 Paper Source: https://github.com/fremycompany/biolord-2023/blob/main/README.md When using the BioLORD-2023 model, please cite the accompanying paper using this LaTeX format. ```latex @article{remy-etal-2023-biolord, author = {Remy, François and Demuynck, Kris and Demeester, Thomas}, title = "{BioLORD-2023: semantic textual representations fusing large language models and clinical knowledge graph insights}", journal = {Journal of the American Medical Informatics Association}, pages = {ocae029}, year = {2024}, month = {02}, issn = {1527-974X}, doi = {10.1093/jamia/ocae029}, url = {https://doi.org/10.1093/jamia/ocae029}, eprint = {https://academic.oup.com/jamia/advance-article-pdf/doi/10.1093/jamia/ocae029/56772025/ocae029.pdf}, } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.