Update Readme
Browse files
README.md
CHANGED
|
@@ -51,7 +51,6 @@ model-index:
|
|
| 51 |
- **Sparse retrieval**: Generates interpretable sparse vectors for efficient search
|
| 52 |
- **Lightweight**: Only 14M parameters for fast inference - even on CPU.
|
| 53 |
- **Ready-to-use**: Compatible with the `light-splade` package and `transformers` package.
|
| 54 |
-
|
| 55 |
## Model Details
|
| 56 |
|
| 57 |
### Architecture
|
|
@@ -67,6 +66,7 @@ This model is based on a compact BERT architecture optimized for Japanese text p
|
|
| 67 |
- **Max Sequence Length**: 2048 tokens
|
| 68 |
- **Vocabulary Size**: 32,768 tokens (same as [tohoku-nlp/bert-base-japanese-v3](https://huggingface.co/tohoku-nlp/bert-base-japanese-v3))
|
| 69 |
|
|
|
|
| 70 |
### Tokenization
|
| 71 |
|
| 72 |
- **Tokenizer**: `BertJapaneseTokenizer` from Hugging Face Transformers
|
|
@@ -127,8 +127,11 @@ from light_splade.models.splade import SpladeEncoder
|
|
| 127 |
encoder = SpladeEncoder(model_path="bizreach-inc/light-splade-japanese-14M")
|
| 128 |
|
| 129 |
# Tokenize input text
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
# Generate sparse representation
|
| 134 |
with torch.inference_mode():
|
|
@@ -138,20 +141,62 @@ with torch.inference_mode():
|
|
| 138 |
)
|
| 139 |
|
| 140 |
print(sparse_vecs[0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
```
|
| 142 |
|
| 143 |
**Example Output:**
|
| 144 |
|
| 145 |
```python
|
| 146 |
-
{
|
| 147 |
-
|
| 148 |
-
'江戸': 1.51, '都市': 1.42, '駐日': 1.37, 'ニッポン': 1.2,
|
| 149 |
-
'北京': 0.93, '所在': 0.91, '大阪': 0.78, 'ロンドン': 0.68,
|
| 150 |
-
'天皇': 0.6, 'ソウル': 0.54, '京都': 0.53, '明治': 0.52,
|
| 151 |
-
'札幌': 0.5, '省': 0.48, 'ジャ': 0.45, 'シティ': 0.42,
|
| 152 |
-
'ステート': 0.42, '市': 0.41, '国': 0.39
|
| 153 |
-
# ... additional terms
|
| 154 |
-
}
|
| 155 |
```
|
| 156 |
|
| 157 |
|
|
@@ -173,7 +218,7 @@ print(sparse_vecs[0])
|
|
| 173 |
If you use this model in your research, please cite:
|
| 174 |
|
| 175 |
```bibtex
|
| 176 |
-
@misc{light-splade-japanese-
|
| 177 |
title={Light SPLADE Japanese 14M},
|
| 178 |
author={Bizreach Inc.},
|
| 179 |
year={2025},
|
|
|
|
| 51 |
- **Sparse retrieval**: Generates interpretable sparse vectors for efficient search
|
| 52 |
- **Lightweight**: Only 14M parameters for fast inference - even on CPU.
|
| 53 |
- **Ready-to-use**: Compatible with the `light-splade` package and `transformers` package.
|
|
|
|
| 54 |
## Model Details
|
| 55 |
|
| 56 |
### Architecture
|
|
|
|
| 66 |
- **Max Sequence Length**: 2048 tokens
|
| 67 |
- **Vocabulary Size**: 32,768 tokens (same as [tohoku-nlp/bert-base-japanese-v3](https://huggingface.co/tohoku-nlp/bert-base-japanese-v3))
|
| 68 |
|
| 69 |
+
|
| 70 |
### Tokenization
|
| 71 |
|
| 72 |
- **Tokenizer**: `BertJapaneseTokenizer` from Hugging Face Transformers
|
|
|
|
| 127 |
encoder = SpladeEncoder(model_path="bizreach-inc/light-splade-japanese-14M")
|
| 128 |
|
| 129 |
# Tokenize input text
|
| 130 |
+
corpus = [
|
| 131 |
+
"日本の首都は東京です。",
|
| 132 |
+
"大阪万博は2025年に開催されます。"
|
| 133 |
+
]
|
| 134 |
+
token_outputs = encoder.tokenizer(corpus, padding=True, return_tensors="pt")
|
| 135 |
|
| 136 |
# Generate sparse representation
|
| 137 |
with torch.inference_mode():
|
|
|
|
| 141 |
)
|
| 142 |
|
| 143 |
print(sparse_vecs[0])
|
| 144 |
+
print(sparse_vecs[1])
|
| 145 |
+
```
|
| 146 |
+
|
| 147 |
+
### Usage Example with `transformers` package only
|
| 148 |
+
|
| 149 |
+
```python
|
| 150 |
+
import torch
|
| 151 |
+
from transformers import AutoTokenizer, AutoModelForMaskedLM
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def dense_to_sparse(dense: torch.tensor, idx2token: dict[int, str]) -> list[dict[str, float]]:
|
| 155 |
+
rows, cols = dense.nonzero(as_tuple=True)
|
| 156 |
+
rows = rows.tolist()
|
| 157 |
+
cols = cols.tolist()
|
| 158 |
+
weights = dense[rows, cols].tolist()
|
| 159 |
+
|
| 160 |
+
sparse_vecs = [{} for _ in range(dense.size(0))]
|
| 161 |
+
for row, col, weight in zip(rows, cols, weights):
|
| 162 |
+
sparse_vecs[row][idx2token[col]] = round(weight, 2)
|
| 163 |
+
|
| 164 |
+
for i in range(len(sparse_vecs)):
|
| 165 |
+
sparse_vecs[i] = dict(sorted(sparse_vecs[i].items(), key=lambda x: x[1], reverse=True))
|
| 166 |
+
return sparse_vecs
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
MODEL_PATH = "bizreach-inc/light-splade-japanese-14M"
|
| 170 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 171 |
+
transformer = AutoModelForMaskedLM.from_pretrained(MODEL_PATH).to(device)
|
| 172 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 173 |
+
idx2token = {idx: token for token, idx in tokenizer.get_vocab().items()}
|
| 174 |
+
|
| 175 |
+
corpus = [
|
| 176 |
+
"日本の首都は東京です。",
|
| 177 |
+
"大阪万博は2025年に開催されます。"
|
| 178 |
+
]
|
| 179 |
+
token_outputs = tokenizer(corpus, padding=True, return_tensors="pt")
|
| 180 |
+
attention_mask = token_outputs["attention_mask"].to(device)
|
| 181 |
+
token_outputs = {key: value.to(device) for key, value in token_outputs.items()}
|
| 182 |
+
|
| 183 |
+
with torch.inference_mode():
|
| 184 |
+
outputs = transformer(**token_outputs)
|
| 185 |
+
dense, _ = torch.max(
|
| 186 |
+
torch.log(1 + torch.relu(outputs.logits)) * attention_mask.unsqueeze(-1),
|
| 187 |
+
dim=1,
|
| 188 |
+
)
|
| 189 |
+
sparse_vecs = dense_to_sparse(dense, idx2token)
|
| 190 |
+
|
| 191 |
+
print(sparse_vecs[0])
|
| 192 |
+
print(sparse_vecs[1])
|
| 193 |
```
|
| 194 |
|
| 195 |
**Example Output:**
|
| 196 |
|
| 197 |
```python
|
| 198 |
+
{'首都': 1.83, '日本': 1.71, '東京': 1.64, '富士': 1.07, '地名': 0.91, '都市': 0.8, '##都': 0.77, '領土': 0.73, 'ゼロ': 0.72, 'シティ': 0.71, '位置': 0.58, '大国': 0.58, '本社': 0.55, 'とうきょう': 0.54, '中心': 0.53, '外来': 0.45, '京都': 0.41...}
|
| 199 |
+
{'202': 1.61, '大阪': 1.5, '##5': 1.33, '開催': 1.15, '万博': 1.11, '東京': 1.07, '関西': 1.06, '神戸': 0.99, '京都': 0.96, '年': 0.91, 'まつり': 0.91, '203': 0.9, '月': 0.89, '日程': 0.84, '207': 0.67, 'オリンピック': 0.57, '主催': 0.57...}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
```
|
| 201 |
|
| 202 |
|
|
|
|
| 218 |
If you use this model in your research, please cite:
|
| 219 |
|
| 220 |
```bibtex
|
| 221 |
+
@misc{light-splade-japanese-14M,
|
| 222 |
title={Light SPLADE Japanese 14M},
|
| 223 |
author={Bizreach Inc.},
|
| 224 |
year={2025},
|