ravfogs's picture
Create README.md
8fb1779
|
raw
history blame
1.21 kB
metadata
license: cc-by-3.0
language:
  - en

A model for mapping abstract sentence descriptions to sentences that fit the descriptions. Trained on Pubmed sentences. Use load_finetuned_model to load the query and sentence encoder, and encode_batch() to encode a sentence with the model.


from transformers import AutoTokenizer, AutoModel
import torch

def load_finetuned_model():


        sentence_encoder = AutoModel.from_pretrained("ravfogs/abstract-sim-sentence-pubmed")
        query_encoder = AutoModel.from_pretrained("ravfogs/abstract-sim-query-pubmed")
        tokenizer = AutoTokenizer.from_pretrained("ravfogs/abstract-sim-sentence-pubmed")

        return tokenizer, query_encoder, sentence_encoder


def encode_batch(model, tokenizer, sentences, device):
    input_ids = tokenizer(sentences, padding=True, max_length=512, truncation=True, return_tensors="pt",
                          add_special_tokens=True).to(device)
    features = model(**input_ids)[0]
    features =  torch.sum(features[:,1:,:] * input_ids["attention_mask"][:,1:].unsqueeze(-1), dim=1) / torch.clamp(torch.sum(input_ids["attention_mask"][:,1:], dim=1, keepdims=True), min=1e-9)
    return features