From eb66cbe6c456fc4d78503ce63e34e2fad93b59fa Mon Sep 17 00:00:00 2001 From: FengZiYjun Date: Thu, 12 Jul 2018 21:53:42 +0800 Subject: [PATCH] restructure module: 4 classes; add modules; move prototype and rename --- fastNLP/models/sequence_modeling.py | 2 +- fastNLP/modules/{attention => aggregation}/__init__.py | 0 fastNLP/modules/{attention => aggregation}/attention.py | 0 fastNLP/modules/{convolution => aggregation}/avg_pool.py | 0 fastNLP/modules/{convolution => aggregation}/kmax_pool.py | 0 .../modules/{attention => aggregation}/linear_attention.py | 2 +- fastNLP/modules/{convolution => aggregation}/max_pool.py | 0 .../aggregation.py => aggregation/self_attention.py} | 5 +++-- fastNLP/modules/{ => decoder}/CRF.py | 0 fastNLP/modules/{convolution => decoder}/__init__.py | 0 fastNLP/modules/{recurrent => encoder}/__init__.py | 0 fastNLP/modules/{convolution => encoder}/conv.py | 0 fastNLP/modules/{prototype => encoder}/embedding.py | 3 ++- fastNLP/modules/{prototype/encoder.py => encoder/lstm.py} | 6 +++--- fastNLP/modules/interaction/__init__.py | 0 .../LSTM+self_attention_sentiment_analysis}/README.md | 0 .../LSTM+self_attention_sentiment_analysis}/Word2Idx.py | 0 .../LSTM+self_attention_sentiment_analysis}/dataloader.py | 5 +++-- .../LSTM+self_attention_sentiment_analysis}/example.py | 0 .../LSTM+self_attention_sentiment_analysis}/predict.py | 1 - .../LSTM+self_attention_sentiment_analysis}/prepare.py | 2 ++ 21 files changed, 15 insertions(+), 11 deletions(-) rename fastNLP/modules/{attention => aggregation}/__init__.py (100%) rename fastNLP/modules/{attention => aggregation}/attention.py (100%) rename fastNLP/modules/{convolution => aggregation}/avg_pool.py (100%) rename fastNLP/modules/{convolution => aggregation}/kmax_pool.py (100%) rename fastNLP/modules/{attention => aggregation}/linear_attention.py (78%) rename fastNLP/modules/{convolution => aggregation}/max_pool.py (100%) rename fastNLP/modules/{prototype/aggregation.py => aggregation/self_attention.py} (94%) rename fastNLP/modules/{ => decoder}/CRF.py (100%) rename fastNLP/modules/{convolution => decoder}/__init__.py (100%) rename fastNLP/modules/{recurrent => encoder}/__init__.py (100%) rename fastNLP/modules/{convolution => encoder}/conv.py (100%) rename fastNLP/modules/{prototype => encoder}/embedding.py (98%) rename fastNLP/modules/{prototype/encoder.py => encoder/lstm.py} (83%) create mode 100644 fastNLP/modules/interaction/__init__.py rename {fastNLP/modules/prototype => reproduction/LSTM+self_attention_sentiment_analysis}/README.md (100%) rename {fastNLP/modules/prototype => reproduction/LSTM+self_attention_sentiment_analysis}/Word2Idx.py (100%) rename {fastNLP/modules/prototype => reproduction/LSTM+self_attention_sentiment_analysis}/dataloader.py (99%) rename {fastNLP/modules/prototype => reproduction/LSTM+self_attention_sentiment_analysis}/example.py (100%) rename {fastNLP/modules/prototype => reproduction/LSTM+self_attention_sentiment_analysis}/predict.py (95%) rename {fastNLP/modules/prototype => reproduction/LSTM+self_attention_sentiment_analysis}/prepare.py (99%) diff --git a/fastNLP/models/sequence_modeling.py b/fastNLP/models/sequence_modeling.py index 80d13cf3..de6c2c66 100644 --- a/fastNLP/models/sequence_modeling.py +++ b/fastNLP/models/sequence_modeling.py @@ -3,7 +3,7 @@ import torch.nn as nn from torch.nn import functional as F from fastNLP.models.base_model import BaseModel -from fastNLP.modules.CRF import ContionalRandomField +from fastNLP.modules.decoder.CRF import ContionalRandomField class SeqLabeling(BaseModel): diff --git a/fastNLP/modules/attention/__init__.py b/fastNLP/modules/aggregation/__init__.py similarity index 100% rename from fastNLP/modules/attention/__init__.py rename to fastNLP/modules/aggregation/__init__.py diff --git a/fastNLP/modules/attention/attention.py b/fastNLP/modules/aggregation/attention.py similarity index 100% rename from fastNLP/modules/attention/attention.py rename to fastNLP/modules/aggregation/attention.py diff --git a/fastNLP/modules/convolution/avg_pool.py b/fastNLP/modules/aggregation/avg_pool.py similarity index 100% rename from fastNLP/modules/convolution/avg_pool.py rename to fastNLP/modules/aggregation/avg_pool.py diff --git a/fastNLP/modules/convolution/kmax_pool.py b/fastNLP/modules/aggregation/kmax_pool.py similarity index 100% rename from fastNLP/modules/convolution/kmax_pool.py rename to fastNLP/modules/aggregation/kmax_pool.py diff --git a/fastNLP/modules/attention/linear_attention.py b/fastNLP/modules/aggregation/linear_attention.py similarity index 78% rename from fastNLP/modules/attention/linear_attention.py rename to fastNLP/modules/aggregation/linear_attention.py index 0d34e205..8f761c7a 100644 --- a/fastNLP/modules/attention/linear_attention.py +++ b/fastNLP/modules/aggregation/linear_attention.py @@ -1,4 +1,4 @@ -from fastNLP.modules.attention.attention import Attention +from fastNLP.modules.aggregation.attention import Attention class LinearAttention(Attention): diff --git a/fastNLP/modules/convolution/max_pool.py b/fastNLP/modules/aggregation/max_pool.py similarity index 100% rename from fastNLP/modules/convolution/max_pool.py rename to fastNLP/modules/aggregation/max_pool.py diff --git a/fastNLP/modules/prototype/aggregation.py b/fastNLP/modules/aggregation/self_attention.py similarity index 94% rename from fastNLP/modules/prototype/aggregation.py rename to fastNLP/modules/aggregation/self_attention.py index 59e50e99..f3581b44 100644 --- a/fastNLP/modules/prototype/aggregation.py +++ b/fastNLP/modules/aggregation/self_attention.py @@ -2,7 +2,8 @@ import torch import torch.nn as nn from torch.autograd import Variable -class Selfattention(nn.Module): + +class SelfAttention(nn.Module): """ Self Attention Module. @@ -12,7 +13,7 @@ class Selfattention(nn.Module): r : the number of encoded vectors """ def __init__(self, input_size, d_a, r): - super(Selfattention, self).__init__() + super(SelfAttention, self).__init__() self.W_s1 = nn.Parameter(torch.randn(d_a, input_size), requires_grad=True) self.W_s2 = nn.Parameter(torch.randn(r, d_a), requires_grad=True) self.softmax = nn.Softmax(dim=2) diff --git a/fastNLP/modules/CRF.py b/fastNLP/modules/decoder/CRF.py similarity index 100% rename from fastNLP/modules/CRF.py rename to fastNLP/modules/decoder/CRF.py diff --git a/fastNLP/modules/convolution/__init__.py b/fastNLP/modules/decoder/__init__.py similarity index 100% rename from fastNLP/modules/convolution/__init__.py rename to fastNLP/modules/decoder/__init__.py diff --git a/fastNLP/modules/recurrent/__init__.py b/fastNLP/modules/encoder/__init__.py similarity index 100% rename from fastNLP/modules/recurrent/__init__.py rename to fastNLP/modules/encoder/__init__.py diff --git a/fastNLP/modules/convolution/conv.py b/fastNLP/modules/encoder/conv.py similarity index 100% rename from fastNLP/modules/convolution/conv.py rename to fastNLP/modules/encoder/conv.py diff --git a/fastNLP/modules/prototype/embedding.py b/fastNLP/modules/encoder/embedding.py similarity index 98% rename from fastNLP/modules/prototype/embedding.py rename to fastNLP/modules/encoder/embedding.py index 1ee88a92..17c8f20a 100644 --- a/fastNLP/modules/prototype/embedding.py +++ b/fastNLP/modules/encoder/embedding.py @@ -1,6 +1,6 @@ -import torch import torch.nn as nn + class Lookuptable(nn.Module): """ A simple lookup table @@ -19,5 +19,6 @@ class Lookuptable(nn.Module): def forward(self, x): return self.embed(x) + if __name__ == "__main__": model = Lookuptable(10, 20) diff --git a/fastNLP/modules/prototype/encoder.py b/fastNLP/modules/encoder/lstm.py similarity index 83% rename from fastNLP/modules/prototype/encoder.py rename to fastNLP/modules/encoder/lstm.py index 142496e1..6d110fca 100644 --- a/fastNLP/modules/prototype/encoder.py +++ b/fastNLP/modules/encoder/lstm.py @@ -1,6 +1,6 @@ -import torch import torch.nn as nn + class Lstm(nn.Module): """ LSTM module @@ -14,8 +14,8 @@ class Lstm(nn.Module): """ def __init__(self, input_size, hidden_size, num_layers, dropout, bidirectional): super(Lstm, self).__init__() - self.lstm = nn.LSTM(input_size, hidden_size, num_layers, bias=True, batch_first=True,\ - dropout=dropout, bidirectional=bidirectional) + self.lstm = nn.LSTM(input_size, hidden_size, num_layers, bias=True, batch_first=True, + dropout=dropout, bidirectional=bidirectional) def forward(self, x): x, _ = self.lstm(x) diff --git a/fastNLP/modules/interaction/__init__.py b/fastNLP/modules/interaction/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/fastNLP/modules/prototype/README.md b/reproduction/LSTM+self_attention_sentiment_analysis/README.md similarity index 100% rename from fastNLP/modules/prototype/README.md rename to reproduction/LSTM+self_attention_sentiment_analysis/README.md diff --git a/fastNLP/modules/prototype/Word2Idx.py b/reproduction/LSTM+self_attention_sentiment_analysis/Word2Idx.py similarity index 100% rename from fastNLP/modules/prototype/Word2Idx.py rename to reproduction/LSTM+self_attention_sentiment_analysis/Word2Idx.py diff --git a/fastNLP/modules/prototype/dataloader.py b/reproduction/LSTM+self_attention_sentiment_analysis/dataloader.py similarity index 99% rename from fastNLP/modules/prototype/dataloader.py rename to reproduction/LSTM+self_attention_sentiment_analysis/dataloader.py index af5cd8b8..dd7fc4f8 100644 --- a/fastNLP/modules/prototype/dataloader.py +++ b/reproduction/LSTM+self_attention_sentiment_analysis/dataloader.py @@ -1,9 +1,10 @@ -import random import pickle +import random + import torch -import numpy as np from torch.autograd import Variable + def float_wrapper(x, requires_grad=True, using_cuda=True): """ transform float type list to pytorch variable diff --git a/fastNLP/modules/prototype/example.py b/reproduction/LSTM+self_attention_sentiment_analysis/example.py similarity index 100% rename from fastNLP/modules/prototype/example.py rename to reproduction/LSTM+self_attention_sentiment_analysis/example.py diff --git a/fastNLP/modules/prototype/predict.py b/reproduction/LSTM+self_attention_sentiment_analysis/predict.py similarity index 95% rename from fastNLP/modules/prototype/predict.py rename to reproduction/LSTM+self_attention_sentiment_analysis/predict.py index d5346c0e..31affeb7 100644 --- a/fastNLP/modules/prototype/predict.py +++ b/reproduction/LSTM+self_attention_sentiment_analysis/predict.py @@ -1,4 +1,3 @@ -import torch import torch.nn as nn import torch.nn.functional as F diff --git a/fastNLP/modules/prototype/prepare.py b/reproduction/LSTM+self_attention_sentiment_analysis/prepare.py similarity index 99% rename from fastNLP/modules/prototype/prepare.py rename to reproduction/LSTM+self_attention_sentiment_analysis/prepare.py index 02fd19c5..b8f8d7b8 100644 --- a/fastNLP/modules/prototype/prepare.py +++ b/reproduction/LSTM+self_attention_sentiment_analysis/prepare.py @@ -1,6 +1,8 @@ import pickle + import Word2Idx + def get_sets(m, n): """ get a train set containing m samples and a test set containing n samples