From 333b3620054f32a25653de2f9f3155d7d087dc8c Mon Sep 17 00:00:00 2001 From: Li Fuchen Date: Wed, 16 Oct 2019 17:23:26 +0800 Subject: [PATCH] Use new save/load api and change fluid.layers.data to fluid.data (#3547) * Use new save/load api and change fluid.layers.data to fluid.data * add check for paddle version --- PaddleNLP/language_model/README.md | 2 ++ PaddleNLP/language_model/args.py | 5 +++++ PaddleNLP/language_model/run.sh | 3 ++- PaddleNLP/language_model/train.py | 22 +++++++++++++------ PaddleNLP/models/language_model/lm_model.py | 24 +++++++-------------- PaddleNLP/models/model_check.py | 21 ++++++++++++++++++ 6 files changed, 54 insertions(+), 23 deletions(-) diff --git a/PaddleNLP/language_model/README.md b/PaddleNLP/language_model/README.md index 6a989d49..10b882a7 100644 --- a/PaddleNLP/language_model/README.md +++ b/PaddleNLP/language_model/README.md @@ -5,6 +5,8 @@ ## 1. 任务说明 本文主要介绍基于lstm的语言的模型的实现,给定一个输入词序列(中文分词、英文tokenize),计算其ppl(语言模型困惑度,用户表示句子的流利程度),基于循环神经网络语言模型的介绍可以[参阅论文](https://arxiv.org/abs/1409.2329)。相对于传统的方法,基于循环神经网络的方法能够更好的解决稀疏词的问题。 +**目前语言模型要求使用PaddlePaddle 1.6及以上版本或适当的develop版本。** + 同时推荐用户参考[IPython Notebook demo](https://aistudio.baidu.com/aistudio/projectDetail/122290) ## 2. 效果说明 diff --git a/PaddleNLP/language_model/args.py b/PaddleNLP/language_model/args.py index 958a32e3..8014bb52 100644 --- a/PaddleNLP/language_model/args.py +++ b/PaddleNLP/language_model/args.py @@ -72,6 +72,11 @@ def parse_args(): type=str, default="models", help='dir of the saved model.') + parser.add_argument( + '--init_from_pretrain_model', + type=str, + default=None, + help='dir to init model.') parser.add_argument('--enable_ce', action='store_true') parser.add_argument('--batch_size', type=int, default=0, help='batch size') parser.add_argument('--max_epoch', type=int, default=0, help='max epoch') diff --git a/PaddleNLP/language_model/run.sh b/PaddleNLP/language_model/run.sh index e8c711fa..851c8977 100644 --- a/PaddleNLP/language_model/run.sh +++ b/PaddleNLP/language_model/run.sh @@ -6,7 +6,8 @@ function run_train() { python train.py \ --data_path data/simple-examples/data/ \ --model_type small \ - --use_gpu True \ + --use_gpu True + #--init_from_pretrain_model models/0/params } run_train diff --git a/PaddleNLP/language_model/train.py b/PaddleNLP/language_model/train.py index 423d136a..0c3c3d8b 100644 --- a/PaddleNLP/language_model/train.py +++ b/PaddleNLP/language_model/train.py @@ -40,7 +40,7 @@ import os os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" from args import * -from models.model_check import check_cuda +from models.model_check import check_cuda, check_version from models.language_model import lm_model from config import RNNConfig import logging @@ -88,7 +88,10 @@ def save_para_npz(train_prog, train_exe): def main(): args = parse_args() + # check if set use_gpu=True in paddlepaddle cpu version check_cuda(args.use_gpu) + # check if paddlepaddle version is satisfied + check_version() logger = logging.getLogger("lm") logger.setLevel(logging.INFO) @@ -168,6 +171,15 @@ def main(): exe = Executor(place) exe.run(startup_program) + if args.init_from_pretrain_model: + if not os.path.exists(args.init_from_pretrain_model + '.pdparams'): + print(args.init_from_pretrain_model) + raise Warning("The pretrained params do not exist.") + return + fluid.load(main_program, args.init_from_pretrain_model) + print("finish initing model from pretrained params from %s" % + (args.init_from_pretrain_model)) + device_count = len(fluid.cuda_places()) if args.use_gpu else len( fluid.cpu_places()) @@ -280,7 +292,6 @@ def main(): epoch_id=epoch_id, with_lr=True, device_count=device_count) - batch_start_time = time.time() fetch_outs = exe.run(train_program, feed=input_data_feed, @@ -304,7 +315,6 @@ def main(): print( "-- Epoch:[%d]; Batch:[%d]; Time: %.5f s; ppl: %.5f, lr: %.5f" % (epoch_id, batch_id, batch_time, ppl[0], lr[0])) - ppl = np.exp(total_loss / iters) return ppl @@ -434,9 +444,9 @@ def main(): format( len(valid_data), config.batch_size, config.num_steps)) - save_model_dir = os.path.join(args.save_model_dir, str(epoch_id)) - fluid.io.save_persistables( - executor=exe, dirname=save_model_dir, main_program=main_program) + save_model_dir = os.path.join(args.save_model_dir, + str(epoch_id), "params") + fluid.save(main_program, save_model_dir) print("Saved model to: %s.\n" % save_model_dir) with profile_context(args.profile): diff --git a/PaddleNLP/models/language_model/lm_model.py b/PaddleNLP/models/language_model/lm_model.py index 4dc0f9f3..ff668b0c 100644 --- a/PaddleNLP/models/language_model/lm_model.py +++ b/PaddleNLP/models/language_model/lm_model.py @@ -255,16 +255,10 @@ def lm_model(hidden_size, return real_res, last_hidden, last_cell batch_size_each = batch_size // fluid.core.get_cuda_device_count() - x = layers.data( - name="x", - shape=[batch_size_each, num_steps, 1], - dtype='int64', - append_batch_size=False) - y = layers.data( - name="y", - shape=[batch_size_each * num_steps, 1], - dtype='int64', - append_batch_size=False) + x = fluid.data( + name="x", shape=[batch_size_each, num_steps, 1], dtype='int64') + y = fluid.data( + name="y", shape=[batch_size_each * num_steps, 1], dtype='int64') if use_dataloader: dataloader = fluid.io.DataLoader.from_generator( @@ -273,16 +267,14 @@ def lm_model(hidden_size, iterable=False, use_double_buffer=True) - init_hidden = layers.data( + init_hidden = fluid.data( name="init_hidden", shape=[num_layers, batch_size_each, hidden_size], - dtype='float32', - append_batch_size=False) - init_cell = layers.data( + dtype='float32') + init_cell = fluid.data( name="init_cell", shape=[num_layers, batch_size_each, hidden_size], - dtype='float32', - append_batch_size=False) + dtype='float32') init_cell.persistable = True init_hidden.persistable = True diff --git a/PaddleNLP/models/model_check.py b/PaddleNLP/models/model_check.py index 135ababe..8d261153 100644 --- a/PaddleNLP/models/model_check.py +++ b/PaddleNLP/models/model_check.py @@ -22,6 +22,10 @@ def check_cuda(use_cuda, err = \ "\nYou can not set use_cuda = True in the model because you are using paddlepaddle-cpu.\n \ Please: 1. Install paddlepaddle-gpu to run your models on GPU or 2. Set use_cuda = False to run models on CPU.\n" ): + """ + Log error and exit when set use_gpu=true in paddlepaddle + cpu version. + """ try: if use_cuda == True and fluid.is_compiled_with_cuda() == False: print(err) @@ -29,6 +33,7 @@ def check_cuda(use_cuda, err = \ except Exception as e: pass + def check_version(): """ Log error and exit when the installed version of paddlepaddle is @@ -45,6 +50,22 @@ def check_version(): sys.exit(1) +def check_version(): + """ + Log error and exit when the installed version of paddlepaddle is + not satisfied. + """ + err = "PaddlePaddle version 1.6 or higher is required, " \ + "or a suitable develop version is satisfied as well. \n" \ + "Please make sure the version is good with your code." \ + + try: + fluid.require_version('1.6.0') + except Exception as e: + print(err) + sys.exit(1) + + if __name__ == "__main__": check_cuda(True) -- GitLab