diff --git a/PaddleNLP/examples/language_model/rnnlm/README.md b/PaddleNLP/examples/language_model/rnnlm/README.md index 04fae45b53765e6fd15a9a3a3f5265e6a54f4bf9..8ffc46358231b0276d8a9058b0fda809fa2a35a6 100644 --- a/PaddleNLP/examples/language_model/rnnlm/README.md +++ b/PaddleNLP/examples/language_model/rnnlm/README.md @@ -5,8 +5,6 @@ ## 1. 任务说明 本文主要介绍基于lstm的语言的模型的实现,给定一个输入词序列(中文分词、英文tokenize),计算其ppl(语言模型困惑度,用户表示句子的流利程度),基于循环神经网络语言模型的介绍可以[参阅论文](https://arxiv.org/abs/1409.2329)。相对于传统的方法,基于循环神经网络的方法能够更好的解决稀疏词的问题。 -**目前语言模型要求使用PaddlePaddle 2.0及以上版本或适当的develop版本。** - ## 2. 效果说明 @@ -27,6 +25,22 @@ ## 1. 开始第一次模型调用 +### 安装说明 + +* PaddlePaddle 安装 + + 本项目依赖于 PaddlePaddle 2.0-rc1 及以上版本,请参考 [安装指南](http://www.paddlepaddle.org/#quick-start) 进行安装 + +* PaddleNLP 安装 + + ```shell + pip install paddlenlp>=2.0.0b + ``` + +* 环境依赖 + + Python的版本要求 3.6+ + ### 数据准备 为了方便开发者进行测试,我们内置了数据下载脚本,默认自动下载PTB数据集。 diff --git a/PaddleNLP/examples/language_model/rnnlm/model.py b/PaddleNLP/examples/language_model/rnnlm/model.py index b28fc1cbef9d8c8b3ded75b05c91b2e375d05dc5..35366cf1c5cbb55531925f755fa0ebfb64e04a89 100644 --- a/PaddleNLP/examples/language_model/rnnlm/model.py +++ b/PaddleNLP/examples/language_model/rnnlm/model.py @@ -77,8 +77,8 @@ class CrossEntropyLossForLm(nn.Layer): def forward(self, y, label): label = paddle.unsqueeze(label, axis=2) - loss = paddle.nn.functional.softmax_with_cross_entropy( - logits=y, label=label, soft_label=False) + loss = paddle.nn.functional.cross_entropy( + input=y, label=label, reduction='none') loss = paddle.squeeze(loss, axis=[2]) loss = paddle.mean(loss, axis=[0]) loss = paddle.sum(loss) @@ -89,4 +89,3 @@ class UpdateModel(paddle.callbacks.Callback): # This callback reset model hidden states and update learning rate before each epoch begins def on_epoch_begin(self, epoch=None, logs=None): self.model.network.reset_states() - diff --git a/PaddleNLP/examples/machine_reading_comprehension/DuReader-robust/README.md b/PaddleNLP/examples/machine_reading_comprehension/DuReader-robust/README.md index dfad8eb43e2d9fa8b88a8e063ef62073113a0cc0..f1cfd693f3f2931970bd2bf3722610ff713431d3 100644 --- a/PaddleNLP/examples/machine_reading_comprehension/DuReader-robust/README.md +++ b/PaddleNLP/examples/machine_reading_comprehension/DuReader-robust/README.md @@ -27,7 +27,7 @@ DuReader-robust数据集是单篇章、抽取式阅读理解数据集,具体 * PaddlePaddle 安装 - 本项目依赖于 PaddlePaddle 2.0 及以上版本,请参考 [安装指南](http://www.paddlepaddle.org/#quick-start) 进行安装 + 本项目依赖于 PaddlePaddle 2.0-rc1 及以上版本,请参考 [安装指南](http://www.paddlepaddle.org/#quick-start) 进行安装 * PaddleNLP 安装 diff --git a/PaddleNLP/examples/machine_reading_comprehension/DuReader-robust/run_du.py b/PaddleNLP/examples/machine_reading_comprehension/DuReader-robust/run_du.py index feaed689391bd1de76f9c7acb477379f61bdd30c..6cb0b83d5860450a3905cb57c610e80779ea0697 100644 --- a/PaddleNLP/examples/machine_reading_comprehension/DuReader-robust/run_du.py +++ b/PaddleNLP/examples/machine_reading_comprehension/DuReader-robust/run_du.py @@ -54,12 +54,10 @@ class CrossEntropyLossForSQuAD(paddle.nn.Layer): start_position, end_position = label start_position = paddle.unsqueeze(start_position, axis=-1) end_position = paddle.unsqueeze(end_position, axis=-1) - start_loss = paddle.nn.functional.softmax_with_cross_entropy( - logits=start_logits, label=start_position, soft_label=False) - start_loss = paddle.mean(start_loss) - end_loss = paddle.nn.functional.softmax_with_cross_entropy( - logits=end_logits, label=end_position, soft_label=False) - end_loss = paddle.mean(end_loss) + start_loss = paddle.nn.functional.cross_entropy( + input=start_logits, label=start_position) + end_loss = paddle.nn.functional.cross_entropy( + input=end_logits, label=end_position) loss = (start_loss + end_loss) / 2 return loss diff --git a/PaddleNLP/examples/machine_reading_comprehension/DuReader-yesno/README.md b/PaddleNLP/examples/machine_reading_comprehension/DuReader-yesno/README.md index 9a970ff6edbddad94d41899bc92d7b02d03f2ff2..d1fda2caa1a51b5625ab2a1b98a7a2faa5b82ce1 100644 --- a/PaddleNLP/examples/machine_reading_comprehension/DuReader-yesno/README.md +++ b/PaddleNLP/examples/machine_reading_comprehension/DuReader-yesno/README.md @@ -41,7 +41,7 @@ * PaddlePaddle 安装 - 本项目依赖于 PaddlePaddle 2.0 及以上版本,请参考 [安装指南](http://www.paddlepaddle.org/#quick-start) 进行安装 + 本项目依赖于 PaddlePaddle 2.0-rc1 及以上版本,请参考 [安装指南](http://www.paddlepaddle.org/#quick-start) 进行安装 * PaddleNLP 安装 diff --git a/PaddleNLP/examples/machine_reading_comprehension/SQuAD/README.md b/PaddleNLP/examples/machine_reading_comprehension/SQuAD/README.md index 1ba61f9cb9204d2f2c911313cebc4bdd7b68e3ac..21c5748221a8050941072aa368523dab55daa24c 100644 --- a/PaddleNLP/examples/machine_reading_comprehension/SQuAD/README.md +++ b/PaddleNLP/examples/machine_reading_comprehension/SQuAD/README.md @@ -27,7 +27,7 @@ SQuAD v2.0 * PaddlePaddle 安装 - 本项目依赖于 PaddlePaddle 2.0 及以上版本,请参考 [安装指南](http://www.paddlepaddle.org/#quick-start) 进行安装 + 本项目依赖于 PaddlePaddle 2.0-rc1 及以上版本,请参考 [安装指南](http://www.paddlepaddle.org/#quick-start) 进行安装 * PaddleNLP 安装 @@ -56,7 +56,7 @@ python -u ./run_squad.py \ --batch_size 12 \ --learning_rate 3e-5 \ --num_train_epochs 2 \ - --logging_steps 1000 \ + --logging_steps 100 \ --save_steps 1000 \ --warmup_proportion 0.1 \ --weight_decay 0.01 \ diff --git a/PaddleNLP/examples/machine_reading_comprehension/SQuAD/run_squad.py b/PaddleNLP/examples/machine_reading_comprehension/SQuAD/run_squad.py index 04a1157354d56aa3ae2dc6feb550961b55544e04..e73087e98539d410fe19e96255474c829a0e308c 100644 --- a/PaddleNLP/examples/machine_reading_comprehension/SQuAD/run_squad.py +++ b/PaddleNLP/examples/machine_reading_comprehension/SQuAD/run_squad.py @@ -51,12 +51,10 @@ class CrossEntropyLossForSQuAD(paddle.nn.Layer): start_position, end_position = label start_position = paddle.unsqueeze(start_position, axis=-1) end_position = paddle.unsqueeze(end_position, axis=-1) - start_loss = paddle.nn.functional.softmax_with_cross_entropy( - logits=start_logits, label=start_position, soft_label=False) - start_loss = paddle.mean(start_loss) - end_loss = paddle.nn.functional.softmax_with_cross_entropy( - logits=end_logits, label=end_position, soft_label=False) - end_loss = paddle.mean(end_loss) + start_loss = paddle.nn.functional.cross_entropy( + input=start_logits, label=start_position) + end_loss = paddle.nn.functional.cross_entropy( + input=end_logits, label=end_position) loss = (start_loss + end_loss) / 2 return loss diff --git a/PaddleNLP/paddlenlp/metrics/squad.py b/PaddleNLP/paddlenlp/metrics/squad.py index 23b55b82ef4e42899a2a8df755045c0fbf0f0b4e..da5d11f00ed85093b177f1cc811bf8c20a44c2a6 100644 --- a/PaddleNLP/paddlenlp/metrics/squad.py +++ b/PaddleNLP/paddlenlp/metrics/squad.py @@ -378,8 +378,8 @@ def compute_f1(a_gold, a_pred, is_whitespace_splited=True): pred_toks = normalize_answer(a_pred).split() if not is_whitespace_splited: - gold_toks = gold_toks[0] - pred_toks = pred_toks[0] + gold_toks = gold_toks[0] if gold_toks else "" + pred_toks = pred_toks[0] if pred_toks else "" common = collections.Counter(gold_toks) & collections.Counter(pred_toks) num_same = sum(common.values())