未验证 提交 91d03798 编写于 作者: K kinghuin 提交者: GitHub

fix ernie_gen bug (#858)

上级 0aad25c0
......@@ -18,8 +18,8 @@ def finetune(
use_gpu=True,
max_steps=500,
batch_size=8,
max_encode_len=15,
max_decode_len=15,
max_encode_len=50,
max_decode_len=50,
learning_rate=5e-5,
warmup_proportion=0.1,
weight_decay=0.1,
......@@ -68,6 +68,8 @@ def export(
params_path,
module_name,
author,
max_encode_len=50,
max_decode_len=50,
version="1.0.0",
summary="",
author_email="",
......@@ -81,6 +83,8 @@ module导出API,通过此API可以一键将训练参数打包为hub module。
* params_path(str): 模型参数路径。
* module_name(str): module名称,例如"ernie_gen_couplet"。
* author(str): 作者名称。
* max_encode_len(int): 最大编码长度。
* max_decode_len(int): 最大解码长度。
* version(str): 版本号。
* summary(str): module的英文简介。
* author_email(str): 作者的邮箱地址。
......@@ -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. 命令行预测
......@@ -126,6 +130,36 @@ for result in results:
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。
您也可以将$module_name文件夹打包为tar.gz压缩包并联系PaddleHub工作人员上传至PaddleHub模型仓库,这样更多的用户可以通过一键安装的方式使用您的模型。PaddleHub非常欢迎您的贡献,共同推动开源社区成长。
......@@ -146,3 +180,7 @@ paddlehub >= 1.7.0
* 1.0.0
初始发布
* 1.0.1
修复模型导出bug
......@@ -39,7 +39,7 @@ import ernie_gen.propeller.paddle as propeller
@moduleinfo(
name="ernie_gen",
version="1.0.0",
version="1.0.1",
summary=
"ERNIE-GEN is a multi-flow language generation framework for both pre-training and fine-tuning.",
author="baidu",
......@@ -72,8 +72,8 @@ class ErnieGen(hub.Module):
use_gpu=True,
max_steps=500,
batch_size=8,
max_encode_len=15,
max_decode_len=15,
max_encode_len=50,
max_decode_len=50,
learning_rate=5e-5,
warmup_proportion=0.1,
weight_decay=0.1,
......@@ -277,6 +277,8 @@ class ErnieGen(hub.Module):
params_path,
module_name,
author,
max_encode_len=50,
max_decode_len=50,
version="1.0.0",
summary="",
author_email="",
......@@ -288,6 +290,8 @@ class ErnieGen(hub.Module):
params_path(str): the model params save path.
module_name(str): the module 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.
summary(str): the module brief introduction.
author_email(str): the author email address.
......@@ -322,10 +326,13 @@ class ErnieGen(hub.Module):
module_temp_path, encoding="utf8") as ftemp, open(
module_path, "w") as fmodule:
content = ftemp.read().replace(
r"{module_name}", module_name).replace(
r"{author}", author).replace(r"{version}", version).replace(
r"{summary}", summary).replace(r"{author_email}",
author_email)
r"{module_name}",
module_name).replace(r"{author}", author).replace(
r"{version}",
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)
logger.info("The module has exported to %s" %
......
......@@ -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,
length_penalty):
"""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
onehot_eos = L.cast(
......
......@@ -116,8 +116,8 @@ class ErnieGen(hub.NLPPredictionModule):
eos_id=self.tokenizer.sep_id,
sos_id=self.tokenizer.cls_id,
attn_id=self.tokenizer.vocab['[MASK]'],
max_decode_len=20,
max_encode_len=20,
max_decode_len={max_decode_len},
max_encode_len={max_encode_len},
beam_width=beam_width,
tgt_type_id=1)
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.
先完成此消息的编辑!
想要评论请 注册