| 108 | return output,fea_text |
| 109 | |
| 110 | class bTextCNN(nn.Module): |
| 111 | def __init__(self, fea_dim, vocab_size): |
| 112 | super(bTextCNN, self).__init__() |
| 113 | self.vocab_size = vocab_size |
| 114 | self.fea_dim=fea_dim |
| 115 | |
| 116 | self.channel_in = 1 |
| 117 | self.filter_num = 14 |
| 118 | self.window_size = [3,4,5] |
| 119 | |
| 120 | self.textcnn =nn.ModuleList([nn.Conv2d(self.channel_in, self.filter_num, (K,self.vocab_size)) for K in self.window_size]) |
| 121 | self.linear = nn.Sequential(torch.nn.Linear(len(self.window_size) * self.filter_num, self.fea_dim),torch.nn.ReLU()) |
| 122 | self.classifier = nn.Linear(self.fea_dim,2) |
| 123 | |
| 124 | def forward(self, **kwargs): |
| 125 | title_w2v = kwargs['title_w2v'] |
| 126 | text = title_w2v.unsqueeze(1) |
| 127 | text = [F.relu(conv(text)).squeeze(3) for conv in self.textcnn] |
| 128 | text = [F.max_pool1d(i.squeeze(2), i.shape[-1]).squeeze(2) for i in text] |
| 129 | fea_text = torch.cat(text, 1) |
| 130 | fea_text = self.linear(fea_text) |
| 131 | |
| 132 | output = self.classifier(fea_text) |
| 133 | |
| 134 | return output |
| 135 | |
| 136 | class bComments(torch.nn.Module): |
| 137 | def __init__(self,bert_model,fea_dim): |