Beispiel #1
0
 def forward(self, x):
     batch_size = x.size(0)
     lens = torch.randint(1, self.max_len + 1, (batch_size,)).long()
     mask = utils.mask(lens, self.max_len)
     x = self.in_linear(x).view(batch_size, self.max_len, -1)
     x = x.masked_fill(1 - mask.unsqueeze(-1), float("nan"))
     return self.out_linear(self.pool(x, lens))
Beispiel #2
0
 def forward(self, x):
     batch_size, max_len = x.size(0), self.max_len
     x = self.in_linear(x).view(batch_size, max_len, -1)
     lens = torch.randint(1, max_len + 1, (batch_size,)).long()
     lens = lens.to(x)
     mask = utils.mask(lens, max_len)
     x = x.masked_fill(1 - mask.unsqueeze(-1), float("nan"))
     o, _, h = self.rnn(x, lens)
     x = torch.cat([o.contiguous().view(batch_size, -1), h], 1)
     return self.out_linear(x)
Beispiel #3
0
        def forward(self, x):
            batch_size = x.size(0)
            qrys = self.qry_linear(x).view(batch_size, self.num_qrys, -1)
            keys = self.key_linear(x).view(batch_size, self.max_keys, -1)
            num_keys = torch.randint(1, self.max_keys + 1, (batch_size,))
            num_keys = num_keys.long()
            mask = utils.mask(num_keys, self.max_keys).byte()
            keys = keys.masked_fill(1 - mask.unsqueeze(-1), float("nan"))

            att = self.att(qrys, keys, num_keys)
            return self.out_linear(att.view(batch_size, -1))
Beispiel #4
0
 def forward(self, x, lens=None):
     if lens is None:
         return x.mean(1)
     mask = utils.mask(lens, x.size(1))
     x = x.masked_fill(1 - mask.unsqueeze(-1), 0)
     return x.sum(1) / lens.unsqueeze(-1).float()
Beispiel #5
0
 def forward(self, x, lens=None):
     if lens is not None:
         mask = utils.mask(lens, x.size(1))
         x = x.masked_fill(1 - mask.unsqueeze(-1), 1)
     return x.prod(1)
Beispiel #6
0
 def forward(self, x, lens=None):
     if lens is not None:
         mask = utils.mask(lens, x.size(1))
         x = x.masked_fill(1 - mask.unsqueeze(-1), float("-inf"))
     return x.max(1)[0]
Beispiel #7
0
 def forward(self, x, lens=None):
     if lens is None:
         return x.mean(1)
     mask = utils.mask(lens, x.size(1)).unsqueeze(-1).float()
     return (x * mask).sum(1) / lens.unsqueeze(-1).float()
Beispiel #8
0
 def forward(self, x, lens=None):
     if lens is not None:
         mask = utils.mask(lens, x.size(1)).unsqueeze(-1)
         x *= mask.float()
     return self.pool(x)