Exemplo n.º 1
0
    def __init__(self,
                 inpvocsize=400,
                 inpembdim=None,
                 outvocsize=100,
                 outembdim=None,
                 encdim=100,
                 decdim=100,
                 attdim=100,
                 bidir=False,
                 rnu=GRU,
                 statetrans=None,
                 vecout=False,
                 inconcat=True,
                 outconcat=False,
                 **kw):
        encinnerdim = [encdim] if not issequence(encdim) else encdim
        decinnerdim = [decdim] if not issequence(decdim) else decdim

        self.enclayers, lastencinnerdim = \
            self.getenclayers(inpembdim, inpvocsize, encinnerdim, bidir, rnu)

        self.declayers = \
            self.getdeclayers(outembdim, outvocsize, lastencinnerdim,
                              decinnerdim, rnu, inconcat)

        # attention
        lastdecinnerdim = decinnerdim[-1]
        argdecinnerdim = lastdecinnerdim if outconcat is False else lastencinnerdim + lastdecinnerdim
        attgen = LinearGateAttentionGenerator(indim=lastencinnerdim +
                                              lastdecinnerdim,
                                              attdim=attdim)
        attcon = WeightedSumAttCon()

        if statetrans is True:
            if lastencinnerdim != lastdecinnerdim:  # state shape mismatch
                statetrans = MatDot(lastencinnerdim, lastdecinnerdim)
        elif statetrans == "matdot":
            statetrans = MatDot(lastencinnerdim, lastdecinnerdim)

        super(SimpleSeqEncDecAtt, self).__init__(self.enclayers,
                                                 self.declayers,
                                                 attgen,
                                                 attcon,
                                                 argdecinnerdim,
                                                 statetrans=statetrans,
                                                 vecout=vecout,
                                                 inconcat=inconcat,
                                                 outconcat=outconcat,
                                                 **kw)
Exemplo n.º 2
0
 def remake_encoder(self,
                    inpvocsize=None,
                    inpembdim=None,
                    inpemb=None,
                    innerdim=None,
                    bidir=False,
                    maskid=-1,
                    dropout_in=False,
                    dropout_h=False,
                    rnu=GRU,
                    sepatt=False):
     innerdim = ([innerdim] if not issequence(innerdim) else
                 innerdim) if innerdim is not None else self.encinnerdim
     if sepatt:
         enc = self._getencoder_sepatt(indim=inpvocsize,
                                       inpembdim=inpembdim,
                                       inpemb=inpemb,
                                       innerdim=innerdim,
                                       bidir=bidir,
                                       maskid=maskid,
                                       dropout_in=dropout_in,
                                       dropout_h=dropout_h,
                                       rnu=rnu)
     else:
         enc = self._getencoder(indim=inpvocsize,
                                inpembdim=inpembdim,
                                inpemb=inpemb,
                                innerdim=innerdim,
                                bidir=bidir,
                                maskid=maskid,
                                dropout_in=dropout_in,
                                dropout_h=dropout_h,
                                rnu=rnu)
     self.statetrans = self._build_state_trans(self.statetrans_setting)
     self.enc = enc
Exemplo n.º 3
0
    def __init__(self,
                 inpvocsize=400,
                 inpembdim=None,
                 outvocsize=100,
                 outembdim=None,
                 encdim=100,
                 decdim=100,
                 attdim=100,
                 bidir=False,
                 rnu=GRU,
                 statetrans=None,
                 vecout=False,
                 inconcat=True,
                 outconcat=False,
                 **kw):
        encinnerdim = [encdim] if not issequence(encdim) else encdim
        decinnerdim = [decdim] if not issequence(decdim) else decdim

        self.enclayers, lastencinnerdim = \
            self.getenclayers(inpembdim, inpvocsize, encinnerdim, bidir, rnu)

        self.declayers = \
            self.getdeclayers(outembdim, outvocsize, lastencinnerdim,
                              decinnerdim, rnu, inconcat)

        # attention
        lastdecinnerdim = decinnerdim[-1]
        argdecinnerdim = lastdecinnerdim if outconcat is False else lastencinnerdim + lastdecinnerdim
        attgen = LinearGateAttentionGenerator(indim=lastencinnerdim + lastdecinnerdim,
                                              attdim=attdim)
        attcon = WeightedSumAttCon()

        if statetrans is True:
            if lastencinnerdim != lastdecinnerdim:  # state shape mismatch
                statetrans = MatDot(lastencinnerdim, lastdecinnerdim)
        elif statetrans == "matdot":
            statetrans = MatDot(lastencinnerdim, lastdecinnerdim)

        super(SimpleSeqEncDecAtt, self).__init__(self.enclayers, self.declayers,
            attgen, attcon, argdecinnerdim, statetrans=statetrans, vecout=vecout,
            inconcat=inconcat, outconcat=outconcat, **kw)
Exemplo n.º 4
0
    def get_init_info(self, inpseq, batsize, maskseq=None):     # TODO: must evaluate enc here, in place, without any side effects
        """
        VERY DIFFERENT FROM THE PURELY SYMBOLIC GET_INIT_INFO IN REAL REC BLOCKS !!!
        This one is used in decoder/prediction
        """
        enco, allenco, encmask = self.enc.predict(inpseq, mask=maskseq)

        if self.statetrans is not None:
            topstate = self.statetrans.predict(enco, allenco)   # this gives unused input warning in theano - it's normal
            initstates = [topstate]
        else:
            initstates = batsize
        return self.dec.get_init_info(Val(allenco),
                                      [Val(x) for x in initstates]
                                            if issequence(initstates)
                                            else initstates,
                                      encmask=Val(encmask))
Exemplo n.º 5
0
    def get_init_info(
        self,
        inpseq,
        batsize,
        maskseq=None
    ):  # TODO: must evaluate enc here, in place, without any side effects
        """
        VERY DIFFERENT FROM THE PURELY SYMBOLIC GET_INIT_INFO IN REAL REC BLOCKS !!!
        This one is used in decoder/prediction
        """
        enco, allenco, encmask = self.enc.predict(inpseq, mask=maskseq)

        if self.statetrans is not None:
            topstate = self.statetrans.predict(
                enco, allenco
            )  # this gives unused input warning in theano - it's normal
            initstates = [topstate]
        else:
            initstates = batsize
        return self.dec.get_init_info(
            Val(allenco),
            [Val(x)
             for x in initstates] if issequence(initstates) else initstates,
            encmask=Val(encmask))
Exemplo n.º 6
0
    def __init__(self,
                 inpvocsize=400,
                 inpembdim=None,
                 inpemb=None,
                 outvocsize=100,
                 outembdim=None,
                 outemb=None,
                 encdim=100,
                 decdim=100,
                 bidir=False,
                 rnu=GRU,
                 statetrans=None,
                 vecout=None,
                 inconcat=True,
                 outconcat=False,
                 maskid=-1,
                 dropout=False,
                 attdist=CosineDistance(),
                 sepatt=False,
                 encoder=None,
                 decoder=None,
                 attention=None,
                 **kw):
        self.encinnerdim = [encdim] if not issequence(encdim) else encdim
        self.decinnerdim = [decdim] if not issequence(decdim) else decdim
        self.dropout = dropout

        # encoder
        if encoder is None:
            if sepatt:
                enc = self._getencoder_sepatt(indim=inpvocsize,
                                              inpembdim=inpembdim,
                                              inpemb=inpemb,
                                              innerdim=self.encinnerdim,
                                              bidir=bidir,
                                              maskid=maskid,
                                              dropout_in=dropout,
                                              dropout_h=dropout,
                                              rnu=rnu)
            else:
                enc = self._getencoder(indim=inpvocsize,
                                       inpembdim=inpembdim,
                                       inpemb=inpemb,
                                       innerdim=self.encinnerdim,
                                       bidir=bidir,
                                       maskid=maskid,
                                       dropout_in=dropout,
                                       dropout_h=dropout,
                                       rnu=rnu)
        else:
            enc = encoder

        if attention is None:
            attention = self._getattention(attdist, sepatt=sepatt)

        self.lastencinnerdim = enc.outdim
        if decoder is None:
            dec = self._getdecoder(outvocsize=outvocsize,
                                   outembdim=outembdim,
                                   outemb=outemb,
                                   maskid=maskid,
                                   attention=attention,
                                   lastencinnerdim=self.lastencinnerdim,
                                   decinnerdim=self.decinnerdim,
                                   inconcat=inconcat,
                                   outconcat=outconcat,
                                   softmaxout=vecout,
                                   dropout=dropout,
                                   rnu=rnu)
        else:
            dec = decoder

        self.lastdecinnerdim = self.decinnerdim[-1]
        self.statetrans_setting = statetrans
        statetrans = self._build_state_trans(self.statetrans_setting)

        super(SimpleSeqEncDecAtt, self).__init__(enc,
                                                 dec,
                                                 statetrans=statetrans,
                                                 **kw)