コード例 #1
0
    def __init__(self, corpus, config):
        super(ZeroShotLAPtrHRED, self).__init__(config)

        self.vocab = corpus.vocab
        self.rev_vocab = corpus.rev_vocab
        self.vocab_size = len(self.vocab)
        self.go_id = self.rev_vocab[BOS]
        self.eos_id = self.rev_vocab[EOS]
        self.pad_id = self.rev_vocab[PAD]

        # build model here
        self.embedding = nn.Embedding(self.vocab_size,
                                      config.embed_size,
                                      padding_idx=self.pad_id)
        self.utt_encoder = RnnUttEncoder(
            config.utt_cell_size,
            config.dropout,
            use_attn=config.utt_type == 'rnn_attn',
            vocab_size=self.vocab_size,
            embedding=self.embedding,
            feat_size=1)
        self.ctx_encoder = EncoderRNN(self.utt_encoder.output_size,
                                      config.ctx_cell_size,
                                      0.0,
                                      config.dropout,
                                      config.num_layer,
                                      config.rnn_cell,
                                      variable_lengths=False,
                                      bidirection=config.bi_ctx_cell)
        self.policy = nn.Linear(self.ctx_encoder.output_size,
                                config.dec_cell_size)
        self.utt_policy = lambda x: x
        combined_dec_init_state_size = config.dec_cell_size + corpus.laed_z_size
        self.connector = nn_lib.LinearConnector(
            combined_dec_init_state_size,
            config.dec_cell_size,
            is_lstm=config.rnn_cell == 'lstm')
        self.attn_size = self.ctx_encoder.output_size
        self.decoder = DecoderPointerGen(self.vocab_size,
                                         config.max_dec_len,
                                         config.embed_size,
                                         config.dec_cell_size,
                                         self.go_id,
                                         self.eos_id,
                                         n_layers=1,
                                         rnn_cell=config.rnn_cell,
                                         input_dropout_p=config.dropout,
                                         dropout_p=config.dropout,
                                         attn_size=self.attn_size,
                                         attn_mode=config.attn_type,
                                         use_gpu=config.use_gpu,
                                         embedding=self.embedding)

        self.nll_loss = criterions.NLLEntropy(self.pad_id, config)
        self.l2_loss = criterions.L2Loss()
コード例 #2
0
    def __init__(self, corpus, config):
        super(HRED, self).__init__(config)

        self.vocab = corpus.vocab
        self.rev_vocab = corpus.rev_vocab
        self.vocab_size = len(self.vocab)
        self.go_id = self.rev_vocab[BOS]
        self.eos_id = self.rev_vocab[EOS]
        self.pad_id = self.rev_vocab[PAD]

        # build model here
        self.utt_encoder = ElmoUttEncoder(
            config,
            config.utt_cell_size,
            config.dropout,
            use_attn=config.utt_type == 'attn_rnn',
            feat_size=1)

        self.ctx_encoder = EncoderRNN(self.utt_encoder.output_size,
                                      config.ctx_cell_size,
                                      0.0,
                                      config.dropout,
                                      config.num_layer,
                                      config.rnn_cell,
                                      variable_lengths=False,
                                      bidirection=config.bi_ctx_cell)

        if config.bi_ctx_cell or config.num_layer > 1:
            self.connector = Bi2UniConnector(config.rnn_cell, config.num_layer,
                                             config.ctx_cell_size,
                                             config.dec_cell_size)
        else:
            self.connector = IdentityConnector()

        self.decoder = ElmoDecoderRNN(self.vocab_size,
                                      config.max_dec_len,
                                      self.utt_encoder.embed_size,
                                      config.dec_cell_size,
                                      self.go_id,
                                      self.eos_id,
                                      self.vocab,
                                      n_layers=1,
                                      rnn_cell=config.rnn_cell,
                                      input_dropout_p=config.dropout,
                                      dropout_p=config.dropout,
                                      use_attention=config.use_attn,
                                      embedding=self.utt_encoder.embedding,
                                      attn_size=self.ctx_encoder.output_size,
                                      attn_mode=config.attn_type,
                                      use_gpu=config.use_gpu)
        self.nll = criterions.NLLEntropy(self.pad_id, config)
コード例 #3
0
    def __init__(self, corpus, config):
        super(LAPtrHRED, self).__init__(config, corpus)

        self.ctx_encoder = EncoderRNN(self.utt_encoder.output_size,
                                      config.ctx_cell_size,
                                      0.0,
                                      config.dropout,
                                      config.num_layer,
                                      config.rnn_cell,
                                      variable_lengths=False,
                                      bidirection=config.bi_ctx_cell)

        combined_dec_init_state_size = config.dec_cell_size + corpus.laed_z_size
        if config.bi_ctx_cell or config.num_layer > 1:
            self.connector = Bi2UniConnector(config.rnn_cell,
                                             config.num_layer,
                                             combined_dec_init_state_size,
                                             config.dec_cell_size)
        else:
            self.connector = nn.Linear(combined_dec_init_state_size, config.dec_cell_size)

        self.attn_size = self.ctx_encoder.output_size

        self.plain_embedding = nn.Embedding(self.vocab_size, config.embed_size)
        self.decoder = DecoderPointerGen(self.vocab_size,
                                         config.max_dec_len,
                                         config.embed_size,
                                         config.dec_cell_size,
                                         self.go_id,
                                         self.eos_id,
                                         n_layers=1,
                                         rnn_cell=config.rnn_cell,
                                         input_dropout_p=config.dropout,
                                         dropout_p=config.dropout,
                                         attn_size=self.attn_size,
                                         attn_mode=config.attn_type,
                                         use_gpu=config.use_gpu,
                                         embedding=self.plain_embedding)

        self.nll_loss = criterions.NLLEntropy(self.pad_id, config)
コード例 #4
0
    def __init__(self, corpus, config):
        super(ZeroShotPtrHRED, self).__init__(config, corpus)

        self.ctx_encoder = EncoderRNN(self.utt_encoder.output_size,
                                      config.ctx_cell_size,
                                      0.0,
                                      config.dropout,
                                      config.num_layer,
                                      config.rnn_cell,
                                      variable_lengths=False,
                                      bidirection=config.bi_ctx_cell)

        self.policy = nn.Linear(self.ctx_encoder.output_size,
                                config.dec_cell_size)
        self.utt_policy = lambda x: x

        self.connector = nn_lib.LinearConnector(
            config.dec_cell_size,
            config.dec_cell_size,
            is_lstm=config.rnn_cell == 'lstm')

        self.attn_size = self.ctx_encoder.output_size
        self.decoder = DecoderPointerGen(self.vocab_size,
                                         config.max_dec_len,
                                         self.embedding.embedding_dim,
                                         config.dec_cell_size,
                                         self.go_id,
                                         self.eos_id,
                                         n_layers=1,
                                         rnn_cell=config.rnn_cell,
                                         input_dropout_p=config.dropout,
                                         dropout_p=config.dropout,
                                         attn_size=self.attn_size,
                                         attn_mode=config.attn_type,
                                         use_gpu=config.use_gpu,
                                         embedding=self.embedding)

        self.nll_loss = criterions.NLLEntropy(self.pad_id, config)
        self.l2_loss = criterions.L2Loss()
コード例 #5
0
    def __init__(self,
                 config,
                 utt_cell_size,
                 dropout,
                 rnn_cell='gru',
                 bidirection=True,
                 use_attn=False,
                 embedding=None,
                 vocab_size=None,
                 embed_dim=None,
                 feat_size=0):
        super(ElmoUttEncoder, self).__init__()
        self.bidirection = bidirection
        self.utt_cell_size = utt_cell_size

        if embedding is None:
            self.embed_size = embed_dim
            self.embedding = ElmoEmbedding(config)
            self.embed_size = self.embedding.embedding_dim
        else:
            self.embedding = embedding
            self.embed_size = embedding.embedding_dim

        self.rnn = EncoderRNN(self.embed_size + feat_size,
                              utt_cell_size,
                              0.0,
                              dropout,
                              rnn_cell=rnn_cell,
                              variable_lengths=False,
                              bidirection=bidirection)

        self.multipler = 2 if bidirection else 1
        self.output_size = self.utt_cell_size * self.multipler
        self.use_attn = use_attn
        self.feat_size = feat_size
        if use_attn:
            self.key_w = nn.Linear(self.utt_cell_size * self.multipler,
                                   self.utt_cell_size)
            self.query = nn.Linear(self.utt_cell_size, 1)