Example #1
0
    def __init__(self, **kwargs):
        super(ESIM, self).__init__()
        self.vocab_size = kwargs["vocab_size"]
        self.embed_dim = kwargs["embed_dim"]
        self.hidden_size = kwargs["hidden_size"]
        self.batch_first = kwargs["batch_first"]
        self.dropout = kwargs["dropout"]
        self.n_labels = kwargs["num_classes"]
        self.gpu = kwargs["gpu"] and torch.cuda.is_available()

        self.drop = nn.Dropout(self.dropout)

        self.embedding = Encoder.Embedding(
            self.vocab_size,
            self.embed_dim,
            dropout=self.dropout,
            init_emb=kwargs["init_embedding"]
            if "inin_embedding" in kwargs.keys() else None,
        )

        self.embedding_layer = Encoder.Linear(self.embed_dim, self.hidden_size)

        self.encoder = Encoder.LSTM(input_size=self.embed_dim,
                                    hidden_size=self.hidden_size,
                                    num_layers=1,
                                    bias=True,
                                    batch_first=self.batch_first,
                                    bidirectional=True)

        self.bi_attention = Aggregator.Bi_Attention()
        self.mean_pooling = Aggregator.MeanPoolWithMask()
        self.max_pooling = Aggregator.MaxPoolWithMask()

        self.inference_layer = Encoder.Linear(self.hidden_size * 4,
                                              self.hidden_size)

        self.decoder = Encoder.LSTM(input_size=self.hidden_size,
                                    hidden_size=self.hidden_size,
                                    num_layers=1,
                                    bias=True,
                                    batch_first=self.batch_first,
                                    bidirectional=True)

        self.output = Decoder.MLP(
            [4 * self.hidden_size, self.hidden_size, self.n_labels],
            'tanh',
            dropout=self.dropout)
 def __init__(self, init_embed, hidden_dim, num_classes):
     super(RNN, self).__init__()
     self.embed = encoder.Embedding(init_embed)
     self.rnn = encoder.LSTM(
         input_size=self.embed.embedding_dim,
         hidden_size=hidden_dim,
         num_layers=1,
     )
     self.fc = nn.Linear(hidden_dim, num_classes)
Example #3
0
 def __init__(self, vocab_size, embedding_dim, hidden_dim, num_classes):
     super(RNN_model, self).__init__()
     self.embedding = nn.Embedding(vocab_size, embedding_dim)
     self.rnn = encoder.LSTM(
         input_size=self.embedding.embedding_dim,
         hidden_size=hidden_dim,
         num_layers=1,
     )
     self.output = nn.Linear(hidden_dim, num_classes)
 def __init__(self, embed_num, input_size, hidden_size, target_size):
     super(RNN, self).__init__()
     self.embed = encoder.Embedding((embed_num, input_size))
     self.rnn = encoder.LSTM(  
         input_size=input_size, 
         hidden_size=hidden_size, 
         num_layers=1,  
         batch_first=True, 
     )
     self.output = nn.Linear(hidden_size, target_size)
Example #5
0
    def __init__(self, args, init_embedding=None):
        super(SNLI, self).__init__()
        self.vocab_size = args["vocab_size"]
        self.embed_dim = args["embed_dim"]
        self.hidden_size = args["hidden_size"]
        self.batch_first = args["batch_first"]
        self.dropout = args["dropout"]
        self.n_labels = args["num_classes"]
        self.gpu = args["gpu"] and torch.cuda.is_available()

        self.embedding = Encoder.embedding.Embedding(self.vocab_size,
                                                     self.embed_dim,
                                                     init_emb=init_embedding,
                                                     dropout=self.dropout)

        self.embedding_layer = Encoder.Linear(self.embed_dim, self.hidden_size)

        self.encoder = Encoder.LSTM(input_size=self.embed_dim,
                                    hidden_size=self.hidden_size,
                                    num_layers=1,
                                    bias=True,
                                    batch_first=self.batch_first,
                                    bidirectional=True)

        self.inference_layer = Encoder.Linear(self.hidden_size * 4,
                                              self.hidden_size)

        self.decoder = Encoder.LSTM(input_size=self.hidden_size,
                                    hidden_size=self.hidden_size,
                                    num_layers=1,
                                    bias=True,
                                    batch_first=self.batch_first,
                                    bidirectional=True)

        self.output = Decoder.MLP(
            [4 * self.hidden_size, self.hidden_size, self.n_labels], 'tanh')
Example #6
0
    def __init__(self,
                 char_init_embed,
                 word_init_embed,
                 pos_init_embed,
                 spo_embed_dim,
                 sentence_length,
                 hidden_size,
                 num_classes,
                 dropout=0.3,
                 id2words=None,
                 encoding_type='bmes'):

        super().__init__()

        # self.Embedding = nn.Embedding(init_embed)
        #print(char_init_embed)
        self.char_embed = nn.Embedding(char_init_embed[0], char_init_embed[1])
        self.word_embed = nn.Embedding(word_init_embed[0], word_init_embed[1])
        self.pos_embed = nn.Embedding(pos_init_embed[0], pos_init_embed[1])
        # spo embed size: 50
        self.embed_dim = self.char_embed.embedding_dim + self.word_embed.embedding_dim + self.pos_embed.embedding_dim + spo_embed_dim
        # sentence length
        #self.sen_len = sentence_length
        #self.zeros = torch.zeros(self.sen_len, dtype=torch.long)

        self.norm1 = torch.nn.LayerNorm(self.embed_dim)
        self.Rnn = encoder.LSTM(input_size=self.embed_dim,
                                hidden_size=hidden_size,
                                num_layers=2,
                                dropout=dropout,
                                bidirectional=True,
                                batch_first=True)
        self.Linear1 = nn.Linear(hidden_size * 2, hidden_size * 2 // 3)
        self.norm2 = torch.nn.LayerNorm(hidden_size * 2 // 3)
        self.relu = torch.nn.LeakyReLU()
        self.drop = torch.nn.Dropout(dropout)
        self.Linear2 = nn.Linear(hidden_size * 2 // 3, num_classes)

        if id2words is None:
            self.Crf = CRF(num_classes, include_start_end_trans=False)
        else:
            self.Crf = CRF(num_classes,
                           include_start_end_trans=False,
                           allowed_transitions=allowed_transitions(
                               id2words, encoding_type=encoding_type))