thehosy commited on
Commit
6a2e33c
·
verified ·
1 Parent(s): b459b79

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +126 -1
README.md CHANGED
@@ -1,5 +1,130 @@
1
  ---
2
  license: apache-2.0
 
 
 
 
 
 
 
3
  language:
4
  - vi
5
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
+ pipeline_tag: sentence-similarity
4
+ tags:
5
+ - sentence-transformers
6
+ - feature-extraction
7
+ - sentence-similarity
8
+ - transformers
9
+ library_name: generic
10
  language:
11
  - vi
12
+ ---
13
+
14
+ # thehosy/roberta-base-qa-vietnamese
15
+
16
+ This is a encoder model: It can encodes sentences or paragraphs (maximum 768 tokens) to dense vectors with 760 dimensions. It used for QA semantic search.
17
+
18
+ Datasets:
19
+ - MS Macro (translated into Vietnamese)
20
+ - SQuAD v2 (translated into Vietnamese)
21
+ - ViQuad2.0
22
+ - ZaloQA 2019
23
+
24
+ Roberta-base architecture is used as backbone (Training from scratch).
25
+
26
+ <!--- Describe your model here -->
27
+
28
+ ## Usage (Sentence-Transformers)
29
+
30
+ Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:
31
+
32
+ ```
33
+ pip install -U sentence-transformers
34
+ ```
35
+
36
+ Then you can use the model like this:
37
+
38
+ ```python
39
+ from sentence_transformers import SentenceTransformer
40
+ from torch.nn import functional as F
41
+
42
+
43
+ sentences = ["Mỗi hiệp bóng đá kéo dài bao lâu",
44
+ "Một trận đấu bóng đá thông thường có hai hiệp , mỗi hiệp 45 phút với khoảng thời gian 15 phút nghỉ giữa hai hiệp .",
45
+ "Cũng trong thập niên 1850 , các đội bóng nghiệp dư bắt đầu được thành lập và thường mỗi đội xây dựng cho riêng họ những luật chơi mới của môn bóng đá , trong đó đáng chú ý có câu lạc bộ Sheffield F.C .. Việc mỗi đội bóng có luật chơi khác nhau khiến việc điều hành mỗi trận đấu giữa họ diễn ra rất khó khăn ."]
46
+
47
+ model = SentenceTransformer('thehosy/roberta-base-qa-vietnamese')
48
+ model.eval()
49
+
50
+ embeddings = model.encode(sentences, convert_to_tensor=True)
51
+ vecs = F.normalize(embeddings)
52
+ sim_scores = F.cosine_similarity(vecs[:1], vecs[1:])
53
+ print(sim_scores)
54
+
55
+ # tensor([0.9971, 0.3511])
56
+ ```
57
+
58
+ ## Usage (HuggingFace Transformers)
59
+
60
+ You can alse use the model with transformers by applying the pooling (mean pooling) on-top of the contextualized word embeddings.
61
+
62
+ ```python
63
+ from transformers import AutoTokenizer, AutoModel
64
+ import torch
65
+ from torch.nn import functional as F
66
+
67
+
68
+ #Mean Pooling - Take attention mask into account for correct averaging
69
+ def mean_pooling(model_output, attention_mask):
70
+ token_embeddings = model_output.last_hidden_state
71
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
72
+ return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
73
+
74
+
75
+ sentences = ["Mỗi hiệp bóng đá kéo dài bao lâu",
76
+ "Một trận đấu bóng đá thông thường có hai hiệp , mỗi hiệp 45 phút với khoảng thời gian 15 phút nghỉ giữa hai hiệp .",
77
+ "Cũng trong thập niên 1850 , các đội bóng nghiệp dư bắt đầu được thành lập và thường mỗi đội xây dựng cho riêng họ những luật chơi mới của môn bóng đá , trong đó đáng chú ý có câu lạc bộ Sheffield F.C .. Việc mỗi đội bóng có luật chơi khác nhau khiến việc điều hành mỗi trận đấu giữa họ diễn ra rất khó khăn ."]
78
+
79
+ # Load model from HuggingFace Hub
80
+ tokenizer = AutoTokenizer.from_pretrained('thehosy/roberta-base-qa-vietnamese')
81
+ model = AutoModel.from_pretrained('thehosy/roberta-base-qa-vietnamese')
82
+ model.eval()
83
+
84
+ # Tokenize sentences
85
+ encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
86
+ # Compute token embeddings
87
+ with torch.no_grad():
88
+ model_output = model(**encoded_input)
89
+
90
+ embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
91
+ vecs = F.normalize(embeddings)
92
+ sim_scores = F.cosine_similarity(vecs[:1], vecs[1:])
93
+ print(sim_scores)
94
+
95
+ # tensor([0.9971, 0.3511])
96
+ ```
97
+
98
+ ## Training
99
+
100
+ The model was trained with the parameters:
101
+
102
+ **DataLoader**:
103
+
104
+ `torch.utils.data.dataloader.DataLoader` of length 683064 with parameters:
105
+
106
+ ```json
107
+ {'batch_size': 16, 'sampler': None, 'batch_sampler': None, 'shuffle': true}
108
+ ```
109
+
110
+ **Loss**:
111
+
112
+ `contrastive loss` with `cosine distance` and `euclide distance`:
113
+
114
+ **Training Parameters**
115
+ - epochs: 6
116
+ - optimizer: AdamW
117
+ - learning_rate: 2e-05
118
+ - scheduler: Warmup Linear Scheduler
119
+ - warmup_steps: 10000
120
+ - weight_decay": 0.001
121
+
122
+
123
+ ## Full Model Architecture
124
+
125
+ ```
126
+ SentenceTransformer(
127
+ (0): Transformer({'max_seq_length': 768, 'do_lower_case': False}) with Transformer model: RobertaModel
128
+ (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, 'pooling_mode_weightedmean_tokens': False, 'pooling_mode_lasttoken': False})
129
+ )
130
+ ```