readme: ---
  pipeline_tag: sentence-similarity
  lang:
    - sv
  tags:
    - sentence-transformers
    - feature-extraction
    - sentence-similarity
    - transformers
  widget:
  - source_sentence: "Mannen åt mat."
    sentences:
      - "Han förtärde en närande och nyttig måltid."
      - "Det var ett sunkigt hak med ganska gott käk."
      - "Han inmundigade middagen tillsammans med ett glas rödvin."
      - "Potatischips är jättegoda."
      - "Tryck på knappen för att få tala med kundsupporten."
    example_title: "Mat"
  - source_sentence: "Kan jag deklarera digitalt från utlandet?"
    sentences:
      - "Du som befinner dig i utlandet kan deklarera digitalt på flera olika sätt."
      - "Du som har kvarskatt att betala ska göra en inbetalning till ditt skattekonto."
      - "Efter att du har deklarerat går vi igenom uppgifterna i din deklaration och räknar ut din skatt."
      - "I din deklaration som du får från oss har vi räknat ut vad du ska betala eller få tillbaka."
      - "Tryck på knappen för att få tala med kundsupporten."
    example_title: "Skatteverket FAQ"
  - source_sentence: "Hon kunde göra bakåtvolter."
    sentences:
      - "Hon var atletisk."
      - "Hon var bra på gymnastik."
      - "Hon var inte atletisk."
      - "Hon var oförmögen att flippa baklänges."
    example_title: "Gymnastik"
  ---
  
  # KBLab/sentence-bert-swedish-cased
  
  This is a [sentence-transformers](https://www.SBERT.net) model: It maps Swedish sentences & paragraphs to a 768 dimensional dense vector space and can be used for tasks like clustering or semantic search. This model is a bilingual Swedish-English model trained according to instructions in the paper [Making Monolingual Sentence Embeddings Multilingual using Knowledge Distillation](https://arxiv.org/pdf/2004.09813.pdf) and the [documentation](https://www.sbert.net/examples/training/multilingual/README.html) accompanying its companion python package. We have used the strongest available pretrained English Bi-Encoder ([all-mpnet-base-v2](https://huggingface.co/sentence-transformers/all-mpnet-base-v2)) as a teacher model, and the pretrained Swedish [KB-BERT](https://huggingface.co/KB/bert-base-swedish-cased) as the student model. 
  
  A more detailed description of the model can be found in an article we published on the KBLab blog [here](https://kb-labb.github.io/posts/2021-08-23-a-swedish-sentence-transformer/) and for the updated model [here](https://kb-labb.github.io/posts/2023-01-16-sentence-transformer-20/). 
  
  **Update**: We have released updated versions of the model since the initial release. The original model described in the blog post is **v1.0**. The current version is **v2.0**. The newer versions are trained on longer paragraphs, and have a longer max sequence length. **v2.0** is trained with a stronger teacher model and is the current default.
  
  | Model version | Teacher Model | Max Sequence Length |
  |---------------|---------|----------|
  | v1.0          | [paraphrase-mpnet-base-v2](https://huggingface.co/sentence-transformers/paraphrase-mpnet-base-v2)  | 256   |
  | v1.1          | [paraphrase-mpnet-base-v2](https://huggingface.co/sentence-transformers/paraphrase-mpnet-base-v2)  | 384   |
  | v2.0          | [all-mpnet-base-v2](https://huggingface.co/sentence-transformers/all-mpnet-base-v2)  | 384 |
  
  <!--- Describe your model here -->
  
  ## Usage (Sentence-Transformers)
  
  Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:
  
  ```
  pip install -U sentence-transformers
  ```
  
  Then you can use the model like this:
  
  ```python
  from sentence_transformers import SentenceTransformer
  sentences = ["Det här är en exempelmening", "Varje exempel blir konverterad"]
  
  model = SentenceTransformer('KBLab/sentence-bert-swedish-cased')
  embeddings = model.encode(sentences)
  print(embeddings)
  ```
  
  ### Loading an older model version (Sentence-Transformers)
  
  Currently, the easiest way to load an older model version is to clone the model repository and load it from disk. For example, to clone the **v1.0** model:
  
  ```bash
  git clone --depth 1 --branch v1.0 https://huggingface.co/KBLab/sentence-bert-swedish-cased
  ```
  
  Then you can load the model by pointing to the local folder where you cloned the model:
  
  ```python
  from sentence_transformers import SentenceTransformer
  model = SentenceTransformer("path_to_model_folder/sentence-bert-swedish-cased")
  ```
  
  
  ## Usage (HuggingFace Transformers)
  Without [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.
  
  ```python
  from transformers import AutoTokenizer, AutoModel
  import torch
  
  
  #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