trek90s
commited on
Commit
•
617dc35
1
Parent(s):
e05d080
add model
Browse files- config.json +28 -0
- configuration.py +31 -0
- model.py +121 -0
- pytorch_model.bin +3 -0
config.json
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"BertLSTMForSequenceClassification"
|
4 |
+
],
|
5 |
+
"auto_map": {
|
6 |
+
"AutoConfig": "configuration.BertABSAConfig",
|
7 |
+
"AutoModelForSequenceClassification": "model.BertLSTMForSequenceClassification"
|
8 |
+
},
|
9 |
+
"dropout_rate": 0.1,
|
10 |
+
"embed_dim": 768,
|
11 |
+
"fc_hidden": 256,
|
12 |
+
"hidden_dim_lstm": 128,
|
13 |
+
"id2label": {
|
14 |
+
"0": "negative",
|
15 |
+
"1": "positive",
|
16 |
+
"2": "neutral"
|
17 |
+
},
|
18 |
+
"label2id": {
|
19 |
+
"negative": 0,
|
20 |
+
"neutral": 2,
|
21 |
+
"positive": 1
|
22 |
+
},
|
23 |
+
"model_type": "BertABSAForSequenceClassification",
|
24 |
+
"num_classes": 3,
|
25 |
+
"num_layers": 12,
|
26 |
+
"torch_dtype": "float32",
|
27 |
+
"transformers_version": "4.20.1"
|
28 |
+
}
|
configuration.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import PretrainedConfig
|
2 |
+
|
3 |
+
|
4 |
+
class BertABSAConfig(PretrainedConfig):
|
5 |
+
model_type = "BertABSAForSequenceClassification"
|
6 |
+
|
7 |
+
def __init__(self,
|
8 |
+
num_classes=3,
|
9 |
+
embed_dim=768,
|
10 |
+
num_layers=12,
|
11 |
+
dropout_rate=0.1,
|
12 |
+
fc_hidden=256,
|
13 |
+
hidden_dim_lstm=128,
|
14 |
+
**kwargs):
|
15 |
+
super().__init__(**kwargs)
|
16 |
+
self.num_classes = num_classes
|
17 |
+
self.embed_dim = embed_dim
|
18 |
+
self.num_layers = num_layers
|
19 |
+
self.dropout_rate = dropout_rate
|
20 |
+
self.fc_hidden = fc_hidden
|
21 |
+
self.hidden_dim_lstm = hidden_dim_lstm
|
22 |
+
self.id2label = {
|
23 |
+
0: "negative",
|
24 |
+
1: "positive",
|
25 |
+
2: "neutral",
|
26 |
+
}
|
27 |
+
self.label2id = {
|
28 |
+
"negative": 0,
|
29 |
+
"positive": 1,
|
30 |
+
"neutral": 2,
|
31 |
+
}
|
model.py
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from abc import ABCMeta
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from transformers.pytorch_utils import nn
|
5 |
+
import torch.nn.functional as F
|
6 |
+
from src.configuration import BertABSAConfig
|
7 |
+
from transformers import BertModel, BertForSequenceClassification, PreTrainedModel
|
8 |
+
from transformers.modeling_outputs import SequenceClassifierOutput
|
9 |
+
|
10 |
+
|
11 |
+
class BertBaseForSequenceClassification(PreTrainedModel, metaclass=ABCMeta):
|
12 |
+
config_class = BertABSAConfig
|
13 |
+
|
14 |
+
def __init__(self, config):
|
15 |
+
super(BertBaseForSequenceClassification, self).__init__(config)
|
16 |
+
self.num_classes = config.num_classes
|
17 |
+
self.embed_dim = config.embed_dim
|
18 |
+
self.dropout = nn.Dropout(config.dropout_rate)
|
19 |
+
|
20 |
+
self.bert = BertForSequenceClassification.from_pretrained('bert-base-uncased', # noqa
|
21 |
+
output_hidden_states=False, # noqa
|
22 |
+
output_attentions=False, # noqa
|
23 |
+
num_labels=self.num_classes) # noqa
|
24 |
+
print("BERT Model Loaded")
|
25 |
+
|
26 |
+
def forward(self, input_ids, attention_mask, token_type_ids, labels=None):
|
27 |
+
out = self.bert(input_ids=input_ids, attention_mask=attention_mask,
|
28 |
+
token_type_ids=token_type_ids, labels=labels)
|
29 |
+
return out
|
30 |
+
|
31 |
+
|
32 |
+
class BertLSTMForSequenceClassification(PreTrainedModel, metaclass=ABCMeta):
|
33 |
+
config_class = BertABSAConfig
|
34 |
+
|
35 |
+
def __init__(self, config):
|
36 |
+
super(BertLSTMForSequenceClassification, self).__init__(config)
|
37 |
+
self.num_classes = config.num_classes
|
38 |
+
self.embed_dim = config.embed_dim
|
39 |
+
self.num_layers = config.num_layers
|
40 |
+
self.hidden_dim_lstm = config.hidden_dim_lstm
|
41 |
+
self.dropout = nn.Dropout(config.dropout_rate)
|
42 |
+
|
43 |
+
self.bert = BertModel.from_pretrained('bert-base-uncased',
|
44 |
+
output_hidden_states=True,
|
45 |
+
output_attentions=False)
|
46 |
+
print("BERT Model Loaded")
|
47 |
+
self.lstm = nn.LSTM(self.embed_dim, self.hidden_dim_lstm, batch_first=True) # noqa
|
48 |
+
self.fc = nn.Linear(self.hidden_dim_lstm, self.num_classes)
|
49 |
+
|
50 |
+
def forward(self, input_ids, attention_mask, token_type_ids, labels=None):
|
51 |
+
bert_output = self.bert(input_ids=input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)
|
52 |
+
hidden_states = bert_output["hidden_states"]
|
53 |
+
|
54 |
+
hidden_states = torch.stack([hidden_states[layer_i][:, 0].squeeze()
|
55 |
+
for layer_i in range(0, self.num_layers)], dim=-1) # noqa
|
56 |
+
hidden_states = hidden_states.view(-1, self.num_layers, self.embed_dim)
|
57 |
+
out, _ = self.lstm(hidden_states, None)
|
58 |
+
out = self.dropout(out[:, -1, :])
|
59 |
+
logits = self.fc(out)
|
60 |
+
loss = None
|
61 |
+
if labels is not None:
|
62 |
+
loss = F.cross_entropy(logits, labels)
|
63 |
+
out = SequenceClassifierOutput(
|
64 |
+
loss=loss,
|
65 |
+
logits=logits,
|
66 |
+
hidden_states=bert_output.hidden_states,
|
67 |
+
attentions=bert_output.attentions,
|
68 |
+
)
|
69 |
+
return out
|
70 |
+
|
71 |
+
|
72 |
+
class BertAttentionForSequenceClassification(PreTrainedModel, metaclass=ABCMeta):
|
73 |
+
config_class = BertABSAConfig
|
74 |
+
|
75 |
+
def __init__(self, config):
|
76 |
+
super(BertAttentionForSequenceClassification, self).__init__(config)
|
77 |
+
self.num_classes = config.num_classes
|
78 |
+
self.embed_dim = config.embed_dim
|
79 |
+
self.num_layers = config.num_layers
|
80 |
+
self.fc_hidden = config.fc_hidden
|
81 |
+
self.dropout = nn.Dropout(config.dropout_rate)
|
82 |
+
|
83 |
+
self.bert = BertModel.from_pretrained('bert-base-uncased',
|
84 |
+
output_hidden_states=True,
|
85 |
+
output_attentions=False)
|
86 |
+
print("BERT Model Loaded")
|
87 |
+
|
88 |
+
q_t = np.random.normal(loc=0.0, scale=0.1, size=(1, self.embed_dim))
|
89 |
+
self.q = nn.Parameter(torch.from_numpy(q_t)).float().to(self.device)
|
90 |
+
w_ht = np.random.normal(loc=0.0, scale=0.1, size=(self.embed_dim, self.fc_hidden)) # noqa
|
91 |
+
self.w_h = nn.Parameter(torch.from_numpy(w_ht)).float().to(self.device)
|
92 |
+
|
93 |
+
self.fc = nn.Linear(self.fc_hidden, self.num_classes)
|
94 |
+
|
95 |
+
def forward(self, input_ids, attention_mask, token_type_ids, labels=None):
|
96 |
+
bert_output = self.bert(input_ids=input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)
|
97 |
+
hidden_states = bert_output["hidden_states"]
|
98 |
+
|
99 |
+
hidden_states = torch.stack([hidden_states[layer_i][:, 0].squeeze()
|
100 |
+
for layer_i in range(0, self.num_layers)], dim=-1) # noqa
|
101 |
+
hidden_states = hidden_states.view(-1, self.num_layers, self.embed_dim)
|
102 |
+
out = self.attention(hidden_states)
|
103 |
+
out = self.dropout(out)
|
104 |
+
logits = self.fc(out)
|
105 |
+
loss = None
|
106 |
+
if labels is not None:
|
107 |
+
loss = F.cross_entropy(logits, labels)
|
108 |
+
out = SequenceClassifierOutput(
|
109 |
+
loss=loss,
|
110 |
+
logits=logits,
|
111 |
+
hidden_states=bert_output.hidden_states,
|
112 |
+
attentions=bert_output.attentions,
|
113 |
+
)
|
114 |
+
return out
|
115 |
+
|
116 |
+
def attention(self, h):
|
117 |
+
v = torch.matmul(self.q, h.transpose(-2, -1)).squeeze(1)
|
118 |
+
v = F.softmax(v, -1)
|
119 |
+
v_temp = torch.matmul(v.unsqueeze(1), h).transpose(-2, -1)
|
120 |
+
v = torch.matmul(self.w_h.transpose(1, 0), v_temp).squeeze(2)
|
121 |
+
return v
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:cb67cfc95d551bc3ae74ba297ab0c59e62d3c9ee53154999601576a856cfa9f2
|
3 |
+
size 439841259
|