def _get_rnn_output(self, input_word, input_char, input_pos, mask=None, length=None, hx=None):
# [batch, length, word_dim]
word = self.word_embedd(input_word)
# [batch, length, pos_dim]
pos = self.pos_embedd(input_pos)
# [batch, length, char_length, char_dim]
char = self.char_embedd(input_char)
char_size = char.size()
# first transform to [batch *length, char_length, char_dim]
# then transpose to [batch * length, char_dim, char_length]
char = char.view(char_size[0] * char_size[1], char_size[2], char_size[3]).transpose(1, 2)
# put into cnn [batch*length, char_filters, char_length]
# then put into maxpooling [batch * length, char_filters]
char, _ = self.conv1d(char).max(dim=2)
# reshape to [batch, length, char_filters]
char = torch.tanh(char).view(char_size[0], char_size[1], -1)
# apply dropout on input
word = self.dropout_in(word)
pos = self.dropout_in(pos)
char = self.dropout_in(char)
# concatenate word and char [batch, length, word_dim+char_filter]
input = torch.cat([word, char, pos], dim=2)
# output from rnn [batch, length, hidden_size]
output, hn = self.rnn(input, mask, hx=hx)
# apply dropout for output
# [batch, length, hidden_size] --> [batch, hidden_size, length] --> [batch, length, hidden_size]
output = self.dropout_out(output.transpose(1, 2)).transpose(1, 2)
# output size [batch, length, arc_space]
arc_h = F.elu(self.arc_h(output))
arc_c = F.elu(self.arc_c(output))
# output size [batch, length, type_space]
type_h = F.elu(self.type_h(output))
type_c = F.elu(self.type_c(output))
# apply dropout
# [batch, length, dim] --> [batch, 2 * length, dim]
arc = torch.cat([arc_h, arc_c], dim=1)
type = torch.cat([type_h, type_c], dim=1)
arc = self.dropout_out(arc.transpose(1, 2)).transpose(1, 2)
arc_h, arc_c = arc.chunk(2, 1)
type = self.dropout_out(type.transpose(1, 2)).transpose(1, 2)
type_h, type_c = type.chunk(2, 1)
type_h = type_h.contiguous()
type_c = type_c.contiguous()
return (arc_h, arc_c), (type_h, type_c), hn, mask, length
评论列表
文章目录