コード例 #1
0
 def __init__(self, ernie, crf_lr=100):
     super().__init__()
     self.num_classes = ernie.num_classes
     self.ernie = ernie  # allow ernie to be config
     self.crf = LinearChainCrf(
         self.num_classes, crf_lr=crf_lr, with_start_stop_tag=False)
     self.crf_loss = LinearChainCrfLoss(self.crf)
     self.viterbi_decoder = ViterbiDecoder(
         self.crf.transitions, with_start_stop_tag=False)
コード例 #2
0
    def __init__(self,
                 word_emb_dim,
                 hidden_size,
                 vocab_size,
                 num_labels,
                 emb_lr=2.0,
                 crf_lr=0.2,
                 with_start_stop_tag=True):
        super(BiGruCrf, self).__init__()
        self.word_emb_dim = word_emb_dim
        self.vocab_size = vocab_size
        self.num_labels = num_labels
        self.hidden_size = hidden_size
        self.emb_lr = emb_lr
        self.crf_lr = crf_lr
        self.init_bound = 0.1

        self.word_embedding = nn.Embedding(
            num_embeddings=self.vocab_size,
            embedding_dim=self.word_emb_dim,
            weight_attr=paddle.ParamAttr(learning_rate=self.emb_lr,
                                         initializer=nn.initializer.Uniform(
                                             low=-self.init_bound,
                                             high=self.init_bound)))

        self.gru = nn.GRU(
            input_size=self.word_emb_dim,
            hidden_size=self.hidden_size,
            num_layers=2,
            direction='bidirectional',
            weight_ih_attr=paddle.ParamAttr(
                initializer=nn.initializer.Uniform(low=-self.init_bound,
                                                   high=self.init_bound),
                regularizer=paddle.regularizer.L2Decay(coeff=1e-4)),
            weight_hh_attr=paddle.ParamAttr(
                initializer=nn.initializer.Uniform(low=-self.init_bound,
                                                   high=self.init_bound),
                regularizer=paddle.regularizer.L2Decay(coeff=1e-4)))

        self.fc = nn.Linear(
            in_features=self.hidden_size * 2,
            out_features=self.num_labels + 2 \
                if with_start_stop_tag else self.num_labels,
            weight_attr=paddle.ParamAttr(
                initializer=nn.initializer.Uniform(
                    low=-self.init_bound, high=self.init_bound),
                regularizer=paddle.regularizer.L2Decay(coeff=1e-4)))

        self.crf = LinearChainCrf(self.num_labels, self.crf_lr,
                                  with_start_stop_tag)
        self.crf_loss = LinearChainCrfLoss(self.crf)
        self.viterbi_decoder = ViterbiDecoder(self.crf.transitions,
                                              with_start_stop_tag)
コード例 #3
0
ファイル: model.py プロジェクト: sminvdnmicw/PaddleNLP
 def __init__(self,
              emb_size,
              hidden_size,
              word_num,
              label_num,
              use_w2v_emb=False):
     super(BiGRUWithCRF, self).__init__()
     if use_w2v_emb:
         self.word_emb = TokenEmbedding(
             extended_vocab_path='./data/word.dic', unknown_token='OOV')
     else:
         self.word_emb = nn.Embedding(word_num, emb_size)
     self.gru = nn.GRU(emb_size,
                       hidden_size,
                       num_layers=2,
                       direction='bidirectional')
     self.fc = nn.Linear(hidden_size * 2, label_num + 2)  # BOS EOS
     self.crf = LinearChainCrf(label_num)
     self.crf_loss = LinearChainCrfLoss(self.crf)
     self.viterbi_decoder = ViterbiDecoder(self.crf.transitions)
コード例 #4
0
    def __init__(self, skep, num_classes):
        super().__init__()
        self.num_classes = num_classes
        self.skep = skep  # allow skep to be config
        gru_hidden_size = 128

        self.gru = nn.GRU(self.skep.config["hidden_size"],
                          gru_hidden_size,
                          num_layers=2,
                          direction='bidirect')
        self.fc = nn.Linear(
            gru_hidden_size * 2,
            self.num_classes,
            weight_attr=paddle.ParamAttr(
                initializer=nn.initializer.Uniform(low=-0.1, high=0.1),
                regularizer=paddle.regularizer.L2Decay(coeff=1e-4)))
        self.crf = LinearChainCrf(self.num_classes,
                                  crf_lr=0.2,
                                  with_start_stop_tag=False)
        self.crf_loss = LinearChainCrfLoss(self.crf)
        self.viterbi_decoder = ViterbiDecoder(self.crf.transitions,
                                              with_start_stop_tag=False)
コード例 #5
0
ファイル: modeling.py プロジェクト: sminvdnmicw/PaddleNLP
    def __init__(self,
                 ernie_ctm,
                 num_tag,
                 num_cls_label,
                 crf_lr=100,
                 ignore_index=0):
        super(ErnieCtmWordtagModel, self).__init__()
        self.num_tag = num_tag
        self.num_cls_label = num_cls_label
        self.ernie_ctm = ernie_ctm
        self.tag_classifier = nn.Linear(self.ernie_ctm.config["hidden_size"],
                                        self.num_tag)
        self.sent_classifier = nn.Linear(self.ernie_ctm.config["hidden_size"],
                                         self.num_cls_label)
        self.crf = LinearChainCrf(
            self.num_tag, crf_lr, with_start_stop_tag=False)
        self.crf_loss = LinearChainCrfLoss(self.crf)
        self.viterbi_decoder = ViterbiDecoder(
            self.crf.transitions, with_start_stop_tag=False)
        self.ignore_index = ignore_index

        self.apply(self.init_weights)