arubenruben
commited on
Commit
•
7f341be
1
Parent(s):
f895ace
Upload model.py
Browse files
model.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import BertModel
|
3 |
+
|
4 |
+
class Ensembler(torch.nn.Module):
|
5 |
+
def __init__(self, specialists):
|
6 |
+
super().__init__()
|
7 |
+
|
8 |
+
self.specialists = specialists
|
9 |
+
|
10 |
+
def forward(self, input_ids, attention_mask):
|
11 |
+
outputs = torch.cat([specialist(input_ids, attention_mask)
|
12 |
+
for specialist in self.specialists], dim=1)
|
13 |
+
|
14 |
+
return torch.mean(outputs, dim=1).unsqueeze(1)
|
15 |
+
|
16 |
+
|
17 |
+
class LanguageIdentifier(torch.nn.Module):
|
18 |
+
def __init__(self):
|
19 |
+
super().__init__()
|
20 |
+
|
21 |
+
self.portuguese_bert = BertModel.from_pretrained("neuralmind/bert-large-portuguese-cased")
|
22 |
+
|
23 |
+
self.linear_layer = torch.nn.Sequential(
|
24 |
+
torch.nn.Dropout(p=0.2),
|
25 |
+
torch.nn.Linear(self.portuguese_bert.config.hidden_size, 1),
|
26 |
+
)
|
27 |
+
|
28 |
+
def forward(self, input_ids, attention_mask):
|
29 |
+
|
30 |
+
#(Batch_Size,Sequence Length, Hidden_Size)
|
31 |
+
outputs = self.portuguese_bert(input_ids=input_ids, attention_mask=attention_mask).last_hidden_state[:, 0, :]
|
32 |
+
|
33 |
+
outputs = self.linear_layer(outputs)
|
34 |
+
|
35 |
+
return outputs
|