m3hrdadfi commited on
Commit
3b86867
1 Parent(s): 9864857

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,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 `albert-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
+ pip install -U sentencepiece
18
+ ```
19
+
20
+ Then you can use the model like this:
21
+
22
+ ```python
23
+ from sentence_transformers import SentenceTransformer
24
+
25
+
26
+ sentences = [
27
+ 'اولین حکمران شهر بابل کی بود؟',
28
+ 'در فصل زمستان چه اتفاقی افتاد؟',
29
+ 'میراث کوروش'
30
+ ]
31
+ model = SentenceTransformer('m3hrdadfi/albert-zwnj-wnli-mean-tokens')
32
+ embeddings = model.encode(sentences)
33
+ print(embeddings)
34
+ ```
35
+
36
+ ## Usage (HuggingFace Transformers)
37
+ 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.
38
+
39
+ ```python
40
+ from transformers import AutoTokenizer, AutoModel
41
+ import torch
42
+
43
+
44
+ # Max Pooling - Take the max value over time for every dimension.
45
+ def max_pooling(model_output, attention_mask):
46
+ token_embeddings = model_output[0] #First element of model_output contains all token embeddings
47
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
48
+ token_embeddings[input_mask_expanded == 0] = -1e9 # Set padding tokens to large negative value
49
+ return torch.mean(token_embeddings, 1)[0]
50
+
51
+ # Sentences we want sentence embeddings for
52
+ sentences = [
53
+ 'اولین حکمران شهر بابل کی بود؟',
54
+ 'در فصل زمستان چه اتفاقی افتاد؟',
55
+ 'میراث کوروش'
56
+ ]
57
+
58
+ # Load model from HuggingFace Hub
59
+ tokenizer = AutoTokenizer.from_pretrained('m3hrdadfi/albert-zwnj-wnli-mean-tokens')
60
+ model = AutoModel.from_pretrained('m3hrdadfi/albert-zwnj-wnli-mean-tokens')
61
+
62
+ # Tokenize sentences
63
+ encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
64
+ # Compute token embeddings
65
+ with torch.no_grad():
66
+ model_output = model(**encoded_input)
67
+ # Perform pooling. In this case, max pooling.
68
+ sentence_embeddings = max_pooling(model_output, encoded_input['attention_mask'])
69
+
70
+ print("Sentence embeddings:")
71
+ print(sentence_embeddings)
72
+ ```
73
+
74
+ ## Questions?
75
+ 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
  }