dungeon29 commited on
Commit
b68be21
·
verified ·
1 Parent(s): 8b38011

Upload custom DeBERTa-LSTM-Attention model

Browse files
config.json ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "attention_probs_dropout_prob": 0.1,
3
+ "auto_map": {
4
+ "AutoModel": "model.DeBERTaLSTMClassifier",
5
+ "AutoModelForSequenceClassification": "model.DeBERTaLSTMClassifier"
6
+ },
7
+ "hidden_act": "gelu",
8
+ "hidden_dropout_prob": 0.1,
9
+ "hidden_size": 768,
10
+ "initializer_range": 0.02,
11
+ "intermediate_size": 3072,
12
+ "layer_norm_eps": 1e-07,
13
+ "legacy": true,
14
+ "max_position_embeddings": 512,
15
+ "max_relative_positions": -1,
16
+ "model_type": "deberta",
17
+ "num_attention_heads": 12,
18
+ "num_hidden_layers": 12,
19
+ "pad_token_id": 0,
20
+ "pooler_dropout": 0,
21
+ "pooler_hidden_act": "gelu",
22
+ "pooler_hidden_size": 768,
23
+ "pos_att_type": [
24
+ "c2p",
25
+ "p2c"
26
+ ],
27
+ "position_biased_input": false,
28
+ "relative_attention": true,
29
+ "transformers_version": "4.57.2",
30
+ "type_vocab_size": 0,
31
+ "vocab_size": 50265
32
+ }
merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
model.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ from transformers import AutoModel
5
+
6
+ class DeBERTaLSTMClassifier(nn.Module):
7
+ def __init__(self, hidden_dim=128, num_labels=2):
8
+ super().__init__()
9
+
10
+ self.deberta = AutoModel.from_pretrained("microsoft/deberta-base")
11
+ for param in self.deberta.parameters():
12
+ param.requires_grad = False # freeze DeBERTa (as we don't have enough resources, we will not train DeBERTa in this model)
13
+
14
+ self.lstm = nn.LSTM(
15
+ input_size=self.deberta.config.hidden_size,
16
+ hidden_size=hidden_dim,
17
+ batch_first=True,
18
+ bidirectional=True
19
+ )
20
+
21
+ self.fc = nn.Linear(hidden_dim * 2, num_labels)
22
+
23
+ # Attention layer để tính token importance
24
+ self.attention = nn.Linear(hidden_dim * 2, 1)
25
+
26
+ def forward(self, input_ids, attention_mask, return_attention=False):
27
+ with torch.no_grad():
28
+ outputs = self.deberta(input_ids=input_ids, attention_mask=attention_mask, output_attentions=True)
29
+
30
+ lstm_out, _ = self.lstm(outputs.last_hidden_state) # shape: [batch, seq_len, hidden*2]
31
+
32
+ if return_attention:
33
+ # Tính attention weights cho từng token
34
+ attention_weights = self.attention(lstm_out) # [batch, seq_len, 1]
35
+ attention_weights = F.softmax(attention_weights.squeeze(-1), dim=-1) # [batch, seq_len]
36
+
37
+ # Apply attention mask
38
+ attention_weights = attention_weights * attention_mask.float()
39
+ attention_weights = attention_weights / (attention_weights.sum(dim=-1, keepdim=True) + 1e-8)
40
+
41
+ # Weighted sum of LSTM outputs
42
+ attended_output = torch.sum(lstm_out * attention_weights.unsqueeze(-1), dim=1)
43
+ logits = self.fc(attended_output)
44
+
45
+ return logits, attention_weights, outputs.attentions
46
+ else:
47
+ final_hidden = lstm_out[:, -1, :] # last token output
48
+ logits = self.fc(final_hidden)
49
+ return logits
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ea43b73d419c4314ca74e4d921e6b4bfefa6ed4c6f5805f35720b5b26a365035
3
+ size 558172963
special_tokens_map.json ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "[CLS]",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "cls_token": {
10
+ "content": "[CLS]",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "eos_token": {
17
+ "content": "[SEP]",
18
+ "lstrip": false,
19
+ "normalized": false,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ },
23
+ "mask_token": {
24
+ "content": "[MASK]",
25
+ "lstrip": true,
26
+ "normalized": true,
27
+ "rstrip": false,
28
+ "single_word": false
29
+ },
30
+ "pad_token": {
31
+ "content": "[PAD]",
32
+ "lstrip": false,
33
+ "normalized": false,
34
+ "rstrip": false,
35
+ "single_word": false
36
+ },
37
+ "sep_token": {
38
+ "content": "[SEP]",
39
+ "lstrip": false,
40
+ "normalized": false,
41
+ "rstrip": false,
42
+ "single_word": false
43
+ },
44
+ "unk_token": {
45
+ "content": "[UNK]",
46
+ "lstrip": false,
47
+ "normalized": false,
48
+ "rstrip": false,
49
+ "single_word": false
50
+ }
51
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": false,
3
+ "add_prefix_space": false,
4
+ "added_tokens_decoder": {
5
+ "0": {
6
+ "content": "[PAD]",
7
+ "lstrip": false,
8
+ "normalized": false,
9
+ "rstrip": false,
10
+ "single_word": false,
11
+ "special": true
12
+ },
13
+ "1": {
14
+ "content": "[CLS]",
15
+ "lstrip": false,
16
+ "normalized": false,
17
+ "rstrip": false,
18
+ "single_word": false,
19
+ "special": true
20
+ },
21
+ "2": {
22
+ "content": "[SEP]",
23
+ "lstrip": false,
24
+ "normalized": false,
25
+ "rstrip": false,
26
+ "single_word": false,
27
+ "special": true
28
+ },
29
+ "3": {
30
+ "content": "[UNK]",
31
+ "lstrip": false,
32
+ "normalized": false,
33
+ "rstrip": false,
34
+ "single_word": false,
35
+ "special": true
36
+ },
37
+ "50264": {
38
+ "content": "[MASK]",
39
+ "lstrip": true,
40
+ "normalized": true,
41
+ "rstrip": false,
42
+ "single_word": false,
43
+ "special": true
44
+ }
45
+ },
46
+ "bos_token": "[CLS]",
47
+ "clean_up_tokenization_spaces": false,
48
+ "cls_token": "[CLS]",
49
+ "do_lower_case": false,
50
+ "eos_token": "[SEP]",
51
+ "errors": "replace",
52
+ "extra_special_tokens": {},
53
+ "mask_token": "[MASK]",
54
+ "model_max_length": 1000000000000000019884624838656,
55
+ "pad_token": "[PAD]",
56
+ "sep_token": "[SEP]",
57
+ "tokenizer_class": "DebertaTokenizer",
58
+ "unk_token": "[UNK]",
59
+ "vocab_type": "gpt2"
60
+ }
vocab.json ADDED
The diff for this file is too large to render. See raw diff