Package: dfm-sentence-transformers
dfm-sentence-transformersSentence transformers for the Danish Foundation Models Project.TrainingInstall the package from PyPI:pipinstalldfm-sentence-transformersYou 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="dfm-sentence-encoder-small-v1"
base_model="chcaa/dfm-encoder-small-v1"
device="cpu"

[training]
epochs=50
steps_per_epoch=500
warmup_steps=100
batch_size=64
wandb_project="dfm-sentence-transformers"
checkpoint_repo="checkpoints-dfm-sentence-encoder-small-v1"

[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 thefinetunecommand.python3-mdfm_sentence_trffinetunetraining.cfg-o"model/"You can push the finetuned model to HuggingFace Hub:python3-mdfm_sentence_trfpush_to_hubtraining.cfg--model_path"model/"(NEW) Curating datasets with models you've pretrainedSimilarly to Microsoft's E5 we intend to train models on data that has been curated by models we've trained on huristic-based sentence pairs.
We provide a CLI for filtering the dataset based on consistency.This is based on a batch-based strategy in which we take batches of sentence pairs, create a similarity matrix between left-side and right-side sentences.
If a pair's similarity is over the 1-(1/(N*specificity))'th quantile of all similarities in the matrix,
and is originally annotated as a pair by the heuristics, we accept it as a positive pair.
We also assign hard negatives. These are pairs that have similarity in the lower quantile and are originally not annotated as a pair.The hard positive, hard negative scheme is employed so that we can use AnglE for finetuning the models on this curated data.The config scheme for data cleaning is the following:[cleaning]
batch_size=1000
specificity=1.2
name="kardosdrur/folketing-wiki-clean"

[cleaning.model]
...(same as everywhere else)

[data]

[data.folketinget]
sentence1="comment"
sentence2="response"

[data.folketinget.dataset]
@loaders="load_dataset"
path="kardosdrur/folketinget-discussions"Then you can clean the dataset:python3-mdfm_sentence_trfclean_dataset"config.cfg"This will produce the JSONL file<dataset_name>.jsonlcontaining all examples.Datasets can then be shuffled, split and pushed to the hub with thepush_datasetcommand.python3-mdfm_sentence_trfpush_dataset"config.cfg"(NEW) Finetuning with AnglEYou can finetune a model with AnglE on supervised tasks.
AnglE models have a different config format, namely:[model]
...

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

[angle]
sentence1="premise"
sentence2="hypothesis"
label="label"

[angle.dataset]
@loaders="load_dataset"
path="kardosdrur/nb-nli"AnglE models can only be trained on one supervised task,
where the label is correlated with semantic similarity.Note that you have to manually install AnglE.pipinstallangle_embThen you can finetune:python3-mdfm_sentence_trfangle_finetune"config.cfg"-o"model/"Models can be pushed to the hub the same way as everything else.
We recommend that you pretrain on sentence pair datasets and then finetune with angle
on NLI or STS tasks.EvaluationYou can evaluate trained models with theScandinavian Embedding Benchmark.pipinstallseb
python3-mseb"model/""da"TasksYou 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 RankingIf 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:ParamTypeDescriptionDefaultsentence1strName of the first sentence column in the dataset.-sentence2strName of the second sentence column in the dataset.-scalefloatOutput 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 SimilarityGood for STS datasets.
Minimizes mean squared error of estimated and true sentence cosine similairites.Parameters:ParamTypeDescriptionDefaultsentence1strName of the first sentence column in the dataset.-sentence2strName of the second sentence column in the dataset.-similaritystrName of the gold standard similarity column.-[tasks.sts]
@tasks="cosine_similarity"
sentence1="sent1"
sentence2="sent1"
similarity="label"

[tasks.sts.dataset]
...3. SoftmaxGood 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:ParamTypeDescriptionDefaultsentence1strName of the first sentence column in the dataset.-sentence2strName of the second sentence column in the dataset.-labelstrName of the label column in the dataset.-[tasks.nli]
@tasks="softmax"
sentence1="premise"
sentence2="hypothesis"
label="label"

[tasks.nli.dataset]
...4. Contrastive (new in 0.3.6)Contrastive loss for hard negative and hard positive pairs.Parameters:ParamTypeDescriptionDefaultsentence1strName of the first sentence column in the dataset.-sentence2strName of the second sentence column in the dataset.-labelstrName of the label column in the dataset.-[tasks.contrastive]
@tasks="contrastive"
sentence1="text1"
sentence2="text2"
label="label"

[tasks.contrastive.dataset]
...DatasetsDatasets 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"