Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
be219ac0
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
be219ac0
编写于
5月 02, 2018
作者:
H
Helin Wang
提交者:
GitHub
5月 02, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #10354 from helinwang/scaffold
Improve trainer API
上级
73650a83
0fca8a14
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
37 addition
and
81 deletion
+37
-81
python/paddle/fluid/__init__.py
python/paddle/fluid/__init__.py
+6
-5
python/paddle/fluid/inferencer.py
python/paddle/fluid/inferencer.py
+6
-2
python/paddle/fluid/params.py
python/paddle/fluid/params.py
+0
-39
python/paddle/fluid/tests/book/word2vec/no_test_word2vec_new_api.py
...dle/fluid/tests/book/word2vec/no_test_word2vec_new_api.py
+11
-9
python/paddle/fluid/trainer.py
python/paddle/fluid/trainer.py
+14
-26
未找到文件。
python/paddle/fluid/__init__.py
浏览文件 @
be219ac0
...
...
@@ -21,14 +21,15 @@ import executor
from
executor
import
*
import
trainer
from
trainer
import
*
from
trainer
import
Trainer
from
trainer
import
BeginEpochEvent
from
trainer
import
EndEpochEvent
from
trainer
import
BeginStepEvent
from
trainer
import
EndStepEvent
import
inferencer
from
inferencer
import
Inferencer
import
params
from
params
import
Params
import
io
import
evaluator
import
initializer
...
...
@@ -57,7 +58,7 @@ from parallel_executor import ParallelExecutor
Tensor
=
LoDTensor
__all__
=
framework
.
__all__
+
executor
.
__all__
+
concurrency
.
__all__
+
\
trainer
.
__all__
+
inferencer
.
__all__
+
params
.
__all__
+
[
trainer
.
__all__
+
inferencer
.
__all__
+
[
'io'
,
'initializer'
,
'layers'
,
...
...
python/paddle/fluid/inferencer.py
浏览文件 @
be219ac0
...
...
@@ -12,18 +12,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import
core
__all__
=
[
'Inferencer'
,
]
class
Inferencer
(
object
):
def
__init__
(
self
,
network_func
,
param
s
,
place
=
None
):
def
__init__
(
self
,
network_func
,
param
_path
=
None
,
place
=
None
):
# 1. we need to generate a framework.Program by calling
# network_func. Reference: fluid.program_guard in test_word2vec.py
# 2. move the default_main_program to self.program.
# 3. run the default_startup program.
self
.
params
=
params
# 4. load params from param_path into scope
self
.
scope
=
core
.
Scope
()
self
.
place
=
place
def
infer
(
self
,
inputs
):
...
...
python/paddle/fluid/params.py
已删除
100644 → 0
浏览文件 @
73650a83
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
.
import
core
__all__
=
[
'Params'
,
]
class
Params
(
object
):
def
__init__
(
self
,
path
=
None
):
self
.
scope
=
core
.
Scope
()
if
path
:
self
.
_load
(
path
)
def
_load
(
self
,
path
):
# reference: load_persistables in io.py
pass
def
save
(
self
,
path
):
# reference: save_persistables in io.py
pass
def
add_params
(
self
,
scope
):
# take the keys from the scope,
# if not already exists in self.scope,
# add the key and value into self.scope.
pass
python/paddle/fluid/tests/book/word2vec/no_test_word2vec_new_api.py
浏览文件 @
be219ac0
...
...
@@ -39,7 +39,7 @@ word_dict = paddle.dataset.imikolov.build_dict()
dict_size
=
len
(
word_dict
)
def
inference_
network
(
is_sparse
):
def
inference_
program
(
is_sparse
):
first_word
=
fluid
.
layers
.
data
(
name
=
'firstw'
,
shape
=
[
1
],
dtype
=
'int64'
)
second_word
=
fluid
.
layers
.
data
(
name
=
'secondw'
,
shape
=
[
1
],
dtype
=
'int64'
)
third_word
=
fluid
.
layers
.
data
(
name
=
'thirdw'
,
shape
=
[
1
],
dtype
=
'int64'
)
...
...
@@ -79,9 +79,9 @@ def inference_network(is_sparse):
return
predict_word
def
train_
network
(
is_sparse
):
def
train_
program
(
is_sparse
):
next_word
=
fluid
.
layers
.
data
(
name
=
'nextw'
,
shape
=
[
1
],
dtype
=
'int64'
)
predict_word
=
inference_
network
(
is_sparse
)
predict_word
=
inference_
program
(
is_sparse
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
predict_word
,
label
=
next_word
)
avg_cost
=
fluid
.
layers
.
mean
(
cost
)
return
avg_cost
...
...
@@ -100,23 +100,25 @@ def train(use_cuda, is_sparse, save_path):
word_dict
,
N
))
if
avg_cost
<
5.0
:
trainer
.
params
.
save
(
save_path
)
trainer
.
save_params
(
save_path
)
return
if
math
.
isnan
(
avg_cost
):
sys
.
exit
(
"got NaN loss, training failed."
)
trainer
=
fluid
.
Trainer
(
partial
(
train_
network
,
is_sparse
),
partial
(
train_
program
,
is_sparse
),
fluid
.
optimizer
.
SGD
(
learning_rate
=
0.001
),
place
=
place
)
trainer
.
train
(
reader
=
train_reader
,
num_epochs
=
100
,
event_handler
=
event_handler
)
def
infer
(
use_cuda
,
save_path
):
params
=
fluid
.
Params
(
save_path
)
def
infer
(
use_cuda
,
is_sparse
,
save_path
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
inference_network
,
params
,
place
=
place
)
inferencer
=
fluid
.
Inferencer
(
partial
(
inference_program
,
is_sparse
),
param_path
=
save_path
,
place
=
place
)
lod
=
[
0
,
1
]
first_word
=
create_random_lodtensor
(
lod
,
place
,
low
=
0
,
high
=
dict_size
-
1
)
...
...
@@ -138,7 +140,7 @@ def main(use_cuda, is_sparse):
save_path
=
"word2vec.inference.model"
train
(
use_cuda
,
is_sparse
,
save_path
)
infer
(
use_cuda
,
save_path
)
infer
(
use_cuda
,
is_sparse
,
save_path
)
if
__name__
==
'__main__'
:
...
...
python/paddle/fluid/trainer.py
浏览文件 @
be219ac0
...
...
@@ -56,23 +56,22 @@ class Trainer(object):
"""
Args:
network
_func(callable): A function which will return loss. The loss must be a scaler.
program
_func(callable): A function which will return loss. The loss must be a scaler.
optimizer(optimizer.Optimizer): The optimizer should be an instance of Optimizer
params:
place: The device place of this trainer.
"""
def
__init__
(
self
,
network_func
,
optimizer
,
params
=
None
,
place
=
None
):
def
__init__
(
self
,
program_func
,
optimizer
,
param_path
=
None
,
place
=
None
):
# 1. we need to generate a framework.Program by calling
#
network
_func. Reference: fluid.program_guard in
#
program
_func. Reference: fluid.program_guard in
# test_word2vec.py
self
.
scope
=
self
.
_get_scope_from_params
(
params
)
self
.
scope
=
core
.
Scope
(
)
self
.
startup_program
=
framework
.
Program
()
self
.
train_program
=
framework
.
Program
()
with
framework
.
program_guard
(
self
.
train_program
,
self
.
startup_program
):
loss
=
network
_func
()
loss
=
program
_func
()
if
not
isinstance
(
optimizer
,
opt_module
.
Optimizer
):
raise
TypeError
(
"The optimizer should be an instance of Optimizer"
)
...
...
@@ -84,14 +83,13 @@ class Trainer(object):
# 2. move the default_main_program to self.program and run the
# default_startup program on an empty core.Scope()
# Run startup program
if
params
is
None
:
exe
=
executor
.
Executor
(
place
)
exe
.
run
(
self
.
startup_program
,
scope
=
self
.
scope
)
exe
=
executor
.
Executor
(
place
)
exe
.
run
(
self
.
startup_program
,
scope
=
self
.
scope
)
# 3. call self.params.add_vars with the initialized scope, it
# will add the new vars of the initialized scope into
# self.params
.
# TODO(yuyang): This depends on parameters implementation.
if
param_path
:
# load params from param_path into scope
# TODO(yuyang): This depends on parameters implementation
.
pass
# TODO(helin): support distributed training
...
...
@@ -124,19 +122,9 @@ class Trainer(object):
def
test
(
self
,
reader
):
pass
def
_get_scope_from_params
(
self
,
params
):
"""
Get Scope from parameter object.
Args:
params(Parameter|None): The parameter object instance. Could be None.
Returns: New scope if params is None. Or params.scope()
NOTE: This method is WIP. Not fully implemented.
"""
if
params
is
None
:
return
core
.
Scope
()
# new scope when params is None
else
:
raise
NotImplementedError
(
"Not implemented right now."
)
def
save_params
(
self
,
param_path
):
# reference: save_persistables in io.py
pass
@
staticmethod
def
_check_and_get_place
(
place
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录