未验证 提交 1cb6a643 编写于 作者: L liu zhengxi 提交者: GitHub

update api to 1.8 and update readme (#4609)

* update api to 1.8 for transformer and similarity_net, test=develop

* update readme, test=develop
上级 35db3d17
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
1. paddle安装 1. paddle安装
本项目依赖于 PaddlePaddle 1.6及以上版本或适当的develop版本,请参考 [安装指南](http://www.paddlepaddle.org/#quick-start) 进行安装 本项目依赖于 PaddlePaddle 1.8及以上版本或适当的develop版本,请参考 [安装指南](https://www.paddlepaddle.org.cn/install/quick) 进行安装
2. 下载代码 2. 下载代码
...@@ -44,7 +44,7 @@ ...@@ -44,7 +44,7 @@
3. 环境依赖 3. 环境依赖
请参考PaddlePaddle[安装说明](https://www.paddlepaddle.org.cn/documentation/docs/zh/1.5/beginners_guide/install/index_cn.html)部分的内容 请参考PaddlePaddle[安装说明](https://www.paddlepaddle.org.cn/documentation/docs/zh/install/index_cn.html)部分的内容
### 数据准备 ### 数据准备
......
...@@ -752,18 +752,17 @@ def fast_decode(model_input, src_vocab_size, trg_vocab_size, max_in_len, ...@@ -752,18 +752,17 @@ def fast_decode(model_input, src_vocab_size, trg_vocab_size, max_in_len,
# caches contains states of history steps in decoder self-attention # caches contains states of history steps in decoder self-attention
# and static encoder output projections in encoder-decoder attention # and static encoder output projections in encoder-decoder attention
# to reduce redundant computation. # to reduce redundant computation.
batch_size = layers.shape(start_tokens)[0]
caches = [ caches = [
{ {
"k": # for self attention "k": # for self attention
layers.fill_constant_batch_size_like( layers.fill_constant(
input=start_tokens, shape=[batch_size, n_head, 0, d_key],
shape=[-1, n_head, 0, d_key],
dtype=enc_output.dtype, dtype=enc_output.dtype,
value=0), value=0),
"v": # for self attention "v": # for self attention
layers.fill_constant_batch_size_like( layers.fill_constant(
input=start_tokens, shape=[batch_size, n_head, 0, d_value],
shape=[-1, n_head, 0, d_value],
dtype=enc_output.dtype, dtype=enc_output.dtype,
value=0), value=0),
"static_k": # for encoder-decoder attention "static_k": # for encoder-decoder attention
...@@ -792,12 +791,10 @@ def fast_decode(model_input, src_vocab_size, trg_vocab_size, max_in_len, ...@@ -792,12 +791,10 @@ def fast_decode(model_input, src_vocab_size, trg_vocab_size, max_in_len,
lambda x: layers.gather(x, index=gather_idx), caches) lambda x: layers.gather(x, index=gather_idx), caches)
pre_src_attn_bias = layers.gather( pre_src_attn_bias = layers.gather(
trg_src_attn_bias, index=gather_idx) trg_src_attn_bias, index=gather_idx)
bias_batch_size = layers.shape(pre_src_attn_bias)[0]
pre_pos = layers.elementwise_mul( pre_pos = layers.elementwise_mul(
x=layers.fill_constant_batch_size_like( x=layers.fill_constant(
input=pre_src_attn_bias, # cann't use lod tensor here value=1, shape=[bias_batch_size, 1], dtype=pre_ids.dtype),
value=1,
shape=[-1, 1],
dtype=pre_ids.dtype),
y=step_idx, y=step_idx,
axis=0) axis=0)
logits = wrap_decoder( logits = wrap_decoder(
......
...@@ -210,7 +210,7 @@ class DataLayer(object): ...@@ -210,7 +210,7 @@ class DataLayer(object):
""" """
operation operation
""" """
data = fluid.layers.data( data = fluid.data(
name=name, shape=shape, dtype=dtype, lod_level=lod_level) name=name, shape=shape, dtype=dtype, lod_level=lod_level)
return data return data
...@@ -383,8 +383,10 @@ class ConstantLayer(object): ...@@ -383,8 +383,10 @@ class ConstantLayer(object):
""" """
operation operation
""" """
constant = fluid.layers.fill_constant_batch_size_like(input, shape, shape = list(shape)
dtype, value) input_shape = fluid.layers.shape(input)
shape[0] = input_shape[0]
constant = fluid.layers.fill_constant(shape, dtype, value)
return constant return constant
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
|UNICOM|联通客服|客服| |UNICOM|联通客服|客服|
## 快速开始 ## 快速开始
#### 版本依赖 #### 版本依赖
本项目依赖于 Paddlepaddle Fluid 1.6,请参考[安装指南](http://www.paddlepaddle.org/#quick-start)进行安装。 本项目依赖于 Paddlepaddle Fluid 1.8,请参考[安装指南](https://www.paddlepaddle.org.cn/install/quick)进行安装。
python版本依赖python 2.7 python版本依赖python 2.7
#### 安装代码 #### 安装代码
......
...@@ -47,46 +47,51 @@ from models.model_check import check_version ...@@ -47,46 +47,51 @@ from models.model_check import check_version
from models.model_check import check_cuda from models.model_check import check_cuda
def create_model(args, pyreader_name, is_inference=False, is_pointwise=False): def create_model(args, is_inference=False, is_pointwise=False):
""" """
Create Model for simnet Create Model for simnet
""" """
if is_inference: if is_inference:
inf_pyreader = fluid.layers.py_reader( left = fluid.data(name='left', shape=[None], dtype='int64', lod_level=1)
capacity=16, pos_right = fluid.data(
shapes=([-1], [-1]), name='pos_right', shape=[None], dtype='int64', lod_level=1)
dtypes=('int64', 'int64'), inf_loader = fluid.io.DataLoader.from_generator(
lod_levels=(1, 1), capacity=16,
name=pyreader_name, feed_list=[left, pos_right],
use_double_buffer=False) iterable=False,
use_double_buffer=False)
left, pos_right = fluid.layers.read_file(inf_pyreader) return inf_loader, left, pos_right
return inf_pyreader, left, pos_right
else: else:
if is_pointwise: if is_pointwise:
pointwise_pyreader = fluid.layers.py_reader( left = fluid.data(
capacity=16, name='left', shape=[None], dtype='int64', lod_level=1)
shapes=([-1], [-1], [-1]), right = fluid.data(
dtypes=('int64', 'int64', 'int64'), name='right', shape=[None], dtype='int64', lod_level=1)
lod_levels=(1, 1, 0), label = fluid.data(name='label', shape=[None], dtype='int64')
name=pyreader_name, pointwise_loader = fluid.io.DataLoader.from_generator(
use_double_buffer=False) capacity=16,
feed_list=[left, right, label],
left, right, label = fluid.layers.read_file(pointwise_pyreader) iterable=False,
return pointwise_pyreader, left, right, label use_double_buffer=False)
return pointwise_loader, left, right, label
else: else:
pairwise_pyreader = fluid.layers.py_reader( left = fluid.data(
capacity=16, name='left', shape=[None], dtype='int64', lod_level=1)
shapes=([-1], [-1], [-1]), pos_right = fluid.data(
dtypes=('int64', 'int64', 'int64'), name='pos_right', shape=[None], dtype='int64', lod_level=1)
lod_levels=(1, 1, 1), neg_right = fluid.data(
name=pyreader_name, name='neg_right', shape=[None], dtype='int64', lod_level=1)
use_double_buffer=False) pairwise_loader = fluid.io.DataLoader.from_generator(
capacity=16,
feed_list=[left, pos_right, neg_right],
iterable=False,
use_double_buffer=False)
left, pos_right, neg_right = fluid.layers.read_file(pairwise_pyreader) return pairwise_loader, left, pos_right, neg_right
return pairwise_pyreader, left, pos_right, neg_right
def train(conf_dict, args): def train(conf_dict, args):
...@@ -131,8 +136,7 @@ def train(conf_dict, args): ...@@ -131,8 +136,7 @@ def train(conf_dict, args):
# Build network # Build network
with fluid.program_guard(train_program, startup_prog): with fluid.program_guard(train_program, startup_prog):
with fluid.unique_name.guard(): with fluid.unique_name.guard():
train_pyreader, left, pos_right, neg_right = create_model( train_loader, left, pos_right, neg_right = create_model(args)
args, pyreader_name='train_reader')
left_feat, pos_score = net.predict(left, pos_right) left_feat, pos_score = net.predict(left, pos_right)
pred = pos_score pred = pos_score
_, neg_score = net.predict(left, neg_right) _, neg_score = net.predict(left, neg_right)
...@@ -147,8 +151,8 @@ def train(conf_dict, args): ...@@ -147,8 +151,8 @@ def train(conf_dict, args):
test_prog = fluid.Program() test_prog = fluid.Program()
with fluid.program_guard(test_prog, startup_prog): with fluid.program_guard(test_prog, startup_prog):
with fluid.unique_name.guard(): with fluid.unique_name.guard():
test_pyreader, left, pos_right = create_model( test_loader, left, pos_right = create_model(
args, pyreader_name='test_reader', is_inference=True) args, is_inference=True)
left_feat, pos_score = net.predict(left, pos_right) left_feat, pos_score = net.predict(left, pos_right)
pred = pos_score pred = pos_score
test_prog = test_prog.clone(for_test=True) test_prog = test_prog.clone(for_test=True)
...@@ -157,8 +161,8 @@ def train(conf_dict, args): ...@@ -157,8 +161,8 @@ def train(conf_dict, args):
# Build network # Build network
with fluid.program_guard(train_program, startup_prog): with fluid.program_guard(train_program, startup_prog):
with fluid.unique_name.guard(): with fluid.unique_name.guard():
train_pyreader, left, right, label = create_model( train_loader, left, right, label = create_model(
args, pyreader_name='train_reader', is_pointwise=True) args, is_pointwise=True)
left_feat, pred = net.predict(left, right) left_feat, pred = net.predict(left, right)
avg_cost = loss.compute(pred, label) avg_cost = loss.compute(pred, label)
avg_cost.persistable = True avg_cost.persistable = True
...@@ -171,15 +175,15 @@ def train(conf_dict, args): ...@@ -171,15 +175,15 @@ def train(conf_dict, args):
test_prog = fluid.Program() test_prog = fluid.Program()
with fluid.program_guard(test_prog, startup_prog): with fluid.program_guard(test_prog, startup_prog):
with fluid.unique_name.guard(): with fluid.unique_name.guard():
test_pyreader, left, right = create_model( test_loader, left, right = create_model(
args, pyreader_name='test_reader', is_inference=True) args, is_inference=True)
left_feat, pred = net.predict(left, right) left_feat, pred = net.predict(left, right)
test_prog = test_prog.clone(for_test=True) test_prog = test_prog.clone(for_test=True)
if args.init_checkpoint is not "": if args.init_checkpoint is not "":
utils.init_checkpoint(exe, args.init_checkpoint, startup_prog) utils.init_checkpoint(exe, args.init_checkpoint, startup_prog)
def valid_and_test(test_program, test_pyreader, get_valid_examples, process, def valid_and_test(test_program, test_loader, get_valid_examples, process,
mode, exe, fetch_list): mode, exe, fetch_list):
""" """
return auc and acc return auc and acc
...@@ -187,15 +191,15 @@ def train(conf_dict, args): ...@@ -187,15 +191,15 @@ def train(conf_dict, args):
# Get Batch Data # Get Batch Data
batch_data = fluid.io.batch( batch_data = fluid.io.batch(
get_valid_examples, args.batch_size, drop_last=False) get_valid_examples, args.batch_size, drop_last=False)
test_pyreader.decorate_paddle_reader(batch_data) test_loader.set_sample_list_generator(batch_data)
test_pyreader.start() test_loader.start()
pred_list = [] pred_list = []
while True: while True:
try: try:
_pred = exe.run(program=test_program, fetch_list=[pred.name]) _pred = exe.run(program=test_program, fetch_list=[pred.name])
pred_list += list(_pred) pred_list += list(_pred)
except fluid.core.EOFException: except fluid.core.EOFException:
test_pyreader.reset() test_loader.reset()
break break
pred_list = np.vstack(pred_list) pred_list = np.vstack(pred_list)
if mode == "test": if mode == "test":
...@@ -233,8 +237,8 @@ def train(conf_dict, args): ...@@ -233,8 +237,8 @@ def train(conf_dict, args):
get_train_examples, buf_size=10000), get_train_examples, buf_size=10000),
args.batch_size, args.batch_size,
drop_last=False) drop_last=False)
train_pyreader.decorate_paddle_reader(train_batch_data) train_loader.set_sample_list_generator(train_batch_data)
train_pyreader.start() train_loader.start()
exe.run(startup_prog) exe.run(startup_prog)
losses = [] losses = []
start_time = time.time() start_time = time.time()
...@@ -248,8 +252,8 @@ def train(conf_dict, args): ...@@ -248,8 +252,8 @@ def train(conf_dict, args):
if args.do_valid and global_step % args.validation_steps == 0: if args.do_valid and global_step % args.validation_steps == 0:
get_valid_examples = simnet_process.get_reader("valid") get_valid_examples = simnet_process.get_reader("valid")
valid_result = valid_and_test( valid_result = valid_and_test(
test_prog, test_pyreader, get_valid_examples, test_prog, test_loader, get_valid_examples, simnet_process,
simnet_process, "valid", exe, [pred.name]) "valid", exe, [pred.name])
if args.compute_accuracy: if args.compute_accuracy:
valid_auc, valid_acc = valid_result valid_auc, valid_acc = valid_result
logging.info( logging.info(
...@@ -281,7 +285,7 @@ def train(conf_dict, args): ...@@ -281,7 +285,7 @@ def train(conf_dict, args):
logging.info("saving infer model in %s" % model_path) logging.info("saving infer model in %s" % model_path)
except fluid.core.EOFException: except fluid.core.EOFException:
train_pyreader.reset() train_loader.reset()
break break
end_time = time.time() end_time = time.time()
#logging.info("epoch: %d, loss: %f, used time: %d sec" % #logging.info("epoch: %d, loss: %f, used time: %d sec" %
...@@ -327,9 +331,8 @@ def train(conf_dict, args): ...@@ -327,9 +331,8 @@ def train(conf_dict, args):
else: else:
# Get Feeder and Reader # Get Feeder and Reader
get_test_examples = simnet_process.get_reader("test") get_test_examples = simnet_process.get_reader("test")
test_result = valid_and_test(test_prog, test_pyreader, test_result = valid_and_test(test_prog, test_loader, get_test_examples,
get_test_examples, simnet_process, "test", simnet_process, "test", exe, [pred.name])
exe, [pred.name])
if args.compute_accuracy: if args.compute_accuracy:
test_auc, test_acc = test_result test_auc, test_acc = test_result
logging.info("AUC of test is %f, Accuracy of test is %f" % logging.info("AUC of test is %f, Accuracy of test is %f" %
...@@ -371,8 +374,8 @@ def test(conf_dict, args): ...@@ -371,8 +374,8 @@ def test(conf_dict, args):
if args.task_mode == "pairwise": if args.task_mode == "pairwise":
with fluid.program_guard(test_prog, startup_prog): with fluid.program_guard(test_prog, startup_prog):
with fluid.unique_name.guard(): with fluid.unique_name.guard():
test_pyreader, left, pos_right = create_model( test_loader, left, pos_right = create_model(
args, pyreader_name='test_reader', is_inference=True) args, is_inference=True)
left_feat, pos_score = net.predict(left, pos_right) left_feat, pos_score = net.predict(left, pos_right)
pred = pos_score pred = pos_score
test_prog = test_prog.clone(for_test=True) test_prog = test_prog.clone(for_test=True)
...@@ -380,8 +383,8 @@ def test(conf_dict, args): ...@@ -380,8 +383,8 @@ def test(conf_dict, args):
else: else:
with fluid.program_guard(test_prog, startup_prog): with fluid.program_guard(test_prog, startup_prog):
with fluid.unique_name.guard(): with fluid.unique_name.guard():
test_pyreader, left, right = create_model( test_loader, left, right = create_model(
args, pyreader_name='test_reader', is_inference=True) args, is_inference=True)
left_feat, pred = net.predict(left, right) left_feat, pred = net.predict(left, right)
test_prog = test_prog.clone(for_test=True) test_prog = test_prog.clone(for_test=True)
...@@ -390,10 +393,10 @@ def test(conf_dict, args): ...@@ -390,10 +393,10 @@ def test(conf_dict, args):
utils.init_checkpoint(exe, args.init_checkpoint, main_program=test_prog) utils.init_checkpoint(exe, args.init_checkpoint, main_program=test_prog)
test_exe = exe test_exe = exe
test_pyreader.decorate_paddle_reader(batch_data) test_loader.set_sample_list_generator(batch_data)
logging.info("start test process ...") logging.info("start test process ...")
test_pyreader.start() test_loader.start()
pred_list = [] pred_list = []
fetch_list = [pred.name] fetch_list = [pred.name]
output = [] output = []
...@@ -412,7 +415,7 @@ def test(conf_dict, args): ...@@ -412,7 +415,7 @@ def test(conf_dict, args):
map(lambda item: str(np.argmax(item)), output[0])) + map(lambda item: str(np.argmax(item)), output[0])) +
"\n") "\n")
except fluid.core.EOFException: except fluid.core.EOFException:
test_pyreader.reset() test_loader.reset()
break break
if args.task_mode == "pairwise": if args.task_mode == "pairwise":
pred_list = np.array(pred_list).reshape((-1, 1)) pred_list = np.array(pred_list).reshape((-1, 1))
...@@ -468,16 +471,16 @@ def infer(conf_dict, args): ...@@ -468,16 +471,16 @@ def infer(conf_dict, args):
if args.task_mode == "pairwise": if args.task_mode == "pairwise":
with fluid.program_guard(test_prog, startup_prog): with fluid.program_guard(test_prog, startup_prog):
with fluid.unique_name.guard(): with fluid.unique_name.guard():
infer_pyreader, left, pos_right = create_model( infer_loader, left, pos_right = create_model(
args, pyreader_name='infer_reader', is_inference=True) args, is_inference=True)
left_feat, pos_score = net.predict(left, pos_right) left_feat, pos_score = net.predict(left, pos_right)
pred = pos_score pred = pos_score
test_prog = test_prog.clone(for_test=True) test_prog = test_prog.clone(for_test=True)
else: else:
with fluid.program_guard(test_prog, startup_prog): with fluid.program_guard(test_prog, startup_prog):
with fluid.unique_name.guard(): with fluid.unique_name.guard():
infer_pyreader, left, right = create_model( infer_loader, left, right = create_model(
args, pyreader_name='infer_reader', is_inference=True) args, is_inference=True)
left_feat, pred = net.predict(left, right) left_feat, pred = net.predict(left, right)
test_prog = test_prog.clone(for_test=True) test_prog = test_prog.clone(for_test=True)
...@@ -486,13 +489,13 @@ def infer(conf_dict, args): ...@@ -486,13 +489,13 @@ def infer(conf_dict, args):
utils.init_checkpoint(exe, args.init_checkpoint, main_program=test_prog) utils.init_checkpoint(exe, args.init_checkpoint, main_program=test_prog)
test_exe = exe test_exe = exe
infer_pyreader.decorate_sample_list_generator(batch_data) infer_loader.set_sample_list_generator(batch_data)
logging.info("start test process ...") logging.info("start test process ...")
preds_list = [] preds_list = []
fetch_list = [pred.name] fetch_list = [pred.name]
output = [] output = []
infer_pyreader.start() infer_loader.start()
while True: while True:
try: try:
output = test_exe.run(program=test_prog, fetch_list=fetch_list) output = test_exe.run(program=test_prog, fetch_list=fetch_list)
...@@ -502,7 +505,7 @@ def infer(conf_dict, args): ...@@ -502,7 +505,7 @@ def infer(conf_dict, args):
else: else:
preds_list += map(lambda item: str(np.argmax(item)), output[0]) preds_list += map(lambda item: str(np.argmax(item)), output[0])
except fluid.core.EOFException: except fluid.core.EOFException:
infer_pyreader.reset() infer_loader.reset()
break break
with io.open(args.infer_result_path, "w", encoding="utf8") as infer_file: with io.open(args.infer_result_path, "w", encoding="utf8") as infer_file:
for _data, _pred in zip(simnet_process.get_infer_data(), preds_list): for _data, _pred in zip(simnet_process.get_infer_data(), preds_list):
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
## 快速开始 ## 快速开始
#### 版本依赖 #### 版本依赖
本项目依赖于 Paddlepaddle Fluid 1.7,请参考[安装指南](http://www.paddlepaddle.org/#quick-start)进行安装。 本项目依赖于 Paddlepaddle Fluid 1.8,请参考[安装指南](https://www.paddlepaddle.org.cn/install/quick)进行安装。
#### 安装代码 #### 安装代码
......
...@@ -33,20 +33,21 @@ def check_cuda(use_cuda, err = \ ...@@ -33,20 +33,21 @@ def check_cuda(use_cuda, err = \
except Exception as e: except Exception as e:
pass pass
def check_version(): def check_version():
""" """
Log error and exit when the installed version of paddlepaddle is Log error and exit when the installed version of paddlepaddle is
not satisfied. not satisfied.
""" """
err = "PaddlePaddle version 1.6 or higher is required, " \ err = "PaddlePaddle version 1.6 or higher is required, " \
"or a suitable develop version is satisfied as well. \n" \ "or a suitable develop version is satisfied as well. \n" \
"Please make sure the version is good with your code." \ "Please make sure the version is good with your code." \
try: try:
fluid.require_version('1.6.0') fluid.require_version('1.8.0')
except Exception as e: except Exception as e:
print(err) print(err)
sys.exit(1) sys.exit(1)
def check_version(): def check_version():
...@@ -59,7 +60,7 @@ def check_version(): ...@@ -59,7 +60,7 @@ def check_version():
"Please make sure the version is good with your code." \ "Please make sure the version is good with your code." \
try: try:
fluid.require_version('1.6.0') fluid.require_version('1.8.0')
except Exception as e: except Exception as e:
print(err) print(err)
sys.exit(1) sys.exit(1)
......
...@@ -30,6 +30,7 @@ import paddle.fluid.layers.utils as utils ...@@ -30,6 +30,7 @@ import paddle.fluid.layers.utils as utils
from paddle.fluid.dygraph import Embedding, Conv2D, GRUUnit, Layer, to_variable from paddle.fluid.dygraph import Embedding, Conv2D, GRUUnit, Layer, to_variable
from paddle.fluid.layers.utils import map_structure, flatten, pack_sequence_as from paddle.fluid.layers.utils import map_structure, flatten, pack_sequence_as
class EmbeddingLayer(object): class EmbeddingLayer(object):
""" """
Embedding Layer class Embedding Layer class
...@@ -52,12 +53,12 @@ class EmbeddingLayer(object): ...@@ -52,12 +53,12 @@ class EmbeddingLayer(object):
size=[self.dict_size, self.emb_dim], size=[self.dict_size, self.emb_dim],
is_sparse=True, is_sparse=True,
padding_idx=self.padding_idx, padding_idx=self.padding_idx,
param_attr=attr.ParamAttr(name=self.name, initializer=fluid.initializer.Xavier())) param_attr=attr.ParamAttr(
name=self.name, initializer=fluid.initializer.Xavier()))
return emb return emb
class FCLayer(object): class FCLayer(object):
""" """
Fully Connect Layer class Fully Connect Layer class
...@@ -76,9 +77,9 @@ class FCLayer(object): ...@@ -76,9 +77,9 @@ class FCLayer(object):
operation operation
""" """
fc = FC(size=self.fc_dim, fc = FC(size=self.fc_dim,
param_attr=attr.ParamAttr(name="%s.w" % self.name), param_attr=attr.ParamAttr(name="%s.w" % self.name),
bias_attr=attr.ParamAttr(name="%s.b" % self.name), bias_attr=attr.ParamAttr(name="%s.b" % self.name),
act=self.act) act=self.act)
return fc return fc
...@@ -93,7 +94,7 @@ class DynamicGRULayer(object): ...@@ -93,7 +94,7 @@ class DynamicGRULayer(object):
""" """
self.gru_dim = gru_dim self.gru_dim = gru_dim
self.name = name self.name = name
def ops(self): def ops(self):
""" """
operation operation
...@@ -117,11 +118,13 @@ class DynamicLSTMLayer(object): ...@@ -117,11 +118,13 @@ class DynamicLSTMLayer(object):
self.lstm_dim = lstm_dim self.lstm_dim = lstm_dim
self.name = name self.name = name
self.is_reverse = is_reverse self.is_reverse = is_reverse
def ops(self): def ops(self):
""" """
operation operation
""" """
lstm_cell = BasicLSTMUnit(hidden_size=self.lstm_dim, input_size=self.lstm_dim*4) lstm_cell = BasicLSTMUnit(
hidden_size=self.lstm_dim, input_size=self.lstm_dim * 4)
lstm = RNN(cell=lstm_cell, time_major=True, is_reverse=self.is_reverse) lstm = RNN(cell=lstm_cell, time_major=True, is_reverse=self.is_reverse)
return lstm return lstm
...@@ -141,7 +144,7 @@ class DataLayer(object): ...@@ -141,7 +144,7 @@ class DataLayer(object):
""" """
operation operation
""" """
data = fluid.layers.data( data = fluid.data(
name=name, shape=shape, dtype=dtype, lod_level=lod_level) name=name, shape=shape, dtype=dtype, lod_level=lod_level)
return data return data
...@@ -314,8 +317,10 @@ class ConstantLayer(object): ...@@ -314,8 +317,10 @@ class ConstantLayer(object):
""" """
operation operation
""" """
constant = fluid.layers.fill_constant_batch_size_like(input, shape, shape = list(shape)
dtype, value) input_shape = fluid.layers.shape(input)
shape[0] = input_shape[0]
constant = fluid.layers.fill_constant(shape, dtype, value)
return constant return constant
...@@ -358,26 +363,23 @@ class SoftsignLayer(object): ...@@ -358,26 +363,23 @@ class SoftsignLayer(object):
class SimpleConvPool(Layer): class SimpleConvPool(Layer):
def __init__(self, def __init__(self, num_channels, num_filters, filter_size, use_cudnn=False):
num_channels,
num_filters,
filter_size,
use_cudnn=False
):
super(SimpleConvPool, self).__init__() super(SimpleConvPool, self).__init__()
self._conv2d = Conv2D(num_channels = num_channels, self._conv2d = Conv2D(
num_channels=num_channels,
num_filters=num_filters, num_filters=num_filters,
filter_size=filter_size, filter_size=filter_size,
padding=[1, 1], padding=[1, 1],
use_cudnn=use_cudnn, use_cudnn=use_cudnn,
act='relu') act='relu')
def forward(self, inputs): def forward(self, inputs):
x = self._conv2d(inputs) x = self._conv2d(inputs)
x = fluid.layers.reduce_max(x, dim=-1) x = fluid.layers.reduce_max(x, dim=-1)
x = fluid.layers.reshape(x, shape=[x.shape[0], -1]) x = fluid.layers.reshape(x, shape=[x.shape[0], -1])
return x return x
class FC(Layer): class FC(Layer):
""" """
This interface is used to construct a callable object of the ``FC`` class. This interface is used to construct a callable object of the ``FC`` class.
...@@ -580,7 +582,7 @@ class DynamicGRU(Layer): ...@@ -580,7 +582,7 @@ class DynamicGRU(Layer):
gate_activation='sigmoid', gate_activation='sigmoid',
candidate_activation='tanh', candidate_activation='tanh',
origin_mode=False, origin_mode=False,
init_size = None): init_size=None):
super(DynamicGRU, self).__init__() super(DynamicGRU, self).__init__()
self.gru_unit = GRUUnit( self.gru_unit = GRUUnit(
size * 3, size * 3,
...@@ -591,16 +593,19 @@ class DynamicGRU(Layer): ...@@ -591,16 +593,19 @@ class DynamicGRU(Layer):
origin_mode=origin_mode) origin_mode=origin_mode)
self.size = size self.size = size
self.is_reverse = is_reverse self.is_reverse = is_reverse
def forward(self, inputs, h_0): def forward(self, inputs, h_0):
hidden = h_0 hidden = h_0
res = [] res = []
for i in range(inputs.shape[1]): for i in range(inputs.shape[1]):
if self.is_reverse: if self.is_reverse:
i = inputs.shape[1] - 1 - i i = inputs.shape[1] - 1 - i
input_ = inputs[ :, i:i+1, :] input_ = inputs[:, i:i + 1, :]
input_ = fluid.layers.reshape(input_, [-1, input_.shape[2]], inplace=False) input_ = fluid.layers.reshape(
input_, [-1, input_.shape[2]], inplace=False)
hidden, reset, gate = self.gru_unit(input_, hidden) hidden, reset, gate = self.gru_unit(input_, hidden)
hidden_ = fluid.layers.reshape(hidden, [-1, 1, hidden.shape[1]], inplace=False) hidden_ = fluid.layers.reshape(
hidden, [-1, 1, hidden.shape[1]], inplace=False)
res.append(hidden_) res.append(hidden_)
if self.is_reverse: if self.is_reverse:
res = res[::-1] res = res[::-1]
...@@ -786,18 +791,21 @@ class BasicLSTMUnit(RNNUnit): ...@@ -786,18 +791,21 @@ class BasicLSTMUnit(RNNUnit):
self._weight = self.create_parameter( self._weight = self.create_parameter(
attr=self._param_attr, attr=self._param_attr,
shape=[self._input_size + self._hidden_size, 4 * self._hidden_size], shape=[
self._input_size + self._hidden_size, 4 * self._hidden_size
],
dtype=self._dtype) dtype=self._dtype)
self._bias = self.create_parameter(attr=self._bias_attr, self._bias = self.create_parameter(
shape=[4 * self._hidden_size], attr=self._bias_attr,
dtype=self._dtype, shape=[4 * self._hidden_size],
is_bias=True) dtype=self._dtype,
is_bias=True)
def forward(self, input, state): def forward(self, input, state):
pre_hidden, pre_cell = state pre_hidden, pre_cell = state
concat_input_hidden = layers.concat([input, pre_hidden], axis=1) concat_input_hidden = layers.concat([input, pre_hidden], axis=1)
gate_input = layers.matmul(x=concat_input_hidden, y=self._weight) gate_input = layers.matmul(x=concat_input_hidden, y=self._weight)
gate_input = layers.elementwise_add(gate_input, self._bias) gate_input = layers.elementwise_add(gate_input, self._bias)
...@@ -817,11 +825,7 @@ class BasicLSTMUnit(RNNUnit): ...@@ -817,11 +825,7 @@ class BasicLSTMUnit(RNNUnit):
class RNN(Layer): class RNN(Layer):
def __init__(self, def __init__(self, cell, is_reverse=False, time_major=False, **kwargs):
cell,
is_reverse=False,
time_major=False,
**kwargs):
super(RNN, self).__init__() super(RNN, self).__init__()
self.cell = cell self.cell = cell
if not hasattr(self.cell, "call"): if not hasattr(self.cell, "call"):
...@@ -831,12 +835,17 @@ class RNN(Layer): ...@@ -831,12 +835,17 @@ class RNN(Layer):
self.batch_index, self.time_step_index = (1, 0) if time_major else (0, self.batch_index, self.time_step_index = (1, 0) if time_major else (0,
1) 1)
def forward(self, inputs, initial_states=None, sequence_length=None, **kwargs): def forward(self,
inputs,
initial_states=None,
sequence_length=None,
**kwargs):
if fluid.in_dygraph_mode(): if fluid.in_dygraph_mode():
class OutputArray(object): class OutputArray(object):
def __init__(self, x): def __init__(self, x):
self.array = [x] self.array = [x]
def append(self, x): def append(self, x):
self.array.append(x) self.array.append(x)
...@@ -844,9 +853,8 @@ class RNN(Layer): ...@@ -844,9 +853,8 @@ class RNN(Layer):
# TODO: use where_op # TODO: use where_op
new_state = fluid.layers.elementwise_mul( new_state = fluid.layers.elementwise_mul(
new_state, step_mask, new_state, step_mask,
axis=0) - fluid.layers.elementwise_mul(state, axis=0) - fluid.layers.elementwise_mul(
(step_mask - 1), state, (step_mask - 1), axis=0)
axis=0)
return new_state return new_state
flat_inputs = flatten(inputs) flat_inputs = flatten(inputs)
...@@ -872,16 +880,20 @@ class RNN(Layer): ...@@ -872,16 +880,20 @@ class RNN(Layer):
if self.is_reverse: if self.is_reverse:
inputs = map_structure(lambda x: fluid.layers.reverse(x, axis=[0]), inputs) inputs = map_structure(lambda x: fluid.layers.reverse(x, axis=[0]), inputs)
mask = fluid.layers.reverse(mask, axis=[0]) if sequence_length is not None else None mask = fluid.layers.reverse(
mask, axis=[0]) if sequence_length is not None else None
states = initial_states states = initial_states
outputs = [] outputs = []
for i in range(time_steps): for i in range(time_steps):
step_inputs = map_structure(lambda x:x[i], inputs) step_inputs = map_structure(lambda x: x[i], inputs)
step_outputs, new_states = self.cell(step_inputs, states, **kwargs) step_outputs, new_states = self.cell(step_inputs, states,
**kwargs)
if sequence_length is not None: if sequence_length is not None:
new_states = map_structure( new_states = map_structure(
partial(_maybe_copy, step_mask=mask[i]), states, partial(
_maybe_copy, step_mask=mask[i]),
states,
new_states) new_states)
states = new_states states = new_states
if i == 0: if i == 0:
...@@ -922,10 +934,9 @@ class EncoderCell(RNNUnit): ...@@ -922,10 +934,9 @@ class EncoderCell(RNNUnit):
self.lstm_cells = list() self.lstm_cells = list()
for i in range(self.num_layers): for i in range(self.num_layers):
self.lstm_cells.append( self.lstm_cells.append(
self.add_sublayer( self.add_sublayer("layer_%d" % i,
"layer_%d" % i, BasicLSTMUnit(input_size if i == 0 else
BasicLSTMUnit(input_size if i == 0 else hidden_size, hidden_size, hidden_size)))
hidden_size)))
def forward(self, step_input, states): def forward(self, step_input, states):
new_states = [] new_states = []
...@@ -1040,4 +1051,3 @@ class BasicGRUUnit(Layer): ...@@ -1040,4 +1051,3 @@ class BasicGRUUnit(Layer):
new_hidden = u * pre_hidden + (1 - u) * c new_hidden = u * pre_hidden + (1 - u) * c
return new_hidden return new_hidden
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
1. paddle安装 1. paddle安装
本项目依赖于 PaddlePaddle 1.7及以上版本或适当的develop版本,请参考 [安装指南](http://www.paddlepaddle.org/#quick-start) 进行安装 本项目依赖于 PaddlePaddle 1.8及以上版本或适当的develop版本,请参考 [安装指南](https://www.paddlepaddle.org.cn/install/quick) 进行安装
2. 下载代码 2. 下载代码
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
3. 环境依赖 3. 环境依赖
请参考PaddlePaddle[安装说明](https://www.paddlepaddle.org.cn/documentation/docs/zh/1.6/beginners_guide/install/index_cn.html)部分的内容 请参考PaddlePaddle[安装说明](https://www.paddlepaddle.org.cn/documentation/docs/zh/install/index_cn.html)部分的内容
### 数据准备 ### 数据准备
......
...@@ -42,12 +42,11 @@ class InferTaskConfig(object): ...@@ -42,12 +42,11 @@ class InferTaskConfig(object):
batch_size = 4 batch_size = 4
# the parameters for beam search. # the parameters for beam search.
beam_size = 4 beam_size = 4
alpha=0.6 alpha = 0.6
# max decoded length, should be less than ModelHyperParams.max_length # max decoded length, should be less than ModelHyperParams.max_length
max_out_len = 30 max_out_len = 30
class ModelHyperParams(object): class ModelHyperParams(object):
""" """
ModelHyperParams ModelHyperParams
...@@ -156,38 +155,32 @@ input_descs = { ...@@ -156,38 +155,32 @@ input_descs = {
# Names of word embedding table which might be reused for weight sharing. # Names of word embedding table which might be reused for weight sharing.
word_emb_param_names = ( word_emb_param_names = (
"src_word_emb_table", "src_word_emb_table",
"trg_word_emb_table", "trg_word_emb_table", )
)
# Names of position encoding table which will be initialized externally. # Names of position encoding table which will be initialized externally.
pos_enc_param_names = ( pos_enc_param_names = (
"src_pos_enc_table", "src_pos_enc_table",
"trg_pos_enc_table", "trg_pos_enc_table", )
)
# separated inputs for different usages. # separated inputs for different usages.
encoder_data_input_fields = ( encoder_data_input_fields = (
"src_word", "src_word",
"src_pos", "src_pos",
"src_slf_attn_bias", "src_slf_attn_bias", )
)
decoder_data_input_fields = ( decoder_data_input_fields = (
"trg_word", "trg_word",
"trg_pos", "trg_pos",
"trg_slf_attn_bias", "trg_slf_attn_bias",
"trg_src_attn_bias", "trg_src_attn_bias",
"enc_output", "enc_output", )
)
label_data_input_fields = ( label_data_input_fields = (
"lbl_word", "lbl_word",
"lbl_weight", "lbl_weight", )
)
# In fast decoder, trg_pos (only containing the current time step) is generated # In fast decoder, trg_pos (only containing the current time step) is generated
# by ops and trg_slf_attn_bias is not needed. # by ops and trg_slf_attn_bias is not needed.
fast_decoder_data_input_fields = ( fast_decoder_data_input_fields = (
"trg_word", "trg_word",
# "init_score", # "init_score",
# "init_idx", # "init_idx",
"trg_src_attn_bias", "trg_src_attn_bias", )
)
def merge_cfg_from_list(cfg_list, g_cfgs): def merge_cfg_from_list(cfg_list, g_cfgs):
......
此差异已折叠。
...@@ -306,6 +306,7 @@ class DataProcessor(object): ...@@ -306,6 +306,7 @@ class DataProcessor(object):
:param seed: The seed for random. :param seed: The seed for random.
:type seed: int :type seed: int
""" """
def __init__(self, def __init__(self,
src_vocab_fpath, src_vocab_fpath,
trg_vocab_fpath, trg_vocab_fpath,
...@@ -360,21 +361,23 @@ class DataProcessor(object): ...@@ -360,21 +361,23 @@ class DataProcessor(object):
def load_src_trg_ids(self, fpattern, tar_fname): def load_src_trg_ids(self, fpattern, tar_fname):
converters = [ converters = [
Converter(vocab=self._src_vocab, Converter(
beg=self._bos_idx, vocab=self._src_vocab,
end=self._eos_idx, beg=self._bos_idx,
unk=self._unk_idx, end=self._eos_idx,
delimiter=self._token_delimiter, unk=self._unk_idx,
add_beg=False) delimiter=self._token_delimiter,
add_beg=False)
] ]
if not self._only_src: if not self._only_src:
converters.append( converters.append(
Converter(vocab=self._trg_vocab, Converter(
beg=self._bos_idx, vocab=self._trg_vocab,
end=self._eos_idx, beg=self._bos_idx,
unk=self._unk_idx, end=self._eos_idx,
delimiter=self._token_delimiter, unk=self._unk_idx,
add_beg=True)) delimiter=self._token_delimiter,
add_beg=True))
converters = ComposedConverter(converters) converters = ComposedConverter(converters)
...@@ -402,9 +405,8 @@ class DataProcessor(object): ...@@ -402,9 +405,8 @@ class DataProcessor(object):
f = tarfile.open(fpaths[0], "rb") f = tarfile.open(fpaths[0], "rb")
for line in f.extractfile(tar_fname): for line in f.extractfile(tar_fname):
fields = line.strip(b"\n").split(self._field_delimiter) fields = line.strip(b"\n").split(self._field_delimiter)
if (not self._only_src if (not self._only_src and len(fields) == 2) or (
and len(fields) == 2) or (self._only_src self._only_src and len(fields) == 1):
and len(fields) == 1):
yield fields yield fields
else: else:
for fpath in fpaths: for fpath in fpaths:
...@@ -414,9 +416,8 @@ class DataProcessor(object): ...@@ -414,9 +416,8 @@ class DataProcessor(object):
with open(fpath, "rb") as f: with open(fpath, "rb") as f:
for line in f: for line in f:
fields = line.strip(b"\n").split(self._field_delimiter) fields = line.strip(b"\n").split(self._field_delimiter)
if (not self._only_src if (not self._only_src and len(fields) == 2) or (
and len(fields) == 2) or (self._only_src self._only_src and len(fields) == 1):
and len(fields) == 1):
yield fields yield fields
@staticmethod @staticmethod
...@@ -512,8 +513,8 @@ class DataProcessor(object): ...@@ -512,8 +513,8 @@ class DataProcessor(object):
for item in data_reader(): for item in data_reader():
inst_num_per_part = len(item) // count inst_num_per_part = len(item) // count
for i in range(count): for i in range(count):
yield item[inst_num_per_part * i:inst_num_per_part * yield item[inst_num_per_part * i:inst_num_per_part * (i + 1
(i + 1)] )]
return __impl__ return __impl__
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册