hieuhocnlp commited on
Commit
737e9a2
·
1 Parent(s): fb2488f

Upload blstm_model.py

Browse files
Files changed (1) hide show
  1. blstm_model.py +44 -0
blstm_model.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import PyTorchModelHubMixin
2
+
3
+ from torch import nn
4
+ import torch
5
+
6
+ class BiLSTM(nn.Module, PyTorchModelHubMixin):
7
+ def __init__(self, vocab_size=23626, embed_dim=100,
8
+ num_layers=1, hidden_dim=256, dropout=0.33,
9
+ output_dim=128, predict_output=10, device="cuda:0"):
10
+ super().__init__()
11
+ self.hidden_dim = hidden_dim
12
+ self.predict_output = predict_output
13
+
14
+ self.embed_layer = nn.Embedding(vocab_size, embed_dim, padding_idx=0)
15
+ self.biLSTM = nn.LSTM(input_size=embed_dim,
16
+ hidden_size=hidden_dim // 2, # BiLSTM will concatenate the 2 directional LSTMs
17
+ num_layers=num_layers,
18
+ bidirectional=True,
19
+ batch_first=True)
20
+ self.linear = nn.Linear(hidden_dim, output_dim)
21
+ self.dropout = nn.Dropout(dropout)
22
+ self.elu = nn.ELU()
23
+ self.fc = nn.Linear(output_dim, predict_output)
24
+ self.device_ = device
25
+
26
+ def forward(self, input): # input is a list of indices, shape batch_size, seq_len
27
+ x = self.embed_layer(input) # batch_size, seq_len, 100 (This is only when batch_first=True!!!!)
28
+ batch_size = x.size(0)
29
+ hidden, cell = self.init_hidden(batch_size)
30
+
31
+ out, hidden = self.biLSTM(x, (hidden, cell)) # seq_len, batch_size, (hidden_dim//2) * 2
32
+
33
+ out = self.dropout(out)
34
+
35
+ out = self.elu(self.linear(out)) # self.linear(out): batch_size, seq_len, output_dim
36
+
37
+ out = self.fc(out)
38
+
39
+ return out, hidden
40
+
41
+ def init_hidden(self, batch_size):
42
+ hidden = torch.zeros(2, batch_size, self.hidden_dim//2, device=self.device_)
43
+ cell = torch.zeros(2, batch_size, self.hidden_dim//2, device=self.device_)
44
+ return hidden, cell