赞
踩
ICLR2017 双向attention 机制的QA模型
Code
1. Character Embedding Layer 使用字符级CNNs将每个单词映射到向量空间,CNN的输出在整个宽度上最大化,以获得每个单词的固定大小向量。
- #Init函数中定义 1. Character Embedding Layer
- self.char_emb = nn.Embedding(args.char_vocab_size, args.char_dim, padding_idx=1)
- nn.init.uniform_(self.char_emb.weight, -0.001, 0.001)
-
- self.char_conv = nn.Sequential(
- nn.Conv2d(1, args.char_channel_size, (args.char_dim, args.char_channel_width)),
- nn.ReLU()
- )
-
-
- def char_emb_layer(x):
- """
- :param x: (batch, seq_len, word_len)
- :return: (batch, seq_len, char_channel_size)
- """
- batch_size = x.size(0)
- # (batch, seq_len, word_len, char_dim)
- x = self.dropout(self.char_emb(x))
- # (batch, seq_len, char_dim, word_len)
- x = x.transpose(2, 3)
- # (batch * seq_len, 1, char_dim, word_len)
- x = x.view(-1, self.args.char_dim, x.size(3)).unsqueeze(1)
- # (batch * seq_len, char_channel_size, 1, conv_len) -> (batch * seq_len, char_channel_size, conv_len)
- x = self.char_conv(x).squeeze()
- # (batch * seq_len, char_channel_size, 1) -> (batch * seq_len, char_channel_size)
- x &
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。