File size: 6,409 Bytes
c93c7e4
 
 
ecd1b60
c93c7e4
 
 
 
ecd1b60
 
 
 
 
 
 
c93c7e4
 
ecd1b60
c93c7e4
ecd1b60
c93c7e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecd1b60
c93c7e4
 
 
 
 
 
 
 
 
 
 
 
 
ecd1b60
c93c7e4
 
ecd1b60
c93c7e4
ecd1b60
c93c7e4
ecd1b60
 
 
c93c7e4
ecd1b60
c93c7e4
ecd1b60
 
 
 
 
 
 
 
 
c93c7e4
 
ecd1b60
 
 
cb576c7
 
 
 
 
ecd1b60
 
 
 
cb576c7
 
 
 
 
 
 
 
 
c93c7e4
 
 
 
ecd1b60
c93c7e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
---
library_name: model2vec
license: mit
model_name: model2vecdk
tags:
- embeddings
- static-embeddings
- sentence-transformers
base_model:
  - jealk/TTC-L2V-supervised-2
language:
  - da
datasets:
  - DDSC/nordic-embedding-training-data
repo_url: https://github.com/andersborges/dkmodel2vec
---

# dkmodel2vec Model Card

This [Model2Vec](https://github.com/MinishLab/model2vec) model is a distilled version of a [LLM2Vec](https://github.com/McGill-NLP/llm2vec) model. It uses static embeddings, allowing text embeddings to be computed orders of magnitude faster on both GPU and CPU. It is designed for applications where computational resources are limited or where real-time performance is critical. Model2Vec models are the smallest, fastest, and most performant static embedders available. The distilled models are up to 50 times smaller and 500 times faster than traditional Sentence Transformers.


## Installation

Install model2vec using pip:
```
pip install model2vec
```

## Usage

### Using Model2Vec

The [Model2Vec library](https://github.com/MinishLab/model2vec) is the fastest and most lightweight way to run Model2Vec models.

Load this model using the `from_pretrained` method:
```python
from model2vec import StaticModel

# Load a pretrained Model2Vec model
model = StaticModel.from_pretrained("andersborges/model2vecdk")

# Compute text embeddings
embeddings = model.encode(["Jeg elsker kage"])
```

### Using Sentence Transformers

You can also use the [Sentence Transformers library](https://github.com/UKPLab/sentence-transformers) to load and use the model:

```python
from sentence_transformers import SentenceTransformer

# Load a pretrained Sentence Transformer model
model = SentenceTransformer("andersborges/model2vecdk")

# Compute text embeddings
embeddings = model.encode(["Jeg elsker kage"])
```

## How it works

Model2vec creates a small, fast, and powerful model that outperforms other static embedding models by a large margin on all tasks we could find, while being much faster to create than traditional static embedding models such as GloVe. Best of all, you don't need any data to distill a model using Model2Vec.

It works by passing a vocabulary through a sentence transformer model, then reducing the dimensionality of the resulting embeddings using PCA, and finally weighting the embeddings using [SIF weighting](https://openreview.net/pdf?id=SyK00v5xx). During inference, we simply take the mean of all token embeddings occurring in a sentence.

## Training

See [repo](https://github.com/andersborges/dkmodel2vec). The model was trained with the following commands: 

```bash 
# distill model 
python scripts/hyperparams.py --output-dim 256 --sif-coefficient 0.0005 --strip-upper-case --strip-exotic --focus-pca --normalize-embeddings --vocab-size 150000

# dump features
python scripts/featurize.py --max-means 100000 --max-length 800

#fine tune
python scripts/finetune.py --model2vec-model-name scripts/models/dk-llm2vec-model2vec-dim256_sif0.0005_strip_upper_case_strip_exotic_focus_pca_normalize_embeddings --data-path features/features_100000_max_length_800 --lr 0.0001
```

## Evaluation
The model was evaluated on the 10% of unseen data from the DDSC/nordic-embedding-training-data which contains examples of triplets containing a query, a positive (relevant) document and a negative (not relevant) document. The model achieved the following performance: 

| Model                          | Accuracy  |
| ------------------------------ | --------- |
| **model2vecdk**                | 0.867 |
| BM25                           | 0.882     |
| multilingual-e5-large-instruct | 0.963     |


The model was also evaluated using the [Scandinavian Embedding Benchmark](https://kennethenevoldsen.github.io/scandinavian-embedding-benchmark/) and achieved the following performance: 

| Rank | Model | Average Score | Average Rank | Angry Tweets | Bornholm Parallel | DKHate | Da Political Comments | DanFEVER | LCC | Language Identification | Massive Intent | Massive Scenario | ScaLA | TV2Nord Retrieval | Twitterhjerne |
|------|-------|---------------|--------------|--------------|-------------------|--------|----------------------|----------|-----|------------------------|----------------|------------------|-------|-------------------|---------------|
| 1 | TTC-L2V-supervised-2 | 0.68 | 4.75 | 67.09 | 54.59 | 69.00 | 45.84 | 38.31 | 73.67 | 88.61 | 74.80 | 78.35 | 53.04 | 92.79 | 85.02 |
| 2 | multilingual-e5-large-instruct | 0.66 | 7.75 | 64.57 | 55.02 | 67.14 | 45.33 | 39.52 | 70.60 | 82.48 | 71.89 | 77.51 | 50.18 | 93.69 | 77.23 |
| 3 | text-embedding-3-large | 0.64 | 8.92 | 57.80 | 43.34 | 70.21 | 43.41 | 39.61 | 58.07 | 79.74 | 69.27 | 75.92 | 50.69 | 95.20 | 81.08 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 42 | dfm-encoder-small-v1 (SimCSE) | 0.42 | 34.12 | 51.92 | 40.82 | 60.00 | 35.25 | 16.99 | 58.53 | 50.50 | 47.92 | 52.95 | 51.36 | 22.28 | 20.02 |
| 43 | **NEW: dk-model2vec-scripts_models_dk-ll** | **0.42** | **37.12** | **47.83** | **8.19** | **59.45** | **32.28** | **26.12** | **46.73** | **63.32** | **51.73** | **61.34** | **50.22** | **57.02** | **21.05** |
| 44 | dk-model2vec-model2vecdk-stem | 0.42 | 38.25 | 46.18 | 9.17 | 60.76 | 29.86 | 27.69 | 43.93 | 61.55 | 48.78 | 55.90 | 50.12 | 57.34 | 25.56 |


## Additional Resources

- [Repo used to finetune](https://github.com/andersborges/dkmodel2vec)
- [Model2Vec Repo](https://github.com/MinishLab/model2vec)
- [Model2Vec Base Models](https://huggingface.co/collections/minishlab/model2vec-base-models-66fd9dd9b7c3b3c0f25ca90e)
- [Model2Vec Results](https://github.com/MinishLab/model2vec/tree/main/results)
- [Model2Vec Tutorials](https://github.com/MinishLab/model2vec/tree/main/tutorials)
- [Website](https://minishlab.github.io/)


## Library Authors

Model2Vec was developed by the [Minish Lab](https://github.com/MinishLab) team consisting of [Stephan Tulkens](https://github.com/stephantul) and [Thomas van Dongen](https://github.com/Pringled).

## Citation

Please cite the [Model2Vec repository](https://github.com/MinishLab/model2vec) if you use this model in your work.
```
@article{minishlab2024model2vec,
  author = {Tulkens, Stephan and {van Dongen}, Thomas},
  title = {Model2Vec: Fast State-of-the-Art Static Embeddings},
  year = {2024},
  url = {https://github.com/MinishLab/model2vec}
}
```