Skip to main content

Module for finetuning dfm base-models to sentence transformers

Project description

dfm-sentence-transformers

Code for training sentence transformers for the Danish Foundation Models project.

Training

Install the package from PyPI:

pip install dfm-sentence-transformers

You have to specify basic model and training parameters, as well as all the tasks/datasets the model should be trained on.

Here is an example of a config:

[model]
name="chcaa/dfm-sentence-encoder-small-v1"
base_model="chcaa/dfm-encoder-small-v1"
device="cpu"

[training]
epochs=5
warmup_steps=100
batch_size=120

[tasks]

[tasks.bornholmsk]
@tasks="multiple_negatives_ranking"
sentence1="da_bornholm"
sentence2="da"

[tasks.bornholmsk.dataset]
@loaders="load_dataset"
path="strombergnlp/bornholmsk_parallel"

Then you can train a sentence transformer by using the finetune command.

python3 -m dfm_sentence_trf finetune training.cfg -o "model/"

You can push the finetuned model to HuggingFace Hub:

python3 -m dfm_sentence_trf push_to_hub training.cfg --model_path "model/"

Evaluation

You can evaluate trained models with the Scandinavian Embedding Benchmark.

pip install seb
python3 -m seb "model/" "da"

Tasks

You can add an arbitrary number of tasks to the model's config. All tasks must have a unique name but their name is ignored in the actual training procedure. Datasets of tasks with the same loss function are mixed together so that the model can learn them simultaneously in mixed batches. The package comes with three default tasks you can use for different objectives:

1. Multiple Negatives Ranking

If you have a parallel corpus of sentences (paraphrase, translation, etc.) use this task. Batches consist of positive sentence pairs, and negative samples are constructed by taking all non-matching pairs in a batch.

Parameters:

Param Type Description Default
sentence1 str Name of the first sentence column in the dataset. -
sentence2 str Name of the second sentence column in the dataset. -
scale float Output of similarity function is multiplied by scale value. 20.0
[tasks.faroese]
@tasks="multiple_negatives_ranking"
sentence1="fo"
sentence2="da"

[tasks.faroese.dataset]
@loaders="load_dataset"
path="strombergnlp/itu_faroese_danish"

2. Cosine Similarity

Good for STS datasets. Minimizes mean squared error of estimated and true sentence cosine similairites.

Parameters:

Param Type Description Default
sentence1 str Name of the first sentence column in the dataset. -
sentence2 str Name of the second sentence column in the dataset. -
similarity str Name of the gold standard similarity column. -
[tasks.sts]
@tasks="cosine_similarity"
sentence1="sent1"
sentence2="sent1"
similarity="label"

[tasks.sts.dataset]
...

3. Softmax

Good for NLI datasets. Uses softmax classification loss based on concatenated embeddings and their difference. Beware that these tasks are never joined due to potentially different labeling schemes.

Parameters:

Param Type Description Default
sentence1 str Name of the first sentence column in the dataset. -
sentence2 str Name of the second sentence column in the dataset. -
label str Name of the label column in the dataset. -
[tasks.nli]
@tasks="softmax"
sentence1="premise"
sentence2="hypothesis"
label="label"

[tasks.nli.dataset]
...

Datasets

Datasets for each task are loaded with :hugs: load_dataset() function, but only the first argument, and a name are accepted. You can use local or remote datasets, and they can be of any of the canonical file formats (JSON, JSONL, CSV, Parquet...).

...

[tasks.local.dataset]
@loaders="load_dataset"
path="local/dataset/file.jsonl"

...

[tasks.huggingface_hub.dataset]
@loaders="load_dataset"
path="username/dataset"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dfm_sentence_transformers-0.2.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dfm_sentence_transformers-0.2.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file dfm_sentence_transformers-0.2.0.tar.gz.

File metadata

  • Download URL: dfm_sentence_transformers-0.2.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.9.13 Linux/5.15.0-88-generic

File hashes

Hashes for dfm_sentence_transformers-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0fe4f6759a44510d83ec09bb8eeeb8953cb3ae67c7309691011b6be4029ef1db
MD5 ff2d9cdccab236c0434c6a42203c6004
BLAKE2b-256 3b0d9f69efcef522267f18337bcdadc05026e632a358bcd09e1febf7b8f3e434

See more details on using hashes here.

File details

Details for the file dfm_sentence_transformers-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dfm_sentence_transformers-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 54ef508409557c2dfcf02c527c1fe464f6b92bb0db97450b1a72a097a3c45edd
MD5 16a00f2bd8f0418304e36284db54d7b7
BLAKE2b-256 d3543999c1501c5be4049d9b15c26777b842273040a79184bae9bd59bd9c6065

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page