S Pub Med Bert Med Qu A D

TimKond

Introduction

S-PubMedBert-MedQuAD is a sentence-transformers model designed to map sentences and paragraphs to a 768-dimensional dense vector space. This model is suitable for tasks such as clustering and semantic search.

Architecture

The model is based on a SentenceTransformer architecture, which includes:

  • A Transformer layer with a maximum sequence length of 512 and without lowercasing.
  • A Pooling layer that uses mean tokens for pooling, producing a word embedding dimension of 768.

Training

The training of S-PubMedBert-MedQuAD involved:

  • A DataLoader with 82,590 samples, using a batch size of 2 and shuffling.
  • A SoftmaxLoss function for training with two labels and a sentence embedding dimension of 768.
  • Training parameters included one epoch, a learning rate of 2e-5, AdamW optimizer, no bias correction, a warmup scheduler, and a weight decay of 0.01.

Guide: Running Locally

  1. Install Dependencies: Ensure you have sentence-transformers installed.

    pip install -U sentence-transformers
    
  2. Usage with Sentence-Transformers:

    from sentence_transformers import SentenceTransformer
    sentences = ["This is an example sentence", "Each sentence is converted"]
    model = SentenceTransformer('TimKond/S-PubMedBert-MedQuAD')
    embeddings = model.encode(sentences)
    print(embeddings)
    
  3. Usage without Sentence-Transformers:

    from transformers import AutoTokenizer, AutoModel
    import torch
    
    def mean_pooling(model_output, attention_mask):
        token_embeddings = model_output[0]
        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)
    
    sentences = ['This is an example sentence', 'Each sentence is converted']
    tokenizer = AutoTokenizer.from_pretrained('TimKond/S-PubMedBert-MedQuAD')
    model = AutoModel.from_pretrained('TimKond/S-PubMedBert-MedQuAD')
    encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
    
    with torch.no_grad():
        model_output = model(**encoded_input)
    
    sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
    print("Sentence embeddings:")
    print(sentence_embeddings)
    

For better performance, consider using cloud GPUs from providers like AWS, GCP, or Azure.

License

The S-PubMedBert-MedQuAD model is released under the MIT License.

More Related APIs in Sentence Similarity