added custom config
Browse files- config.json +1 -0
- config_TunBERT.py +44 -0
- modeling_tunbert.py +9 -5
config.json
CHANGED
@@ -4,6 +4,7 @@
|
|
4 |
"BertForSequenceClassification"
|
5 |
],
|
6 |
"auto_map": {
|
|
|
7 |
"AutoModelForSequenceClassification": "modeling_tunbert.TunBERT"
|
8 |
},
|
9 |
"attention_probs_dropout_prob": 0.1,
|
|
|
4 |
"BertForSequenceClassification"
|
5 |
],
|
6 |
"auto_map": {
|
7 |
+
"AutoConfig": "config_tunbert.TunBertConfig",
|
8 |
"AutoModelForSequenceClassification": "modeling_tunbert.TunBERT"
|
9 |
},
|
10 |
"attention_probs_dropout_prob": 0.1,
|
config_TunBERT.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import PretrainedConfig
|
2 |
+
class TunBertConfig(PretrainedConfig):
|
3 |
+
model_type = "bert"
|
4 |
+
def __init__(self,
|
5 |
+
attention_probs_dropout_prob = 0.1,
|
6 |
+
classifier_dropout = None,
|
7 |
+
gradient_checkpointing = False,
|
8 |
+
hidden_act = "gelu",
|
9 |
+
hidden_dropout_prob = 0.1,
|
10 |
+
hidden_size = 768,
|
11 |
+
initializer_range = 0.02,
|
12 |
+
intermediate_size = 3072,
|
13 |
+
layer_norm_eps = 1e-12,
|
14 |
+
max_position_embeddings = 512,
|
15 |
+
model_type = "bert",
|
16 |
+
num_attention_heads = 12,
|
17 |
+
num_hidden_layers = 12,
|
18 |
+
pad_token_id = 0,
|
19 |
+
position_embedding_type = "absolute",
|
20 |
+
transformers_version = "4.35.2",
|
21 |
+
type_vocab_size = 2,
|
22 |
+
use_cache = True,
|
23 |
+
vocab_size = 30522,
|
24 |
+
**kwargs):
|
25 |
+
self.attention_probs_dropout_prob = attention_probs_dropout_prob
|
26 |
+
self.classifier_dropout = classifier_dropout
|
27 |
+
self.gradient_checkpointing = gradient_checkpointing
|
28 |
+
self.hidden_act = hidden_act
|
29 |
+
self.hidden_dropout_prob = hidden_dropout_prob
|
30 |
+
self.hidden_size = hidden_size
|
31 |
+
self.initializer_range = initializer_range
|
32 |
+
self.intermediate_size = intermediate_size
|
33 |
+
self.layer_norm_eps = layer_norm_eps
|
34 |
+
self.max_position_embeddings = max_position_embeddings
|
35 |
+
self.model_type = model_type
|
36 |
+
self.num_attention_heads = num_attention_heads
|
37 |
+
self.num_hidden_layers = num_hidden_layers
|
38 |
+
self.pad_token_id = pad_token_id
|
39 |
+
self.position_embedding_type = position_embedding_type
|
40 |
+
self.transformers_version = transformers_version
|
41 |
+
self.type_vocab_size = type_vocab_size
|
42 |
+
self.use_cache = use_cache
|
43 |
+
self.vocab_size = vocab_size
|
44 |
+
super().__init__(**kwargs)
|
modeling_tunbert.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import torch.nn as nn
|
2 |
-
from transformers import
|
3 |
from transformers.modeling_outputs import SequenceClassifierOutput
|
4 |
-
from
|
5 |
class classifier(nn.Module):
|
6 |
def __init__(self,config):
|
7 |
super().__init__()
|
@@ -14,7 +14,7 @@ class classifier(nn.Module):
|
|
14 |
|
15 |
|
16 |
class TunBERT(PreTrainedModel):
|
17 |
-
config_class =
|
18 |
def __init__(self, config):
|
19 |
super().__init__(config)
|
20 |
self.BertModel = BertModel(config)
|
@@ -27,6 +27,10 @@ class TunBERT(PreTrainedModel):
|
|
27 |
logits = self.classifier(sequence_output)
|
28 |
loss =None
|
29 |
if labels is not None :
|
30 |
-
loss_func = nn.
|
31 |
loss = loss_func(logits.view(-1,self.config.type_vocab_size),labels.view(-1))
|
32 |
-
return SequenceClassifierOutput(loss = loss, logits= logits, hidden_states=outputs.last_hidden_state,attentions=outputs.attentions)
|
|
|
|
|
|
|
|
|
|
1 |
import torch.nn as nn
|
2 |
+
from transformers import PreTrainedModel, BertModel
|
3 |
from transformers.modeling_outputs import SequenceClassifierOutput
|
4 |
+
from .config_TunBERT import TunBertConfig
|
5 |
class classifier(nn.Module):
|
6 |
def __init__(self,config):
|
7 |
super().__init__()
|
|
|
14 |
|
15 |
|
16 |
class TunBERT(PreTrainedModel):
|
17 |
+
config_class = TunBertConfig
|
18 |
def __init__(self, config):
|
19 |
super().__init__(config)
|
20 |
self.BertModel = BertModel(config)
|
|
|
27 |
logits = self.classifier(sequence_output)
|
28 |
loss =None
|
29 |
if labels is not None :
|
30 |
+
loss_func = nn.CrossEntropyLoss()
|
31 |
loss = loss_func(logits.view(-1,self.config.type_vocab_size),labels.view(-1))
|
32 |
+
return SequenceClassifierOutput(loss = loss, logits= logits, hidden_states=outputs.last_hidden_state,attentions=outputs.attentions)
|
33 |
+
|
34 |
+
|
35 |
+
TunBertConfig.register_for_auto_class()
|
36 |
+
TunBERT.register_for_auto_class("AutoModelForSequenceClassification")
|