Exemple #1
0
    def __init__(self,
                        vector_size,
                        num_label,
                        rnn_layers=3,
                        arc_mlp_size=500,
                        label_mlp_size=100,
                        dropout=0.3,
                        use_greedy_infer=False):


        super(BiaffineParser, self).__init__()
        rnn_out_size = vector_size
        self.timestep_drop = TimestepDropout(dropout)

        self.mlp = nn.Sequential(nn.Linear(rnn_out_size, arc_mlp_size * 2 + label_mlp_size * 2),
                                          nn.LeakyReLU(0.1),
                                          TimestepDropout(p=dropout),)
        self.arc_mlp_size = arc_mlp_size
        self.label_mlp_size = label_mlp_size
        self.arc_predictor = ArcBiaffine(arc_mlp_size, bias=True)
        self.label_predictor = LabelBilinear(label_mlp_size, label_mlp_size, num_label, bias=True)
        self.use_greedy_infer = use_greedy_infer
        self.reset_parameters()
        self.dropout = dropout


        self.num_label = num_label
    def __init__(self, embed, num_label, arc_mlp_size=500, label_mlp_size=100, dropout=0.5, use_greedy_infer=False, app_index=0):
        super(BiaffineParser, self).__init__()

        self.embed = embed

        self.mlp = nn.Sequential(nn.Linear(self.embed.embed_size, arc_mlp_size * 2 + label_mlp_size * 2),
                                          nn.LeakyReLU(0.1),
                                          TimestepDropout(p=dropout),)
        self.arc_mlp_size = arc_mlp_size
        self.label_mlp_size = label_mlp_size
        self.arc_predictor = ArcBiaffine(arc_mlp_size, bias=True)
        self.label_predictor = LabelBilinear(label_mlp_size, label_mlp_size, num_label, bias=True)
        self.use_greedy_infer = use_greedy_infer
        self.reset_parameters()

        self.app_index = app_index
        self.num_label = num_label
        if self.app_index != 0:
            raise ValueError("现在app_index必须等于0")

        self.dropout = nn.Dropout(dropout)
    def __init__(self, char_vocab_size,
                        emb_dim,
                         bigram_vocab_size,
                        trigram_vocab_size,
                        num_label,
                        rnn_layers=3,
                        rnn_hidden_size=800, #单向的数量
                        arc_mlp_size=500,
                        label_mlp_size=100,
                        dropout=0.3,
                        encoder='lstm',
                        use_greedy_infer=False,
                         app_index = 0,
                         pre_chars_embed=None,
                         pre_bigrams_embed=None,
                         pre_trigrams_embed=None):


        super(BiaffineParser, self).__init__()
        rnn_out_size = 2 * rnn_hidden_size
        self.char_embed = Embedding((char_vocab_size, emb_dim))
        self.bigram_embed = Embedding((bigram_vocab_size, emb_dim))
        self.trigram_embed = Embedding((trigram_vocab_size, emb_dim))
        if pre_chars_embed:
            self.pre_char_embed = Embedding(pre_chars_embed)
            self.pre_char_embed.requires_grad = False
        if pre_bigrams_embed:
            self.pre_bigram_embed = Embedding(pre_bigrams_embed)
            self.pre_bigram_embed.requires_grad = False
        if pre_trigrams_embed:
            self.pre_trigram_embed = Embedding(pre_trigrams_embed)
            self.pre_trigram_embed.requires_grad = False
        self.timestep_drop = TimestepDropout(dropout)
        self.encoder_name = encoder

        if encoder == 'var-lstm':
            self.encoder = VarLSTM(input_size=emb_dim*3,
                                   hidden_size=rnn_hidden_size,
                                   num_layers=rnn_layers,
                                   bias=True,
                                   batch_first=True,
                                   input_dropout=dropout,
                                   hidden_dropout=dropout,
                                   bidirectional=True)
        elif encoder == 'lstm':
            self.encoder = nn.LSTM(input_size=emb_dim*3,
                                   hidden_size=rnn_hidden_size,
                                   num_layers=rnn_layers,
                                   bias=True,
                                   batch_first=True,
                                   dropout=dropout,
                                   bidirectional=True)

        else:
            raise ValueError('unsupported encoder type: {}'.format(encoder))

        self.mlp = nn.Sequential(nn.Linear(rnn_out_size, arc_mlp_size * 2 + label_mlp_size * 2),
                                          nn.LeakyReLU(0.1),
                                          TimestepDropout(p=dropout),)
        self.arc_mlp_size = arc_mlp_size
        self.label_mlp_size = label_mlp_size
        self.arc_predictor = ArcBiaffine(arc_mlp_size, bias=True)
        self.label_predictor = LabelBilinear(label_mlp_size, label_mlp_size, num_label, bias=True)
        self.use_greedy_infer = use_greedy_infer
        self.reset_parameters()
        self.dropout = dropout

        self.app_index = app_index
        self.num_label = num_label
        if self.app_index != 0:
            raise ValueError("现在app_index必须等于0")