Example #1
0
 def __init__(self, opt):
     super(MultiTaskBasicModel, self).__init__()
     memory_bank_size = 2 * opt.encoder_size if opt.bidirectional else opt.encoder_size
     self.seq2seq_model = Seq2SeqModel(opt)
     if opt.classifier_type == "max":
         self.classifier_model = MaxPoolClassifier(memory_bank_size, opt.num_classes, opt.classifier_dropout, opt.ordinal)
     elif opt.classifier_type == "word_attn":
         self.classifier_model = WordAttnClassifier(opt.query_hidden_size, memory_bank_size, opt.num_classes, opt.attn_mode, opt.classifier_dropout, opt.ordinal)
     elif opt.classifier_type == "word_attn_no_query":
         self.classifier_model = WordAttnNoQueryClassifier(memory_bank_size, opt.num_classes, opt.classifier_dropout,
                                                     opt.ordinal)
     elif opt.classifier_type == "word_multi_hop_attn":
         self.classifier_model = WordMultiHopAttnClassifier(opt.query_hidden_size, memory_bank_size, opt.num_classes,
                                                      opt.attn_mode, opt.classifier_dropout, opt.ordinal)
     else:
         raise ValueError
     self.model_type = opt.model_type
     self.classifier_type = opt.classifier_type
    def __init__(self, opt, rating_tokens_tensor=None):
        """Initialize model.
        :param rating_tokens_tensor: a LongTensor, [5, rating_v_size], stores the top rating_v_size tokens' indexs of each rating score
        """
        super(MultiTaskBasicClassifySeq2Seq, self).__init__()

        self.vocab_size = len(opt.word2idx)
        self.emb_dim = opt.word_vec_size
        self.num_directions = 2 if opt.bidirectional else 1
        self.encoder_size = opt.encoder_size
        self.decoder_size = opt.decoder_size
        self.memory_bank_size = self.num_directions * self.encoder_size
        #self.ctx_hidden_dim = opt.rnn_size
        self.batch_size = opt.batch_size
        self.bidirectional = opt.bidirectional
        self.enc_layers = opt.enc_layers
        self.dec_layers = opt.dec_layers
        self.dropout = opt.dropout
        self.model_type = opt.model_type

        self.bridge = opt.bridge

        self.coverage_attn = opt.coverage_attn
        self.copy_attn = opt.copy_attention

        # for rating memory
        self.rating_memory_pred = opt.rating_memory_pred
        self.rating_memory_type = opt.rating_memory_type
        self.rating_bridge_type = opt.rating_bridge_type
        if self.rating_memory_pred:
            assert rating_tokens_tensor is not None, "The rating_tokens_tensor is needed when rating_memory_pred is True"
            self.rating_tokens_tensor = rating_tokens_tensor.cuda()
            if self.rating_bridge_type == 'relu_one_layer':
                self.rating_bridge = nn.Sequential(nn.Linear(self.emb_dim, self.emb_dim),
                                                   nn.Dropout(p=self.dropout),
                                                   nn.ReLU())
            elif self.rating_bridge_type == 'tanh_one_layer':
                self.rating_bridge = nn.Sequential(nn.Linear(self.emb_dim, self.emb_dim),
                                                   nn.Dropout(p=self.dropout),
                                                   nn.Tanh())
            else:
                self.rating_bridge = None
        else:
            self.rating_tokens_tensor = None
            self.rating_bridge = None

        self.pad_idx_src = io.PAD
        self.pad_idx_trg = io.PAD
        self.bos_idx = io.BOS
        self.eos_idx = io.EOS
        self.unk_idx = io.UNK
        self.sep_idx = None
        # self.sep_idx = opt.word2idx['.']
        self.orthogonal_loss = opt.orthogonal_loss

        if self.orthogonal_loss:
            assert self.sep_idx is not None

        self.share_embeddings = opt.share_embeddings
        self.review_attn = opt.review_attn

        self.attn_mode = opt.attn_mode

        self.hr_enc = opt.encoder_type == "hre_brnn"
        #self.separate_layer_enc = opt.encoder_type == 'sep_layers_brnn' or opt.encoder_type == 'sep_layers_brnn_reverse'
        if opt.encoder_type == 'sep_layers_brnn':
            self.separate_mode = 1
            self.separate_layer_enc = True
        elif opt.encoder_type == 'sep_layers_brnn_reverse':
            self.separate_mode = 2
            self.separate_layer_enc = True
        elif opt.encoder_type == 'rnn' and opt.enc_layers == 2 and opt.residual:
            self.separate_mode = 0
            self.separate_layer_enc = False
        elif opt.residual and opt.enc_layers != 2:
            raise ValueError
        else:
            self.separate_mode = -1
            self.separate_layer_enc = False
        #self.separate_layer_enc_reverse = opt.encoder_type == 'sep_layers_brnn_reverse'

        self.num_classes = opt.num_classes
        self.classifier_type = opt.classifier_type
        self.dec_classify_input_type = opt.dec_classify_input_type
        if opt.classifier_type == "word_attn":
            self.enc_classifier = WordAttnClassifier(opt.query_hidden_size, self.memory_bank_size, opt.num_classes, opt.attn_mode, opt.classifier_dropout, opt.ordinal, self.hr_enc)
        elif opt.classifier_type == "max":
            self.enc_classifier = MaxPoolClassifier(self.memory_bank_size, opt.num_classes, opt.classifier_dropout, opt.ordinal, self.hr_enc)
        elif opt.classifier_type == "word_attn_no_query":
            self.enc_classifier = WordAttnNoQueryClassifier(self.memory_bank_size, opt.num_classes, opt.classifier_dropout, opt.ordinal, self.hr_enc)
        elif opt.classifier_type == "word_multi_hop_attn":
            self.enc_classifier = WordMultiHopAttnClassifier(opt.query_hidden_size, self.memory_bank_size, opt.num_classes, opt.attn_mode, opt.classifier_dropout, opt.ordinal, self.hr_enc)
        else:
            raise ValueError

        if self.hr_enc:
            self.encoder = CatHirRNNEncoder(
                vocab_size=self.vocab_size,
                embed_size=self.emb_dim,
                hidden_size=self.encoder_size,
                num_layers=self.enc_layers,
                bidirectional=self.bidirectional,
                pad_token=self.pad_idx_src,
                dropout=self.dropout
            )
        elif self.separate_mode >= 0:
            self.encoder = TwoLayerRNNEncoder(
                vocab_size=self.vocab_size,
                embed_size=self.emb_dim,
                hidden_size=self.encoder_size,
                num_layers=self.enc_layers,
                bidirectional=self.bidirectional,
                pad_token=self.pad_idx_src,
                dropout=self.dropout,
                separate_mode=self.separate_mode,
                residual=opt.residual
            )
        else:
            self.encoder = RNNEncoderBasic(
                vocab_size=self.vocab_size,
                embed_size=self.emb_dim,
                hidden_size=self.encoder_size,
                num_layers=self.enc_layers,
                bidirectional=self.bidirectional,
                pad_token=self.pad_idx_src,
                dropout=self.dropout
            )

        self.decoder = MultiTaskBasicDecoder(
            vocab_size=self.vocab_size,
            embed_size=self.emb_dim,
            hidden_size=self.decoder_size,
            num_layers=self.dec_layers,
            memory_bank_size=self.num_directions * self.encoder_size,
            coverage_attn=self.coverage_attn,
            copy_attn=self.copy_attn,
            review_attn=self.review_attn,
            pad_idx=self.pad_idx_trg,
            attn_mode=self.attn_mode,
            dropout=self.dropout,
            hr_enc=self.hr_enc,
            out_sentiment_context=False,
            rating_memory_pred=self.rating_memory_pred
        )

        if self.bridge == 'dense':
            self.bridge_layer = nn.Linear(self.encoder_size * self.num_directions, self.decoder_size)
        elif opt.bridge == 'dense_nonlinear':
            self.bridge_layer = nn.tanh(nn.Linear(self.encoder_size * self.num_directions, self.decoder_size))
        else:
            self.bridge_layer = None

        if self.bridge == 'copy':
            assert self.encoder_size * self.num_directions == self.decoder_size, 'encoder hidden size and decoder hidden size are not match, please use a bridge layer'

        if self.share_embeddings:
            self.encoder.embedding.weight = self.decoder.embedding.weight

        self.init_embedding_weights()
    def __init__(self, opt):
        """Initialize model."""
        super(ExternalFeedClassifySeq2Seq, self).__init__()

        self.vocab_size = len(opt.word2idx)
        self.emb_dim = opt.word_vec_size
        self.num_directions = 2 if opt.bidirectional else 1
        self.encoder_size = opt.encoder_size
        self.decoder_size = opt.decoder_size
        self.memory_bank_size = self.num_directions * self.encoder_size
        #self.ctx_hidden_dim = opt.rnn_size
        self.batch_size = opt.batch_size
        self.bidirectional = opt.bidirectional
        self.enc_layers = opt.enc_layers
        self.dec_layers = opt.dec_layers
        self.dropout = opt.dropout
        self.model_type = opt.model_type

        self.rating_embedding = nn.Embedding(opt.num_classes, self.emb_dim)

        self.bridge = opt.bridge

        self.coverage_attn = opt.coverage_attn
        self.copy_attn = opt.copy_attention

        self.pad_idx_src = io.PAD
        self.pad_idx_trg = io.PAD
        self.bos_idx = io.BOS
        self.eos_idx = io.EOS
        self.unk_idx = io.UNK
        self.sep_idx = None
        # self.sep_idx = opt.word2idx['.']
        self.orthogonal_loss = opt.orthogonal_loss

        if self.orthogonal_loss:
            assert self.sep_idx is not None

        self.share_embeddings = opt.share_embeddings
        self.review_attn = opt.review_attn

        self.attn_mode = opt.attn_mode

        self.num_classes = opt.num_classes
        if opt.classifier_type == "word_attn":
            self.classifier = WordAttnClassifier(
                opt.query_hidden_size, self.memory_bank_size, opt.num_classes,
                opt.attn_mode, opt.classifier_dropout, opt.ordinal)
        elif opt.classifier_type == "max":
            self.classifier = MaxPoolClassifier(self.memory_bank_size,
                                                opt.num_classes,
                                                opt.classifier_dropout,
                                                opt.ordinal)
        elif opt.classifier_type == "word_attn_no_query":
            self.classifier = WordAttnNoQueryClassifier(
                self.memory_bank_size, opt.num_classes, opt.classifier_dropout,
                opt.ordinal)
        elif opt.classifier_type == "word_multi_hop_attn":
            self.classifier = WordMultiHopAttnClassifier(
                opt.query_hidden_size, self.memory_bank_size, opt.num_classes,
                opt.attn_mode, opt.classifier_dropout, opt.ordinal)
        else:
            raise ValueError

        self.classifier_type = opt.classifier_type

        #self.goal_vector_mode = opt.goal_vector_mode
        #self.goal_vector_size = opt.goal_vector_size
        #self.manager_mode = opt.manager_mode

        self.hr_enc = opt.encoder_type == "hre_brnn"

        if self.hr_enc:
            self.encoder = CatHirRNNEncoder(vocab_size=self.vocab_size,
                                            embed_size=self.emb_dim,
                                            hidden_size=self.encoder_size,
                                            num_layers=self.enc_layers,
                                            bidirectional=self.bidirectional,
                                            pad_token=self.pad_idx_src,
                                            dropout=self.dropout)
        else:
            self.encoder = RNNEncoderBasic(vocab_size=self.vocab_size,
                                           embed_size=self.emb_dim,
                                           hidden_size=self.encoder_size,
                                           num_layers=self.enc_layers,
                                           bidirectional=self.bidirectional,
                                           pad_token=self.pad_idx_src,
                                           dropout=self.dropout)

        self.decoder = ExternalFeedDecoder(
            vocab_size=self.vocab_size,
            embed_size=self.emb_dim,
            hidden_size=self.decoder_size,
            num_layers=self.dec_layers,
            memory_bank_size=self.num_directions * self.encoder_size,
            coverage_attn=self.coverage_attn,
            copy_attn=self.copy_attn,
            review_attn=self.review_attn,
            pad_idx=self.pad_idx_trg,
            attn_mode=self.attn_mode,
            dropout=self.dropout,
            hr_enc=self.hr_enc)

        if self.bridge == 'dense':
            self.bridge_layer = nn.Linear(
                self.encoder_size * self.num_directions, self.decoder_size)
        elif opt.bridge == 'dense_nonlinear':
            self.bridge_layer = nn.tanh(
                nn.Linear(self.encoder_size * self.num_directions,
                          self.decoder_size))
        else:
            self.bridge_layer = None

        if self.bridge == 'copy':
            assert self.encoder_size * self.num_directions == self.decoder_size, 'encoder hidden size and decoder hidden size are not match, please use a bridge layer'
        """
        if self.separate_present_absent and self.goal_vector_mode > 0:
            if self.manager_mode == 2:  # use GRU as a manager
                self.manager = nn.GRU(input_size=self.decoder_size, hidden_size=self.goal_vector_size, num_layers=1, bidirectional=False, batch_first=False, dropout=self.dropout)
                self.bridge_manager = opt.bridge_manager
                if self.bridge_manager:
                    self.manager_bridge_layer = nn.Linear(self.encoder_size * self.num_directions, self.goal_vector_size)
                else:
                    self.manager_bridge_layer = None
            elif self.manager_mode == 1:  # use two trainable vectors only
                self.manager = ManagerBasic(self.goal_vector_size)
        """

        if self.share_embeddings:
            self.encoder.embedding.weight = self.decoder.embedding.weight

        self.init_embedding_weights()
    def __init__(self, opt, rating_tokens_tensor=None):
        """Initialize model.
        :param rating_tokens_tensor: a LongTensor, [5, rating_v_size], stores the top rating_v_size tokens' indexs of each rating score
        """
        super(RnnEncSingleClassifier, self).__init__()

        self.vocab_size = len(opt.word2idx)
        self.emb_dim = opt.word_vec_size
        self.num_directions = 2 if opt.bidirectional else 1
        self.encoder_size = opt.encoder_size
        self.memory_bank_size = self.num_directions * self.encoder_size
        self.batch_size = opt.batch_size
        self.bidirectional = opt.bidirectional
        self.enc_layers = opt.enc_layers
        self.dropout = opt.dropout
        self.model_type = opt.model_type

        self.pad_idx_src = io.PAD
        # self.pad_idx_trg = io.PAD
        # self.bos_idx = io.BOS
        # self.eos_idx = io.EOS
        self.unk_idx = io.UNK
        self.hr_enc = opt.encoder_type == "hre_brnn"
        if opt.encoder_type == 'sep_layers_brnn':
            self.separate_mode = 1
            self.separate_layer_enc = True
        elif opt.encoder_type == 'sep_layers_brnn_reverse':
            self.separate_mode = 2
            self.separate_layer_enc = True
        elif opt.encoder_type == 'rnn' and opt.enc_layers == 2 and opt.residual:
            self.separate_mode = 0
            self.separate_layer_enc = False
        elif opt.residual and opt.enc_layers != 2:
            raise ValueError
        else:
            self.separate_mode = -1
            self.separate_layer_enc = False
        #self.separate_layer_enc_reverse = opt.encoder_type == 'sep_layers_brnn_reverse'

        self.num_classes = opt.num_classes
        self.classifier_type = opt.classifier_type
        if opt.classifier_type == "word_attn":
            self.enc_classifier = WordAttnClassifier(opt.query_hidden_size,
                                                     self.memory_bank_size,
                                                     opt.num_classes,
                                                     opt.attn_mode,
                                                     opt.classifier_dropout,
                                                     opt.ordinal, self.hr_enc)
        elif opt.classifier_type == "max":
            self.enc_classifier = MaxPoolClassifier(self.memory_bank_size,
                                                    opt.num_classes,
                                                    opt.classifier_dropout,
                                                    opt.ordinal, self.hr_enc)
        elif opt.classifier_type == "word_attn_no_query":
            self.enc_classifier = WordAttnNoQueryClassifier(
                self.memory_bank_size, opt.num_classes, opt.classifier_dropout,
                opt.ordinal, self.hr_enc)
        elif opt.classifier_type == "word_multi_hop_attn":
            self.enc_classifier = WordMultiHopAttnClassifier(
                opt.query_hidden_size, self.memory_bank_size, opt.num_classes,
                opt.attn_mode, opt.classifier_dropout, opt.ordinal,
                self.hr_enc)
        else:
            raise ValueError

        if self.hr_enc:
            self.encoder = CatHirRNNEncoder(vocab_size=self.vocab_size,
                                            embed_size=self.emb_dim,
                                            hidden_size=self.encoder_size,
                                            num_layers=self.enc_layers,
                                            bidirectional=self.bidirectional,
                                            pad_token=self.pad_idx_src,
                                            dropout=self.dropout)
        elif self.separate_mode >= 0:
            self.encoder = TwoLayerRNNEncoder(vocab_size=self.vocab_size,
                                              embed_size=self.emb_dim,
                                              hidden_size=self.encoder_size,
                                              num_layers=self.enc_layers,
                                              bidirectional=self.bidirectional,
                                              pad_token=self.pad_idx_src,
                                              dropout=self.dropout,
                                              separate_mode=self.separate_mode,
                                              residual=opt.residual)
        else:
            self.encoder = RNNEncoderBasic(vocab_size=self.vocab_size,
                                           embed_size=self.emb_dim,
                                           hidden_size=self.encoder_size,
                                           num_layers=self.enc_layers,
                                           bidirectional=self.bidirectional,
                                           pad_token=self.pad_idx_src,
                                           dropout=self.dropout)

        self.init_embedding_weights()