| 40 | |
| 41 | |
| 42 | class Model(nn.Module): |
| 43 | def __init__(self, config): |
| 44 | super(Model, self).__init__() |
| 45 | if config.embedding_pretrained is not None: |
| 46 | self.embedding = nn.Embedding.from_pretrained(config.embedding_pretrained, freeze=False) |
| 47 | else: |
| 48 | self.embedding = nn.Embedding(config.n_vocab, config.embed, padding_idx=config.n_vocab - 1) |
| 49 | self.conv_region = nn.Conv2d(1, config.num_filters, (3, config.embed), stride=1) |
| 50 | self.conv = nn.Conv2d(config.num_filters, config.num_filters, (3, 1), stride=1) |
| 51 | self.max_pool = nn.MaxPool2d(kernel_size=(3, 1), stride=2) |
| 52 | self.padding1 = nn.ZeroPad2d((0, 0, 1, 1)) # top bottom |
| 53 | self.padding2 = nn.ZeroPad2d((0, 0, 0, 1)) # bottom |
| 54 | self.relu = nn.ReLU() |
| 55 | self.fc = nn.Linear(config.num_filters, config.num_classes) |
| 56 | |
| 57 | def forward(self, x): |
| 58 | x = x[0] |
| 59 | x = self.embedding(x) |
| 60 | x = x.unsqueeze(1) # [batch_size, 250, seq_len, 1] |
| 61 | x = self.conv_region(x) # [batch_size, 250, seq_len-3+1, 1] |
| 62 | |
| 63 | x = self.padding1(x) # [batch_size, 250, seq_len, 1] |
| 64 | x = self.relu(x) |
| 65 | x = self.conv(x) # [batch_size, 250, seq_len-3+1, 1] |
| 66 | x = self.padding1(x) # [batch_size, 250, seq_len, 1] |
| 67 | x = self.relu(x) |
| 68 | x = self.conv(x) # [batch_size, 250, seq_len-3+1, 1] |
| 69 | while x.size()[2] > 2: |
| 70 | x = self._block(x) |
| 71 | x = x.squeeze() # [batch_size, num_filters(250)] |
| 72 | x = self.fc(x) |
| 73 | return x |
| 74 | |
| 75 | def _block(self, x): |
| 76 | x = self.padding2(x) |
| 77 | px = self.max_pool(x) |
| 78 | |
| 79 | x = self.padding1(px) |
| 80 | x = F.relu(x) |
| 81 | x = self.conv(x) |
| 82 | |
| 83 | x = self.padding1(x) |
| 84 | x = F.relu(x) |
| 85 | x = self.conv(x) |
| 86 | |
| 87 | # Short Cut |
| 88 | x = x + px |
| 89 | return x |
nothing calls this directly
no outgoing calls
no test coverage detected