Ejemplo n.º 1
0
def build_encoder(opt, embeddings):
    """
    Various encoder dispatcher function.
    Args:
        opt: the option in current environment.
        embeddings (Embeddings): vocab embeddings for this encoder.
    """
    if opt.encoder_type == "transformer":
        return TransformerEncoder(opt.enc_layers, opt.enc_rnn_size,
                                  opt.heads, opt.transformer_ff,
                                  opt.dropout, embeddings)
    elif opt.encoder_type == "cnn":
        return CNNEncoder(opt.enc_layers, opt.enc_rnn_size,
                          opt.cnn_kernel_width,
                          opt.dropout, embeddings)
    elif opt.encoder_type == "mean":
        return MeanEncoder(opt.enc_layers, embeddings)
    elif opt.encoder_type == "dnc":
        #import pdb; pdb.set_trace()
        return MEMEncoder(opt.rnn_type, opt.brnn, opt.enc_layers,
                          opt.enc_rnn_size, opt.dropout, embeddings)
    else:
        # "rnn" or "brnn"
        return RNNEncoder(opt.rnn_type, opt.brnn, opt.enc_layers,
                          opt.enc_rnn_size, opt.dropout, embeddings,
                          opt.bridge)
Ejemplo n.º 2
0
def build_encoder(opt, embeddings, embeddings_latt = False, feat_vec_size = 512):
    """
    Various encoder dispatcher function.
    Args:
        opt: the option in current environment.
        embeddings (Embeddings): vocab embeddings for this encoder.
        embeddings_latt: embeddings of senses if lattice is used    # latt!!!
        feat_vec_size: for adaptable feat_vec_size             # latt!!!
    """
#latt
    if opt.encoder_type == "transformer":
        if embeddings_latt != False:  #latt
            return TransformerEncoder(opt.enc_layers, opt.rnn_size,
                                      opt.heads, opt.transformer_ff,
                                      opt.dropout, embeddings, embeddings_latt, feat_vec_size) #latt
        else:
            return TransformerEncoder(opt.enc_layers, opt.rnn_size,
                                      opt.heads, opt.transformer_ff,
                                      opt.dropout, embeddings, embeddings_latt, feat_vec_size) #latt
#latt
    elif opt.encoder_type == "cnn":
        return CNNEncoder(opt.enc_layers, opt.rnn_size,
                          opt.cnn_kernel_width,
                          opt.dropout, embeddings)
    elif opt.encoder_type == "mean":
        return MeanEncoder(opt.enc_layers, embeddings)
    else:
        # "rnn" or "brnn"
        return RNNEncoder(opt.rnn_type, opt.brnn, opt.enc_layers,
                          opt.rnn_size, opt.dropout, embeddings,
                          opt.bridge)
Ejemplo n.º 3
0
def build_encoder(opt, embeddings):
    """
    Various encoder dispatcher function.
    Args:
        opt: the option in current environment.
        embeddings (Embeddings): vocab embeddings for this encoder.
    """
    if opt.encoder_type == "transformer":
        return TransformerEncoder(opt.enc_layers, opt.rnn_size,
                                  opt.heads, opt.transformer_ff,
                                  opt.dropout, embeddings), None
    elif opt.encoder_type == "cnn":
        return CNNEncoder(opt.enc_layers, opt.rnn_size,
                          opt.cnn_kernel_width,
                          opt.dropout, embeddings), None
    elif opt.encoder_type == "mean":
        return MeanEncoder(opt.enc_layers, embeddings), None
    else:
        # "rnn" or "brnn"
        word_encoder = RNNEncoder(opt.rnn_type, opt.brnn, opt.enc_layers,
                          opt.rnn_size, opt.dropout, embeddings, None, opt.bridge)
        if opt.rnn_type == "LSTM":
            emb_size = opt.enc_layers * opt.rnn_size * 2
        else:
            emb_size = opt.enc_layers * opt.rnn_size
        sen_encoder = RNNEncoder(opt.rnn_type, opt.brnn, opt.sen_enc_layers,
                          opt.sen_rnn_size, opt.dropout, None, emb_size, opt.bridge)
        return word_encoder, sen_encoder
Ejemplo n.º 4
0
def build_encoder(opt, embeddings, main_encoder=None):
    """
    Various encoder dispatcher function.
    Args:
        opt: the option in current environment.
        embeddings (Embeddings): vocab embeddings for this encoder.
    """
    if opt.encoder_type == "transformer":
        encoder = TransformerEncoder(opt.enc_layers,
                                     opt.enc_rnn_size,
                                     opt.heads,
                                     opt.transformer_ff,
                                     opt.dropout,
                                     embeddings,
                                     main_encoder=main_encoder,
                                     mtl_opt=opt)
    elif opt.encoder_type == "cnn":
        encoder = CNNEncoder(opt.enc_layers, opt.enc_rnn_size,
                             opt.cnn_kernel_width, opt.dropout, embeddings)
    elif opt.encoder_type == "mean":
        encoder = MeanEncoder(opt.enc_layers, embeddings)
    else:
        encoder = RNNEncoder(opt.rnn_type,
                             opt.brnn,
                             opt.enc_layers,
                             opt.enc_rnn_size,
                             opt.dropout,
                             embeddings,
                             opt.bridge,
                             main_encoder=main_encoder,
                             mtl_opt=opt)
    return encoder
Ejemplo n.º 5
0
    def __init__(self,
                 rnn_type,
                 encoder_type,
                 enc_layers,
                 hidden_size,
                 dropout=0.0,
                 embeddings=None):
        super(QueryEncoder, self).__init__()

        assert embeddings is not None
        self.embeddings = embeddings

        self.rnn_type = rnn_type

        bidirectional = True if encoder_type == 'brnn' else False

        self.src_encoder = RNNEncoder(rnn_type, bidirectional, enc_layers,
                                      hidden_size, dropout,
                                      embeddings.embedding_size)
        self.answer_encoder = MeanEncoder()
Ejemplo n.º 6
0
def build_encoder(opt, embeddings):
    """
    Various encoder dispatcher function.
    Args:
        opt: the option in current environment.
        embeddings (Embeddings): vocab embeddings for this encoder.
    """
    if opt.encoder_type == "transformer":
        encoder = TransformerEncoder(opt.enc_layers, opt.enc_rnn_size,
                                     opt.heads, opt.transformer_ff,
                                     opt.dropout, embeddings)
    elif opt.encoder_type == "cnn":
        encoder = CNNEncoder(opt.enc_layers, opt.enc_rnn_size,
                             opt.cnn_kernel_width, opt.dropout, embeddings)
    elif opt.encoder_type == "mean":
        encoder = MeanEncoder(opt.enc_layers, embeddings)
    elif opt.encoder_type == "hr_brnn":
        bi_enc = True
        encoder = HREncoder(opt.rnn_type, bi_enc, opt.enc_layers,
                            opt.enc_rnn_size, opt.dropout, embeddings,
                            opt.bridge)
    elif opt.encoder_type == "seq_hr_brnn":
        bi_enc = True
        encoder = SeqHREncoder(opt.rnn_type, bi_enc, opt.enc_layers,
                               opt.enc_rnn_size, opt.dropout, embeddings,
                               opt.bridge)
    elif opt.encoder_type == "tg_brnn":
        bi_enc = True
        encoder = TGEncoder(opt.rnn_type, bi_enc, opt.enc_layers,
                            opt.enc_rnn_size, opt.dropout, embeddings)
    else:
        bi_enc = 'brnn' in opt.encoder_type
        encoder = RNNEncoder(opt.rnn_type,
                             bi_enc,
                             opt.enc_layers,
                             opt.enc_rnn_size,
                             opt.dropout,
                             embeddings,
                             opt.bridge,
                             use_catSeq_dp=opt.use_catSeq_dp)
    return encoder
def build_encoder(opt, embeddings):
    """
    Various encoder dispatcher function.
    Args:
        opt: the option in current environment.
        embeddings (Embeddings): vocab embeddings for this encoder.
    """
    if opt.encoder_type == "transformer":
        return TransformerEncoder(
            opt.enc_layers,
            opt.enc_rnn_size,
            opt.heads,
            opt.transformer_ff,
            opt.dropout,
            embeddings,
            opt.self_attention_function,
            opt.self_attention_alpha,
            opt.self_attention_bisect_iter,
        )
    elif opt.encoder_type == "cnn":
        return CNNEncoder(
            opt.enc_layers,
            opt.enc_rnn_size,
            opt.cnn_kernel_width,
            opt.dropout,
            embeddings,
        )
    elif opt.encoder_type == "mean":
        return MeanEncoder(opt.enc_layers, embeddings)
    else:
        # "rnn" or "brnn"
        return RNNEncoder(
            opt.rnn_type,
            opt.brnn,
            opt.enc_layers,
            opt.enc_rnn_size,
            opt.dropout,
            embeddings,
            opt.bridge,
        )
Ejemplo n.º 8
0
def build_encoder(opt, embeddings):
    """
    Various encoder dispatcher function.
    Args:
        opt: the option in current environment.
        embeddings (Embeddings): vocab embeddings for this encoder.
    """
    if opt.encoder_type == "transformer":
        return TransformerEncoder(opt.enc_layers, opt.enc_rnn_size,
                                  opt.heads, opt.transformer_ff,
                                  opt.dropout, embeddings)
    elif opt.encoder_type == "cnn":
        return CNNEncoder(opt.enc_layers, opt.enc_rnn_size,
                          opt.cnn_kernel_width,
                          opt.dropout, embeddings)
    elif opt.encoder_type == "mean":
        return MeanEncoder(opt.enc_layers, embeddings)
    elif opt.encoder_type == "rnntreelstm" or opt.encoder_type == "treelstm":
        opt.brnn = True if opt.encoder_type == "rnntreelstm" else False
        return TreeLSTMEncoder(opt.rnn_type, opt.brnn, opt.enc_layers,
                        opt.rnn_size, opt.dropout, embeddings,
                        opt.bridge, False)
    elif opt.encoder_type == "rnnbitreelstm" or opt.encoder_type == "bitreelstm":
        opt.brnn = True if opt.encoder_type == "rnnbitreelstm" else False
        return TreeLSTMEncoder(opt.rnn_type, opt.brnn, opt.enc_layers,
                        opt.rnn_size, opt.dropout, embeddings,
                        opt.bridge, True)    
    elif opt.encoder_type == "rnngcn" or opt.encoder_type == "gcn":
        opt.brnn = True if opt.encoder_type == "rnngcn" else False
        return GCNEncoder(opt.rnn_type, opt.brnn, opt.enc_layers,
                          opt.rnn_size, opt.dropout, embeddings,
                          opt.bridge, opt.gcn_dropout, 
                          opt.gcn_edge_dropout, opt.n_gcn_layers, 
                          opt.activation, opt.highway)    
    else:
        # "rnn" or "brnn"
        return RNNEncoder(opt.rnn_type, opt.brnn, opt.enc_layers,
                          opt.enc_rnn_size, opt.dropout, embeddings,
                          opt.bridge)
Ejemplo n.º 9
0
def build_encoder(opt, embeddings, fields=None):
    """
    Various encoder dispatcher function.
    Args:
        opt: the option in current environment.
        embeddings (Embeddings): vocab embeddings for this encoder.
    """
    if opt.encoder_type == 'simple_context_0':
        # bottom n-1 layers are shared
        return SimpleContextTransformerEncoder(
                                  opt.enc_layers - 1, opt.rnn_size,
                                  opt.heads, opt.transformer_ff,
                                  opt.dropout, embeddings,
                                  selected_ctx=0)
    elif opt.encoder_type == 'simple_context_1':
        # bottom n-1 layers are shared
        return SimpleContextTransformerEncoder(
                                  opt.enc_layers - 1, opt.rnn_size,
                                  opt.heads, opt.transformer_ff,
                                  opt.dropout, embeddings,
                                  selected_ctx=1)

    elif opt.encoder_type == "transformer":
        return TransformerEncoder(opt.enc_layers, opt.rnn_size,
                                  opt.heads, opt.transformer_ff,
                                  opt.dropout, embeddings)
    elif opt.encoder_type == "cnn":
        return CNNEncoder(opt.enc_layers, opt.rnn_size,
                          opt.cnn_kernel_width,
                          opt.dropout, embeddings)
    elif opt.encoder_type == "mean":
        return MeanEncoder(opt.enc_layers, embeddings)
    else:
        # "rnn" or "brnn"
        return RNNEncoder(opt.rnn_type, opt.brnn, opt.enc_layers,
                          opt.rnn_size, opt.dropout, embeddings,
                          opt.bridge)