File size: 2,595 Bytes
6ce56a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23a9cf3
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
---

license: apache-2.0
pipeline_tag: text-ranking
language:
- en
library_name: sentence-transformers
base_model:
- google/electra-base-discriminator
tags:
- transformers
---


## Cross-Encoder for Text Ranking

This model is a port of the [webis/monoelectra-base](https://huggingface.co/webis/monoelectra-base) model from [lightning-ir](https://github.com/webis-de/lightning-ir) to [Sentence Transformers](https://sbert.net/) and [Transformers](https://huggingface.co/docs/transformers). 

The original model was introduced in the paper [A Systematic Investigation of Distilling Large Language Models into Cross-Encoders for Passage Re-ranking](https://arxiv.org/abs/2405.07920). See https://github.com/webis-de/rank-distillm for code used to train the original model.

The model can be used as a reranker in a 2-stage "retrieve-rerank" pipeline, where it reorders passages returned by a retriever model (e.g. an embedding model or BM25) given some query. See [SBERT.net Retrieve & Re-rank](https://www.sbert.net/examples/applications/retrieve_rerank/README.html) for more details.

## Usage with Sentence Transformers

The usage is easy when you have [SentenceTransformers](https://www.sbert.net/) installed. 

```bash

pip install sentence-transformers

```

Then you can use the pre-trained model like this:

```python

from sentence_transformers import CrossEncoder



model = CrossEncoder("cross-encoder/monoelectra-base", trust_remote_code=True)

scores = model.predict([

    ("How many people live in Berlin?", "Berlin had a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers."),

    ("How many people live in Berlin?", "Berlin is well known for its museums."),

])

print(scores)

# [ 8.122868 -4.292924]

```

## Usage with Transformers

```python

from transformers import AutoTokenizer, AutoModelForSequenceClassification

import torch



model = AutoModelForSequenceClassification.from_pretrained("cross-encoder/monoelectra-base", trust_remote_code=True)

tokenizer = AutoTokenizer.from_pretrained("cross-encoder/monoelectra-base")



features = tokenizer(

    [

        ("How many people live in Berlin?", "Berlin had a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers."),

        ("How many people live in Berlin?", "Berlin is well known for its museums."),

    ],

    padding=True,

    truncation=True,

    return_tensors="pt",

)



model.eval()

with torch.no_grad():

    scores = model(**features).logits.view(-1)

print(scores)

# tensor([ 8.1229, -4.2929])

```