kadabengaran commited on
Commit
401d6fa
·
1 Parent(s): ee9b192

Delete app/model.py

Browse files
Files changed (1) hide show
  1. app/model.py +0 -76
app/model.py DELETED
@@ -1,76 +0,0 @@
1
- import torch.nn as nn
2
- import torch
3
- from transformers import BertModel, BertConfig, PreTrainedModel
4
-
5
- def get_device():
6
- if torch.cuda.is_available():
7
- return torch.device('cuda')
8
- else:
9
- return torch.device('cpu')
10
-
11
- USE_CUDA = False
12
- device = get_device()
13
- if device.type == 'cuda':
14
- USE_CUDA = True
15
-
16
- base_bert = 'indobenchmark/indobert-base-p2'
17
- HIDDEN_DIM = 768
18
- OUTPUT_DIM = 2 # 2 if Binary Classification
19
- BIDIRECTIONAL = True
20
- DROPOUT = 0.2 # 0.2
21
-
22
- class IndoBERTBiLSTM(PreTrainedModel):
23
- config_class = BertConfig
24
- def __init__(self, bert_config):
25
- super().__init__(bert_config)
26
- self.output_dim = OUTPUT_DIM
27
- self.n_layers = 1
28
- self.hidden_dim = HIDDEN_DIM
29
- self.bidirectional = BIDIRECTIONAL
30
-
31
- self.bert = BertModel.from_pretrained(base_bert)
32
- self.lstm = nn.LSTM(input_size=self.bert.config.hidden_size,
33
- hidden_size=self.hidden_dim,
34
- num_layers=self.n_layers,
35
- bidirectional=self.bidirectional,
36
- batch_first=True)
37
- self.dropout = nn.Dropout(DROPOUT)
38
- self.output_layer = nn.Linear(self.hidden_dim * 2 if self.bidirectional else self.hidden_dim, self.output_dim)
39
-
40
- def forward(self, input_ids, attention_mask):
41
-
42
- hidden = self.init_hidden(input_ids.shape[0])
43
- output = self.bert(input_ids=input_ids, attention_mask=attention_mask)
44
- sequence_output = output.last_hidden_state
45
-
46
- lstm_output, (hidden_last, cn_last) = self.lstm(sequence_output, hidden)
47
-
48
- hidden_last_L=hidden_last[-2]
49
- hidden_last_R=hidden_last[-1]
50
- hidden_last_out=torch.cat([hidden_last_L,hidden_last_R],dim=-1) #[16, 1536]
51
-
52
- # apply dropout
53
- out = self.dropout(hidden_last_out)
54
-
55
- # output layer
56
- logits = self.output_layer(out)
57
-
58
- return logits
59
-
60
- def init_hidden(self, batch_size):
61
- weight = next(self.parameters()).data
62
-
63
- number = 1
64
- if self.bidirectional:
65
- number = 2
66
-
67
- if (USE_CUDA):
68
- hidden = (weight.new(self.n_layers*number, batch_size, self.hidden_dim).zero_().float().cuda(),
69
- weight.new(self.n_layers*number, batch_size, self.hidden_dim).zero_().float().cuda()
70
- )
71
- else:
72
- hidden = (weight.new(self.n_layers*number, batch_size, self.hidden_dim).zero_().float(),
73
- weight.new(self.n_layers*number, batch_size, self.hidden_dim).zero_().float()
74
- )
75
-
76
- return hidden