| 41 | |
| 42 | |
| 43 | class Model(nn.Module): |
| 44 | def __init__(self, config): |
| 45 | super(Model, self).__init__() |
| 46 | if config.embedding_pretrained is not None: |
| 47 | self.embedding = nn.Embedding.from_pretrained(config.embedding_pretrained, freeze=False) |
| 48 | else: |
| 49 | self.embedding = nn.Embedding(config.n_vocab, config.embed, padding_idx=config.n_vocab - 1) |
| 50 | self.lstm = nn.LSTM(config.embed, config.hidden_size, config.num_layers, |
| 51 | bidirectional=True, batch_first=True, dropout=config.dropout) |
| 52 | self.maxpool = nn.MaxPool1d(config.pad_size) |
| 53 | self.fc = nn.Linear(config.hidden_size * 2 + config.embed, config.num_classes) |
| 54 | |
| 55 | def forward(self, x): |
| 56 | x, _ = x |
| 57 | embed = self.embedding(x) # [batch_size, seq_len, embeding]=[64, 32, 64] |
| 58 | out, _ = self.lstm(embed) |
| 59 | out = torch.cat((embed, out), 2) |
| 60 | out = F.relu(out) |
| 61 | out = out.permute(0, 2, 1) |
| 62 | out = self.maxpool(out).squeeze() |
| 63 | out = self.fc(out) |
| 64 | return out |
nothing calls this directly
no outgoing calls
no test coverage detected