初始化项目,由ModelHub XC社区提供模型

Model: NbAiLab/nb-sbert-base
Source: Original Platform
This commit is contained in:
ModelHub XC
2026-07-29 20:17:55 +08:00
commit 65c27cf3c9
15 changed files with 239638 additions and 0 deletions

34
.gitattributes vendored Normal file
View File

@@ -0,0 +1,34 @@
*.7z filter=lfs diff=lfs merge=lfs -text
*.arrow filter=lfs diff=lfs merge=lfs -text
*.bin filter=lfs diff=lfs merge=lfs -text
*.bz2 filter=lfs diff=lfs merge=lfs -text
*.ckpt filter=lfs diff=lfs merge=lfs -text
*.ftz filter=lfs diff=lfs merge=lfs -text
*.gz filter=lfs diff=lfs merge=lfs -text
*.h5 filter=lfs diff=lfs merge=lfs -text
*.joblib filter=lfs diff=lfs merge=lfs -text
*.lfs.* filter=lfs diff=lfs merge=lfs -text
*.mlmodel filter=lfs diff=lfs merge=lfs -text
*.model filter=lfs diff=lfs merge=lfs -text
*.msgpack filter=lfs diff=lfs merge=lfs -text
*.npy filter=lfs diff=lfs merge=lfs -text
*.npz filter=lfs diff=lfs merge=lfs -text
*.onnx filter=lfs diff=lfs merge=lfs -text
*.ot filter=lfs diff=lfs merge=lfs -text
*.parquet filter=lfs diff=lfs merge=lfs -text
*.pb filter=lfs diff=lfs merge=lfs -text
*.pickle filter=lfs diff=lfs merge=lfs -text
*.pkl filter=lfs diff=lfs merge=lfs -text
*.pt filter=lfs diff=lfs merge=lfs -text
*.pth filter=lfs diff=lfs merge=lfs -text
*.rar filter=lfs diff=lfs merge=lfs -text
*.safetensors filter=lfs diff=lfs merge=lfs -text
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
*.tar.* filter=lfs diff=lfs merge=lfs -text
*.tflite filter=lfs diff=lfs merge=lfs -text
*.tgz filter=lfs diff=lfs merge=lfs -text
*.wasm filter=lfs diff=lfs merge=lfs -text
*.xz filter=lfs diff=lfs merge=lfs -text
*.zip filter=lfs diff=lfs merge=lfs -text
*.zst filter=lfs diff=lfs merge=lfs -text
*tfevents* filter=lfs diff=lfs merge=lfs -text

7
1_Pooling/config.json Normal file
View File

@@ -0,0 +1,7 @@
{
"word_embedding_dimension": 768,
"pooling_mode_cls_token": false,
"pooling_mode_mean_tokens": true,
"pooling_mode_max_tokens": false,
"pooling_mode_mean_sqrt_len_tokens": false
}

242
README.md Normal file
View File

@@ -0,0 +1,242 @@
---
tags:
- sentence-transformers
- feature-extraction
- sentence-similarity
- transformers
datasets: NbAiLab/mnli-norwegian
pipeline_tag: sentence-similarity
widget:
- source_sentence: This is a Norwegian boy
sentences:
- Dette er en norsk gutt
- This is an English boy
- This is a dog
example_title: Cross Language
- source_sentence: Det er noen dyr utenfor vinduet
sentences:
- På utsiden kan jeg høre noen hunder
- Noen mennesker prater utenfor vinduet
- Alle burde ha kjæledyr
example_title: Paraphrases
- source_sentence: En kvinne sitter i en stol
sentences:
- A woman is sitting in a chair
- Hun slapper av og leser i en bok
- Hun løper maraton
example_title: Paraphrases across language
license: apache-2.0
language:
- 'no'
---
# NB-SBERT-BASE
> [!NOTE]
> As of April 13th 2026, there are now new versions of this model, with improved performance and increased context length.
> - [NbAiLab/nb-sbert-v2-base](https://huggingface.co/NbAiLab/nb-sbert-v2-base)
> - [NbAiLab/nb-sbert-v2-large](https://huggingface.co/NbAiLab/nb-sbert-v2-large)
NB-SBERT-BASE is a [SentenceTransformers](https://www.SBERT.net) model trained on a [machine translated version of the MNLI dataset](https://huggingface.co/datasets/NbAiLab/mnli-norwegian), starting from [nb-bert-base](https://huggingface.co/NbAiLab/nb-bert-base).
The model maps sentences & paragraphs to a 768 dimensional dense vector space. This vector can be used for tasks like clustering and semantic search. Below we give some examples on how to use the model. The easiest way is to simply measure the cosine distance between two sentences. Sentences that are close to each other in meaning, will have a small cosine distance and a similarity close to 1. The model is trained in such a way that similar sentences in different languages should also be close to each other. Ideally, an English-Norwegian sentence pair should have high similarity.
## Embeddings and Sentence Similarity (Sentence-Transformers)
As seen above, using the library [sentence-transformers](https://www.SBERT.net) makes the use of these models quite convenient:
```bash
pip install -U sentence-transformers
```
Then you can use the model like this:
```python
from sentence_transformers import SentenceTransformer, util
sentences = ["This is a Norwegian boy", "Dette er en norsk gutt"]
model = SentenceTransformer('NbAiLab/nb-sbert-base')
embeddings = model.encode(sentences)
print(embeddings)
# Compute cosine-similarities with sentence transformers
cosine_scores = util.cos_sim(embeddings[0],embeddings[1])
print(cosine_scores)
# Compute cosine-similarities with SciPy
from scipy import spatial
scipy_cosine_scores = 1 - spatial.distance.cosine(embeddings[0],embeddings[1])
print(scipy_cosine_scores)
# Both should give 0.8250 in the example above.
```
## Embeddings and Sentence Similarity (HuggingFace Transformers)
Without [sentence-transformers](https://www.SBERT.net), you can still use the model. First, you pass in your input through the transformer model, then you have to apply the right pooling-operation on top of the contextualized word embeddings.
```python
from transformers import AutoTokenizer, AutoModel
import torch
#Mean Pooling - Take attention mask into account for correct averaging
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
# Sentences we want sentence embeddings for
sentences = ["This is a Norwegian boy", "Dette er en norsk gutt"]
# Load model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained('NbAiLab/nb-sbert-base')
model = AutoModel.from_pretrained('NbAiLab/nb-sbert-base')
# Tokenize sentences
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
# Compute token embeddings
with torch.no_grad():
model_output = model(**encoded_input)
# Perform pooling. In this case, mean pooling.
embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
print(embeddings)
# Compute cosine-similarities with SciPy
from scipy import spatial
scipy_cosine_scores = 1 - spatial.distance.cosine(embeddings[0],embeddings[1])
print(scipy_cosine_scores)
# This should give 0.8250 in the example above.
```
## SetFit - Few Shot Classification
[SetFit](https://github.com/huggingface/setfit) is a method for using sentence-transformers to solve one of major problem that all NLP researchers are facing: Too few labeled training examples. The 'nb-sbert-base' can be plugged directly into the SetFit library. Please see [this tutorial](https://huggingface.co/blog/setfit) for how to use this technique.
## Keyword Extraction
The model can be used for extracting keywords from text. The basic technique is to find the words that are most similar to the document. There are various frameworks for doing this. An easy way is to use [KeyBERT](https://github.com/MaartenGr/KeyBERT). This example shows how this can be done.
```bash
pip install keybert
```
```python
from keybert import KeyBERT
from sentence_transformers import SentenceTransformer
sentence_model = SentenceTransformer("NbAiLab/nb-sbert-base")
kw_model = KeyBERT(model=sentence_model)
doc = """
De første nasjonale bibliotek har sin opprinnelse i kongelige samlinger eller en annen framstående myndighet eller statsoverhode.
Et av de første planene for et nasjonalbibliotek i England ble fremmet av den walisiske matematikeren og mystikeren John Dee som
i 1556 presenterte en visjonær plan om et nasjonalt bibliotek for gamle bøker, manuskripter og opptegnelser for dronning Maria I
av England. Hans forslag ble ikke tatt til følge.
"""
kw_model.extract_keywords(doc, stop_words=None)
# [('nasjonalbibliotek', 0.5242), ('bibliotek', 0.4342), ('samlinger', 0.3334), ('statsoverhode', 0.33), ('manuskripter', 0.3061)]
```
The [KeyBERT homepage](https://github.com/MaartenGr/KeyBERT) provides other several interesting examples: combining KeyBERT with stop words, extracting longer phrases, or directly producing highlighted text.
## Topic Modeling
To analyse a group of documents and determine the topics, has a lot of use cases. [BERTopic](https://github.com/MaartenGr/BERTopic) combines the power of sentence transformers with c-TF-IDF to create clusters for easily interpretable topics.
It would take too much time to explain topic modeling here. Instead we recommend that you take a look at the link above, as well as the [documentation](https://maartengr.github.io/BERTopic/index.html). The main adaptation you would need to do to use the Norwegian nb-sbert-base, is to add the following:
```python
topic_model = BERTopic(embedding_model='NbAiLab/nb-sbert-base').fit(docs)
```
## Similarity Search
Another common use case for a SentenceTransformers model is to find relevant documents or passages of documents given a certain query text. In this scenario, it is pretty common to have a vector database that stores the embedding vectors for all our documents. Then, at runtime, an embedding for the query text is generated and compared efficiently against the vector database.
While production vector databases exist, a quick way to experiment with them is by using [`autofaiss`](https://github.com/criteo/autofaiss):
```bash
pip install autofaiss sentence-transformers
```
```python
from autofaiss import build_index
import numpy as np
from sentence_transformers import SentenceTransformer, util
sentences = ["This is a Norwegian boy", "Dette er en norsk gutt", "A red house"]
model = SentenceTransformer('NbAiLab/nb-sbert-base')
embeddings = model.encode(sentences)
index, index_infos = build_index(embeddings, save_on_disk=False)
# Search for the closest matches
query = model.encode(["A young boy"])
_, index_matches = index.search(query, 1)
print(index_matches)
```
# Evaluation and Parameters
## Evaluation
Evaluation results on the sts-test dataset:
| | Pearson | Spearman |
|------------------------|------------|------------|
| Cosine Similarity | **0.8275** | **0.8245** |
| Manhattan Distance | 0.8193 | 0.8182 |
| Euclidean Distance | 0.8190 | 0.8180 |
| Dot Product Similarity | 0.8039 | 0.7951 |
## Training
The model was trained with the parameters:
**DataLoader**:
`sentence_transformers.datasets.NoDuplicatesDataLoader.NoDuplicatesDataLoader` of length 16471 with parameters:
```
{'batch_size': 32}
```
**Loss**:
`sentence_transformers.losses.MultipleNegativesRankingLoss.MultipleNegativesRankingLoss` with parameters:
```
{'scale': 20.0, 'similarity_fct': 'cos_sim'}
```
Parameters of the fit()-Method:
```
{
"epochs": 1,
"evaluation_steps": 1647,
"evaluator": "sentence_transformers.evaluation.EmbeddingSimilarityEvaluator.EmbeddingSimilarityEvaluator",
"max_grad_norm": 1,
"optimizer_class": "<class 'torch.optim.adamw.AdamW'>",
"optimizer_params": {
"lr": 2e-05
},
"scheduler": "WarmupLinear",
"steps_per_epoch": null,
"warmup_steps": 1648,
"weight_decay": 0.01
}
```
## Full Model Architecture
```
SentenceTransformer(
(0): Transformer({'max_seq_length': 75, 'do_lower_case': False}) with Transformer model: BertModel
(1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})
)
```
## Citing & Authors
The model was trained by Rolv-Arild Braaten and Per Egil Kummervold. Documentation written by Javier de la Rosa, Rov-Arild Braaten and Per Egil Kummervold.

32
config.json Normal file
View File

@@ -0,0 +1,32 @@
{
"_name_or_path": "NbAiLab/nb-bert-base",
"architectures": [
"BertModel"
],
"attention_probs_dropout_prob": 0.1,
"classifier_dropout": null,
"directionality": "bidi",
"gradient_checkpointing": false,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 768,
"initializer_range": 0.02,
"intermediate_size": 3072,
"layer_norm_eps": 1e-12,
"max_position_embeddings": 512,
"model_type": "bert",
"num_attention_heads": 12,
"num_hidden_layers": 12,
"pad_token_id": 0,
"pooler_fc_size": 768,
"pooler_num_attention_heads": 12,
"pooler_num_fc_layers": 3,
"pooler_size_per_head": 128,
"pooler_type": "first_token_transform",
"position_embedding_type": "absolute",
"torch_dtype": "float32",
"transformers_version": "4.20.0",
"type_vocab_size": 2,
"use_cache": true,
"vocab_size": 119547
}

View File

@@ -0,0 +1,7 @@
{
"__version__": {
"sentence_transformers": "2.2.2",
"transformers": "4.20.0",
"pytorch": "1.11.0+cu113"
}
}

View File

@@ -0,0 +1,12 @@
epoch,steps,cosine_pearson,cosine_spearman,euclidean_pearson,euclidean_spearman,manhattan_pearson,manhattan_spearman,dot_pearson,dot_spearman
0,1647,0.823375587514228,0.8265825704334538,0.8273972164909515,0.8292214672723786,0.8268840482871875,0.8283000360315067,0.8022955514671963,0.8006369955886341
0,3294,0.8318956230225233,0.8340475951581009,0.8349323944058863,0.835464435993529,0.8345877379588028,0.834995629472717,0.8103564960851812,0.8066427524023101
0,4941,0.828103495909552,0.831719114903505,0.8292618436661121,0.8318879068349369,0.8289486077904916,0.8315578247566098,0.8080373423735806,0.8069119429795405
0,6588,0.8256351268185107,0.8299253289680852,0.8287684115268311,0.830869096523304,0.8290049577465493,0.8311304472374726,0.8009607820136238,0.7986429675221425
0,8235,0.8235746706117698,0.8264818265916876,0.826661103205969,0.8295939052518218,0.8265861275744042,0.8294574834494906,0.7993636707446155,0.7966738647397561
0,9882,0.8333018947237008,0.8353349127724712,0.8336345633856252,0.8368681832577541,0.8340102013902385,0.8374881829417399,0.8086112939096168,0.8058078425734608
0,11529,0.833741268658269,0.8363397426474096,0.8301428078897971,0.8335788510623737,0.8300640056874611,0.8334126079162164,0.813440371609937,0.8114798251354435
0,13176,0.8303966197645557,0.8327355088433634,0.830594023192226,0.8340560293276226,0.8304148121939335,0.8338880327704051,0.8058855480985928,0.8035656381791165
0,14823,0.834921212698747,0.8369408800729372,0.833990310947668,0.8374576330985931,0.8337706796275132,0.8372014318541703,0.8136963551460357,0.8112018516340024
0,16470,0.8382865802719606,0.8397542610180401,0.8352120088313325,0.8390880986366062,0.8350079932947272,0.8387915325956439,0.8176968683400706,0.8152200305635741
0,-1,0.8382865920818949,0.8397542610180401,0.835212018493872,0.8390880986366062,0.8350080052606882,0.8387915325956439,0.8176968836585897,0.8152200305635741
1 epoch steps cosine_pearson cosine_spearman euclidean_pearson euclidean_spearman manhattan_pearson manhattan_spearman dot_pearson dot_spearman
2 0 1647 0.823375587514228 0.8265825704334538 0.8273972164909515 0.8292214672723786 0.8268840482871875 0.8283000360315067 0.8022955514671963 0.8006369955886341
3 0 3294 0.8318956230225233 0.8340475951581009 0.8349323944058863 0.835464435993529 0.8345877379588028 0.834995629472717 0.8103564960851812 0.8066427524023101
4 0 4941 0.828103495909552 0.831719114903505 0.8292618436661121 0.8318879068349369 0.8289486077904916 0.8315578247566098 0.8080373423735806 0.8069119429795405
5 0 6588 0.8256351268185107 0.8299253289680852 0.8287684115268311 0.830869096523304 0.8290049577465493 0.8311304472374726 0.8009607820136238 0.7986429675221425
6 0 8235 0.8235746706117698 0.8264818265916876 0.826661103205969 0.8295939052518218 0.8265861275744042 0.8294574834494906 0.7993636707446155 0.7966738647397561
7 0 9882 0.8333018947237008 0.8353349127724712 0.8336345633856252 0.8368681832577541 0.8340102013902385 0.8374881829417399 0.8086112939096168 0.8058078425734608
8 0 11529 0.833741268658269 0.8363397426474096 0.8301428078897971 0.8335788510623737 0.8300640056874611 0.8334126079162164 0.813440371609937 0.8114798251354435
9 0 13176 0.8303966197645557 0.8327355088433634 0.830594023192226 0.8340560293276226 0.8304148121939335 0.8338880327704051 0.8058855480985928 0.8035656381791165
10 0 14823 0.834921212698747 0.8369408800729372 0.833990310947668 0.8374576330985931 0.8337706796275132 0.8372014318541703 0.8136963551460357 0.8112018516340024
11 0 16470 0.8382865802719606 0.8397542610180401 0.8352120088313325 0.8390880986366062 0.8350079932947272 0.8387915325956439 0.8176968683400706 0.8152200305635741
12 0 -1 0.8382865920818949 0.8397542610180401 0.835212018493872 0.8390880986366062 0.8350080052606882 0.8387915325956439 0.8176968836585897 0.8152200305635741

3
model.safetensors Normal file
View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:542120491898049bda60a6e83ef8ff17e1e9366f3562a5bfc1715efaacdb9668
size 711440320

14
modules.json Normal file
View File

@@ -0,0 +1,14 @@
[
{
"idx": 0,
"name": "0",
"path": "",
"type": "sentence_transformers.models.Transformer"
},
{
"idx": 1,
"name": "1",
"path": "1_Pooling",
"type": "sentence_transformers.models.Pooling"
}
]

3
pytorch_model.bin Normal file
View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:e6ff8649a20f3d7379d927298b15d08ec88a2c7dc76a5948a3ff4cffe2842f12
size 711483185

View File

@@ -0,0 +1,4 @@
{
"max_seq_length": 75,
"do_lower_case": false
}

View File

@@ -0,0 +1,2 @@
epoch,steps,cosine_pearson,cosine_spearman,euclidean_pearson,euclidean_spearman,manhattan_pearson,manhattan_spearman,dot_pearson,dot_spearman
-1,-1,0.8275085075461329,0.82454653044575,0.8189631397786282,0.8179737003889682,0.8192793931752765,0.8181757117136191,0.8038510401130279,0.7950975162189595
1 epoch steps cosine_pearson cosine_spearman euclidean_pearson euclidean_spearman manhattan_pearson manhattan_spearman dot_pearson dot_spearman
2 -1 -1 0.8275085075461329 0.82454653044575 0.8189631397786282 0.8179737003889682 0.8192793931752765 0.8181757117136191 0.8038510401130279 0.7950975162189595

7
special_tokens_map.json Normal file
View File

@@ -0,0 +1,7 @@
{
"cls_token": "[CLS]",
"mask_token": "[MASK]",
"pad_token": "[PAD]",
"sep_token": "[SEP]",
"unk_token": "[UNK]"
}

119709
tokenizer.json Normal file

File diff suppressed because it is too large Load Diff

15
tokenizer_config.json Normal file
View File

@@ -0,0 +1,15 @@
{
"cls_token": "[CLS]",
"do_basic_tokenize": true,
"do_lower_case": false,
"mask_token": "[MASK]",
"name_or_path": "NbAiLab/nb-bert-base",
"never_split": null,
"pad_token": "[PAD]",
"sep_token": "[SEP]",
"special_tokens_map_file": null,
"strip_accents": null,
"tokenize_chinese_chars": true,
"tokenizer_class": "BertTokenizer",
"unk_token": "[UNK]"
}

119547
vocab.txt Normal file

File diff suppressed because it is too large Load Diff