From 391793a9614b373fc8ee1659e256adc70b824a86 Mon Sep 17 00:00:00 2001 From: ChenXin Date: Fri, 12 Jul 2019 10:36:46 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E6=9C=80=E6=96=B0=E7=9A=84docs=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/source/fastNLP.core.batch.rst | 6 ++--- docs/source/fastNLP.core.callback.rst | 6 ++--- docs/source/fastNLP.core.const.rst | 6 ++--- docs/source/fastNLP.core.dataset.rst | 6 ++--- docs/source/fastNLP.core.field.rst | 6 ++--- docs/source/fastNLP.core.instance.rst | 6 ++--- docs/source/fastNLP.core.losses.rst | 6 ++--- docs/source/fastNLP.core.metrics.rst | 6 ++--- docs/source/fastNLP.core.optimizer.rst | 6 ++--- docs/source/fastNLP.core.rst | 9 ++++---- docs/source/fastNLP.core.sampler.rst | 6 ++--- docs/source/fastNLP.core.tester.rst | 6 ++--- docs/source/fastNLP.core.trainer.rst | 6 ++--- docs/source/fastNLP.core.utils.rst | 6 ++--- docs/source/fastNLP.core.vocabulary.rst | 6 ++--- .../fastNLP.embeddings.bert_embedding.rst | 7 ++++++ .../fastNLP.embeddings.char_embedding.rst | 7 ++++++ ...astNLP.embeddings.contextual_embedding.rst | 7 ++++++ .../fastNLP.embeddings.elmo_embedding.rst | 7 ++++++ docs/source/fastNLP.embeddings.embedding.rst | 7 ++++++ docs/source/fastNLP.embeddings.rst | 21 ++++++++++++++++++ .../fastNLP.embeddings.stack_embedding.rst | 7 ++++++ .../fastNLP.embeddings.static_embedding.rst | 7 ++++++ docs/source/fastNLP.embeddings.utils.rst | 7 ++++++ docs/source/fastNLP.io.base_loader.rst | 6 ++--- docs/source/fastNLP.io.config_io.rst | 7 ++++++ docs/source/fastNLP.io.data_loader.rst | 6 ++--- docs/source/fastNLP.io.dataset_loader.rst | 6 ++--- docs/source/fastNLP.io.embed_loader.rst | 6 ++--- docs/source/fastNLP.io.file_utils.rst | 7 ++++++ docs/source/fastNLP.io.model_io.rst | 6 ++--- docs/source/fastNLP.io.rst | 17 +++++++++----- docs/source/fastNLP.io.utils.rst | 7 ++++++ docs/source/fastNLP.models.base_model.rst | 7 ++++++ docs/source/fastNLP.models.bert.rst | 7 ++++++ .../source/fastNLP.models.biaffine_parser.rst | 6 ++--- ...fastNLP.models.cnn_text_classification.rst | 6 ++--- .../source/fastNLP.models.enas_controller.rst | 7 ++++++ docs/source/fastNLP.models.enas_model.rst | 7 ++++++ docs/source/fastNLP.models.enas_trainer.rst | 7 ++++++ docs/source/fastNLP.models.enas_utils.rst | 7 ++++++ docs/source/fastNLP.models.rst | 14 +++++++----- .../fastNLP.models.sequence_labeling.rst | 6 ++--- docs/source/fastNLP.models.snli.rst | 6 ++--- .../fastNLP.models.star_transformer.rst | 6 ++--- docs/source/fastNLP.modules.decoder.crf.rst | 7 ------ docs/source/fastNLP.modules.decoder.mlp.rst | 7 ------ docs/source/fastNLP.modules.decoder.rst | 16 +++----------- docs/source/fastNLP.modules.decoder.utils.rst | 7 ------ docs/source/fastNLP.modules.encoder.bert.rst | 7 ------ .../fastNLP.modules.encoder.char_encoder.rst | 7 ------ .../fastNLP.modules.encoder.conv_maxpool.rst | 7 ------ .../fastNLP.modules.encoder.embedding.rst | 7 ------ docs/source/fastNLP.modules.encoder.lstm.rst | 7 ------ docs/source/fastNLP.modules.encoder.rst | 22 +++---------------- ...stNLP.modules.encoder.star_transformer.rst | 7 ------ .../fastNLP.modules.encoder.transformer.rst | 7 ------ ...astNLP.modules.encoder.variational_rnn.rst | 7 ------ docs/source/fastNLP.modules.rst | 6 ++--- docs/source/fastNLP.rst | 18 +++++++-------- 60 files changed, 254 insertions(+), 209 deletions(-) create mode 100644 docs/source/fastNLP.embeddings.bert_embedding.rst create mode 100644 docs/source/fastNLP.embeddings.char_embedding.rst create mode 100644 docs/source/fastNLP.embeddings.contextual_embedding.rst create mode 100644 docs/source/fastNLP.embeddings.elmo_embedding.rst create mode 100644 docs/source/fastNLP.embeddings.embedding.rst create mode 100644 docs/source/fastNLP.embeddings.rst create mode 100644 docs/source/fastNLP.embeddings.stack_embedding.rst create mode 100644 docs/source/fastNLP.embeddings.static_embedding.rst create mode 100644 docs/source/fastNLP.embeddings.utils.rst create mode 100644 docs/source/fastNLP.io.config_io.rst create mode 100644 docs/source/fastNLP.io.file_utils.rst create mode 100644 docs/source/fastNLP.io.utils.rst create mode 100644 docs/source/fastNLP.models.base_model.rst create mode 100644 docs/source/fastNLP.models.bert.rst create mode 100644 docs/source/fastNLP.models.enas_controller.rst create mode 100644 docs/source/fastNLP.models.enas_model.rst create mode 100644 docs/source/fastNLP.models.enas_trainer.rst create mode 100644 docs/source/fastNLP.models.enas_utils.rst delete mode 100644 docs/source/fastNLP.modules.decoder.crf.rst delete mode 100644 docs/source/fastNLP.modules.decoder.mlp.rst delete mode 100644 docs/source/fastNLP.modules.decoder.utils.rst delete mode 100644 docs/source/fastNLP.modules.encoder.bert.rst delete mode 100644 docs/source/fastNLP.modules.encoder.char_encoder.rst delete mode 100644 docs/source/fastNLP.modules.encoder.conv_maxpool.rst delete mode 100644 docs/source/fastNLP.modules.encoder.embedding.rst delete mode 100644 docs/source/fastNLP.modules.encoder.lstm.rst delete mode 100644 docs/source/fastNLP.modules.encoder.star_transformer.rst delete mode 100644 docs/source/fastNLP.modules.encoder.transformer.rst delete mode 100644 docs/source/fastNLP.modules.encoder.variational_rnn.rst diff --git a/docs/source/fastNLP.core.batch.rst b/docs/source/fastNLP.core.batch.rst index 33a5b730..03008b52 100644 --- a/docs/source/fastNLP.core.batch.rst +++ b/docs/source/fastNLP.core.batch.rst @@ -2,6 +2,6 @@ fastNLP.core.batch ================== .. automodule:: fastNLP.core.batch - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.core.callback.rst b/docs/source/fastNLP.core.callback.rst index 31ec627b..74a7825d 100644 --- a/docs/source/fastNLP.core.callback.rst +++ b/docs/source/fastNLP.core.callback.rst @@ -2,6 +2,6 @@ fastNLP.core.callback ===================== .. automodule:: fastNLP.core.callback - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.core.const.rst b/docs/source/fastNLP.core.const.rst index c9e3bd97..330a8883 100644 --- a/docs/source/fastNLP.core.const.rst +++ b/docs/source/fastNLP.core.const.rst @@ -2,6 +2,6 @@ fastNLP.core.const ================== .. automodule:: fastNLP.core.const - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.core.dataset.rst b/docs/source/fastNLP.core.dataset.rst index b377cb0f..1ad94bb6 100644 --- a/docs/source/fastNLP.core.dataset.rst +++ b/docs/source/fastNLP.core.dataset.rst @@ -2,6 +2,6 @@ fastNLP.core.dataset ==================== .. automodule:: fastNLP.core.dataset - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.core.field.rst b/docs/source/fastNLP.core.field.rst index 7686e79a..7fc099c9 100644 --- a/docs/source/fastNLP.core.field.rst +++ b/docs/source/fastNLP.core.field.rst @@ -2,6 +2,6 @@ fastNLP.core.field ================== .. automodule:: fastNLP.core.field - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.core.instance.rst b/docs/source/fastNLP.core.instance.rst index 14393a91..6e496ac1 100644 --- a/docs/source/fastNLP.core.instance.rst +++ b/docs/source/fastNLP.core.instance.rst @@ -2,6 +2,6 @@ fastNLP.core.instance ===================== .. automodule:: fastNLP.core.instance - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.core.losses.rst b/docs/source/fastNLP.core.losses.rst index d2dd492b..8e63dfa1 100644 --- a/docs/source/fastNLP.core.losses.rst +++ b/docs/source/fastNLP.core.losses.rst @@ -2,6 +2,6 @@ fastNLP.core.losses =================== .. automodule:: fastNLP.core.losses - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.core.metrics.rst b/docs/source/fastNLP.core.metrics.rst index 69afff36..d3b87bb8 100644 --- a/docs/source/fastNLP.core.metrics.rst +++ b/docs/source/fastNLP.core.metrics.rst @@ -2,6 +2,6 @@ fastNLP.core.metrics ==================== .. automodule:: fastNLP.core.metrics - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.core.optimizer.rst b/docs/source/fastNLP.core.optimizer.rst index e2100d2e..c80be53f 100644 --- a/docs/source/fastNLP.core.optimizer.rst +++ b/docs/source/fastNLP.core.optimizer.rst @@ -2,6 +2,6 @@ fastNLP.core.optimizer ====================== .. automodule:: fastNLP.core.optimizer - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.core.rst b/docs/source/fastNLP.core.rst index 82c13e46..cacc6622 100644 --- a/docs/source/fastNLP.core.rst +++ b/docs/source/fastNLP.core.rst @@ -2,15 +2,15 @@ fastNLP.core ============ .. automodule:: fastNLP.core - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: 子模块 ---------- .. toctree:: - :titlesonly: + :maxdepth: 1 fastNLP.core.batch fastNLP.core.callback @@ -26,4 +26,3 @@ fastNLP.core fastNLP.core.trainer fastNLP.core.utils fastNLP.core.vocabulary - diff --git a/docs/source/fastNLP.core.sampler.rst b/docs/source/fastNLP.core.sampler.rst index 1810d59c..0110f0c0 100644 --- a/docs/source/fastNLP.core.sampler.rst +++ b/docs/source/fastNLP.core.sampler.rst @@ -2,6 +2,6 @@ fastNLP.core.sampler ==================== .. automodule:: fastNLP.core.sampler - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.core.tester.rst b/docs/source/fastNLP.core.tester.rst index a9e7e09f..4d71a27b 100644 --- a/docs/source/fastNLP.core.tester.rst +++ b/docs/source/fastNLP.core.tester.rst @@ -2,6 +2,6 @@ fastNLP.core.tester =================== .. automodule:: fastNLP.core.tester - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.core.trainer.rst b/docs/source/fastNLP.core.trainer.rst index 9e518d4b..60bf2d5b 100644 --- a/docs/source/fastNLP.core.trainer.rst +++ b/docs/source/fastNLP.core.trainer.rst @@ -2,6 +2,6 @@ fastNLP.core.trainer ==================== .. automodule:: fastNLP.core.trainer - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.core.utils.rst b/docs/source/fastNLP.core.utils.rst index fcd3f50c..3f80b4e8 100644 --- a/docs/source/fastNLP.core.utils.rst +++ b/docs/source/fastNLP.core.utils.rst @@ -2,6 +2,6 @@ fastNLP.core.utils ================== .. automodule:: fastNLP.core.utils - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.core.vocabulary.rst b/docs/source/fastNLP.core.vocabulary.rst index b3bf4bac..ba9598b9 100644 --- a/docs/source/fastNLP.core.vocabulary.rst +++ b/docs/source/fastNLP.core.vocabulary.rst @@ -2,6 +2,6 @@ fastNLP.core.vocabulary ======================= .. automodule:: fastNLP.core.vocabulary - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.embeddings.bert_embedding.rst b/docs/source/fastNLP.embeddings.bert_embedding.rst new file mode 100644 index 00000000..24ceff1c --- /dev/null +++ b/docs/source/fastNLP.embeddings.bert_embedding.rst @@ -0,0 +1,7 @@ +fastNLP.embeddings.bert\_embedding +================================== + +.. automodule:: fastNLP.embeddings.bert_embedding + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.embeddings.char_embedding.rst b/docs/source/fastNLP.embeddings.char_embedding.rst new file mode 100644 index 00000000..501089d8 --- /dev/null +++ b/docs/source/fastNLP.embeddings.char_embedding.rst @@ -0,0 +1,7 @@ +fastNLP.embeddings.char\_embedding +================================== + +.. automodule:: fastNLP.embeddings.char_embedding + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.embeddings.contextual_embedding.rst b/docs/source/fastNLP.embeddings.contextual_embedding.rst new file mode 100644 index 00000000..92e25c4d --- /dev/null +++ b/docs/source/fastNLP.embeddings.contextual_embedding.rst @@ -0,0 +1,7 @@ +fastNLP.embeddings.contextual\_embedding +======================================== + +.. automodule:: fastNLP.embeddings.contextual_embedding + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.embeddings.elmo_embedding.rst b/docs/source/fastNLP.embeddings.elmo_embedding.rst new file mode 100644 index 00000000..76669ee3 --- /dev/null +++ b/docs/source/fastNLP.embeddings.elmo_embedding.rst @@ -0,0 +1,7 @@ +fastNLP.embeddings.elmo\_embedding +================================== + +.. automodule:: fastNLP.embeddings.elmo_embedding + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.embeddings.embedding.rst b/docs/source/fastNLP.embeddings.embedding.rst new file mode 100644 index 00000000..5960d2cd --- /dev/null +++ b/docs/source/fastNLP.embeddings.embedding.rst @@ -0,0 +1,7 @@ +fastNLP.embeddings.embedding +============================ + +.. automodule:: fastNLP.embeddings.embedding + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.embeddings.rst b/docs/source/fastNLP.embeddings.rst new file mode 100644 index 00000000..25d05246 --- /dev/null +++ b/docs/source/fastNLP.embeddings.rst @@ -0,0 +1,21 @@ +fastNLP.embeddings +================== + +.. automodule:: fastNLP.embeddings + :members: + :undoc-members: + :show-inheritance: + +子模块 +---------- + +.. toctree:: + + fastNLP.embeddings.bert_embedding + fastNLP.embeddings.char_embedding + fastNLP.embeddings.contextual_embedding + fastNLP.embeddings.elmo_embedding + fastNLP.embeddings.embedding + fastNLP.embeddings.stack_embedding + fastNLP.embeddings.static_embedding + fastNLP.embeddings.utils diff --git a/docs/source/fastNLP.embeddings.stack_embedding.rst b/docs/source/fastNLP.embeddings.stack_embedding.rst new file mode 100644 index 00000000..4d2115f7 --- /dev/null +++ b/docs/source/fastNLP.embeddings.stack_embedding.rst @@ -0,0 +1,7 @@ +fastNLP.embeddings.stack\_embedding +=================================== + +.. automodule:: fastNLP.embeddings.stack_embedding + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.embeddings.static_embedding.rst b/docs/source/fastNLP.embeddings.static_embedding.rst new file mode 100644 index 00000000..e46de81a --- /dev/null +++ b/docs/source/fastNLP.embeddings.static_embedding.rst @@ -0,0 +1,7 @@ +fastNLP.embeddings.static\_embedding +==================================== + +.. automodule:: fastNLP.embeddings.static_embedding + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.embeddings.utils.rst b/docs/source/fastNLP.embeddings.utils.rst new file mode 100644 index 00000000..263bfbd6 --- /dev/null +++ b/docs/source/fastNLP.embeddings.utils.rst @@ -0,0 +1,7 @@ +fastNLP.embeddings.utils +======================== + +.. automodule:: fastNLP.embeddings.utils + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.io.base_loader.rst b/docs/source/fastNLP.io.base_loader.rst index c1f9ac14..057867f4 100644 --- a/docs/source/fastNLP.io.base_loader.rst +++ b/docs/source/fastNLP.io.base_loader.rst @@ -2,6 +2,6 @@ fastNLP.io.base\_loader ======================= .. automodule:: fastNLP.io.base_loader - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.io.config_io.rst b/docs/source/fastNLP.io.config_io.rst new file mode 100644 index 00000000..106d083d --- /dev/null +++ b/docs/source/fastNLP.io.config_io.rst @@ -0,0 +1,7 @@ +fastNLP.io.config\_io +===================== + +.. automodule:: fastNLP.io.config_io + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.io.data_loader.rst b/docs/source/fastNLP.io.data_loader.rst index 9261fa5c..8f990102 100644 --- a/docs/source/fastNLP.io.data_loader.rst +++ b/docs/source/fastNLP.io.data_loader.rst @@ -2,6 +2,6 @@ fastNLP.io.data\_loader ========================== .. automodule:: fastNLP.io.data_loader - :members: - :undoc-members: - :show-inheritance: \ No newline at end of file + :members: + :undoc-members: + :show-inheritance: \ No newline at end of file diff --git a/docs/source/fastNLP.io.dataset_loader.rst b/docs/source/fastNLP.io.dataset_loader.rst index d6663e59..e7990714 100644 --- a/docs/source/fastNLP.io.dataset_loader.rst +++ b/docs/source/fastNLP.io.dataset_loader.rst @@ -2,6 +2,6 @@ fastNLP.io.dataset\_loader ========================== .. automodule:: fastNLP.io.dataset_loader - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.io.embed_loader.rst b/docs/source/fastNLP.io.embed_loader.rst index 7a8e730c..69e1f7ff 100644 --- a/docs/source/fastNLP.io.embed_loader.rst +++ b/docs/source/fastNLP.io.embed_loader.rst @@ -2,6 +2,6 @@ fastNLP.io.embed\_loader ======================== .. automodule:: fastNLP.io.embed_loader - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.io.file_utils.rst b/docs/source/fastNLP.io.file_utils.rst new file mode 100644 index 00000000..4a46e889 --- /dev/null +++ b/docs/source/fastNLP.io.file_utils.rst @@ -0,0 +1,7 @@ +fastNLP.io.file_utils +===================== + +.. automodule:: fastNLP.io.file_utils + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.io.model_io.rst b/docs/source/fastNLP.io.model_io.rst index 50d4c25a..537ce752 100644 --- a/docs/source/fastNLP.io.model_io.rst +++ b/docs/source/fastNLP.io.model_io.rst @@ -2,6 +2,6 @@ fastNLP.io.model\_io ==================== .. automodule:: fastNLP.io.model_io - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.io.rst b/docs/source/fastNLP.io.rst index 33afbbee..da9dbcfe 100644 --- a/docs/source/fastNLP.io.rst +++ b/docs/source/fastNLP.io.rst @@ -2,19 +2,26 @@ fastNLP.io ========== .. automodule:: fastNLP.io - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: 子模块 ---------- .. toctree:: - :titlesonly: fastNLP.io.data_loader + +Submodules +---------- + +.. toctree:: + fastNLP.io.base_loader + fastNLP.io.config_io fastNLP.io.dataset_loader fastNLP.io.embed_loader + fastNLP.io.file_utils fastNLP.io.model_io - + fastNLP.io.utils diff --git a/docs/source/fastNLP.io.utils.rst b/docs/source/fastNLP.io.utils.rst new file mode 100644 index 00000000..0b3f3938 --- /dev/null +++ b/docs/source/fastNLP.io.utils.rst @@ -0,0 +1,7 @@ +fastNLP.io.utils +================ + +.. automodule:: fastNLP.io.utils + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.models.base_model.rst b/docs/source/fastNLP.models.base_model.rst new file mode 100644 index 00000000..fab1e6bf --- /dev/null +++ b/docs/source/fastNLP.models.base_model.rst @@ -0,0 +1,7 @@ +fastNLP.models.base\_model +========================== + +.. automodule:: fastNLP.models.base_model + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.models.bert.rst b/docs/source/fastNLP.models.bert.rst new file mode 100644 index 00000000..3b9482eb --- /dev/null +++ b/docs/source/fastNLP.models.bert.rst @@ -0,0 +1,7 @@ +fastNLP.models.bert +=================== + +.. automodule:: fastNLP.models.bert + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.models.biaffine_parser.rst b/docs/source/fastNLP.models.biaffine_parser.rst index a3dd1836..f19504e8 100644 --- a/docs/source/fastNLP.models.biaffine_parser.rst +++ b/docs/source/fastNLP.models.biaffine_parser.rst @@ -2,6 +2,6 @@ fastNLP.models.biaffine\_parser =============================== .. automodule:: fastNLP.models.biaffine_parser - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.models.cnn_text_classification.rst b/docs/source/fastNLP.models.cnn_text_classification.rst index a935d0bf..eacf6916 100644 --- a/docs/source/fastNLP.models.cnn_text_classification.rst +++ b/docs/source/fastNLP.models.cnn_text_classification.rst @@ -2,6 +2,6 @@ fastNLP.models.cnn\_text\_classification ======================================== .. automodule:: fastNLP.models.cnn_text_classification - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.models.enas_controller.rst b/docs/source/fastNLP.models.enas_controller.rst new file mode 100644 index 00000000..1ac97fac --- /dev/null +++ b/docs/source/fastNLP.models.enas_controller.rst @@ -0,0 +1,7 @@ +fastNLP.models.enas\_controller module +====================================== + +.. automodule:: fastNLP.models.enas_controller + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.models.enas_model.rst b/docs/source/fastNLP.models.enas_model.rst new file mode 100644 index 00000000..92dbb810 --- /dev/null +++ b/docs/source/fastNLP.models.enas_model.rst @@ -0,0 +1,7 @@ +fastNLP.models.enas\_model +========================== + +.. automodule:: fastNLP.models.enas_model + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.models.enas_trainer.rst b/docs/source/fastNLP.models.enas_trainer.rst new file mode 100644 index 00000000..eea79599 --- /dev/null +++ b/docs/source/fastNLP.models.enas_trainer.rst @@ -0,0 +1,7 @@ +fastNLP.models.enas\_trainer +============================ + +.. automodule:: fastNLP.models.enas_trainer + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.models.enas_utils.rst b/docs/source/fastNLP.models.enas_utils.rst new file mode 100644 index 00000000..c5e45b5f --- /dev/null +++ b/docs/source/fastNLP.models.enas_utils.rst @@ -0,0 +1,7 @@ +fastNLP.models.enas\_utils +========================== + +.. automodule:: fastNLP.models.enas_utils + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.models.rst b/docs/source/fastNLP.models.rst index 5858ebcd..5a237569 100644 --- a/docs/source/fastNLP.models.rst +++ b/docs/source/fastNLP.models.rst @@ -2,19 +2,23 @@ fastNLP.models ============== .. automodule:: fastNLP.models - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: 子模块 ---------- .. toctree:: - :titlesonly: + fastNLP.models.base_model + fastNLP.models.bert fastNLP.models.biaffine_parser fastNLP.models.cnn_text_classification + fastNLP.models.enas_controller + fastNLP.models.enas_model + fastNLP.models.enas_trainer + fastNLP.models.enas_utils fastNLP.models.sequence_labeling fastNLP.models.snli fastNLP.models.star_transformer - diff --git a/docs/source/fastNLP.models.sequence_labeling.rst b/docs/source/fastNLP.models.sequence_labeling.rst index 6d569fe1..85e28f06 100644 --- a/docs/source/fastNLP.models.sequence_labeling.rst +++ b/docs/source/fastNLP.models.sequence_labeling.rst @@ -2,6 +2,6 @@ fastNLP.models.sequence\_labeling ================================= .. automodule:: fastNLP.models.sequence_labeling - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.models.snli.rst b/docs/source/fastNLP.models.snli.rst index 24c2cc53..3b9b555c 100644 --- a/docs/source/fastNLP.models.snli.rst +++ b/docs/source/fastNLP.models.snli.rst @@ -2,6 +2,6 @@ fastNLP.models.snli =================== .. automodule:: fastNLP.models.snli - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.models.star_transformer.rst b/docs/source/fastNLP.models.star_transformer.rst index c93fb8cd..69d5c5b2 100644 --- a/docs/source/fastNLP.models.star_transformer.rst +++ b/docs/source/fastNLP.models.star_transformer.rst @@ -2,6 +2,6 @@ fastNLP.models.star\_transformer ================================ .. automodule:: fastNLP.models.star_transformer - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.modules.decoder.crf.rst b/docs/source/fastNLP.modules.decoder.crf.rst deleted file mode 100644 index 6d5b0d5b..00000000 --- a/docs/source/fastNLP.modules.decoder.crf.rst +++ /dev/null @@ -1,7 +0,0 @@ -fastNLP.modules.decoder.CRF -=========================== - -.. automodule:: fastNLP.modules.decoder.crf - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/source/fastNLP.modules.decoder.mlp.rst b/docs/source/fastNLP.modules.decoder.mlp.rst deleted file mode 100644 index 7d661ebf..00000000 --- a/docs/source/fastNLP.modules.decoder.mlp.rst +++ /dev/null @@ -1,7 +0,0 @@ -fastNLP.modules.decoder.MLP -=========================== - -.. automodule:: fastNLP.modules.decoder.mlp - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/source/fastNLP.modules.decoder.rst b/docs/source/fastNLP.modules.decoder.rst index e42a9f39..ecc2adbd 100644 --- a/docs/source/fastNLP.modules.decoder.rst +++ b/docs/source/fastNLP.modules.decoder.rst @@ -2,17 +2,7 @@ fastNLP.modules.decoder ======================= .. automodule:: fastNLP.modules.decoder - :members: - :undoc-members: - :show-inheritance: - -子模块 ----------- - -.. toctree:: - :titlesonly: - - fastNLP.modules.decoder.crf - fastNLP.modules.decoder.mlp - fastNLP.modules.decoder.utils + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.modules.decoder.utils.rst b/docs/source/fastNLP.modules.decoder.utils.rst deleted file mode 100644 index da979d99..00000000 --- a/docs/source/fastNLP.modules.decoder.utils.rst +++ /dev/null @@ -1,7 +0,0 @@ -fastNLP.modules.decoder.utils -============================= - -.. automodule:: fastNLP.modules.decoder.utils - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/source/fastNLP.modules.encoder.bert.rst b/docs/source/fastNLP.modules.encoder.bert.rst deleted file mode 100644 index 66bd0bbd..00000000 --- a/docs/source/fastNLP.modules.encoder.bert.rst +++ /dev/null @@ -1,7 +0,0 @@ -fastNLP.modules.encoder.bert -============================ - -.. automodule:: fastNLP.modules.encoder.bert - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/source/fastNLP.modules.encoder.char_encoder.rst b/docs/source/fastNLP.modules.encoder.char_encoder.rst deleted file mode 100644 index 61ea3340..00000000 --- a/docs/source/fastNLP.modules.encoder.char_encoder.rst +++ /dev/null @@ -1,7 +0,0 @@ -fastNLP.modules.encoder.char\_encoder -===================================== - -.. automodule:: fastNLP.modules.encoder.char_encoder - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/source/fastNLP.modules.encoder.conv_maxpool.rst b/docs/source/fastNLP.modules.encoder.conv_maxpool.rst deleted file mode 100644 index 7058a723..00000000 --- a/docs/source/fastNLP.modules.encoder.conv_maxpool.rst +++ /dev/null @@ -1,7 +0,0 @@ -fastNLP.modules.encoder.conv\_maxpool -===================================== - -.. automodule:: fastNLP.modules.encoder.conv_maxpool - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/source/fastNLP.modules.encoder.embedding.rst b/docs/source/fastNLP.modules.encoder.embedding.rst deleted file mode 100644 index 4427b3bf..00000000 --- a/docs/source/fastNLP.modules.encoder.embedding.rst +++ /dev/null @@ -1,7 +0,0 @@ -fastNLP.modules.encoder.embedding -================================= - -.. automodule:: fastNLP.modules.encoder.embedding - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/source/fastNLP.modules.encoder.lstm.rst b/docs/source/fastNLP.modules.encoder.lstm.rst deleted file mode 100644 index f9cbea88..00000000 --- a/docs/source/fastNLP.modules.encoder.lstm.rst +++ /dev/null @@ -1,7 +0,0 @@ -fastNLP.modules.encoder.lstm -============================ - -.. automodule:: fastNLP.modules.encoder.lstm - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/source/fastNLP.modules.encoder.rst b/docs/source/fastNLP.modules.encoder.rst index b15232fa..0562f12d 100644 --- a/docs/source/fastNLP.modules.encoder.rst +++ b/docs/source/fastNLP.modules.encoder.rst @@ -2,22 +2,6 @@ fastNLP.modules.encoder ======================= .. automodule:: fastNLP.modules.encoder - :members: - :undoc-members: - :show-inheritance: - -子模块 ----------- - -.. toctree:: - :titlesonly: - - fastNLP.modules.encoder.bert - fastNLP.modules.encoder.char_encoder - fastNLP.modules.encoder.conv_maxpool - fastNLP.modules.encoder.embedding - fastNLP.modules.encoder.lstm - fastNLP.modules.encoder.star_transformer - fastNLP.modules.encoder.transformer - fastNLP.modules.encoder.variational_rnn - + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/fastNLP.modules.encoder.star_transformer.rst b/docs/source/fastNLP.modules.encoder.star_transformer.rst deleted file mode 100644 index 0c406782..00000000 --- a/docs/source/fastNLP.modules.encoder.star_transformer.rst +++ /dev/null @@ -1,7 +0,0 @@ -fastNLP.modules.encoder.star\_transformer -========================================= - -.. automodule:: fastNLP.modules.encoder.star_transformer - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/source/fastNLP.modules.encoder.transformer.rst b/docs/source/fastNLP.modules.encoder.transformer.rst deleted file mode 100644 index 6a40c597..00000000 --- a/docs/source/fastNLP.modules.encoder.transformer.rst +++ /dev/null @@ -1,7 +0,0 @@ -fastNLP.modules.encoder.transformer -=================================== - -.. automodule:: fastNLP.modules.encoder.transformer - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/source/fastNLP.modules.encoder.variational_rnn.rst b/docs/source/fastNLP.modules.encoder.variational_rnn.rst deleted file mode 100644 index 348fb3d8..00000000 --- a/docs/source/fastNLP.modules.encoder.variational_rnn.rst +++ /dev/null @@ -1,7 +0,0 @@ -fastNLP.modules.encoder.variational\_rnn -======================================== - -.. automodule:: fastNLP.modules.encoder.variational_rnn - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/source/fastNLP.modules.rst b/docs/source/fastNLP.modules.rst index 7f75cfdc..3160fe99 100644 --- a/docs/source/fastNLP.modules.rst +++ b/docs/source/fastNLP.modules.rst @@ -2,9 +2,9 @@ fastNLP.modules =============== .. automodule:: fastNLP.modules - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: 子模块 ----------- diff --git a/docs/source/fastNLP.rst b/docs/source/fastNLP.rst index f0c3d41c..076d9df0 100644 --- a/docs/source/fastNLP.rst +++ b/docs/source/fastNLP.rst @@ -2,19 +2,17 @@ API 文档 =============== .. automodule:: fastNLP - :members: - :undoc-members: - :show-inheritance: + :members: + :undoc-members: + :show-inheritance: 内部模块 ----------- .. toctree:: - :titlesonly: - :maxdepth: 3 - - fastNLP.core - fastNLP.io - fastNLP.modules - fastNLP.models + fastNLP.core + fastNLP.embeddings + fastNLP.io + fastNLP.models + fastNLP.modules From c99f02aa3b9abf1d2ccd1db3bccbea0407cfb1ec Mon Sep 17 00:00:00 2001 From: ChenXin Date: Fri, 12 Jul 2019 10:46:29 +0800 Subject: [PATCH 02/11] =?UTF-8?q?API=E6=96=87=E6=A1=A3=E5=85=A5=E5=8F=A3?= =?UTF-8?q?=E7=9A=84=E4=BB=8B=E7=BB=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastNLP/__init__.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/fastNLP/__init__.py b/fastNLP/__init__.py index 0eaa5d81..ea2cd13a 100644 --- a/fastNLP/__init__.py +++ b/fastNLP/__init__.py @@ -1,11 +1,12 @@ """ -fastNLP 由 :mod:`~fastNLP.core` 、 :mod:`~fastNLP.io` 、:mod:`~fastNLP.modules`、:mod:`~fastNLP.models` -等子模块组成,你可以点进去查看每个模块的文档。 +fastNLP 由 :mod:`~fastNLP.core` 、 :mod:`~fastNLP.io` 、:mod:`~fastNLP.embeddings` 、 :mod:`~fastNLP.modules`、 +:mod:`~fastNLP.models` 等子模块组成,你可以查看每个模块的文档。 -- :mod:`~fastNLP.core` 是fastNLP 的核心模块,包括 DataSet、 Trainer、 Tester 等组件。详见文档 :doc:`/fastNLP.core` -- :mod:`~fastNLP.io` 是实现输入输出的模块,包括了数据集的读取,模型的存取等功能。详见文档 :doc:`/fastNLP.io` -- :mod:`~fastNLP.modules` 包含了用于搭建神经网络模型的诸多组件,可以帮助用户快速搭建自己所需的网络。详见文档 :doc:`/fastNLP.modules` -- :mod:`~fastNLP.models` 包含了一些使用 fastNLP 实现的完整网络模型,包括 :class:`~fastNLP.models.CNNText` 、 :class:`~fastNLP.models.SeqLabeling` 等常见模型。详见文档 :doc:`/fastNLP.models` +- :mod:`~fastNLP.core` 是fastNLP 的核心模块,包括 DataSet、 Trainer、 Tester 等组件。详见文档 :doc:`fastNLP.core` +- :mod:`~fastNLP.io` 是实现输入输出的模块,包括了数据集的读取,模型的存取等功能。详见文档 :doc:`fastNLP.io` +- :mod:`~fastNLP.embeddings` 提供用于构建复杂网络模型所需的各种embedding。详见文档 :doc:`fastNLP.embeddings` +- :mod:`~fastNLP.modules` 包含了用于搭建神经网络模型的诸多组件,可以帮助用户快速搭建自己所需的网络。详见文档 :doc:`fastNLP.modules` +- :mod:`~fastNLP.models` 包含了一些使用 fastNLP 实现的完整网络模型,包括 :class:`~fastNLP.models.CNNText` 、 :class:`~fastNLP.models.SeqLabeling` 等常见模型。详见文档 :doc:`fastNLP.models` fastNLP 中最常用的组件可以直接从 fastNLP 包中 import ,他们的文档如下: """ @@ -61,4 +62,5 @@ __version__ = '0.4.5' from .core import * from . import models from . import modules +from . import embeddings from .io import data_loader From d6ae241bbb51df3c8636331f4fc4607741cd3dd7 Mon Sep 17 00:00:00 2001 From: ChenXin Date: Fri, 12 Jul 2019 10:49:18 +0800 Subject: [PATCH 03/11] =?UTF-8?q?decoder=E9=83=A8=E5=88=86=E7=9A=84?= =?UTF-8?q?=E5=88=AB=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastNLP/modules/decoder/crf.py | 52 ++++++++++++++++---------------- fastNLP/modules/decoder/mlp.py | 10 +++--- fastNLP/modules/decoder/utils.py | 10 +++--- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/fastNLP/modules/decoder/crf.py b/fastNLP/modules/decoder/crf.py index c0717d6f..7c496868 100644 --- a/fastNLP/modules/decoder/crf.py +++ b/fastNLP/modules/decoder/crf.py @@ -11,7 +11,7 @@ from ..utils import initial_parameter def allowed_transitions(id2target, encoding_type='bio', include_start_end=False): """ - 别名::class:`fastNLP.modules.allowed_transitions` :class:`fastNLP.modules.decoder.crf.allowed_transitions` + 别名::class:`fastNLP.modules.allowed_transitions` :class:`fastNLP.modules.decoder.allowed_transitions` 给定一个id到label的映射表,返回所有可以跳转的(from_tag_id, to_tag_id)列表。 @@ -31,7 +31,7 @@ def allowed_transitions(id2target, encoding_type='bio', include_start_end=False) id_label_lst = list(id2target.items()) if include_start_end: id_label_lst += [(start_idx, 'start'), (end_idx, 'end')] - + def split_tag_label(from_label): from_label = from_label.lower() if from_label in ['start', 'end']: @@ -41,7 +41,7 @@ def allowed_transitions(id2target, encoding_type='bio', include_start_end=False) from_tag = from_label[:1] from_label = from_label[2:] return from_tag, from_label - + for from_id, from_label in id_label_lst: if from_label in ['', '']: continue @@ -93,7 +93,7 @@ def _is_transition_allowed(encoding_type, from_tag, from_label, to_tag, to_label return to_tag in ['end', 'b', 'o'] else: raise ValueError("Unexpect tag {}. Expect only 'B', 'I', 'O'.".format(from_tag)) - + elif encoding_type == 'bmes': """ 第一行是to_tag, 第一列是from_tag,y任意条件下可转,-只有在label相同时可转,n不可转 @@ -151,7 +151,7 @@ def _is_transition_allowed(encoding_type, from_tag, from_label, to_tag, to_label class ConditionalRandomField(nn.Module): """ - 别名::class:`fastNLP.modules.ConditionalRandomField` :class:`fastNLP.modules.decoder.crf.ConditionalRandomField` + 别名::class:`fastNLP.modules.ConditionalRandomField` :class:`fastNLP.modules.decoder.ConditionalRandomField` 条件随机场。 提供forward()以及viterbi_decode()两个方法,分别用于训练与inference。 @@ -163,21 +163,21 @@ class ConditionalRandomField(nn.Module): allowed_transitions()函数得到;如果为None,则所有跃迁均为合法 :param str initial_method: 初始化方法。见initial_parameter """ - + def __init__(self, num_tags, include_start_end_trans=False, allowed_transitions=None, initial_method=None): - + super(ConditionalRandomField, self).__init__() - + self.include_start_end_trans = include_start_end_trans self.num_tags = num_tags - + # the meaning of entry in this matrix is (from_tag_id, to_tag_id) score self.trans_m = nn.Parameter(torch.randn(num_tags, num_tags)) if self.include_start_end_trans: self.start_scores = nn.Parameter(torch.randn(num_tags)) self.end_scores = nn.Parameter(torch.randn(num_tags)) - + if allowed_transitions is None: constrain = torch.zeros(num_tags + 2, num_tags + 2) else: @@ -185,9 +185,9 @@ class ConditionalRandomField(nn.Module): for from_tag_id, to_tag_id in allowed_transitions: constrain[from_tag_id, to_tag_id] = 0 self._constrain = nn.Parameter(constrain, requires_grad=False) - + initial_parameter(self, initial_method) - + def _normalizer_likelihood(self, logits, mask): """Computes the (batch_size,) denominator term for the log-likelihood, which is the sum of the likelihoods across all possible state sequences. @@ -200,21 +200,21 @@ class ConditionalRandomField(nn.Module): alpha = logits[0] if self.include_start_end_trans: alpha = alpha + self.start_scores.view(1, -1) - + flip_mask = mask.eq(0) - + for i in range(1, seq_len): emit_score = logits[i].view(batch_size, 1, n_tags) trans_score = self.trans_m.view(1, n_tags, n_tags) tmp = alpha.view(batch_size, n_tags, 1) + emit_score + trans_score alpha = torch.logsumexp(tmp, 1).masked_fill(flip_mask[i].view(batch_size, 1), 0) + \ alpha.masked_fill(mask[i].byte().view(batch_size, 1), 0) - + if self.include_start_end_trans: alpha = alpha + self.end_scores.view(1, -1) - + return torch.logsumexp(alpha, 1) - + def _gold_score(self, logits, tags, mask): """ Compute the score for the gold path. @@ -226,7 +226,7 @@ class ConditionalRandomField(nn.Module): seq_len, batch_size, _ = logits.size() batch_idx = torch.arange(batch_size, dtype=torch.long, device=logits.device) seq_idx = torch.arange(seq_len, dtype=torch.long, device=logits.device) - + # trans_socre [L-1, B] mask = mask.byte() flip_mask = mask.eq(0) @@ -243,7 +243,7 @@ class ConditionalRandomField(nn.Module): score = score + st_scores + ed_scores # return [B,] return score - + def forward(self, feats, tags, mask): """ 用于计算CRF的前向loss,返回值为一个batch_size的FloatTensor,可能需要mean()求得loss。 @@ -258,9 +258,9 @@ class ConditionalRandomField(nn.Module): mask = mask.transpose(0, 1).float() all_path_score = self._normalizer_likelihood(feats, mask) gold_path_score = self._gold_score(feats, tags, mask) - + return all_path_score - gold_path_score - + def viterbi_decode(self, logits, mask, unpad=False): """给定一个特征矩阵以及转移分数矩阵,计算出最佳的路径以及对应的分数 @@ -277,7 +277,7 @@ class ConditionalRandomField(nn.Module): batch_size, seq_len, n_tags = logits.size() logits = logits.transpose(0, 1).data # L, B, H mask = mask.transpose(0, 1).data.byte() # L, B - + # dp vpath = logits.new_zeros((seq_len, batch_size, n_tags), dtype=torch.long) vscore = logits[0] @@ -286,7 +286,7 @@ class ConditionalRandomField(nn.Module): if self.include_start_end_trans: transitions[n_tags, :n_tags] += self.start_scores.data transitions[:n_tags, n_tags + 1] += self.end_scores.data - + vscore += transitions[n_tags, :n_tags] trans_score = transitions[:n_tags, :n_tags].view(1, n_tags, n_tags).data for i in range(1, seq_len): @@ -297,17 +297,17 @@ class ConditionalRandomField(nn.Module): vpath[i] = best_dst vscore = best_score.masked_fill(mask[i].eq(0).view(batch_size, 1), 0) + \ vscore.masked_fill(mask[i].view(batch_size, 1), 0) - + if self.include_start_end_trans: vscore += transitions[:n_tags, n_tags + 1].view(1, -1) - + # backtrace batch_idx = torch.arange(batch_size, dtype=torch.long, device=logits.device) seq_idx = torch.arange(seq_len, dtype=torch.long, device=logits.device) lens = (mask.long().sum(0) - 1) # idxes [L, B], batched idx from seq_len-1 to 0 idxes = (lens.view(1, -1) - seq_idx.view(-1, 1)) % seq_len - + ans = logits.new_empty((seq_len, batch_size), dtype=torch.long) ans_score, last_tags = vscore.max(1) ans[idxes[0], batch_idx] = last_tags diff --git a/fastNLP/modules/decoder/mlp.py b/fastNLP/modules/decoder/mlp.py index 418b3a77..9d9d80f2 100644 --- a/fastNLP/modules/decoder/mlp.py +++ b/fastNLP/modules/decoder/mlp.py @@ -10,7 +10,7 @@ from ..utils import initial_parameter class MLP(nn.Module): """ - 别名::class:`fastNLP.modules.MLP` :class:`fastNLP.modules.decoder.mlp.MLP` + 别名::class:`fastNLP.modules.MLP` :class:`fastNLP.modules.decoder.MLP` 多层感知器 @@ -40,7 +40,7 @@ class MLP(nn.Module): >>> print(x) >>> print(y) """ - + def __init__(self, size_layer, activation='relu', output_activation=None, initial_method=None, dropout=0.0): super(MLP, self).__init__() self.hiddens = nn.ModuleList() @@ -51,9 +51,9 @@ class MLP(nn.Module): self.output = nn.Linear(size_layer[i - 1], size_layer[i]) else: self.hiddens.append(nn.Linear(size_layer[i - 1], size_layer[i])) - + self.dropout = nn.Dropout(p=dropout) - + actives = { 'relu': nn.ReLU(), 'tanh': nn.Tanh(), @@ -82,7 +82,7 @@ class MLP(nn.Module): else: raise ValueError("should set activation correctly: {}".format(activation)) initial_parameter(self, initial_method) - + def forward(self, x): """ :param torch.Tensor x: MLP接受的输入 diff --git a/fastNLP/modules/decoder/utils.py b/fastNLP/modules/decoder/utils.py index 249f3ff6..9e773336 100644 --- a/fastNLP/modules/decoder/utils.py +++ b/fastNLP/modules/decoder/utils.py @@ -6,7 +6,7 @@ import torch def viterbi_decode(logits, transitions, mask=None, unpad=False): r""" - 别名::class:`fastNLP.modules.viterbi_decode` :class:`fastNLP.modules.decoder.utils.viterbi_decode` + 别名::class:`fastNLP.modules.viterbi_decode` :class:`fastNLP.modules.decoder.viterbi_decode` 给定一个特征矩阵以及转移分数矩阵,计算出最佳的路径以及对应的分数 @@ -30,11 +30,11 @@ def viterbi_decode(logits, transitions, mask=None, unpad=False): mask = mask.transpose(0, 1).data.byte() # L, B else: mask = logits.new_ones((seq_len, batch_size), dtype=torch.uint8) - + # dp vpath = logits.new_zeros((seq_len, batch_size, n_tags), dtype=torch.long) vscore = logits[0] - + trans_score = transitions.view(1, n_tags, n_tags).data for i in range(1, seq_len): prev_score = vscore.view(batch_size, n_tags, 1) @@ -44,14 +44,14 @@ def viterbi_decode(logits, transitions, mask=None, unpad=False): vpath[i] = best_dst vscore = best_score.masked_fill(mask[i].eq(0).view(batch_size, 1), 0) + \ vscore.masked_fill(mask[i].view(batch_size, 1), 0) - + # backtrace batch_idx = torch.arange(batch_size, dtype=torch.long, device=logits.device) seq_idx = torch.arange(seq_len, dtype=torch.long, device=logits.device) lens = (mask.long().sum(0) - 1) # idxes [L, B], batched idx from seq_len-1 to 0 idxes = (lens.view(1, -1) - seq_idx.view(-1, 1)) % seq_len - + ans = logits.new_empty((seq_len, batch_size), dtype=torch.long) ans_score, last_tags = vscore.max(1) ans[idxes[0], batch_idx] = last_tags From a09cf518d7d923c5adae48d3f8f54009682a795b Mon Sep 17 00:00:00 2001 From: ChenXin Date: Fri, 12 Jul 2019 11:05:57 +0800 Subject: [PATCH 04/11] =?UTF-8?q?modules=E5=85=A5=E5=8F=A3=E7=9A=84?= =?UTF-8?q?=E4=BB=8B=E7=BB=8D=E5=92=8Cdropout=E7=9A=84=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastNLP/modules/__init__.py | 27 +++++++++++++-------------- fastNLP/modules/dropout.py | 6 ++---- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/fastNLP/modules/__init__.py b/fastNLP/modules/__init__.py index 43ec3f5f..7b0237dc 100644 --- a/fastNLP/modules/__init__.py +++ b/fastNLP/modules/__init__.py @@ -1,22 +1,19 @@ """ -大部分用于的 NLP 任务神经网络都可以看做由编码 :mod:`~fastNLP.modules.encoder` 、 -解码 :mod:`~fastNLP.modules.decoder` 两种模块组成。 .. image:: figures/text_classification.png -:mod:`~fastNLP.modules` 中实现了 fastNLP 提供的诸多模块组件,可以帮助用户快速搭建自己所需的网络。 -两种模块的功能和常见组件如下: +大部分用于的 NLP 任务神经网络都可以看做由 :mod:`embedding` 、 :mod:`~fastNLP.modules.encoder` 、 +:mod:`~fastNLP.modules.decoder` 三种模块组成。 本模块中实现了 fastNLP 提供的诸多模块组件, +可以帮助用户快速搭建自己所需的网络。几种模块的功能和常见组件如下: + +.. csv-table:: + :header: "类型", "功能", "常见组件" + + "embedding", 参见 :doc:`fastNLP.embeddings` , "Elmo, Bert" + "encoder", "将输入编码为具有表示能力的向量", "CNN, LSTM, Transformer" + "decoder", "将具有某种表示意义的向量解码为需要的输出形式 ", "MLP, CRF" + "其它", "配合其它组件使用的组件", "Dropout" -+-----------------------+-----------------------+-----------------------+ -| module type | functionality | example | -+=======================+=======================+=======================+ -| encoder | 将输入编码为具有具 | embedding, RNN, CNN, | -| | 有表示能力的向量 | transformer | -+-----------------------+-----------------------+-----------------------+ -| decoder | 将具有某种表示意义的 | MLP, CRF | -| | 向量解码为需要的输出 | | -| | 形式 | | -+-----------------------+-----------------------+-----------------------+ """ __all__ = [ @@ -40,6 +37,8 @@ __all__ = [ "ConditionalRandomField", "viterbi_decode", "allowed_transitions", + + "TimestepDropout", ] from . import decoder diff --git a/fastNLP/modules/dropout.py b/fastNLP/modules/dropout.py index 1363165c..0ea2a2d9 100644 --- a/fastNLP/modules/dropout.py +++ b/fastNLP/modules/dropout.py @@ -5,10 +5,8 @@ import torch class TimestepDropout(torch.nn.Dropout): """ - 别名::class:`fastNLP.modules.TimestepDropout` - - 接受的参数shape为``[batch_size, num_timesteps, embedding_dim)]`` 使用同一个mask(shape为``(batch_size, embedding_dim)``) - 在每个timestamp上做dropout。 + 传入参数的shape为 ``(batch_size, num_timesteps, embedding_dim)`` + 使用同一个shape为 ``(batch_size, embedding_dim)`` 的mask在每个timestamp上做dropout。 """ def forward(self, x): From f3a9fc5b7912038bc41cfd6f53515f88405f8b9d Mon Sep 17 00:00:00 2001 From: ChenXin Date: Fri, 12 Jul 2019 11:16:21 +0800 Subject: [PATCH 05/11] =?UTF-8?q?encoder=E9=87=8C=E9=9D=A2=E7=9A=84?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E5=92=8C=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastNLP/modules/__init__.py | 14 ++++-- fastNLP/modules/encoder/__init__.py | 12 ++--- fastNLP/modules/encoder/attention.py | 54 ++++++++++----------- fastNLP/modules/encoder/bert.py | 44 ++++++++++------- fastNLP/modules/encoder/char_encoder.py | 23 ++++----- fastNLP/modules/encoder/conv_maxpool.py | 15 +++--- fastNLP/modules/encoder/lstm.py | 5 +- fastNLP/modules/encoder/pooling.py | 30 ++++++------ fastNLP/modules/encoder/star_transformer.py | 50 +++++++++---------- fastNLP/modules/encoder/transformer.py | 10 ++-- fastNLP/modules/encoder/variational_rnn.py | 48 +++++++++--------- 11 files changed, 161 insertions(+), 144 deletions(-) diff --git a/fastNLP/modules/__init__.py b/fastNLP/modules/__init__.py index 7b0237dc..0e16d48c 100644 --- a/fastNLP/modules/__init__.py +++ b/fastNLP/modules/__init__.py @@ -17,22 +17,30 @@ """ __all__ = [ - # "BertModel", + "BertModel", + "ConvolutionCharEncoder", "LSTMCharEncoder", + "ConvMaxpool", + "LSTM", + "StarTransformer", + "TransformerEncoder", + "VarRNN", "VarLSTM", "VarGRU", - + "MaxPool", "MaxPoolWithMask", "AvgPool", + "AvgPoolWithMask", + "MultiHeadAttention", - + "MLP", "ConditionalRandomField", "viterbi_decode", diff --git a/fastNLP/modules/encoder/__init__.py b/fastNLP/modules/encoder/__init__.py index 051a0c01..4946a70f 100644 --- a/fastNLP/modules/encoder/__init__.py +++ b/fastNLP/modules/encoder/__init__.py @@ -1,17 +1,17 @@ __all__ = [ "BertModel", - + "ConvolutionCharEncoder", "LSTMCharEncoder", - + "ConvMaxpool", - + "LSTM", - + "StarTransformer", - + "TransformerEncoder", - + "VarRNN", "VarLSTM", "VarGRU", diff --git a/fastNLP/modules/encoder/attention.py b/fastNLP/modules/encoder/attention.py index 0a42d889..fe3f7fd8 100644 --- a/fastNLP/modules/encoder/attention.py +++ b/fastNLP/modules/encoder/attention.py @@ -8,8 +8,6 @@ import torch import torch.nn.functional as F from torch import nn -from fastNLP.modules.dropout import TimestepDropout - from fastNLP.modules.utils import initial_parameter @@ -18,7 +16,7 @@ class DotAttention(nn.Module): .. todo:: 补上文档 """ - + def __init__(self, key_size, value_size, dropout=0.0): super(DotAttention, self).__init__() self.key_size = key_size @@ -26,7 +24,7 @@ class DotAttention(nn.Module): self.scale = math.sqrt(key_size) self.drop = nn.Dropout(dropout) self.softmax = nn.Softmax(dim=2) - + def forward(self, Q, K, V, mask_out=None): """ @@ -45,7 +43,7 @@ class DotAttention(nn.Module): class MultiHeadAttention(nn.Module): """ - 别名::class:`fastNLP.modules.MultiHeadAttention` :class:`fastNLP.modules.encoder.attention.MultiHeadAttention` + 别名::class:`fastNLP.modules.MultiHeadAttention` :class:`fastNLP.modules.encoder.MultiHeadAttention` :param input_size: int, 输入维度的大小。同时也是输出维度的大小。 :param key_size: int, 每个head的维度大小。 @@ -53,14 +51,14 @@ class MultiHeadAttention(nn.Module): :param num_head: int,head的数量。 :param dropout: float。 """ - + def __init__(self, input_size, key_size, value_size, num_head, dropout=0.1): super(MultiHeadAttention, self).__init__() self.input_size = input_size self.key_size = key_size self.value_size = value_size self.num_head = num_head - + in_size = key_size * num_head self.q_in = nn.Linear(input_size, in_size) self.k_in = nn.Linear(input_size, in_size) @@ -69,14 +67,14 @@ class MultiHeadAttention(nn.Module): self.attention = DotAttention(key_size=key_size, value_size=value_size, dropout=dropout) self.out = nn.Linear(value_size * num_head, input_size) self.reset_parameters() - + def reset_parameters(self): sqrt = math.sqrt nn.init.normal_(self.q_in.weight, mean=0, std=sqrt(2.0 / (self.input_size + self.key_size))) nn.init.normal_(self.k_in.weight, mean=0, std=sqrt(2.0 / (self.input_size + self.key_size))) nn.init.normal_(self.v_in.weight, mean=0, std=sqrt(2.0 / (self.input_size + self.value_size))) nn.init.xavier_normal_(self.out.weight) - + def forward(self, Q, K, V, atte_mask_out=None): """ @@ -92,7 +90,7 @@ class MultiHeadAttention(nn.Module): q = self.q_in(Q).view(batch, sq, n_head, d_k) k = self.k_in(K).view(batch, sk, n_head, d_k) v = self.v_in(V).view(batch, sk, n_head, d_v) - + # transpose q, k and v to do batch attention q = q.permute(2, 0, 1, 3).contiguous().view(-1, sq, d_k) k = k.permute(2, 0, 1, 3).contiguous().view(-1, sk, d_k) @@ -100,7 +98,7 @@ class MultiHeadAttention(nn.Module): if atte_mask_out is not None: atte_mask_out = atte_mask_out.repeat(n_head, 1, 1) atte = self.attention(q, k, v, atte_mask_out).view(n_head, batch, sq, d_v) - + # concat all heads, do output linear atte = atte.permute(1, 2, 0, 3).contiguous().view(batch, sq, -1) output = self.out(atte) @@ -124,11 +122,11 @@ class BiAttention(nn.Module): \end{array} """ - + def __init__(self): super(BiAttention, self).__init__() self.inf = 10e12 - + def forward(self, in_x1, in_x2, x1_len, x2_len): """ :param torch.Tensor in_x1: [batch_size, x1_seq_len, hidden_size] 第一句的特征表示 @@ -139,36 +137,36 @@ class BiAttention(nn.Module): torch.Tensor out_x2: [batch_size, x2_seq_len, hidden_size] 第一句attend到的特征表示 """ - + assert in_x1.size()[0] == in_x2.size()[0] assert in_x1.size()[2] == in_x2.size()[2] # The batch size and hidden size must be equal. assert in_x1.size()[1] == x1_len.size()[1] and in_x2.size()[1] == x2_len.size()[1] # The seq len in in_x and x_len must be equal. assert in_x1.size()[0] == x1_len.size()[0] and x1_len.size()[0] == x2_len.size()[0] - + batch_size = in_x1.size()[0] x1_max_len = in_x1.size()[1] x2_max_len = in_x2.size()[1] - + in_x2_t = torch.transpose(in_x2, 1, 2) # [batch_size, hidden_size, x2_seq_len] - + attention_matrix = torch.bmm(in_x1, in_x2_t) # [batch_size, x1_seq_len, x2_seq_len] - + a_mask = x1_len.le(0.5).float() * -self.inf # [batch_size, x1_seq_len] a_mask = a_mask.view(batch_size, x1_max_len, -1) a_mask = a_mask.expand(-1, -1, x2_max_len) # [batch_size, x1_seq_len, x2_seq_len] b_mask = x2_len.le(0.5).float() * -self.inf b_mask = b_mask.view(batch_size, -1, x2_max_len) b_mask = b_mask.expand(-1, x1_max_len, -1) # [batch_size, x1_seq_len, x2_seq_len] - + attention_a = F.softmax(attention_matrix + a_mask, dim=2) # [batch_size, x1_seq_len, x2_seq_len] attention_b = F.softmax(attention_matrix + b_mask, dim=1) # [batch_size, x1_seq_len, x2_seq_len] - + out_x1 = torch.bmm(attention_a, in_x2) # [batch_size, x1_seq_len, hidden_size] attention_b_t = torch.transpose(attention_b, 1, 2) out_x2 = torch.bmm(attention_b_t, in_x1) # [batch_size, x2_seq_len, hidden_size] - + return out_x1, out_x2 @@ -182,10 +180,10 @@ class SelfAttention(nn.Module): :param float drop: dropout概率,默认值为0.5 :param str initial_method: 初始化参数方法 """ - + def __init__(self, input_size, attention_unit=300, attention_hops=10, drop=0.5, initial_method=None, ): super(SelfAttention, self).__init__() - + self.attention_hops = attention_hops self.ws1 = nn.Linear(input_size, attention_unit, bias=False) self.ws2 = nn.Linear(attention_unit, attention_hops, bias=False) @@ -194,7 +192,7 @@ class SelfAttention(nn.Module): self.drop = nn.Dropout(drop) self.tanh = nn.Tanh() initial_parameter(self, initial_method) - + def _penalization(self, attention): """ compute the penalization term for attention module @@ -208,7 +206,7 @@ class SelfAttention(nn.Module): mat = torch.bmm(attention, attention_t) - self.I[:attention.size(0)] ret = (torch.sum(torch.sum((mat ** 2), 2), 1).squeeze() + 1e-10) ** 0.5 return torch.sum(ret) / size[0] - + def forward(self, input, input_origin): """ :param torch.Tensor input: [baz, senLen, h_dim] 要做attention的矩阵 @@ -218,14 +216,14 @@ class SelfAttention(nn.Module): """ input = input.contiguous() size = input.size() # [bsz, len, nhid] - + input_origin = input_origin.expand(self.attention_hops, -1, -1) # [hops,baz, len] input_origin = input_origin.transpose(0, 1).contiguous() # [baz, hops,len] - + y1 = self.tanh(self.ws1(self.drop(input))) # [baz,len,dim] -->[bsz,len, attention-unit] attention = self.ws2(y1).transpose(1, 2).contiguous() # [bsz,len, attention-unit]--> [bsz, len, hop]--> [baz,hop,len] - + attention = attention + (-999999 * (input_origin == 0).float()) # remove the weight on padding token. attention = F.softmax(attention, 2) # [baz ,hop, len] return torch.bmm(attention, input), self._penalization(attention) # output1 --> [baz ,hop ,nhid] diff --git a/fastNLP/modules/encoder/bert.py b/fastNLP/modules/encoder/bert.py index 6d32ae74..ddd22ed0 100644 --- a/fastNLP/modules/encoder/bert.py +++ b/fastNLP/modules/encoder/bert.py @@ -1,11 +1,11 @@ - - - """ 这个页面的代码很大程度上参考(复制粘贴)了https://github.com/huggingface/pytorch-pretrained-BERT的代码, 如果你发现该代码对你 有用,也请引用一下他们。 """ +__all__ = [ + "BertModel" +] import collections @@ -26,6 +26,7 @@ CONFIG_FILE = 'bert_config.json' class BertConfig(object): """Configuration class to store the configuration of a `BertModel`. """ + def __init__(self, vocab_size_or_config_json_file, hidden_size=768, @@ -65,7 +66,7 @@ class BertConfig(object): layer_norm_eps: The epsilon used by LayerNorm. """ if isinstance(vocab_size_or_config_json_file, str) or (sys.version_info[0] == 2 - and isinstance(vocab_size_or_config_json_file, unicode)): + and isinstance(vocab_size_or_config_json_file, unicode)): with open(vocab_size_or_config_json_file, "r", encoding='utf-8') as reader: json_config = json.loads(reader.read()) for key, value in json_config.items(): @@ -150,6 +151,7 @@ class BertLayerNorm(nn.Module): class BertEmbeddings(nn.Module): """Construct the embeddings from word, position and token_type embeddings. """ + def __init__(self, config): super(BertEmbeddings, self).__init__() self.word_embeddings = nn.Embedding(config.vocab_size, config.hidden_size, padding_idx=0) @@ -331,7 +333,10 @@ class BertPooler(nn.Module): class BertModel(nn.Module): - """BERT(Bidirectional Embedding Representations from Transformers). + """ + 别名::class:`fastNLP.modules.BertModel` :class:`fastNLP.modules.encoder.BertModel` + + BERT(Bidirectional Embedding Representations from Transformers). 如果你想使用预训练好的权重矩阵,请在以下网址下载. sources:: @@ -449,9 +454,9 @@ class BertModel(nn.Module): model = cls(config, *inputs, **kwargs) if state_dict is None: files = glob.glob(os.path.join(pretrained_model_dir, '*.bin')) - if len(files)==0: + if len(files) == 0: raise FileNotFoundError(f"There is no *.bin file in {pretrained_model_dir}") - elif len(files)>1: + elif len(files) > 1: raise FileExistsError(f"There are multiple *.bin files in {pretrained_model_dir}") weights_path = files[0] state_dict = torch.load(weights_path, map_location='cpu') @@ -580,6 +585,7 @@ def load_vocab(vocab_file): index += 1 return vocab + class BasicTokenizer(object): """Runs basic tokenization (punctuation splitting, lower casing, etc.).""" @@ -765,8 +771,8 @@ class BertTokenizer(object): [(ids, tok) for tok, ids in self.vocab.items()]) self.do_basic_tokenize = do_basic_tokenize if do_basic_tokenize: - self.basic_tokenizer = BasicTokenizer(do_lower_case=do_lower_case, - never_split=never_split) + self.basic_tokenizer = BasicTokenizer(do_lower_case=do_lower_case, + never_split=never_split) self.wordpiece_tokenizer = WordpieceTokenizer(vocab=self.vocab) self.max_len = max_len if max_len is not None else int(1e12) @@ -821,7 +827,7 @@ class BertTokenizer(object): for token, token_index in sorted(self.vocab.items(), key=lambda kv: kv[1]): if index != token_index: print("Saving vocabulary to {}: vocabulary indices are not consecutive." - " Please check that the vocabulary is not corrupted!".format(vocab_file)) + " Please check that the vocabulary is not corrupted!".format(vocab_file)) index = token_index writer.write(token + u'\n') index += 1 @@ -841,6 +847,7 @@ class BertTokenizer(object): tokenizer = cls(pretrained_model_name_or_path, *inputs, **kwargs) return tokenizer + VOCAB_NAME = 'vocab.txt' @@ -849,7 +856,8 @@ class _WordPieceBertModel(nn.Module): 这个模块用于直接计算word_piece的结果. """ - def __init__(self, model_dir:str, layers:str='-1'): + + def __init__(self, model_dir: str, layers: str = '-1'): super().__init__() self.tokenzier = BertTokenizer.from_pretrained(model_dir) @@ -858,11 +866,11 @@ class _WordPieceBertModel(nn.Module): encoder_layer_number = len(self.encoder.encoder.layer) self.layers = list(map(int, layers.split(','))) for layer in self.layers: - if layer<0: - assert -layer<=encoder_layer_number, f"The layer index:{layer} is out of scope for " \ + if layer < 0: + assert -layer <= encoder_layer_number, f"The layer index:{layer} is out of scope for " \ f"a bert model with {encoder_layer_number} layers." else: - assert layer