You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

lstm_self_attention.py 1.4 kB

12345678910111213141516171819202122232425262728293031323334
  1. import torch.nn as nn
  2. from fastNLP.core.const import Const as C
  3. from fastNLP.modules.encoder.lstm import LSTM
  4. from fastNLP.embeddings.utils import get_embeddings
  5. from fastNLP.modules.attention import SelfAttention
  6. from fastNLP.modules.decoder.mlp import MLP
  7. class BiLSTM_SELF_ATTENTION(nn.Module):
  8. def __init__(self, init_embed,
  9. num_classes,
  10. hidden_dim=256,
  11. num_layers=1,
  12. attention_unit=256,
  13. attention_hops=1,
  14. nfc=128):
  15. super(BiLSTM_SELF_ATTENTION,self).__init__()
  16. self.embed = get_embeddings(init_embed)
  17. self.lstm = LSTM(input_size=self.embed.embedding_dim, hidden_size=hidden_dim, num_layers=num_layers, bidirectional=True)
  18. self.attention = SelfAttention(input_size=hidden_dim * 2 , attention_unit=attention_unit, attention_hops=attention_hops)
  19. self.mlp = MLP(size_layer=[hidden_dim* 2*attention_hops, nfc, num_classes])
  20. def forward(self, words):
  21. x_emb = self.embed(words)
  22. output, _ = self.lstm(x_emb)
  23. after_attention, penalty = self.attention(output,words)
  24. after_attention =after_attention.view(after_attention.size(0),-1)
  25. output = self.mlp(after_attention)
  26. return {C.OUTPUT: output}
  27. def predict(self, words):
  28. output = self(words)
  29. _, predict = output[C.OUTPUT].max(dim=1)
  30. return {C.OUTPUT: predict}