U
niversal
S
entence
E
ncoder for
R
ussian (USER) is a
sentence-transformer
model for extracting embeddings exclusively for Russian language.
It maps sentences & paragraphs to a 768 dimensional dense vector space and can be used for tasks like clustering or semantic search.
This model is initialized from
deepvk/deberta-v1-base
and trained to work exclusively with the Russian language. Its quality on other languages was not evaluated.
from sentence_transformers import SentenceTransformer
# Each input text should start with "query: " or "passage: ".# For tasks other than retrieval, you can simply use the "query: " prefix.
input_texts = [
"query: Когда был спущен на воду первый миноносец «Спокойный»?",
"query: Есть ли нефть в Удмуртии?",
"passage: Спокойный (эсминец)\nЗачислен в списки ВМФ СССР 19 августа 1952 года.",
"passage: Нефтепоисковые работы в Удмуртии были начаты сразу после Второй мировой войны в 1945 году и продолжаются по сей день. Добыча нефти началась в 1967 году."
]
model = SentenceTransformer("deepvk/USER-base")
embeddings = model.encode(input_texts, normalize_embeddings=True)
import torch.nn.functional as F
from torch import Tensor, inference_mode
from transformers import AutoTokenizer, AutoModel
defaverage_pool(
last_hidden_states: Tensor, attention_mask: Tensor
) -> Tensor:
last_hidden = last_hidden_states.masked_fill(
~attention_mask[..., None].bool(), 0.0
)
return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
# Each input text should start with "query: " or "passage: ".# For tasks other than retrieval, you can simply use the "query: " prefix.
input_texts = [
"query: Когда был спущен на воду первый миноносец «Спокойный»?",
"query: Есть ли нефть в Удмуртии?",
"passage: Спокойный (эсминец)\nЗачислен в списки ВМФ СССР 19 августа 1952 года.",
"passage: Нефтепоисковые работы в Удмуртии были начаты сразу после Второй мировой войны в 1945 году и продолжаются по сей день. Добыча нефти началась в 1967 году."
]
tokenizer = AutoTokenizer.from_pretrained("deepvk/USER-base")
model = AutoModel.from_pretrained("deepvk/USER-base")
batch_dict = tokenizer(
input_texts, padding=True, truncation=True, return_tensors="pt"
)
with inference_mode():
outputs = model(**batch_dict)
embeddings = average_pool(
outputs.last_hidden_state, batch_dict["attention_mask"]
)
embeddings = F.normalize(embeddings, p=2, dim=1)
# Scores for query-passage
scores = (embeddings[:2] @ embeddings[2:].T) * 100# [[55.86, 30.95],# [22.82, 59.46]]print(scores.round(decimals=2))
⚠️
Attention
⚠️
Each input text should start with "query: " or "passage: ".
For tasks other than retrieval, you can simply use the "query: " prefix.
Training Details
We aimed to follow the
bge-base-en
model training algorithm, but we made several improvements along the way.
First-stage:
Contrastive pre-training with weak supervision on the Russian part of
mMarco corpus
.
Second-stage:
Supervised fine-tuning two different models based on data symmetry and then merging via
LM-Cocktail
:
We modified the instruction design by simplifying the multilingual approach to facilitate easier inference.
For symmetric data
(S1, S2)
, we used the instructions:
"query: S1"
and
"query: S2"
, and for asymmetric data, we used
"query: S1"
with
"passage: S2"
.
Since we split the data, we could additionally apply the
AnglE loss
to the symmetric model, which enhances performance on symmetric tasks.
Finally, we combined the two models, tuning the weights for the merger using
LM-Cocktail
to produce the final model,
USER
.
Total positive pairs:
3,352,653
Total negative pairs:
792,644 (negative pairs from AIINLI, MIRACL, deepvk/ru-WANLI, deepvk/ru-HNP)
For all labeled datasets, we only use its training set for fine-tuning.
For datasets Gazeta, Mlsum, Xlsum: pairs (title/text) and (title/summary) are combined and used as asymmetric data.
AllNLI
is an translated to Russian combination of SNLI, MNLI, and ANLI.
Experiments
As a baseline, we chose the current top models from the
encodechka
leaderboard table. In addition, we evaluate model on the russian subset of
MTEB
, which include 10 tasks. Unfortunately, we could not validate the bge-m3 on some MTEB tasks, specifically clustering, due to excessive computational resources. Besides these two benchmarks, we also evaluated the models on the
MIRACL
. All experiments were conducted using NVIDIA TESLA A100 40 GB GPU. We use validation scripts from the official repositories for each of the tasks.
Model sizes are shown, with larger models visually distinct from the others.
Absolute leaders in the metrics are highlighted in bold, and the leaders among models of our size is underlined.
In this way, our solution outperforms all other models of the same size on both Encodechka and MTEB. Given that the model is slightly underperforming in retrieval tasks relative to existing solutions, we aim to address this in our future research.
FAQ
Do I need to add the prefix "query: " and "passage: " to input texts?
Yes, this is how the model is trained, otherwise you will see a performance degradation.
Here are some rules of thumb:
Use
"query: "
and
"passage: "
correspondingly for asymmetric tasks such as passage retrieval in open QA, ad-hoc information retrieval.
Use
"query: "
prefix for symmetric tasks such as semantic similarity, bitext mining, paraphrase retrieval.
Use
"query: "
prefix if you want to use embeddings as features, such as linear probing classification, clustering.
Citations
@misc{deepvk2024user,
title={USER: Universal Sentence Encoder for Russian},
author={Malashenko, Boris and Zemerov, Anton and Spirin, Egor},
url={https://huggingface.co/datasets/deepvk/USER-base},
publisher={Hugging Face}
year={2024},
}
Runs of deepvk USER-base on huggingface.co
79.6K
Total runs
-188
24-hour runs
20
3-day runs
-244
7-day runs
3.4K
30-day runs
More Information About USER-base huggingface.co Model
USER-base huggingface.co is an AI model on huggingface.co that provides USER-base's model effect (), which can be used instantly with this deepvk USER-base model. huggingface.co supports a free trial of the USER-base model, and also provides paid use of the USER-base. Support call USER-base model through api, including Node.js, Python, http.
USER-base huggingface.co is an online trial and call api platform, which integrates USER-base's modeling effects, including api services, and provides a free online trial of USER-base, you can try USER-base online for free by clicking the link below.
deepvk USER-base online free url in huggingface.co:
USER-base is an open source model from GitHub that offers a free installation service, and any user can find USER-base on GitHub to install. At the same time, huggingface.co provides the effect of USER-base install, users can directly use USER-base installed effect in huggingface.co for debugging and trial. It also supports api for free installation.