def forward(self, x):
embed = self.embed(x)
# CNN
cnn_x = embed
cnn_x = torch.transpose(cnn_x, 0, 1)
cnn_x = cnn_x.unsqueeze(1)
# cnn_x = [F.relu(conv(cnn_x)).squeeze(3) for conv in self.convs1] # [(N,Co,W), ...]*len(Ks)
cnn_x = [conv(cnn_x).squeeze(3) for conv in self.convs1] # [(N,Co,W), ...]*len(Ks)
# cnn_x = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in cnn_x] # [(N,Co), ...]*len(Ks)
cnn_x = [F.tanh(F.max_pool1d(i, i.size(2)).squeeze(2)) for i in cnn_x] # [(N,Co), ...]*len(Ks)
cnn_x = torch.cat(cnn_x, 1)
cnn_x = self.dropout(cnn_x)
# BiLSTM
bilstm_x = embed.view(len(x), embed.size(1), -1)
bilstm_out, self.hidden = self.bilstm(bilstm_x, self.hidden)
bilstm_out = torch.transpose(bilstm_out, 0, 1)
bilstm_out = torch.transpose(bilstm_out, 1, 2)
# bilstm_out = F.tanh(bilstm_out)
bilstm_out = F.max_pool1d(bilstm_out, bilstm_out.size(2)).squeeze(2)
bilstm_out = F.tanh(bilstm_out)
# CNN and BiLSTM CAT
cnn_x = torch.transpose(cnn_x, 0, 1)
bilstm_out = torch.transpose(bilstm_out, 0, 1)
cnn_bilstm_out = torch.cat((cnn_x, bilstm_out), 0)
cnn_bilstm_out = torch.transpose(cnn_bilstm_out, 0, 1)
# linear
cnn_bilstm_out = self.hidden2label1(F.tanh(cnn_bilstm_out))
# cnn_bilstm_out = F.tanh(self.hidden2label1(cnn_bilstm_out))
cnn_bilstm_out = self.hidden2label2(F.tanh(cnn_bilstm_out))
# cnn_bilstm_out = self.hidden2label2(cnn_bilstm_out)
# output
logit = cnn_bilstm_out
return logit
model_CNN_BiLSTM.py 文件源码
python
阅读 26
收藏 0
点赞 0
评论 0
评论列表
文章目录