Пример #1
0
    def __init__(self, param):
        """
        :param param: embedding, hidden_size, dropout_p, encoder_dropout_p, encoder_direction_num, encoder_layer_num
        """
        super(Model, self).__init__()

        self.mode = param['mode']
        self.hidden_size = param['hidden_size']
        self.dropout_p = param['dropout_p']
        self.encoder_dropout_p = param['encoder_dropout_p']
        self.encoder_layer_num = param['encoder_layer_num']
        self.is_bn = param['is_bn']

        self.embedding = embedding.ExtendEmbedding(param['embedding'])

        # encoder
        input_size = self.embedding.embedding_dim - 9
        self.encoder_a = encoder.Rnn(mode=self.mode,
                                     input_size=input_size,
                                     hidden_size=self.hidden_size,
                                     dropout_p=self.encoder_dropout_p,
                                     bidirectional=True,
                                     layer_num=self.encoder_layer_num,
                                     is_bn=False)

        input_size = self.embedding.embedding_dim
        self.encoder_p_q = encoder.Rnn(mode=self.mode,
                                       input_size=input_size,
                                       hidden_size=self.hidden_size,
                                       dropout_p=self.encoder_dropout_p,
                                       bidirectional=True,
                                       layer_num=self.encoder_layer_num,
                                       is_bn=True)

        # match rnn
        input_size = self.hidden_size * 2
        self.match_rnn = match_rnn.MatchRNN(mode=self.mode,
                                            input_size=input_size,
                                            hidden_size=self.hidden_size,
                                            dropout_p=self.dropout_p,
                                            gated_attention=True,
                                            is_bn=self.is_bn)

        # addition_rnn
        input_size = self.hidden_size * 2
        self.addition_rnn = encoder.Rnn(mode=self.mode,
                                        input_size=input_size,
                                        hidden_size=self.hidden_size,
                                        bidirectional=True,
                                        dropout_p=self.dropout_p,
                                        layer_num=1,
                                        is_bn=self.is_bn)

        # mean passage based on attn
        self.mean_p = pointer.AttentionPooling(input_size=self.hidden_size * 2,
                                               output_size=self.hidden_size *
                                               2)

        # outputs
        self.choose = choose.Choose(self.hidden_size * 2, self.hidden_size * 2)
Пример #2
0
    def __init__(self, param):
        """
        :param param: embedding, hidden_size, dropout_p, encoder_dropout_p, encoder_direction_num, encoder_layer_num
        """
        super(Model, self).__init__()

        self.mode = param['mode']
        self.hidden_size = param['hidden_size']
        self.dropout_p = param['dropout_p']
        self.encoder_dropout_p = param['encoder_dropout_p']
        self.encoder_layer_num = param['encoder_layer_num']
        self.is_bn = param['is_bn']

        self.embedding = embedding.ExtendEmbedding(param['embedding'])

        # encoder
        input_size = self.embedding.embedding_dim
        self.encoder = encoder.Rnn(mode=self.mode,
                                   input_size=input_size,
                                   hidden_size=self.hidden_size,
                                   dropout_p=self.encoder_dropout_p,
                                   bidirectional=True,
                                   layer_num=self.encoder_layer_num,
                                   is_bn=True)

        # match rnn
        input_size = self.hidden_size * 2
        self.match_rnn = match_rnn.MatchRNN(mode=self.mode,
                                            input_size=input_size,
                                            hidden_size=self.hidden_size,
                                            dropout_p=self.dropout_p,
                                            gated_attention=True,
                                            is_bn=self.is_bn)

        # addition_rnn
        input_size = self.hidden_size * 2
        self.addition_rnn = encoder.Rnn(mode=self.mode,
                                        input_size=input_size,
                                        hidden_size=self.hidden_size,
                                        bidirectional=True,
                                        dropout_p=self.dropout_p,
                                        layer_num=1,
                                        is_bn=self.is_bn)

        # init state of pointer
        self.init_state = pointer.AttentionPooling(
            input_size=self.hidden_size * 2, output_size=self.hidden_size)

        # pointer
        self.pointer_net = pointer.BoundaryPointer(
            mode=self.mode,
            input_size=self.hidden_size * 2,
            hidden_size=self.hidden_size,
            dropout_p=self.dropout_p,
            bidirectional=True,
            is_bn=self.is_bn)
Пример #3
0
    def __init__(self, param):
        super(Model, self).__init__()

        self.mode = param['mode']
        self.hidden_size = param['hidden_size']
        self.dropout_p = param['dropout_p']
        self.encoder_dropout_p = param['encoder_dropout_p']
        self.encoder_layer_num = param['encoder_layer_num']
        self.is_bn = param['is_bn']

        # embedding
        self.embedding = embedding.ExtendEmbedding(param['embedding'])

        # encoder: p
        input_size = self.embedding.embedding_dim
        self.encoder = encoder.Rnn(
            mode=self.mode,
            input_size=input_size,
            hidden_size=self.hidden_size,
            dropout_p=self.encoder_dropout_p,
            bidirectional=True,
            layer_num=self.encoder_layer_num,
            is_bn=self.is_bn
        )

        self.mean_q = pointer.AttentionPooling(self.hidden_size*2, self.hidden_size)
        self.mean_a = pointer.AttentionPooling(self.hidden_size*2, self.hidden_size)

        # align
        input_size = self.hidden_size * 2
        self.align_1 = Aligner(input_size, self.dropout_p, self.mode, self.is_bn, False)
        self.align_2 = Aligner(input_size, self.dropout_p, self.mode, self.is_bn, False)
        self.align_3 = Aligner(input_size, self.dropout_p, self.mode, self.is_bn, True)

        # p_rep, choosing
        self.wp1 = nn.Linear(self.hidden_size*2, self.hidden_size)
        self.wp2 = nn.Linear(self.hidden_size*2, self.hidden_size)
        self.vp = nn.Linear(self.hidden_size, 1)
        self.bi_linear = nn.Linear(self.hidden_size*2, self.hidden_size*2)

        self.dropout = nn.Dropout(self.dropout_p)

        self.reset_param()
Пример #4
0
    def __init__(self, param):
        super(Model, self).__init__()

        self.mode = param['mode']
        self.hidden_size = param['hidden_size']
        self.dropout_p = param['dropout_p']
        self.encoder_dropout_p = param['encoder_dropout_p']
        self.encoder_layer_num = param['encoder_layer_num']
        self.is_bn = param['is_bn']
        self.k = 2

        self.embedding = embedding.ExtendEmbedding(param['embedding'])

        # encoder_a
        input_size = self.embedding.embedding_dim - 9
        self.encoder_a = encoder.Rnn(mode=self.mode,
                                     input_size=input_size,
                                     hidden_size=self.hidden_size,
                                     dropout_p=self.encoder_dropout_p,
                                     bidirectional=True,
                                     layer_num=self.encoder_layer_num,
                                     is_bn=False)

        # encoder p, q
        self.doc_list = nn.ModuleList()
        self.query_list = nn.ModuleList()
        input_size = self.embedding.embedding_dim
        for i in range(self.k):
            di_enc = encoder.Rnn(
                mode=self.mode,
                input_size=input_size if i == 0 else self.hidden_size * 2,
                hidden_size=self.hidden_size,
                dropout_p=self.dropout_p,
                bidirectional=True,
                layer_num=1,
                is_bn=self.is_bn)

            qi_enc = encoder.Rnn(mode=self.mode,
                                 input_size=input_size,
                                 hidden_size=self.hidden_size,
                                 dropout_p=self.dropout_p,
                                 bidirectional=True,
                                 layer_num=1,
                                 is_bn=self.is_bn)
            self.doc_list.append(di_enc)
            self.query_list.append(qi_enc)

        # mean passage based on attn
        self.mean_p = pointer.AttentionPooling(input_size=self.hidden_size * 2,
                                               output_size=self.hidden_size *
                                               2)

        # mean answer based on attn
        self.mean_a = pointer.AttentionPooling(input_size=self.hidden_size * 2,
                                               output_size=self.hidden_size *
                                               2)

        # outputs
        self.choose = choose.Choose(self.hidden_size * 2, self.hidden_size * 2)

        self.dropout = nn.Dropout(self.dropout_p)
Пример #5
0
    def __init__(self, param):
        super(Model, self).__init__()

        self.mode = param['mode']
        self.hidden_size = param['hidden_size']
        self.dropout_p = param['dropout_p']
        self.encoder_dropout_p = param['encoder_dropout_p']
        self.encoder_layer_num = param['encoder_layer_num']
        self.is_bn = param['is_bn']
        self.num_align_hops = param['num_align_hops']  # 2

        # embedding
        self.embedding = embedding.ExtendEmbedding(param['embedding'])

        # encoder
        input_size = self.embedding.embedding_dim
        self.encoder = encoder.Rnn(
            mode=self.mode,
            input_size=input_size,
            hidden_size=self.hidden_size,
            dropout_p=self.encoder_dropout_p,
            bidirectional=True,
            layer_num=self.encoder_layer_num,
            is_bn=self.is_bn  # 应该修改一下
        )

        self.mean_q = pointer.AttentionPooling(self.hidden_size * 2,
                                               self.hidden_size)
        self.mean_a = pointer.AttentionPooling(self.hidden_size * 2,
                                               self.hidden_size)

        # merge q into p
        self.aligner = nn.ModuleList(
            [SeqToSeqAtten() for _ in range(self.num_align_hops)])
        self.aligner_sfu = nn.ModuleList([
            SFU(self.hidden_size * 2,
                self.hidden_size * 2 * 3,
                dropout_p=self.dropout_p) for _ in range(self.num_align_hops)
        ])
        # self align
        self.self_aligner = nn.ModuleList(
            [SelfSeqAtten() for _ in range(self.num_align_hops)])
        self.self_aligner_sfu = nn.ModuleList([
            SFU(self.hidden_size * 2,
                self.hidden_size * 2 * 3,
                dropout_p=self.dropout_p) for _ in range(self.num_align_hops)
        ])
        # aggregation
        self.choose_agg = nn.ModuleList([
            encoder.Rnn(mode=self.mode,
                        input_size=self.hidden_size * 2,
                        hidden_size=self.hidden_size,
                        bidirectional=True,
                        dropout_p=self.dropout_p,
                        layer_num=1,
                        is_bn=self.is_bn) for _ in range(self.num_align_hops)
        ])

        # p_rep, choosing
        self.wp1 = nn.Linear(self.hidden_size * 2, self.hidden_size)
        self.wp2 = nn.Linear(self.hidden_size * 2, self.hidden_size)
        self.vp = nn.Linear(self.hidden_size, 1)
        self.bi_linear = nn.Linear(self.hidden_size * 2, self.hidden_size * 2)

        self.dropout = nn.Dropout(self.dropout_p)

        self.reset_param()
Пример #6
0
    def __init__(self, param):
        super(Model, self).__init__()

        self.mode = param['mode']
        self.hidden_size = param['hidden_size']
        self.dropout_p = param['dropout_p']
        self.encoder_dropout_p = param['encoder_dropout_p']
        self.encoder_layer_num = param['encoder_layer_num']
        self.is_bn = param['is_bn']

        # embedding
        self.embedding = embedding.ExtendEmbedding(param['embedding'])

        # encoder
        input_size = self.embedding.embedding_dim
        self.encoder = encoder.Rnn(mode=self.mode,
                                   input_size=input_size,
                                   hidden_size=self.hidden_size,
                                   dropout_p=self.encoder_dropout_p,
                                   bidirectional=True,
                                   layer_num=self.encoder_layer_num,
                                   is_bn=True)

        # align
        self.aligner = nn.ModuleList(
            [SeqToSeqAtten() for _ in range(num_align_hops)])
        self.aligner_sfu = nn.ModuleList([
            SFU(self.hidden_size * 2,
                self.hidden_size * 2 * 3,
                dropout_p=self.dropout_p) for _ in range(num_align_hops)
        ])

        # self align
        self.self_aligner = nn.ModuleList(
            [SelfSeqAtten() for _ in range(num_align_hops)])
        self.self_aligner_sfu = nn.ModuleList([
            SFU(self.hidden_size * 2,
                self.hidden_size * 2 * 3,
                dropout_p=self.dropout_p) for _ in range(num_align_hops)
        ])

        # aggregation
        self.aggregation = nn.ModuleList([
            encoder.Rnn(mode=self.mode,
                        input_size=self.hidden_size * 2,
                        hidden_size=self.hidden_size,
                        bidirectional=True,
                        dropout_p=self.dropout_p,
                        layer_num=1,
                        is_bn=self.is_bn) for _ in range(num_align_hops)
        ])

        # init zs
        self.init_state = pointer.AttentionPooling(
            input_size=self.hidden_size * 2, output_size=self.hidden_size * 2)

        # pointer
        self.ptr_net = nn.ModuleList([
            Pointer(self.hidden_size * 2, self.hidden_size, self.dropout_p)
            for _ in range(num_ptr_hops)
        ])