From 667b3126a969e0121b03849de365853fe5b21580 Mon Sep 17 00:00:00 2001 From: Xipeng Qiu Date: Mon, 8 Apr 2019 20:11:47 +0800 Subject: [PATCH 1/7] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 5346fbd7..18781dc5 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ FastNLP is a modular Natural Language Processing system based on PyTorch, built for fast development of NLP models. +*Very sad day for all of us. @FengZiYjun is no more with us. May his soul rest in peace. We will miss you very very much!* + A deep learning NLP model is the composition of three types of modules: From c344f7deace4603720a3973af9284417cd8bb968 Mon Sep 17 00:00:00 2001 From: Xipeng Qiu Date: Mon, 8 Apr 2019 20:14:12 +0800 Subject: [PATCH 2/7] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 18781dc5..2b4627f6 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,9 @@ ![Hex.pm](https://img.shields.io/hexpm/l/plug.svg) [![Documentation Status](https://readthedocs.org/projects/fastnlp/badge/?version=latest)](http://fastnlp.readthedocs.io/?badge=latest) -FastNLP is a modular Natural Language Processing system based on PyTorch, built for fast development of NLP models. +Very sad day for all of us. @FengZiYjun is no more with us. May his soul rest in peace. We will miss you very very much! -*Very sad day for all of us. @FengZiYjun is no more with us. May his soul rest in peace. We will miss you very very much!* +FastNLP is a modular Natural Language Processing system based on PyTorch, built for fast development of NLP models. A deep learning NLP model is the composition of three types of modules:
From 0f8bed739c71e3f834f213e45dc1694f8b61e74d Mon Sep 17 00:00:00 2001 From: Xipeng Qiu Date: Mon, 8 Apr 2019 20:14:46 +0800 Subject: [PATCH 3/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2b4627f6..106fcdaa 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ ![Hex.pm](https://img.shields.io/hexpm/l/plug.svg) [![Documentation Status](https://readthedocs.org/projects/fastnlp/badge/?version=latest)](http://fastnlp.readthedocs.io/?badge=latest) -Very sad day for all of us. @FengZiYjun is no more with us. May his soul rest in peace. We will miss you very very much! +*Very sad day for all of us. @FengZiYjun is no more with us. May his soul rest in peace. We will miss you very very much!* FastNLP is a modular Natural Language Processing system based on PyTorch, built for fast development of NLP models. From cc900a0e01c5555e4b6bd078d9d92a107572c21e Mon Sep 17 00:00:00 2001 From: Xipeng Qiu Date: Fri, 3 May 2019 10:29:14 +0800 Subject: [PATCH 4/7] Update README.md --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 106fcdaa..a94d7791 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,6 @@ ![Hex.pm](https://img.shields.io/hexpm/l/plug.svg) [![Documentation Status](https://readthedocs.org/projects/fastnlp/badge/?version=latest)](http://fastnlp.readthedocs.io/?badge=latest) -*Very sad day for all of us. @FengZiYjun is no more with us. May his soul rest in peace. We will miss you very very much!* - FastNLP is a modular Natural Language Processing system based on PyTorch, built for fast development of NLP models. A deep learning NLP model is the composition of three types of modules: @@ -89,3 +87,7 @@ pip install fastNLP
readers & savers
+ + +In memory of @FengZiYjun. May his soul rest in peace. We will miss you very very much!* + From b8214f548d4caca5c1011dfeef6508d9d96b1266 Mon Sep 17 00:00:00 2001 From: Xipeng Qiu Date: Fri, 3 May 2019 10:30:06 +0800 Subject: [PATCH 5/7] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index a94d7791..f1cdf128 100644 --- a/README.md +++ b/README.md @@ -89,5 +89,4 @@ pip install fastNLP -In memory of @FengZiYjun. May his soul rest in peace. We will miss you very very much!* - +*In memory of @FengZiYjun. May his soul rest in peace. We will miss you very very much!* \ No newline at end of file From ae3356b0bb6b14dec5fed33efd2e6b0cad76a29a Mon Sep 17 00:00:00 2001 From: Yunfan Shao Date: Fri, 3 May 2019 13:32:36 +0800 Subject: [PATCH 6/7] fix for changing torch API --- fastNLP/modules/encoder/variational_rnn.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fastNLP/modules/encoder/variational_rnn.py b/fastNLP/modules/encoder/variational_rnn.py index a7902813..0d58d67b 100644 --- a/fastNLP/modules/encoder/variational_rnn.py +++ b/fastNLP/modules/encoder/variational_rnn.py @@ -41,7 +41,7 @@ class VarRnnCellWrapper(nn.Module): return torch.cat([hi, h0[:h0_size]], dim=0) return hi[:size] is_lstm = isinstance(hidden, tuple) - input, batch_sizes = input_x + input, batch_sizes = input_x.data, input_x.batch_sizes output = [] cell = self.cell if is_reversed: @@ -127,10 +127,10 @@ class VarRNNBase(nn.Module): seq_len = input.size(1) if self.batch_first else input.size(0) max_batch_size = input.size(0) if self.batch_first else input.size(1) seq_lens = torch.LongTensor([seq_len for _ in range(max_batch_size)]) - input, batch_sizes = pack_padded_sequence(input, seq_lens, batch_first=self.batch_first) + input = pack_padded_sequence(input, seq_lens, batch_first=self.batch_first) else: max_batch_size = int(input.batch_sizes[0]) - input, batch_sizes = input + input, batch_sizes = input.data, input.batch_sizes if hx is None: hx = input.new_zeros(self.num_layers * self.num_directions, From 87e21a26a31b27a60c832fa8103ef3aaf619b059 Mon Sep 17 00:00:00 2001 From: yunfan Date: Thu, 16 May 2019 14:17:53 +0800 Subject: [PATCH 7/7] add model summary --- fastNLP/modules/utils.py | 47 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/fastNLP/modules/utils.py b/fastNLP/modules/utils.py index 047ebb78..23768b03 100644 --- a/fastNLP/modules/utils.py +++ b/fastNLP/modules/utils.py @@ -1,3 +1,5 @@ +from functools import reduce +from collections import OrderedDict import numpy as np import torch import torch.nn as nn @@ -71,7 +73,7 @@ def initial_parameter(net, initial_method=None): def get_embeddings(init_embed): """ 得到词嵌入 - + .. todo:: 补上文档 @@ -81,7 +83,8 @@ def get_embeddings(init_embed): :return nn.Embedding embeddings: """ if isinstance(init_embed, tuple): - res = nn.Embedding(num_embeddings=init_embed[0], embedding_dim=init_embed[1]) + res = nn.Embedding( + num_embeddings=init_embed[0], embedding_dim=init_embed[1]) elif isinstance(init_embed, nn.Embedding): res = init_embed elif isinstance(init_embed, torch.Tensor): @@ -90,5 +93,43 @@ def get_embeddings(init_embed): init_embed = torch.tensor(init_embed, dtype=torch.float32) res = nn.Embedding.from_pretrained(init_embed, freeze=False) else: - raise TypeError('invalid init_embed type: {}'.format((type(init_embed)))) + raise TypeError( + 'invalid init_embed type: {}'.format((type(init_embed)))) return res + + +def summary(model: nn.Module): + """ + 得到模型的总参数量 + + :params model: Pytorch 模型 + :return tuple: 包含总参数量,可训练参数量,不可训练参数量 + """ + train = [] + nontrain = [] + + def layer_summary(module: nn.Module): + def count_size(sizes): + return reduce(lambda x, y: x*y, sizes) + + for p in module.parameters(recurse=False): + if p.requires_grad: + train.append(count_size(p.shape)) + else: + nontrain.append(count_size(p.shape)) + for subm in module.children(): + layer_summary(subm) + + layer_summary(model) + total_train = sum(train) + total_nontrain = sum(nontrain) + total = total_train + total_nontrain + strings = [] + strings.append('Total params: {:,}'.format(total)) + strings.append('Trainable params: {:,}'.format(total_train)) + strings.append('Non-trainable params: {:,}'.format(total_nontrain)) + max_len = len(max(strings, key=len)) + bar = '-'*(max_len + 3) + strings = [bar] + strings + [bar] + print('\n'.join(strings)) + return total, total_train, total_nontrain