My notes on ModernBERT. Source
BERT
DECODER ONLY MODELS
ENCODER ONLY MODELS
MODERN BERT
USAGE
To use it as a sentence transformer, we need to use an unofficial, fine tuned version.
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("joe32140/ModernBERT-base-msmarco")
sentences = [
'what county is hayden in',
"Hayden is a city in Kootenai County, Idaho, United States. Located in the northern portion of the state, just north of Coeur d'Alene, its population was 13,294 at the 2010 census.",
"According to the United States Census Bureau, the city has a total area of 9.61 square miles (24.89 km2), of which 9.60 square miles (24.86 km2) is land and 0.01 square miles (0.03 km2) is water. It lies at the southwestern end of Hayden Lake, and the elevation of the city is 2,287 feet (697 m) above sea level. Hayden is located on U.S. Route 95 at the junction of Route 41. It is also four miles (6 km) north of Interstate 90 and Coeur d'Alene. The Coeur d'Alene airport is northwest of Hayden.",
]
embeddings = model.encode(sentences)
similarities = model.similarity(embeddings, embeddings)
[for idx_i, sentence in enumerate(sentences):
print(sentence)
for idx_j, sentence in enumerate(sentences):
print(f" - {similarities[idx_i][idx_j]:.4f}: {sentence: <30} ")](<for idx_i, sentence in enumerate(sentences):
print(sentence[:100])
for idx_j, sentence in enumerate(sentences):
print(f" - {similarities[idx_i][idx_j]:.4f}: {sentence[:100]}... ")>)
what county is hayden in
- 1.0000: what county is hayden in...
- 0.7469: Hayden is a city in Kootenai County, Idaho, United States. Located in the northern portion of the st...
- 0.6398: According to the United States Census Bureau, the city has a total area of 9.61 square miles (24.89 ... - -0.1262: organization name...
Hayden is a city in Kootenai County, Idaho, United States. Located in the northern portion of the st
- 0.7469: what county is hayden in...
- 1.0000: Hayden is a city in Kootenai County, Idaho, United States. Located in the northern portion of the st...
- 0.6513: According to the United States Census Bureau, the city has a total area of 9.61 square miles (24.89 ... -
- -0.0741: organization name...
According to the United States Census Bureau, the city has a total area of 9.61 square miles (24.89
- 0.6398: what county is hayden in...
- 0.6513: Hayden is a city in Kootenai County, Idaho, United States. Located in the northern portion of the st...
- 1.0000: According to the United States Census Bureau, the city has a total area of 9.61 square miles (24.89 ... -
- -0.0760: organization name...
organization name
- -0.1262: what county is hayden in...
- -0.0741: Hayden is a city in Kootenai County, Idaho, United States. Located in the northern portion of the st...
- -0.0760: According to the United States Census Bureau, the city has a total area of 9.61 square miles (24.89 ...
- 1.0000: organization name...
NOTE, if you run into NameError: name 'init_empty_weights' is not defined
error, it’s due to bug in transformers. You can solve it by doing pip install accelerate
or most likely the fix will be merged and available by the time you read this.