Example #1
0
 def apply(self, x):  # (batsize, outerseqlen, innerseqlen)
     y = x
     xm = T.neq(x, self.maskid) if self.maskid is not None else None
     for enc in self.encoders:
         y = EncLastDim(enc)(y, mask=xm)
         xm = T.sum(xm, axis=-1) > 0 if self.maskid is not None else None
     return y
Example #2
0
 def apply(self, x):
     if self.l2emb is not None:
         l1tensor = x[:, :, 1:]
         l1encs = EncLastDim(self.l1enc)(l1tensor)
         l2mat = x[:, :, 0]
         assert (l2mat.ndim == 2)
         l2embs = self.l2emb(l2mat)
         l2vecs = T.concatenate([l1encs, l2embs], axis=2)
         wmask = T.neq(l2mat,
                       self.maskid) if self.maskid is not None else None
     else:
         l2vecs = EncLastDim(self.l1enc)(x)
         wmask = T.gt(T.sum(T.eq(x, self.maskid), axis=2), 0)
     l2vecs.mask = wmask
     fenc = self.l2enc(l2vecs)
     return fenc  #, wmask #mask for debug
Example #3
0
 def apply(self, x):
     # word vectors and mask
     charten = x[:, :, 1:]
     charencs = EncLastDim(self.charenc)(charten)
     wordmat = x[:, :, 0]
     wordembs = self.wordemb(wordmat)
     wordvecs = T.concatenate([charencs, wordembs], axis=2)
     wordmask = T.neq(wordmat, self.maskid)
     wordvecs.mask = wordmask
     # do outerpolation
     weights, mask = self.outerpol(wordvecs)
     leftenco = self.leftenc(wordvecs,
                             weights=weights).dimshuffle(0, 'x', 1)
     rightenco = self.rightenc(wordvecs,
                               weights=(1 - weights)).dimshuffle(0, 'x', 1)
     ret = T.concatenate([self.leftlin(leftenco),
                          self.rightlin(rightenco)],
                         axis=1)
     return ret  # (batsize, 2, decdim)
Example #4
0
 def _autogenerate_mask(self, seq, seqemb):
     assert(seqemb.ndim == 3)
     print "automasking in SeqEncoder (%s)" % __file__
     axes = range(2, seq.ndim)       # mask must be 2D
     if "int" in seq.dtype:       # ==> indexes  # mask must be 2D
         if seq.ndim < 2:
             raise AttributeError("CAN NOT GENERATE MASK FOR NON-SEQUENCE")
         elif seq.ndim == 2:
             seqformask = seq
         else:
             print "generating default mask for non-standard seq shape (SeqEncoder, %s)" % __file__
             seqformask = seq[(slice(None, None, None),) * 2 + (0,) * (seq.ndim-2)]
             #if self._maskconfig.maskid != 0:
             #    raise AttributeError("CAN NOT CREATE MASK USING CUSTOM MASKID %d BECAUSE OF NON-STANDARD SEQ (%d dims, %s)" % (self._maskconfig.maskid, seq.ndim, str(seq.dtype)))
             #mask = T.gt(seq.sum(axis=axes), 0)      # 0 is TERMINUS
         assert(seqformask.ndim == 2)
         mask = T.neq(seqformask, self._maskconfig.maskid)
     else:
         #TODO raise AttributeError("CAN NOT GENERATE MASK FOR NON-INT SEQ")
         mask = T.gt(seq.norm(2, axis=axes), 0)
     return mask
Example #5
0
 def _maskfrom(self, ret, x):
     if self.maskid is not None:
         mask = T.neq(x, self.maskid)
     else:
         mask = None
     ret.mask = mask