未验证 提交 333b3620 编写于 作者: L Li Fuchen 提交者: GitHub

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
上级 9db963b0
......@@ -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. 效果说明
......
......@@ -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')
......
......@@ -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
......@@ -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):
......
......@@ -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
......
......@@ -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,23 @@ 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
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)
def check_version():
"""
Log error and exit when the installed version of paddlepaddle is
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册