Exemplo n.º 1
0
 def __init__(self, embedding_matrix, opt):
     super(PBAN, self).__init__()
     self.text_embed = nn.Embedding.from_pretrained(
         torch.tensor(embedding_matrix, dtype=torch.float))
     self.pos_embed = nn.Embedding(opt.max_length, opt.position_dim)
     self.left_gru = DynamicLSTM(opt.embed_dim,
                                 opt.hidden_dim,
                                 num_layers=1,
                                 batch_first=True,
                                 bidirectional=True,
                                 rnn_type='GRU')
     self.right_gru = DynamicLSTM(opt.embed_dim + opt.position_dim,
                                  opt.hidden_dim,
                                  num_layers=1,
                                  batch_first=True,
                                  bidirectional=True,
                                  rnn_type='GRU')
     self.weight_m = nn.Parameter(
         torch.Tensor(opt.hidden_dim * 2, opt.hidden_dim * 2))
     self.bias_m = nn.Parameter(torch.Tensor(1))
     self.weight_n = nn.Parameter(
         torch.Tensor(opt.hidden_dim * 2, opt.hidden_dim * 2))
     self.bias_n = nn.Parameter(torch.Tensor(1))
     self.w_r = nn.Linear(opt.hidden_dim * 2, opt.hidden_dim)
     self.w_s = nn.Linear(opt.hidden_dim, opt.polarities_dim)
Exemplo n.º 2
0
 def __init__(self, embedding_matrix, opt):
     super(IAN, self).__init__()
     self.opt = opt
     self.embed = nn.Embedding.from_pretrained(
         torch.tensor(embedding_matrix, dtype=torch.float))
     self.lstm_context = DynamicLSTM(opt.embed_dim,
                                     opt.hidden_dim,
                                     num_layers=1,
                                     batch_first=True)
     self.lstm_aspect = DynamicLSTM(opt.embed_dim,
                                    opt.hidden_dim,
                                    num_layers=1,
                                    batch_first=True)
     self.attention_aspect = Attention(opt.hidden_dim,
                                       score_function='bi_linear')
     self.attention_context = Attention(opt.hidden_dim,
                                        score_function='bi_linear')
     self.dense = nn.Linear(opt.hidden_dim * 2, opt.polarities_dim)
Exemplo n.º 3
0
 def __init__(self, embedding_matrix, opt):
     super(LSTM, self).__init__()
     self.embed = nn.Embedding.from_pretrained(
         torch.tensor(embedding_matrix, dtype=torch.float))
     self.lstm = DynamicLSTM(opt.embed_dim,
                             opt.hidden_dim,
                             num_layers=1,
                             batch_first=True)
     self.dense = nn.Linear(opt.hidden_dim, opt.polarities_dim)
Exemplo n.º 4
0
    def __init__(self, embedding_matrix, opt):
        super(BiLSTMCRF, self).__init__()
        self.opt = opt

        WD = opt.word_emb_dim  # dimension of word embeddings
        CD = opt.char_emb_dim  # dimension of character embeddings
        CN = len(
            opt.tokenizer.vocab['char'])  # number of characters in vocabulary
        C_PAD = opt.tokenizer.vocab[
            'char'].pad_id  # padding index of characters
        WHD = opt.word_hidden_dim  # dimension of word-level rnn hidden state
        CHD = opt.char_hidden_dim if opt.char_emb_dim > 0 else 0  # dimension of character-level rnn hidden state
        LN = len(
            opt.tokenizer.vocab['label'])  # number of labels in vocabulary

        self.word_embedding = nn.Embedding.from_pretrained(
            torch.tensor(embedding_matrix,
                         dtype=torch.float))  # word embedding layer

        if opt.char_emb_dim > 0:
            self.char_embedding = nn.Embedding(
                CN, CD, padding_idx=C_PAD)  # character embedding layer
            self.char_rnn = DynamicLSTM(
                CD,
                CHD,
                num_layers=1,
                batch_first=True,
                bidirectional=True,
                rnn_type='LSTM',
                only_use_last_hidden_state=True)  # character-level RNN layer
        self.word_rnn = DynamicLSTM(WD + CHD * 2,
                                    WHD,
                                    num_layers=1,
                                    batch_first=True,
                                    bidirectional=True,
                                    rnn_type='LSTM')  # word-level RNN layer
        self.fc_hidden = nn.Linear(WHD * 2,
                                   WHD)  # fully-connected hidden layer
        self.fc_out = nn.Linear(WHD, LN)  # fully-connected output layer
        self.crf = CRF(LN, batch_first=True)  # CRF layer
        self.dropout = nn.Dropout(opt.dropout)  # dropout
Exemplo n.º 5
0
    def __init__(self, embedding_matrix, opt):
        super(TransDelta, self).__init__()

        WD = opt.word_dim
        HD = opt.hidden_dim

        self.word_embedding = nn.Embedding.from_pretrained(
            torch.tensor(embedding_matrix, dtype=torch.float))
        self.shared_params = nn.ParameterDict()
        self.shared_modules = nn.ModuleDict({
            'rnn':
            DynamicLSTM(WD,
                        HD,
                        num_layers=opt.layer_num,
                        batch_first=True,
                        bidirectional=opt.bidirectional,
                        rnn_type=opt.rnn_type)
        })
        reset_params(self.shared_modules['rnn'], opt.initializer)

        for name, param in self.shared_modules.named_parameters():
            name = name.split('.')[-1]
            self.shared_params[f"common_{name}"] = nn.Parameter(
                param.data, requires_grad=True)
            self.shared_params[f"main_{name}"] = nn.Parameter(
                torch.zeros_like(param), requires_grad=True)
            self.shared_params[f"aux_{name}"] = nn.Parameter(
                torch.zeros_like(param), requires_grad=True)

        output_layers = {
            'attention': {
                'asc': Attention,
                'dsc': MeanPooling,
                'ae': LinearLayer
            },
            'mean_pooling': {
                'asc': MeanPooling,
                'dsc': MeanPooling,
                'ae': LinearLayer
            }
        }
        self.output = nn.ModuleDict({
            side: output_layers[opt.output_layer][opt.tasks[side]](opt)
            for side in ['main', 'aux']
        })
        self.threshold = nn.Parameter(
            torch.tensor(-math.log(1 / opt.beta - 1)), requires_grad=True)
        self.dropout = nn.Dropout(opt.dropout)
        self.zeta = 0.0 if opt.hard_M else opt.zeta
        self.model_hard_transfer = opt.hard_M