Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
d0937f0b
M
models
项目概览
PaddlePaddle
/
models
大约 1 年 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
d0937f0b
编写于
2月 22, 2020
作者:
G
Guo Sheng
提交者:
GitHub
2月 22, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make dygraph Transformer support to load py2 saved models. (#4329)
上级
e15f205a
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
30 addition
and
3 deletion
+30
-3
dygraph/transformer/predict.py
dygraph/transformer/predict.py
+2
-1
dygraph/transformer/train.py
dygraph/transformer/train.py
+3
-2
dygraph/transformer/utils/load.py
dygraph/transformer/utils/load.py
+25
-0
未找到文件。
dygraph/transformer/predict.py
浏览文件 @
d0937f0b
...
@@ -24,6 +24,7 @@ import paddle.fluid as fluid
...
@@ -24,6 +24,7 @@ import paddle.fluid as fluid
from
utils.configure
import
PDConfig
from
utils.configure
import
PDConfig
from
utils.check
import
check_gpu
,
check_version
from
utils.check
import
check_gpu
,
check_version
from
utils.load
import
load_dygraph
# include task-specific libs
# include task-specific libs
import
reader
import
reader
...
@@ -95,7 +96,7 @@ def do_predict(args):
...
@@ -95,7 +96,7 @@ def do_predict(args):
# load the trained model
# load the trained model
assert
args
.
init_from_params
,
(
assert
args
.
init_from_params
,
(
"Please set init_from_params to load the infer model."
)
"Please set init_from_params to load the infer model."
)
model_dict
,
_
=
fluid
.
load_dygraph
(
model_dict
,
_
=
load_dygraph
(
os
.
path
.
join
(
args
.
init_from_params
,
"transformer"
))
os
.
path
.
join
(
args
.
init_from_params
,
"transformer"
))
# to avoid a longer length than training, reset the size of position
# to avoid a longer length than training, reset the size of position
# encoding to max_length
# encoding to max_length
...
...
dygraph/transformer/train.py
浏览文件 @
d0937f0b
...
@@ -24,6 +24,7 @@ import paddle.fluid as fluid
...
@@ -24,6 +24,7 @@ import paddle.fluid as fluid
from
utils.configure
import
PDConfig
from
utils.configure
import
PDConfig
from
utils.check
import
check_gpu
,
check_version
from
utils.check
import
check_gpu
,
check_version
from
utils.load
import
load_dygraph
# include task-specific libs
# include task-specific libs
import
reader
import
reader
...
@@ -97,13 +98,13 @@ def do_train(args):
...
@@ -97,13 +98,13 @@ def do_train(args):
## init from some checkpoint, to resume the previous training
## init from some checkpoint, to resume the previous training
if
args
.
init_from_checkpoint
:
if
args
.
init_from_checkpoint
:
model_dict
,
opt_dict
=
fluid
.
load_dygraph
(
model_dict
,
opt_dict
=
load_dygraph
(
os
.
path
.
join
(
args
.
init_from_checkpoint
,
"transformer"
))
os
.
path
.
join
(
args
.
init_from_checkpoint
,
"transformer"
))
transformer
.
load_dict
(
model_dict
)
transformer
.
load_dict
(
model_dict
)
optimizer
.
set_dict
(
opt_dict
)
optimizer
.
set_dict
(
opt_dict
)
## init from some pretrain models, to better solve the current task
## init from some pretrain models, to better solve the current task
if
args
.
init_from_pretrain_model
:
if
args
.
init_from_pretrain_model
:
model_dict
,
_
=
fluid
.
load_dygraph
(
model_dict
,
_
=
load_dygraph
(
os
.
path
.
join
(
args
.
init_from_pretrain_model
,
"transformer"
))
os
.
path
.
join
(
args
.
init_from_pretrain_model
,
"transformer"
))
transformer
.
load_dict
(
model_dict
)
transformer
.
load_dict
(
model_dict
)
...
...
dygraph/transformer/utils/load.py
0 → 100644
浏览文件 @
d0937f0b
import
pickle
import
six
import
warnings
from
functools
import
partial
import
paddle.fluid
as
fluid
def
load_dygraph
(
model_path
,
keep_name_table
=
False
):
"""
To load python2 saved models in python3.
"""
try
:
para_dict
,
opti_dict
=
fluid
.
load_dygraph
(
model_path
,
keep_name_table
)
return
para_dict
,
opti_dict
except
UnicodeDecodeError
:
warnings
.
warn
(
"An UnicodeDecodeError is catched, which might be caused by loading "
"a python2 saved model. Encoding of pickle.load would be set and "
"load again automatically."
)
if
six
.
PY3
:
load_bak
=
pickle
.
load
pickle
.
load
=
partial
(
load_bak
,
encoding
=
"latin1"
)
para_dict
,
opti_dict
=
fluid
.
load_dygraph
(
model_path
,
keep_name_table
)
pickle
.
load
=
load_bak
return
para_dict
,
opti_dict
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录