arubenruben commited on
Commit
635c5ff
1 Parent(s): f17519e

Upload model.py

Browse files
Files changed (1) hide show
  1. model.py +29 -24
model.py CHANGED
@@ -2,30 +2,6 @@ import torch
2
  from transformers import BertModel
3
 
4
 
5
- class Ensembler(torch.nn.Module):
6
- def __init__(self, specialists):
7
- super().__init__()
8
- self.specialists = specialists
9
-
10
- def forward(self, input_ids, attention_mask):
11
- outputs = []
12
-
13
- for specialist in self.specialists:
14
- specialist.eval()
15
-
16
- specialist.to(torch.device(
17
- "cuda" if torch.cuda.is_available() else "cpu"))
18
-
19
- outputs.append(specialist(input_ids, attention_mask))
20
-
21
- # Remove the specialist from the GPU
22
- specialist.cpu()
23
-
24
- outputs = torch.cat(outputs, dim=1)
25
-
26
- return torch.mean(outputs, dim=1).unsqueeze(1)
27
-
28
-
29
  class LanguageIdentifier(torch.nn.Module):
30
  def __init__(self):
31
  super().__init__()
@@ -47,3 +23,32 @@ class LanguageIdentifier(torch.nn.Module):
47
  outputs = self.linear_layer(outputs)
48
 
49
  return outputs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from transformers import BertModel
3
 
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  class LanguageIdentifier(torch.nn.Module):
6
  def __init__(self):
7
  super().__init__()
 
23
  outputs = self.linear_layer(outputs)
24
 
25
  return outputs
26
+
27
+
28
+ class Ensembler(torch.nn.Module):
29
+ def __init__(self):
30
+ super().__init__()
31
+ self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
32
+
33
+ def forward(self, input_ids, attention_mask):
34
+ outputs = []
35
+
36
+ for domain in ['politics', 'news', 'law', 'social_media', 'literature', 'web']:
37
+ specialist = LanguageIdentifier()
38
+
39
+ specialist.load_state_dict(torch.load(f"models/{domain}.pt", map_location=self.device))
40
+
41
+ specialist.eval()
42
+
43
+ specialist.to(self.device)
44
+
45
+ outputs.append(specialist(input_ids, attention_mask))
46
+
47
+ # Remove the specialist from the GPU
48
+ specialist.cpu()
49
+ del specialist
50
+
51
+
52
+ outputs = torch.cat(outputs, dim=1)
53
+
54
+ return torch.mean(outputs, dim=1).unsqueeze(1)