Ты старший учёный по данным ,который изъясняется научным языком
The best of both worlds
As previously mentioned, IDF is the most important part of the BM25 formula. In fact it is so important, that we decided to build its calculation into the Qdrant engine itself. Check out our latest release notes. This type of separation allows streaming updates of the sparse embeddings while keeping the IDF calculation up-to-date.
As for the second part of the formula, the term importance within the document needs to be rethought.
Since we can’t rely on the statistics within the document, we can try to use the semantics of the document instead. And semantics is what transformers are good at. Therefore, we only need to solve two problems:
How does one extract the importance information from the transformer?
How can tokenization issues be avoided?
Attention is all you need
Transformer models, even those used to generate embeddings, generate a bunch of different outputs. Some of those outputs are used to generate embeddings.
Others are used to solve other kinds of tasks, such as classification, text generation, etc.
The one particularly interesting output for us is the attention matrix.
Attention matrix
Attention matrix
The attention matrix is a square matrix, where each row and column corresponds to the token in the input sequence. It represents the importance of each token in the input sequence for each other.
The classical transformer models are trained to predict masked tokens in the context, so the attention weights define which context tokens influence the masked token most.
Apart from regular text tokens, the transformer model also has a special token called [CLS]. This token represents the whole sequence in the classification tasks, which is exactly what we need.
By looking at the attention row for the [CLS] token, we can get the importance of each token in the document for the whole document.
sentences = "Hello, World - is the starting point in most programming languages"
features = transformer.tokenize(sentences)
...
attentions = transformer.auto_model(**features, output_attentions=True).attentions
weights = torch.mean(attentions[-1][0,:,0], axis=0)
▲ ▲ ▲ ▲
│ │ │ └─── [CLS] token is the first one
│ │ └─────── First item of the batch
│ └────────── Last transformer layer
└────────────────────────── Average all 6 attention heads
for weight, token in zip(weights, tokens):
print(f"{token}: {weight}")
[CLS] : 0.434 // Filter out the [CLS] token
hello : 0.039
, : 0.039
world : 0.107 // <-- The most important token
- : 0.033
is : 0.024
the : 0.031
starting : 0.054
point : 0.028
in : 0.018
most : 0.016
programming : 0.060 // <-- The third most important token
languages : 0.062 // <-- The second most important token
[SEP] : 0.047 // Filter out the [SEP] token
The resulting formula for the BM42 score would look like this:
Note that classical transformers have multiple attention heads, so we can get multiple importance vectors for the same document. The simplest way to combine them is to simply average them.
These averaged attention vectors make up the importance information we were looking for. The best part is, one can get them from any transformer model, without any additional training. Therefore, BM42 can support any natural language as long as there is a transformer model for it.
In our implementation, we use the sentence-transformers/all-MiniLM-L6-v2 model, which gives a huge boost in the inference speed compared to the SPLADE models. In practice, any transformer model can be used. It doesn’t require any additional training, and can be easily adapted to work as BM42 backend.
Перепиши данный текст