updating model.py
Browse files
model.py
CHANGED
@@ -1,15 +1,36 @@
|
|
1 |
-
import
|
2 |
-
import torch.nn as nn
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import PreTrainedModel, PretrainedConfig
|
2 |
+
import torch.nn as nn
|
3 |
+
import torch
|
4 |
+
import torch.nn.functional as F
|
5 |
+
|
6 |
+
class BiLSTMConfig(PretrainedConfig):
|
7 |
+
model_type = "bilstm"
|
8 |
+
|
9 |
+
def __init__(self, vocab_size=64000, embedding_dim=1024, hidden_dim=512, num_labels=3, **kwargs):
|
10 |
+
super().__init__(**kwargs)
|
11 |
+
self.vocab_size = vocab_size
|
12 |
+
self.embedding_dim = embedding_dim
|
13 |
+
self.hidden_dim = hidden_dim
|
14 |
+
self.num_labels = num_labels
|
15 |
+
|
16 |
+
class BiLSTMClassifier(PreTrainedModel):
|
17 |
+
config_class = BiLSTMConfig
|
18 |
+
|
19 |
+
def __init__(self, config: BiLSTMConfig):
|
20 |
+
super().__init__(config)
|
21 |
+
self.embedding = nn.Embedding(config.vocab_size, config.embedding_dim)
|
22 |
+
self.lstm = nn.LSTM(config.embedding_dim, config.hidden_dim, batch_first=True, bidirectional=True)
|
23 |
+
self.fc = nn.Linear(config.hidden_dim * 2, config.num_labels)
|
24 |
+
|
25 |
+
self.post_init()
|
26 |
+
|
27 |
+
def forward(self, input_ids, attention_mask=None, labels=None):
|
28 |
+
x = self.embedding(input_ids)
|
29 |
+
_, (h_n, _) = self.lstm(x)
|
30 |
+
h_cat = torch.cat((h_n[0], h_n[1]), dim=1)
|
31 |
+
logits = self.fc(h_cat)
|
32 |
+
|
33 |
+
if labels is not None:
|
34 |
+
loss = F.cross_entropy(logits, labels)
|
35 |
+
return {"loss": loss, "logits": logits}
|
36 |
+
return {"logits": logits}
|