Guest User

Untitled

a guest
Mar 21st, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. # Create custom classifier
  2. class PoolingLinearClassifierSoftmax(nn.Module):
  3. def __init__(self, layers, drops):
  4. super().__init__()
  5. self.layers = nn.ModuleList([
  6. LinearBlock(layers[i], layers[i + 1], drops[i]) for i in range(len(layers) - 1)])
  7.  
  8. def pool(self, x, bs, is_max):
  9. f = F.adaptive_max_pool1d if is_max else F.adaptive_avg_pool1d
  10. return f(x.permute(1,2,0), (1,)).view(bs,-1)
  11.  
  12. def forward(self, input):
  13. raw_outputs, outputs = input
  14. output = outputs[-1]
  15. sl,bs,_ = output.size()
  16. avgpool = self.pool(output, bs, False)
  17. mxpool = self.pool(output, bs, True)
  18. x = torch.cat([output[-1], mxpool, avgpool], 1)
  19. for l in self.layers:
  20. l_x = l(x)
  21. x = F.relu(l_x)
  22. l_x = F.softmax(l_x)
  23. return l_x, raw_outputs, outputs
Add Comment
Please, Sign In to add comment