Exemplo n.º 1
0
    def network(self):
        """
        Implements the whole network of Match-LSTM.

        Returns:
            A tuple of LayerOutput objects containing the start and end
            probability distributions respectively.
        """
        self.check_and_create_data()
        self.create_shared_params()
        q_enc = self.get_enc(self.q_ids, type='q')
        p_encs = []
        p_matches = []
        for p in self.p_ids:
            p_encs.append(self.get_enc(p, type='p'))

        q_proj_left = layer.fc(size=self.emb_dim * 2,
                               bias_attr=False,
                               param_attr=Attr.Param(self.name + '_left_' +
                                                     '.wq'),
                               input=q_enc)
        q_proj_right = layer.fc(size=self.emb_dim * 2,
                                bias_attr=False,
                                param_attr=Attr.Param(self.name + '_right_' +
                                                      '.wq'),
                                input=q_enc)
        for i, p in enumerate(p_encs):
            left_out = self.recurrent_group(
                self.name + '_left_' + str(i),
                [layer.StaticInput(q_enc),
                 layer.StaticInput(q_proj_left), p],
                reverse=False)
            right_out = self.recurrent_group(
                self.name + '_right_' + str(i),
                [layer.StaticInput(q_enc),
                 layer.StaticInput(q_proj_right), p],
                reverse=True)
            match_seq = layer.concat(input=[left_out, right_out])
            match_seq_dropped = self.drop_out(match_seq, drop_rate=0.5)
            bi_match_seq = paddle.networks.bidirectional_lstm(
                input=match_seq_dropped,
                size=match_seq.size,
                fwd_mat_param_attr=Attr.Param('pn_f_enc_mat.w'),
                fwd_bias_param_attr=Attr.Param('pn_f_enc.bias',
                                               initial_std=0.),
                fwd_inner_param_attr=Attr.Param('pn_f_enc_inn.w'),
                bwd_mat_param_attr=Attr.Param('pn_b_enc_mat.w'),
                bwd_bias_param_attr=Attr.Param('pn_b_enc.bias',
                                               initial_std=0.),
                bwd_inner_param_attr=Attr.Param('pn_b_enc_inn.w'),
                return_seq=True)
            p_matches.append(bi_match_seq)

        all_docs = reduce(lambda x, y: layer.seq_concat(a=x, b=y), p_matches)
        all_docs_dropped = self.drop_out(all_docs, drop_rate=0.5)
        start = self.decode('start', all_docs_dropped)
        end = self.decode('end', all_docs_dropped)
        return start, end
Exemplo n.º 2
0
 def _attention_flow(self, h, u):
     bs = layer.recurrent_group(input=[h, layer.StaticInput(u)],
                                step=self._h_step,
                                reverse=False)
     b_weights = layer.mixed(act=Act.SequenceSoftmax(),
                             bias_attr=False,
                             input=layer.identity_projection(bs))
     h_step_scaled = layer.scaling(input=h, weight=b_weights)
     h_step = layer.pooling(input=h_step_scaled,
                            pooling_type=paddle.pooling.Sum())
     h_expr = layer.expand(input=h_step, expand_as=h)
     u_expr = layer.recurrent_group(input=[h, layer.StaticInput(u)],
                                    step=self._u_step,
                                    reverse=False)
     g = self._beta(h, u_expr, h_expr)
     return g
Exemplo n.º 3
0
    def network(self):
        """
        Implements the detail of the model.
        """
        self.check_and_create_data()
        self.create_shared_params()
        q_enc = self.get_enc(self.q_ids, type='q')
        a_enc = self.get_enc(self.a_ids, type='q')

        q_proj_left = layer.fc(size=self.emb_dim * 2,
                               bias_attr=False,
                               param_attr=Attr.Param(self.name + '_left.wq'),
                               input=q_enc)
        q_proj_right = layer.fc(size=self.emb_dim * 2,
                                bias_attr=False,
                                param_attr=Attr.Param(self.name + '_right.wq'),
                                input=q_enc)
        left_match = self.recurrent_group(
            self.name + '_left',
            [layer.StaticInput(q_enc),
             layer.StaticInput(q_proj_left), a_enc],
            reverse=False)
        right_match = self.recurrent_group(
            self.name + '_right',
            [layer.StaticInput(q_enc),
             layer.StaticInput(q_proj_right), a_enc],
            reverse=True)
        match_seq = layer.concat(input=[left_match, right_match])
        with layer.mixed(size=match_seq.size,
                         act=Act.Identity(),
                         layer_attr=Attr.ExtraLayerAttribute(drop_rate=0.2),
                         bias_attr=False) as dropped:
            dropped += layer.identity_projection(match_seq)
        match_result = layer.pooling(input=dropped,
                                     pooling_type=paddle.pooling.Max())
        cls = layer.fc(input=match_result,
                       act=Act.Softmax(),
                       size=self.label_dim)
        return cls
Exemplo n.º 4
0
    def network(self, question, evidence, qe_comm, ee_comm, conf):
        """
        Implements the whole network of Match-LSTM.

        Returns:
            A tuple of LayerOutput objects containing the start and end
            probability distributions respectively.
        """

        q_enc = self.get_enc(question, conf, type='q')
        p_enc = self.get_enc(evidence, conf, type='q')

        q_proj_left = layer.fc(size=conf.word_vec_dim * 2,
                               bias_attr=False,
                               param_attr=Attr.Param(self.name + '_left_' +
                                                     '.wq'),
                               input=q_enc)
        q_proj_right = layer.fc(size=conf.word_vec_dim * 2,
                                bias_attr=False,
                                param_attr=Attr.Param(self.name + '_right_' +
                                                      '.wq'),
                                input=q_enc)
        # StaticInput 定义了一个只读的Memory,由StaticInput指定的输入不会被recurrent_group拆解,
        # recurrent_group 循环展开的每个时间步总是能够引用所有输入,可以是一个非序列,或者一个单层序列。
        left_out = self.recurrent_group(self.name + '_left', [
            layer.StaticInput(q_enc),
            layer.StaticInput(q_proj_left), p_enc, qe_comm, ee_comm
        ],
                                        reverse=False)
        right_out = self.recurrent_group(self.name + '_right_', [
            layer.StaticInput(q_enc),
            layer.StaticInput(q_proj_right), p_enc, qe_comm, ee_comm
        ],
                                         reverse=True)
        match_seq = layer.concat(input=[left_out, right_out])
        return self.drop_out(match_seq, drop_rate=0.5)