m3hrdadfi commited on
Commit
36f912a
1 Parent(s): 513241c

Add st hub-features

Browse files
1_Pooling/config.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
1
+ {
2
+ "word_embedding_dimension": 768,
3
+ "pooling_mode_cls_token": false,
4
+ "pooling_mode_mean_tokens": true,
5
+ "pooling_mode_max_tokens": false,
6
+ "pooling_mode_mean_sqrt_len_tokens": false
7
+ }
README.md ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ pipeline_tag: feature-extraction
3
+ tags:
4
+ - sentence-transformers
5
+ - feature-extraction
6
+ - sentence-similarity
7
+ - transformers
8
+ ---
9
+
10
+ # Sentence Embeddings with `roberta-zwnj-wnli-mean-tokens`
11
+
12
+ ## Usage (Sentence-Transformers)
13
+ Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:
14
+
15
+ ```
16
+ pip install -U sentence-transformers
17
+ ```
18
+
19
+ Then you can use the model like this:
20
+
21
+ ```python
22
+ from sentence_transformers import SentenceTransformer
23
+
24
+
25
+ sentences = [
26
+ 'اولین حکمران شهر بابل کی بود؟',
27
+ 'در فصل زمستان چه اتفاقی افتاد؟',
28
+ 'میراث کوروش'
29
+ ]
30
+ model = SentenceTransformer('m3hrdadfi/roberta-zwnj-wnli-mean-tokens')
31
+ embeddings = model.encode(sentences)
32
+ print(embeddings)
33
+ ```
34
+
35
+ ## Usage (HuggingFace Transformers)
36
+ Without [sentence-transformers](https://www.SBERT.net), you can use the model like this: First, you pass your input through the transformer model, then you have to apply the right pooling-operation on-top of the contextualized word embeddings.
37
+
38
+ ```python
39
+ from transformers import AutoTokenizer, AutoModel
40
+ import torch
41
+
42
+
43
+ # Max Pooling - Take the max value over time for every dimension.
44
+ def max_pooling(model_output, attention_mask):
45
+ token_embeddings = model_output[0] #First element of model_output contains all token embeddings
46
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
47
+ token_embeddings[input_mask_expanded == 0] = -1e9 # Set padding tokens to large negative value
48
+ return torch.mean(token_embeddings, 1)[0]
49
+
50
+ # Sentences we want sentence embeddings for
51
+ sentences = [
52
+ 'اولین حکمران شهر بابل کی بود؟',
53
+ 'در فصل زمستان چه اتفاقی افتاد؟',
54
+ 'میراث کوروش'
55
+ ]
56
+
57
+ # Load model from HuggingFace Hub
58
+ tokenizer = AutoTokenizer.from_pretrained('m3hrdadfi/roberta-zwnj-wnli-mean-tokens')
59
+ model = AutoModel.from_pretrained('m3hrdadfi/roberta-zwnj-wnli-mean-tokens')
60
+
61
+ # Tokenize sentences
62
+ encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
63
+ # Compute token embeddings
64
+ with torch.no_grad():
65
+ model_output = model(**encoded_input)
66
+ # Perform pooling. In this case, max pooling.
67
+ sentence_embeddings = max_pooling(model_output, encoded_input['attention_mask'])
68
+
69
+ print("Sentence embeddings:")
70
+ print(sentence_embeddings)
71
+ ```
72
+
73
+ ## Questions?
74
+ Post a Github issue from [HERE](https://github.com/m3hrdadfi/sentence-transformers).
config_sentence_transformers.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
1
+ {
2
+ "__version__": {
3
+ "sentence_transformers": "2.0.0",
4
+ "transformers": "4.7.0",
5
+ "pytorch": "1.9.0+cu102"
6
+ }
7
+ }
modules.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "idx": 0,
4
+ "name": "0",
5
+ "path": "",
6
+ "type": "sentence_transformers.models.Transformer"
7
+ },
8
+ {
9
+ "idx": 1,
10
+ "name": "1",
11
+ "path": "1_Pooling",
12
+ "type": "sentence_transformers.models.Pooling"
13
+ }
14
+ ]
sentence_bert_config.json CHANGED
@@ -1,4 +1,4 @@
1
  {
2
- "max_seq_length": null,
3
  "do_lower_case": false
4
  }
1
  {
2
+ "max_seq_length": 128,
3
  "do_lower_case": false
4
  }