예제 #1
0
    def __init__(self,
                 hidden_size,
                 cell_type="lstm",
                 dropout=0.0,
                 init_scale=-1,
                 name="BasicRNNCell"):
        """init of class

        Args:
            hidden_size (TYPE): NULL
            cell_type (str): lstm|gru
            dropout (TYPE): Default is 0.0
            init_scale (TYPE): Default is -1
            name (TYPE): Default is "BasicRNNCell"

        """
        super(BasicRNNCell, self).__init__()

        self._hidden_size = hidden_size
        self._cell_type = cell_type.lower()
        self._dropout = dropout
        self._init_scale = init_scale
        self._name = name

        param = fluid.ParamAttr(initializer=nn_utils.uniform(self._init_scale))
        bias = fluid.ParamAttr(initializer=nn_utils.zero)
        if self._cell_type == 'lstm':
            self._cell = layers.LSTMCell(self._hidden_size,
                                         param,
                                         bias,
                                         name=self._name)
        elif self._cell_type == 'gru':
            self._cell = layers.GRUCell(self._hidden_size,
                                        param,
                                        bias,
                                        name=self._name)
        else:
            raise ValueError(
                "cell type only supported <lstm|gru>, but got %s" %
                (cell_type))
예제 #2
0
    def __init__(self,
                 hidden_size,
                 dropout=0.,
                 init_scale=-1,
                 name="rnn_decode_cell"):
        """init of class

        Args:
            hidden_size (TYPE): NULL
            dropout (TYPE): Default is 0.
            init_scale (TYPE): Default is -1, means paddle default initializer is used.
            name (str): param name scope

        """
        super(RNNDecodeCell, self).__init__()

        self._hidden_size = hidden_size
        self._dropout = dropout
        self._init_scale = init_scale
        self._name = name

        param = fluid.ParamAttr(initializer=nn_utils.uniform(self._init_scale))
        bias = fluid.ParamAttr(initializer=nn_utils.zero)
        self.rnn_cell = layers.LSTMCell(hidden_size, param, bias, name=name)
    def _lf_embedder(self, tokens, token_lens=None):
        """lf embedder.

        Args:
            tokens (Variable): [batch_size, seq_len]
            token_lens (Variable): Default is None.

        Returns: TODO

        Raises: NULL

        """
        self._batch_size = layers.shape(self.question_encoding)[0]

        ##  Grammar Rule Embedding
        self._grammar_vocab = tensor.cast(tensor.assign(
            self.grammar.gmr_vocab.astype(np.int32)),
                                          dtype='int64')
        self._grammar_emb = fluid.embedding(
            input=self._grammar_vocab,
            size=[self.grammar.grammar_size, self.lf_emb_size],
            dtype='float32',
            is_sparse=False,
            param_attr=fluid.ParamAttr(name="lf_embedding",
                                       initializer=nn_utils.uniform(
                                           self.init_scale)))

        batch_emb_lookup_grammar = layers.expand(
            layers.unsqueeze(self._grammar_emb, [0]), [self._batch_size, 1, 1])

        def _table_to_lf_input(ori_encoding):
            """trans ori_encoding to size of lf_embedding
            """
            output = layers.fc(input=ori_encoding,
                               size=self.lf_emb_size,
                               num_flatten_dims=2,
                               **nn_utils.param_attr('fc_table2lf_input',
                                                     self.init_scale,
                                                     need_bias=False))
            return output

        batch_emb_lookup_all = tensor.concat([
            batch_emb_lookup_grammar,
            _table_to_lf_input(self.tname_encoding),
            _table_to_lf_input(self.cname_encoding),
            _table_to_lf_input(self.value_encoding)
        ],
                                             axis=1)
        lf_embedding = nn_utils.batch_gather_2d(batch_emb_lookup_all, tokens)

        ## Grammar Rule 类型 Embedding
        self._grammar2name = layers.cast(layers.assign(
            self.grammar.gmr2name_arr.astype(np.int32)),
                                         dtype='int64')
        lf_name = layers.reshape(layers.gather(
            self._grammar2name, layers.reshape(tokens, shape=[-1])),
                                 shape=tokens.shape)
        lf_name.stop_gradient = True
        lf_name_emb = fluid.embedding(
            input=lf_name,
            size=[self.grammar.name_size, self.lf_name_emb_size],
            dtype='float32',
            is_sparse=False,
            param_attr=fluid.ParamAttr(name="lf_name_embedding",
                                       initializer=nn_utils.uniform(
                                           self.init_scale)))

        output = layers.concat([lf_embedding, lf_name_emb], axis=-1)
        if token_lens is not None:
            mask = layers.sequence_mask(token_lens,
                                        maxlen=layers.shape(tokens)[1],
                                        dtype='float32')
            output = layers.elementwise_mul(output, mask, axis=0)
        return output
    def __init__(self, params_dict, save_predict_file=None):
        """init of class

        Args:
            params_dict (TYPE): NULL
            save_predict_file (str): 保存预测结果。仅在预测阶段使用。默认为 None


        """
        super(Text2SQL, self).__init__(params_dict)

        self.optimizer_params = params_dict['optimizer']
        self.hidden_size = params_dict["hidden_size"]
        self.grammar = Grammar(params_dict['grammar_file'])
        self._original_dropout = params_dict['dropout']
        self.dropout = self._original_dropout
        self.init_scale = -1  # '<0' means use paddle default initializer

        self.encoder_params = params_dict["encoder"]
        self.encoder_type = self.encoder_params["type"]
        if self.encoder_type == 'LSTM':
            self.embedding_size = self.encoder_params["embedding_size"]
            self.encoder_layers = self.encoder_params["encoder_layers"]
            self.table_enc_type = self.encoder_params.get(
                "table_encoder", "simple_sum")
            self.table_attention = self.encoder_params["table_attention"]
            self.finetune_emb = self.encoder_params["finetune_emb"]

            with open(self.encoder_params['vocab_file']) as ifs:
                vocab = [x.rstrip('\n') for x in ifs]
            self.src_vocab_size = len(vocab)

            embedding_file = self.encoder_params.get('embedding_file', None)
            if embedding_file is None:  # uniform 初始化,Embedding 从头训
                self._emb_weight_init = nn_utils.uniform(self.init_scale)
            else:  # 使用预训练的 Embedding 初始化
                dct_vocab = dict([(word, idx)
                                  for idx, word in enumerate(vocab)])
                weight_mat = np.random.normal(
                    0., 1., size=[self.src_vocab_size, self.embedding_size])
                with open(embedding_file) as ifs:
                    for line in ifs:
                        lst_fields = line.rstrip().split(' ')
                        if len(lst_fields) != self.embedding_size + 1:
                            raise ValueError(
                                'embedding file format error: %s' %
                                (line.rstrip('\n')))
                        word_id = dct_vocab[lst_fields[0]]
                        emb = [float(x) for x in lst_fields[1:]]
                        weight_mat[word_id, :] = emb
                self._emb_weight_init = fluid.initializer.NumpyArrayInitializer(
                    weight_mat)
        elif self.encoder_type == "ErnieTokenEmbedding":
            self.table_enc_type = self.encoder_params.get(
                "table_encoder", "simple_sum")
            self.table_attention = self.encoder_params["table_attention"]
        else:
            raise ValueError(
                "Encoder Type Error. Expect LSTM/ErnieTokenEmbedding, bug got %s"
                % (self.encoder_type))

        self.decoder_params = params_dict['decoder']
        self.max_infer_step = self.decoder_params["max_infer_step"]
        self.lf_emb_size = self.decoder_params["lf_emb_size"]
        self.lf_name_emb_size = self.decoder_params["lf_name_emb_size"]
        self.beam_size = self.decoder_params["beam_size"]

        self._batch_size = None  # Variable, 会在 forward 中初始化
        self.all_inputs_name = None  # list, 在 _rnn_encoder/_ernie_encoder 中初始化
        #### 运行时创建并初始化的成员 ########################
        ### self._read_question() --> self.question_xxx     ##
        ### self._read_table_name() --> self.tname_xxx      ##
        ### self._read_column_name() --> self.cname_xxx     ##
        ### self._read_value() --> self.value_xxx           ##

        self.ofs_predict_result = None
        if save_predict_file is not None:
            self.ofs_predict_result = open(save_predict_file, 'w')
        self.best_acc = -1
        self.best_step = -1
        self.best_epoch = -1