File size: 1,332 Bytes
aaa3776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69

import torch
import torch.nn as nn
import torch.nn.functional as F

class StrongTextCNN(nn.Module):

    def __init__(
        self,
        vocab_size,
        embed_dim=300,
        num_filters=128,
        dropout=0.4
    ):
        super().__init__()

        self.embedding = nn.Embedding(
            vocab_size,
            embed_dim,
            padding_idx=0
        )

        self.convs = nn.ModuleList([
            nn.Conv1d(embed_dim, num_filters, k)
            for k in [2,3,4,5]
        ])

        self.bn = nn.BatchNorm1d(num_filters * 4)

        self.fc1 = nn.Linear(num_filters * 4, 256)
        self.fc2 = nn.Linear(256, 64)
        self.out = nn.Linear(64, 1)

        self.dropout = nn.Dropout(dropout)

    def forward(self, x):

        x = self.embedding(x)
        x = x.permute(0,2,1)

        conv_outputs = []

        for conv in self.convs:

            c = F.relu(conv(x))

            p = F.max_pool1d(
                c,
                kernel_size=c.shape[2]
            ).squeeze(2)

            conv_outputs.append(p)

        x = torch.cat(conv_outputs, dim=1)

        x = self.bn(x)

        x = self.dropout(x)

        x = F.relu(self.fc1(x))
        x = self.dropout(x)

        x = F.relu(self.fc2(x))
        x = self.dropout(x)

        x = self.out(x)

        return x.squeeze(1)