arthurbr11 commited on
Commit
0751606
·
1 Parent(s): 1acd452

Add support to Sentence Transformers

Browse files
1_SpladePooling/config.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "pooling_strategy": "max",
3
+ "activation_function": "relu",
4
+ "word_embedding_dimension": null
5
+ }
README.md CHANGED
@@ -10,6 +10,12 @@ tags:
10
  - query-expansion
11
  - document-expansion
12
  - bag-of-words
 
 
 
 
 
 
13
  ---
14
 
15
  # opensearch-neural-sparse-encoding-v1
@@ -37,6 +43,69 @@ This model is trained on MS MARCO dataset.
37
 
38
  OpenSearch neural sparse feature supports learned sparse retrieval with lucene inverted index. Link: https://opensearch.org/docs/latest/query-dsl/specialized/neural-sparse/. The indexing and search can be performed with OpenSearch high-level API.
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  ## Usage (HuggingFace)
41
  This model is supposed to run inside OpenSearch cluster. But you can also use it outside the cluster, with HuggingFace models API.
42
 
 
10
  - query-expansion
11
  - document-expansion
12
  - bag-of-words
13
+ - sentence-transformers
14
+ - sparse-encoder
15
+ - sparse
16
+ - splade
17
+ pipeline_tag: feature-extraction
18
+ library_name: sentence-transformers
19
  ---
20
 
21
  # opensearch-neural-sparse-encoding-v1
 
43
 
44
  OpenSearch neural sparse feature supports learned sparse retrieval with lucene inverted index. Link: https://opensearch.org/docs/latest/query-dsl/specialized/neural-sparse/. The indexing and search can be performed with OpenSearch high-level API.
45
 
46
+ ## Usage (Sentence Transformers)
47
+
48
+ First install the Sentence Transformers library:
49
+
50
+ ```bash
51
+ pip install -U sentence-transformers
52
+ ```
53
+
54
+ Then you can load this model and run inference.
55
+
56
+ ```python
57
+ from sentence_transformers.sparse_encoder import SparseEncoder
58
+
59
+ # Download from the 🤗 Hub
60
+ model = SparseEncoder("opensearch-project/opensearch-neural-sparse-encoding-v1")
61
+
62
+ query = "What's the weather in ny now?"
63
+ document = "Currently New York is rainy."
64
+
65
+ query_embed = model.encode_query(query)
66
+ document_embed = model.encode_document(document)
67
+
68
+ sim = model.similarity(query_embed, document_embed)
69
+ print(f"Similarity: {sim}")
70
+ # Similarity: tensor([[22.3299]])
71
+
72
+ # Visualize top tokens for each text
73
+ top_k = 8
74
+ print(f"\nTop tokens {top_k} for each text:")
75
+
76
+ decoded_query = model.decode(query_embed, top_k=top_k)
77
+ decoded_document = model.decode(document_embed)
78
+
79
+ for i in range(top_k):
80
+ query_token, query_score = decoded_query[i]
81
+ doc_score = next((score for token, score in decoded_document if token == query_token), 0)
82
+ if doc_score != 0:
83
+ print(f"Token: {query_token}, Query score: {query_score:.4f}, Document score: {doc_score:.4f}")
84
+
85
+ # Top tokens 30 for each text:
86
+ # Token: ny, Query score: 2.9262, Document score: 2.1335
87
+ # Token: weather, Query score: 2.5206, Document score: 1.5277
88
+ # Token: york, Query score: 2.0373, Document score: 2.3489
89
+ # Token: cool, Query score: 1.5786, Document score: 0.8752
90
+ # Token: current, Query score: 1.4636, Document score: 1.5132
91
+ # Token: season, Query score: 0.7761, Document score: 0.8860
92
+ # Token: 2020, Query score: 0.7560, Document score: 0.6726
93
+ # Token: summer, Query score: 0.7222, Document score: 0.6292
94
+ # Token: nina, Query score: 0.6888, Document score: 0.6419
95
+ # Token: storm, Query score: 0.6451, Document score: 0.8200
96
+ # Token: brooklyn, Query score: 0.4698, Document score: 0.7635
97
+ # Token: julian, Query score: 0.4562, Document score: 0.1208
98
+ # Token: wow, Query score: 0.3484, Document score: 0.3903
99
+ # Token: usa, Query score: 0.3439, Document score: 0.4160
100
+ # Token: manhattan, Query score: 0.2751, Document score: 0.8260
101
+ # Token: fog, Query score: 0.2013, Document score: 0.7735
102
+ # Token: mood, Query score: 0.1989, Document score: 0.2961
103
+ # Token: climate, Query score: 0.1653, Document score: 0.3437
104
+ # Token: nature, Query score: 0.1191, Document score: 0.1533
105
+ # Token: temperature, Query score: 0.0665, Document score: 0.0599
106
+ # Token: windy, Query score: 0.0552, Document score: 0.3396
107
+ ```
108
+
109
  ## Usage (HuggingFace)
110
  This model is supposed to run inside OpenSearch cluster. But you can also use it outside the cluster, with HuggingFace models API.
111
 
config_sentence_transformers.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "SparseEncoder",
3
+ "__version__": {
4
+ "sentence_transformers": "5.0.0",
5
+ "transformers": "4.50.3",
6
+ "pytorch": "2.6.0+cu124"
7
+ },
8
+ "prompts": {
9
+ "query": "",
10
+ "document": ""
11
+ },
12
+ "default_prompt_name": null,
13
+ "similarity_fn_name": "dot"
14
+ }
modules.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "idx": 0,
4
+ "name": "0",
5
+ "path": "",
6
+ "type": "sentence_transformers.sparse_encoder.models.MLMTransformer"
7
+ },
8
+ {
9
+ "idx": 1,
10
+ "name": "1",
11
+ "path": "1_SpladePooling",
12
+ "type": "sentence_transformers.sparse_encoder.models.SpladePooling"
13
+ }
14
+ ]
sentence_bert_config.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "max_seq_length": 512,
3
+ "do_lower_case": false
4
+ }