KennethTM/MiniLM-L6-danish-encoder
1680
1---2pipeline_tag: sentence-similarity3tags:4- sentence-transformers5- feature-extraction6- sentence-similarity7license: mit8datasets:9- squad10- eli511- sentence-transformers/embedding-training-data12language:13- da14library_name: sentence-transformers15---16 17*New version available, trained on more data and otherwise identical [KennethTM/MiniLM-L6-danish-encoder-v2](https://huggingface.co/KennethTM/MiniLM-L6-danish-encoder-v2)*18 19# MiniLM-L6-danish-encoder 20 21This is a lightweight (~22 M parameters) [sentence-transformers](https://www.SBERT.net) model for Danish NLP: It maps sentences & paragraphs to a 384-dimensional dense vector space and can be used for tasks like clustering or semantic search. 22 23The maximum sequence length is 512 tokens.24 25The model was not pre-trained from scratch but adapted from the English version of [sentence-transformers/all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2) with a [Danish tokenizer](https://huggingface.co/KennethTM/bert-base-uncased-danish).26 27Trained on ELI5 and SQUAD data machine translated from English to Danish.28 29# Usage (Sentence-Transformers)30 31Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:32 33```34pip install -U sentence-transformers35```36Then you can use the model like this:37 38```python39from sentence_transformers import SentenceTransformer40sentences = ["Kører der cykler på vejen?", "En panda løber på vejen.", "En mand kører hurtigt forbi på cykel."]41 42model = SentenceTransformer('KennethTM/MiniLM-L6-danish-encoder')43embeddings = model.encode(sentences)44print(embeddings)45```46# Usage (HuggingFace Transformers)47Without [sentence-transformers](https://www.SBERT.net), you can use the model like this: First, you pass your input through the transformer model, then you have to apply the right pooling-operation on-top of the contextualized word embeddings.48 49```python50from transformers import AutoTokenizer, AutoModel51import torch52import torch.nn.functional as F53 54#Mean Pooling - Take attention mask into account for correct averaging55def mean_pooling(model_output, attention_mask):56 token_embeddings = model_output[0] #First element of model_output contains all token embeddings57 input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()58 return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)59 60# Sentences we want sentence embeddings for61sentences = ["Kører der cykler på vejen?", "En panda løber på vejen.", "En mand kører hurtigt forbi på cykel."]62 63# Load model from HuggingFace Hub64tokenizer = AutoTokenizer.from_pretrained('KennethTM/MiniLM-L6-danish-encoder')65model = AutoModel.from_pretrained('KennethTM/MiniLM-L6-danish-encoder')66 67# Tokenize sentences68encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')69 70# Compute token embeddings71with torch.no_grad():72 model_output = model(**encoded_input)73 74# Perform pooling75sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])76 77# Normalize embeddings78sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)79 80print("Sentence embeddings:")81print(sentence_embeddings)82```