Views
No views yet
cde-small-v1 is the best small model (under 400M params) on the MTEB leaderboard for text embedding models, with an average score of 65.00.
cde-small-v1transformers out-of-the-box with "trust remote code" enabled. We use the default BERT uncased tokenizer:1import transformers
2
3model = transformers.AutoModel.from_pretrained("jxm/cde-small-v1", trust_remote_code=True)
4tokenizer = transformers.AutoTokenizer.from_pretrained("bert-base-uncased")1query_prefix = "search_query: "
2document_prefix = "search_document: "1minicorpus_size = model.config.transductive_corpus_size
2minicorpus_docs = [ ... ] # Put some strings here that are representative of your corpus, for example by calling random.sample(corpus, k=minicorpus_size)
3assert len(minicorpus_docs) == minicorpus_size # You must use exactly this many documents in the minicorpus. You can oversample if your corpus is smaller.
4minicorpus_docs = tokenizer(
5 [document_prefix + doc for doc in minicorpus_docs],
6 truncation=True,
7 padding=True,
8 max_length=512,
9 return_tensors="pt"
10).to(model.device)
11import torch
12from tqdm.autonotebook import tqdm
13
14batch_size = 32
15
16dataset_embeddings = []
17for i in tqdm(range(0, len(minicorpus_docs["input_ids"]), batch_size)):
18 minicorpus_docs_batch = {k: v[i:i+batch_size] for k,v in minicorpus_docs.items()}
19 with torch.no_grad():
20 dataset_embeddings.append(
21 model.first_stage_model(**minicorpus_docs_batch)
22 )
23
24dataset_embeddings = torch.cat(dataset_embeddings)1docs = tokenizer(
2 [document_prefix + doc for doc in docs],
3 truncation=True,
4 padding=True,
5 max_length=512,
6 return_tensors="pt"
7).to(model.device)
8
9with torch.no_grad():
10 doc_embeddings = model.second_stage_model(
11 input_ids=docs["input_ids"],
12 attention_mask=docs["attention_mask"],
13 dataset_embeddings=dataset_embeddings,
14 )
15doc_embeddings /= doc_embeddings.norm(p=2, dim=1, keepdim=True)1queries = queries.select(range(16))["text"]
2queries = tokenizer(
3 [query_prefix + query for query in queries],
4 truncation=True,
5 padding=True,
6 max_length=512,
7 return_tensors="pt"
8).to(model.device)
9
10with torch.no_grad():
11 query_embeddings = model.second_stage_model(
12 input_ids=queries["input_ids"],
13 attention_mask=queries["attention_mask"],
14 dataset_embeddings=dataset_embeddings,
15 )
16query_embeddings /= query_embeddings.norm(p=2, dim=1, keepdim=True)sentence-transformers out-of-the-box with "trust remote code" enabled:1from sentence_transformers import SentenceTransformer
2
3model = SentenceTransformer("jxm/cde-small-v1", trust_remote_code=True)prompt_name="query" and prompt_name="document" in the encode method of the model when embedding queries and documents, respectively.1minicorpus_size = model[0].config.transductive_corpus_size
2minicorpus_docs = [ ... ] # Put some strings here that are representative of your corpus, for example by calling random.sample(corpus, k=minicorpus_size)
3assert len(minicorpus_docs) == minicorpus_size # You must use exactly this many documents in the minicorpus. You can oversample if your corpus is smaller.
4
5dataset_embeddings = model.encode(
6 minicorpus_docs,
7 prompt_name="document",
8 convert_to_tensor=True
9)1docs = [...]
2queries = [...]
3
4doc_embeddings = model.encode(
5 docs,
6 prompt_name="document",
7 dataset_embeddings=dataset_embeddings,
8 convert_to_tensor=True,
9)
10query_embeddings = model.encode(
11 queries,
12 prompt_name="query",
13 dataset_embeddings=dataset_embeddings,
14 convert_to_tensor=True,
15) model.similarity:1similarities = model.similarity(query_embeddings, doc_embeddings)
2topk_values, topk_indices = similarities.topk(5)1from sentence_transformers import SentenceTransformer
2from datasets import load_dataset
3
4# 1. Load the Sentence Transformer model
5model = SentenceTransformer("jxm/cde-small-v1", trust_remote_code=True)
6context_docs_size = model[0].config.transductive_corpus_size # 512
7
8# 2. Load the dataset: context dataset, docs, and queries
9dataset = load_dataset("sentence-transformers/natural-questions", split="train")
10dataset.shuffle(seed=42)
11# 10 queries, 512 context docs, 500 docs
12queries = dataset["query"][:10]
13docs = dataset["answer"][:2000]
14context_docs = dataset["answer"][-context_docs_size:] # Last 512 docs
15
16# 3. First stage: embed the context docs
17dataset_embeddings = model.encode(
18 context_docs,
19 prompt_name="document",
20 convert_to_tensor=True,
21)
22
23# 4. Second stage: embed the docs and queries
24doc_embeddings = model.encode(
25 docs,
26 prompt_name="document",
27 dataset_embeddings=dataset_embeddings,
28 convert_to_tensor=True,
29)
30query_embeddings = model.encode(
31 queries,
32 prompt_name="query",
33 dataset_embeddings=dataset_embeddings,
34 convert_to_tensor=True,
35)
36
37# 5. Compute the similarity between the queries and docs
38similarities = model.similarity(query_embeddings, doc_embeddings)
39topk_values, topk_indices = similarities.topk(5)
40print(topk_values)
41print(topk_indices)
42
43"""
44tensor([[0.5495, 0.5426, 0.5423, 0.5292, 0.5286],
45 [0.6357, 0.6334, 0.6177, 0.5862, 0.5794],
46 [0.7648, 0.5452, 0.5000, 0.4959, 0.4881],
47 [0.6802, 0.5225, 0.5178, 0.5160, 0.5075],
48 [0.6947, 0.5843, 0.5619, 0.5344, 0.5298],
49 [0.7742, 0.7742, 0.7742, 0.7231, 0.6224],
50 [0.8853, 0.6667, 0.5829, 0.5795, 0.5769],
51 [0.6911, 0.6127, 0.6003, 0.5986, 0.5936],
52 [0.6796, 0.6053, 0.6000, 0.5911, 0.5884],
53 [0.7624, 0.5589, 0.5428, 0.5278, 0.5275]], device='cuda:0')
54tensor([[ 0, 296, 234, 1651, 1184],
55 [1542, 466, 438, 1207, 1911],
56 [ 2, 1562, 632, 1852, 382],
57 [ 3, 694, 932, 1765, 662],
58 [ 4, 35, 747, 26, 432],
59 [ 534, 175, 5, 1495, 575],
60 [ 6, 1802, 1875, 747, 21],
61 [ 7, 1913, 1936, 640, 6],
62 [ 8, 747, 167, 1318, 1743],
63 [ 9, 1583, 1145, 219, 357]], device='cuda:0')
64"""
65# As you can see, almost every query_i has document_i as the most similar document.
66
67# 6. Print the top-k results
68for query_idx, top_doc_idx in enumerate(topk_indices[:, 0]):
69 print(f"Query {query_idx}: {queries[query_idx]}")
70 print(f"Top Document: {docs[top_doc_idx]}")
71 print()
72"""
73Query 0: when did richmond last play in a preliminary final
74Top Document: Richmond Football Club Richmond began 2017 with 5 straight wins, a feat it had not achieved since 1995. A series of close losses hampered the Tigers throughout the middle of the season, including a 5-point loss to the Western Bulldogs, 2-point loss to Fremantle, and a 3-point loss to the Giants. Richmond ended the season strongly with convincing victories over Fremantle and St Kilda in the final two rounds, elevating the club to 3rd on the ladder. Richmond's first final of the season against the Cats at the MCG attracted a record qualifying final crowd of 95,028; the Tigers won by 51 points. Having advanced to the first preliminary finals for the first time since 2001, Richmond defeated Greater Western Sydney by 36 points in front of a crowd of 94,258 to progress to the Grand Final against Adelaide, their first Grand Final appearance since 1982. The attendance was 100,021, the largest crowd to a grand final since 1986. The Crows led at quarter time and led by as many as 13, but the Tigers took over the game as it progressed and scored seven straight goals at one point. They eventually would win by 48 points – 16.12 (108) to Adelaide's 8.12 (60) – to end their 37-year flag drought.[22] Dustin Martin also became the first player to win a Premiership medal, the Brownlow Medal and the Norm Smith Medal in the same season, while Damien Hardwick was named AFL Coaches Association Coach of the Year. Richmond's jump from 13th to premiers also marked the biggest jump from one AFL season to the next.
75
76Query 1: who sang what in the world's come over you
77Top Document: Life's What You Make It (Talk Talk song) "Life's What You Make It" is a song by the English band Talk Talk. It was released as a single in 1986, the first from the band's album The Colour of Spring. The single was a hit in the UK, peaking at No. 16, and charted in numerous other countries, often reaching the Top 20.
78
79Query 2: who produces the most wool in the world
80Top Document: Wool Global wool production is about 2 million tonnes per year, of which 60% goes into apparel. Wool comprises ca 3% of the global textile market, but its value is higher owing to dying and other modifications of the material.[1] Australia is a leading producer of wool which is mostly from Merino sheep but has been eclipsed by China in terms of total weight.[30] New Zealand (2016) is the third-largest producer of wool, and the largest producer of crossbred wool. Breeds such as Lincoln, Romney, Drysdale, and Elliotdale produce coarser fibers, and wool from these sheep is usually used for making carpets.
81
82Query 3: where does alaska the last frontier take place
83Top Document: Alaska: The Last Frontier Alaska: The Last Frontier is an American reality cable television series on the Discovery Channel, currently in its 7th season of broadcast. The show documents the extended Kilcher family, descendants of Swiss immigrants and Alaskan pioneers, Yule and Ruth Kilcher, at their homestead 11 miles outside of Homer.[1] By living without plumbing or modern heating, the clan chooses to subsist by farming, hunting and preparing for the long winters.[2] The Kilcher family are relatives of the singer Jewel,[1][3] who has appeared on the show.[4]
84
85Query 4: a day to remember all i want cameos
86Top Document: All I Want (A Day to Remember song) The music video for the song, which was filmed in October 2010,[4] was released on January 6, 2011.[5] It features cameos of numerous popular bands and musicians. The cameos are: Tom Denney (A Day to Remember's former guitarist), Pete Wentz, Winston McCall of Parkway Drive, The Devil Wears Prada, Bring Me the Horizon, Sam Carter of Architects, Tim Lambesis of As I Lay Dying, Silverstein, Andrew WK, August Burns Red, Seventh Star, Matt Heafy of Trivium, Vic Fuentes of Pierce the Veil, Mike Herrera of MxPx, and Set Your Goals.[5] Rock Sound called the video "quite excellent".[5]
87
88Query 5: what does the red stripes mean on the american flag
89Top Document: Flag of the United States The flag of the United States of America, often referred to as the American flag, is the national flag of the United States. It consists of thirteen equal horizontal stripes of red (top and bottom) alternating with white, with a blue rectangle in the canton (referred to specifically as the "union") bearing fifty small, white, five-pointed stars arranged in nine offset horizontal rows, where rows of six stars (top and bottom) alternate with rows of five stars. The 50 stars on the flag represent the 50 states of the United States of America, and the 13 stripes represent the thirteen British colonies that declared independence from the Kingdom of Great Britain, and became the first states in the U.S.[1] Nicknames for the flag include The Stars and Stripes,[2] Old Glory,[3] and The Star-Spangled Banner.
90
91Query 6: where did they film diary of a wimpy kid
92Top Document: Diary of a Wimpy Kid (film) Filming of Diary of a Wimpy Kid was in Vancouver and wrapped up on October 16, 2009.
93
94Query 7: where was beasts of the southern wild filmed
95Top Document: Beasts of the Southern Wild The film's fictional setting, "Isle de Charles Doucet", known to its residents as the Bathtub, was inspired by several isolated and independent fishing communities threatened by erosion, hurricanes and rising sea levels in Louisiana's Terrebonne Parish, most notably the rapidly eroding Isle de Jean Charles. It was filmed in Terrebonne Parish town Montegut.[5]
96
97Query 8: what part of the country are you likely to find the majority of the mollisols
98Top Document: Mollisol Mollisols occur in savannahs and mountain valleys (such as Central Asia, or the North American Great Plains). These environments have historically been strongly influenced by fire and abundant pedoturbation from organisms such as ants and earthworms. It was estimated that in 2003, only 14 to 26 percent of grassland ecosystems still remained in a relatively natural state (that is, they were not used for agriculture due to the fertility of the A horizon). Globally, they represent ~7% of ice-free land area. As the world's most agriculturally productive soil order, the Mollisols represent one of the more economically important soil orders.
99
100Query 9: when did fosters home for imaginary friends start
101Top Document: Foster's Home for Imaginary Friends McCracken conceived the series after adopting two dogs from an animal shelter and applying the concept to imaginary friends. The show first premiered on Cartoon Network on August 13, 2004, as a 90-minute television film. On August 20, it began its normal run of twenty-to-thirty-minute episodes on Fridays, at 7 pm. The series finished its run on May 3, 2009, with a total of six seasons and seventy-nine episodes. McCracken left Cartoon Network shortly after the series ended. Reruns have aired on Boomerang from August 11, 2012 to November 3, 2013 and again from June 1, 2014 to April 3, 2017.
102"""@misc{morris2024contextualdocumentembeddings,
title={Contextual Document Embeddings},
author={John X. Morris and Alexander M. Rush},
year={2024},
eprint={2410.02525},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2410.02525},
}