1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
- import yaml
- import torch
- from torch import nn
- from torch.nn import init
- from model.att import Attention
- from model.base import ModelBase
- with open('params.yaml', 'r') as f:
- PARAMS = yaml.safe_load(f)
- if torch.cuda.is_available():
- DEVICE = torch.device('cuda', PARAMS.get('gpu', 0))
- else:
- DEVICE = torch.device('cpu')
- class Model(ModelBase):
- def __init__(self, vocab_size, embed_dim, hidden_size, n_layers, dropout, num_classes, attention_method,
- padding_idx, *args, **kwargs):
- super(Model, self).__init__()
- self.hidden_size = hidden_size
- self.embedding = nn.Embedding(vocab_size, embed_dim, padding_idx=padding_idx)
- self.lstm = nn.LSTM(embed_dim, hidden_size, n_layers, dropout=dropout, bidirectional=True)
- self.attn = Attention(2 * hidden_size, attention_method)
- self.fc = nn.Linear(2 * hidden_size, 1)
- self.init_weights()
- def init_weights(self):
- nn.init.xavier_normal_(self.embedding.weight)
- for param in self.lstm.parameters():
- if len(param.shape) >= 2:
- init.orthogonal_(param.data)
- else:
- nn.init.zeros_(param.data)
- nn.init.kaiming_normal_(self.fc.weight, mode='fan_out', nonlinearity='sigmoid')
- nn.init.constant_(self.fc.bias, 0)
- def forward(self, text, text_lengths, hidden=None):
- # text = [L x B]
- sorted_lengths, sorted_idx = text_lengths.sort(descending=True)
- _, unsorted_idx = sorted_idx.sort()
- sorted_text = torch.index_select(text, -1, sorted_idx)
- emb = self.embedding(sorted_text)
- packed = nn.utils.rnn.pack_padded_sequence(emb, sorted_lengths.to(torch.device('cpu'), copy=True))
- outputs, hidden = self.lstm(packed, hidden)
- hidden_state, cell_state = hidden
- hidden_state = hidden_state[-2:, :, :].view(1, -1, 2 * self.hidden_size).squeeze(0)
- outputs, _ = nn.utils.rnn.pad_packed_sequence(outputs)
- attn_weights = self.attn(hidden_state, outputs)
- # attn_weights = [batch_size x 1 x lengths]
- context = torch.bmm(attn_weights, outputs.transpose(0, 1)).squeeze(1)
- pred = self.fc(context).sigmoid()
- pred = torch.index_select(pred, 0, unsorted_idx)
- return pred
- def load_model(self, model_path):
- self.load_state_dict(torch.load(model_path))
- self.eval()
- def save_model(self, model_path):
- torch.save(self.state_dict(), model_path)
|