Example #1
0
    def __init__(self, tgt_embeddings, **kwargs):
        """Construct an RNN decoder.  It provides the input size, the rest is up to the impl.

        The default implementation provides an RNN cell, followed by a linear projection, out to a softmax

        :param input_dim: The input size
        :param kwargs:
        :return: void
        """
        super().__init__()
        self.hsz = kwargs['hsz']
        self.arc_policy = create_seq2seq_arc_policy(**kwargs)
        self.tgt_embeddings = tgt_embeddings
        rnntype = kwargs.get('rnntype', 'lstm')
        layers = kwargs.get('layers', 1)
        feed_input = kwargs.get('feed_input', True)
        dsz = tgt_embeddings.get_dsz()
        if feed_input:
            self.input_i = self._feed_input
            dsz += self.hsz
        else:
            self.input_i = self._basic_input
        pdrop = kwargs.get('dropout', 0.5)
        self.decoder_rnn = rnn_cell(dsz, self.hsz, rnntype, layers, pdrop)
        self.dropout = torch.nn.Dropout(pdrop)
        self.init_attn(**kwargs)

        do_weight_tying = bool(kwargs.get('tie_weights', True))

        if do_weight_tying:
            if self.hsz != self.tgt_embeddings.get_dsz():
                raise ValueError("weight tying requires hsz == embedding dsz, got {} hsz and {} dsz".format(self.hsz, self.tgt_embeddings.get_dsz()))
            self.preds = WeightTieDense(self.tgt_embeddings)
        else:
            self.preds = pytorch_linear(self.hsz, self.tgt_embeddings.get_vsz())
Example #2
0
    def __init__(self, tgt_embeddings, **kwargs):
        pc = kwargs.pop('pc').add_subcollection(name=kwargs.get('name', 'rnn-decoder'))
        super(RNNDecoder, self).__init__(pc)
        self.hsz = kwargs['hsz']
        self.arc_policy = create_seq2seq_arc_policy(**kwargs)
        self.tgt_embeddings = tgt_embeddings
        rnntype = kwargs.get('rnntype', 'lstm')
        layers = kwargs['layers']
        feed_input = kwargs.get('feed_input', True)
        dsz = tgt_embeddings.get_dsz()
        if feed_input:
            self.input_i = self._feed_input
            dsz += self.hsz
        else:
            self.input_i = self._basic_input
        self.pdrop = kwargs.get('dropout', 0.5)
        self.decoder_rnn = dy.VanillaLSTMBuilder(layers, dsz, self.hsz, self.pc)
        self.init_attn(**kwargs)

        do_weight_tying = bool(kwargs.get('tie_weights', False))

        if do_weight_tying:
            if self.hsz == tgt_embeddings.get_dsz():
                self.preds = WeightShareLinear(tgt_embeddings.get_vsz(), tgt_embeddings.embeddings, self.pc, transform=squeeze_and_transpose, name=tgt_embeddings.pc.name())
            else:
                raise ValueError("weight tying only valid when prediction projection \
layer's hidden size == embedding weight dimensions")
        else:
            self.preds = Linear(self.tgt_embeddings.get_vsz(), self.hsz, self.pc)
Example #3
0
    def __init__(self, tgt_embedding, **kwargs):
        super(RNNDecoder, self).__init__(tgt_embedding, **kwargs)
        self.hsz = kwargs['hsz']
        self.arc_policy = create_seq2seq_arc_policy(**kwargs)
        self.final_decoder_state = None
        self.do_weight_tying = bool(kwargs.get('tie_weights', False))
        if self.do_weight_tying:
            if self.hsz != self.tgt_embedding.get_dsz():
                raise ValueError("weight tying requires hsz == embedding dsz, \
got {} hsz and {} dsz".format(self.hsz, self.tgt_embedding.get_dsz()))
Example #4
0
    def __init__(self, tgt_embedding, **kwargs):
        super(RNNDecoder, self).__init__(tgt_embedding, **kwargs)
        self.hsz = kwargs['hsz']
        self.arc_policy = create_seq2seq_arc_policy(**kwargs)
        self.final_decoder_state = None
        self.do_weight_tying = bool(kwargs.get('tie_weights', False))
        if self.do_weight_tying:
            if self.hsz != self.tgt_embedding.get_dsz():
                raise ValueError("weight tying requires hsz == embedding dsz, \
got {} hsz and {} dsz".format(self.hsz, self.tgt_embedding.get_dsz()))
Example #5
0
    def __init__(self, tgt_embedding, hsz, pdrop, rnntype='lstm', layers=1, vdrop=False, name='encoder', scope='RNNDecoder', **kwargs):
        super().__init__(tgt_embedding, **kwargs)
        self.hsz = hsz
        self.pdrop = pdrop
        self.rnntype = rnntype
        self.layers = layers
        self.vdrop = vdrop
        self.arc_policy = create_seq2seq_arc_policy(**kwargs)
        self.final_decoder_state = None
        self.do_weight_tying = bool(kwargs.get('tie_weights', False))
        if self.do_weight_tying:
            if self.hsz != self.tgt_embedding.get_dsz():
                raise ValueError("weight tying requires hsz == embedding dsz, \
got {} hsz and {} dsz".format(self.hsz, self.tgt_embedding.get_dsz()))
Example #6
0
    def __init__(self, tgt_embeddings, **kwargs):
        """Construct an RNN decoder.  It provides the input size, the rest is up to the impl.

        The default implementation provides an RNN cell, followed by a linear projection, out to a softmax

        :param input_dim: The input size
        :param kwargs:
        :return: void
        """
        super(RNNDecoder, self).__init__()
        self.hsz = kwargs['hsz']
        self.arc_policy = create_seq2seq_arc_policy(**kwargs)
        self.tgt_embeddings = tgt_embeddings
        rnntype = kwargs['rnntype']
        layers = kwargs['layers']
        feed_input = kwargs.get('feed_input', True)
        dsz = tgt_embeddings.get_dsz()
        if feed_input:
            self.input_i = self._feed_input
            dsz += self.hsz
        else:
            self.input_i = self._basic_input
        pdrop = kwargs.get('dropout', 0.5)
        self.decoder_rnn = pytorch_rnn_cell(dsz, self.hsz, rnntype, layers,
                                            pdrop)
        self.dropout = torch.nn.Dropout(pdrop)
        self.init_attn(**kwargs)

        do_weight_tying = bool(kwargs.get('tie_weights', False))
        is_valid_tying = self.hsz == self.tgt_embeddings.get_dsz()

        self.preds = pytorch_linear(self.hsz, self.tgt_embeddings.get_vsz())
        if do_weight_tying:
            if is_valid_tying:
                tie_weight(self.preds, self.tgt_embeddings.embeddings)
            else:
                raise ValueError(
                    "weight tying only valid when prediction projection \
layer's hidden size == embedding weight dimensions")
Example #7
0
    def __init__(self, tgt_embeddings, **kwargs):
        """Construct an RNN decoder.  It provides the input size, the rest is up to the impl.

        The default implementation provides an RNN cell, followed by a linear projection, out to a softmax

        :param input_dim: The input size
        :param kwargs:
        :return: void
        """
        super(RNNDecoder, self).__init__()
        self.hsz = kwargs['hsz']
        self.arc_policy = create_seq2seq_arc_policy(**kwargs)
        self.tgt_embeddings = tgt_embeddings
        rnntype = kwargs['rnntype']
        layers = kwargs['layers']
        feed_input = kwargs.get('feed_input', True)
        dsz = tgt_embeddings.get_dsz()
        if feed_input:
            self.input_i = self._feed_input
            dsz += self.hsz
        else:
            self.input_i = self._basic_input
        pdrop = kwargs.get('dropout', 0.5)
        self.decoder_rnn = pytorch_rnn_cell(dsz, self.hsz, rnntype, layers, pdrop)
        self.dropout = torch.nn.Dropout(pdrop)
        self.init_attn(**kwargs)

        do_weight_tying = bool(kwargs.get('tie_weights', False))
        is_valid_tying = self.hsz == self.tgt_embeddings.get_dsz()

        self.preds = pytorch_linear(self.hsz, self.tgt_embeddings.get_vsz())
        if do_weight_tying:
            if is_valid_tying:
                tie_weight(self.preds, self.tgt_embeddings.embeddings)
            else:
                raise ValueError("weight tying only valid when prediction projection \
layer's hidden size == embedding weight dimensions")