“2eae3616006ef1e1ff440211c5bb8c4399089318”上不存在“doc/design/releasing_process.html”
未验证 提交 91d03798 编写于 作者: K kinghuin 提交者: GitHub

fix ernie_gen bug (#858)

上级 0aad25c0
...@@ -18,8 +18,8 @@ def finetune( ...@@ -18,8 +18,8 @@ def finetune(
use_gpu=True, use_gpu=True,
max_steps=500, max_steps=500,
batch_size=8, batch_size=8,
max_encode_len=15, max_encode_len=50,
max_decode_len=15, max_decode_len=50,
learning_rate=5e-5, learning_rate=5e-5,
warmup_proportion=0.1, warmup_proportion=0.1,
weight_decay=0.1, weight_decay=0.1,
...@@ -68,6 +68,8 @@ def export( ...@@ -68,6 +68,8 @@ def export(
params_path, params_path,
module_name, module_name,
author, author,
max_encode_len=50,
max_decode_len=50,
version="1.0.0", version="1.0.0",
summary="", summary="",
author_email="", author_email="",
...@@ -81,6 +83,8 @@ module导出API,通过此API可以一键将训练参数打包为hub module。 ...@@ -81,6 +83,8 @@ module导出API,通过此API可以一键将训练参数打包为hub module。
* params_path(str): 模型参数路径。 * params_path(str): 模型参数路径。
* module_name(str): module名称,例如"ernie_gen_couplet"。 * module_name(str): module名称,例如"ernie_gen_couplet"。
* author(str): 作者名称。 * author(str): 作者名称。
* max_encode_len(int): 最大编码长度。
* max_decode_len(int): 最大解码长度。
* version(str): 版本号。 * version(str): 版本号。
* summary(str): module的英文简介。 * summary(str): module的英文简介。
* author_email(str): 作者的邮箱地址。 * author_email(str): 作者的邮箱地址。
...@@ -105,7 +109,7 @@ module.export(params_path=result['last_save_path'], module_name="ernie_gen_test" ...@@ -105,7 +109,7 @@ module.export(params_path=result['last_save_path'], module_name="ernie_gen_test"
## 使用方式 ## 使用方式
模型转换完毕之后,通过`hub install $module_name`安装该模型,即可通过以下2种方式调用自制module: 模型转换完毕之后,通过`hub install $module_name`安装该模型,即可通过以下种方式调用自制module:
1. 命令行预测 1. 命令行预测
...@@ -126,6 +130,36 @@ for result in results: ...@@ -126,6 +130,36 @@ for result in results:
print(result) print(result)
``` ```
3. 服务端部署
服务端启动模型服务:
```shell
$ hub serving start -m $module_name -p 8866
```
**NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA\_VISIBLE\_DEVICES环境变量,否则不用设置。
客户端通过以下数行代码即可实现发送预测请求,获取预测结果
```python
import requests
import json
# 发送HTTP请求
data = {'texts':["输入文本1", "输入文本2"],
'use_gpu':True, 'beam_width':5}
headers = {"Content-type": "application/json"}
url = "http://127.0.0.1:8866/predict/$module_name"
r = requests.post(url=url, headers=headers, data=json.dumps(data))
# 保存结果
results = r.json()["results"]
for result in results:
print(result)
```
**NOTE**: 上述`$module_name`为export指定的module_name。 **NOTE**: 上述`$module_name`为export指定的module_name。
您也可以将$module_name文件夹打包为tar.gz压缩包并联系PaddleHub工作人员上传至PaddleHub模型仓库,这样更多的用户可以通过一键安装的方式使用您的模型。PaddleHub非常欢迎您的贡献,共同推动开源社区成长。 您也可以将$module_name文件夹打包为tar.gz压缩包并联系PaddleHub工作人员上传至PaddleHub模型仓库,这样更多的用户可以通过一键安装的方式使用您的模型。PaddleHub非常欢迎您的贡献,共同推动开源社区成长。
...@@ -146,3 +180,7 @@ paddlehub >= 1.7.0 ...@@ -146,3 +180,7 @@ paddlehub >= 1.7.0
* 1.0.0 * 1.0.0
初始发布 初始发布
* 1.0.1
修复模型导出bug
...@@ -39,7 +39,7 @@ import ernie_gen.propeller.paddle as propeller ...@@ -39,7 +39,7 @@ import ernie_gen.propeller.paddle as propeller
@moduleinfo( @moduleinfo(
name="ernie_gen", name="ernie_gen",
version="1.0.0", version="1.0.1",
summary= summary=
"ERNIE-GEN is a multi-flow language generation framework for both pre-training and fine-tuning.", "ERNIE-GEN is a multi-flow language generation framework for both pre-training and fine-tuning.",
author="baidu", author="baidu",
...@@ -72,8 +72,8 @@ class ErnieGen(hub.Module): ...@@ -72,8 +72,8 @@ class ErnieGen(hub.Module):
use_gpu=True, use_gpu=True,
max_steps=500, max_steps=500,
batch_size=8, batch_size=8,
max_encode_len=15, max_encode_len=50,
max_decode_len=15, max_decode_len=50,
learning_rate=5e-5, learning_rate=5e-5,
warmup_proportion=0.1, warmup_proportion=0.1,
weight_decay=0.1, weight_decay=0.1,
...@@ -277,6 +277,8 @@ class ErnieGen(hub.Module): ...@@ -277,6 +277,8 @@ class ErnieGen(hub.Module):
params_path, params_path,
module_name, module_name,
author, author,
max_encode_len=50,
max_decode_len=50,
version="1.0.0", version="1.0.0",
summary="", summary="",
author_email="", author_email="",
...@@ -288,6 +290,8 @@ class ErnieGen(hub.Module): ...@@ -288,6 +290,8 @@ class ErnieGen(hub.Module):
params_path(str): the model params save path. params_path(str): the model params save path.
module_name(str): the module name. module_name(str): the module name.
author(str): the author name. author(str): the author name.
max_encode_len(int): the max encode length.
max_decode_len(int): the max decode length.
version(str): the version information. version(str): the version information.
summary(str): the module brief introduction. summary(str): the module brief introduction.
author_email(str): the author email address. author_email(str): the author email address.
...@@ -322,10 +326,13 @@ class ErnieGen(hub.Module): ...@@ -322,10 +326,13 @@ class ErnieGen(hub.Module):
module_temp_path, encoding="utf8") as ftemp, open( module_temp_path, encoding="utf8") as ftemp, open(
module_path, "w") as fmodule: module_path, "w") as fmodule:
content = ftemp.read().replace( content = ftemp.read().replace(
r"{module_name}", module_name).replace( r"{module_name}",
r"{author}", author).replace(r"{version}", version).replace( module_name).replace(r"{author}", author).replace(
r"{summary}", summary).replace(r"{author_email}", r"{version}",
author_email) version).replace(r"{summary}", summary).replace(
r"{author_email}", author_email).replace(
r"{max_encode_len}", str(max_encode_len)).replace(
r"{max_decode_len}", str(max_decode_len))
fmodule.write(content) fmodule.write(content)
logger.info("The module has exported to %s" % logger.info("The module has exported to %s" %
......
...@@ -133,7 +133,11 @@ def hyp_score(log_probs, length, length_penalty): ...@@ -133,7 +133,11 @@ def hyp_score(log_probs, length, length_penalty):
def beam_search_step(state, logits, eos_id, beam_width, is_first_step, def beam_search_step(state, logits, eos_id, beam_width, is_first_step,
length_penalty): length_penalty):
"""logits.shape == [B*W, V]""" """logits.shape == [B*W, V]"""
_, vocab_size = logits.shape beam_size, vocab_size = logits.shape # as batch size=1 in this hub module. the first dim means bsz * beam_size equals beam_size
logits_np = logits.numpy()
for i in range(beam_size):
logits_np[i][17963] = 0 # make [UNK] prob = 0
logits = D.to_variable(logits_np)
bsz, beam_width = state.log_probs.shape bsz, beam_width = state.log_probs.shape
onehot_eos = L.cast( onehot_eos = L.cast(
......
...@@ -116,8 +116,8 @@ class ErnieGen(hub.NLPPredictionModule): ...@@ -116,8 +116,8 @@ class ErnieGen(hub.NLPPredictionModule):
eos_id=self.tokenizer.sep_id, eos_id=self.tokenizer.sep_id,
sos_id=self.tokenizer.cls_id, sos_id=self.tokenizer.cls_id,
attn_id=self.tokenizer.vocab['[MASK]'], attn_id=self.tokenizer.vocab['[MASK]'],
max_decode_len=20, max_decode_len={max_decode_len},
max_encode_len=20, max_encode_len={max_encode_len},
beam_width=beam_width, beam_width=beam_width,
tgt_type_id=1) tgt_type_id=1)
output_str = self.rev_lookup(output_ids[0].numpy()) output_str = self.rev_lookup(output_ids[0].numpy())
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册