Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
b15c6755
P
Paddle
项目概览
Crayon鑫
/
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看板
未验证
提交
b15c6755
编写于
2月 05, 2018
作者:
E
emailweixu
提交者:
GitHub
2月 05, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #7421 from emailweixu/fetch_var
helper functions fetch_var and get_var
上级
1ead6c26
37a251eb
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
104 addition
and
14 deletion
+104
-14
python/paddle/v2/fluid/executor.py
python/paddle/v2/fluid/executor.py
+42
-11
python/paddle/v2/fluid/framework.py
python/paddle/v2/fluid/framework.py
+20
-0
python/paddle/v2/fluid/layers/tensor.py
python/paddle/v2/fluid/layers/tensor.py
+5
-3
python/paddle/v2/fluid/tests/test_fetch_var.py
python/paddle/v2/fluid/tests/test_fetch_var.py
+37
-0
未找到文件。
python/paddle/v2/fluid/executor.py
浏览文件 @
b15c6755
...
...
@@ -17,7 +17,9 @@ import contextlib
from
framework
import
Program
,
default_main_program
from
.
import
core
__all__
=
[
'Executor'
,
'global_scope'
,
'scope_guard'
,
'switch_scope'
]
__all__
=
[
'Executor'
,
'global_scope'
,
'scope_guard'
,
'switch_scope'
,
'fetch_var'
]
g_scope
=
core
.
Scope
()
...
...
@@ -80,12 +82,12 @@ def has_feed_operators(block, feed_targets, feed_holder_name):
Args:
block: a block instance (typically global block of a program)
feed_targets: a dictionary of {feed_target_name: feed_target_data}
feed_holder_name: the name of the variable that holds the data of
all feed targets. The type of this feed_holder variable is
feed_holder_name: the name of the variable that holds the data of
all feed targets. The type of this feed_holder variable is
FEED_MINIBATCH, which is essentially vector<LoDTensor>.
Returns:
A boolean value that indicates whether a block has feed operators
A boolean value that indicates whether a block has feed operators
that match the info contained in feed_targets and feed_holder_name.
"""
...
...
@@ -108,7 +110,7 @@ def has_feed_operators(block, feed_targets, feed_holder_name):
def
has_fetch_operators
(
block
,
fetch_targets
,
fetch_holder_name
):
""" Check whether the block already has fetch operators.
Return false if the block does not have any fetch operators.
If some fetch operators have been appended to the block, check that
the info contained in these fetch operators matches the fetch_targets
...
...
@@ -118,13 +120,13 @@ def has_fetch_operators(block, fetch_targets, fetch_holder_name):
Args:
block: a block instance (typically global block of a program)
fetch_targets: a dictionary of {fetch_target_name: fetch_target_data}
fetch_holder_name: the name of the variable that holds the data of
all fetch targets. The type of this fetch_holder variable is
FETCH_LIST, which is essentially vector<LoDTensor>.
fetch_holder_name: the name of the variable that holds the data of
all fetch targets. The type of this fetch_holder variable is
FETCH_LIST, which is essentially vector<LoDTensor>.
Return:
A boolean value that indicates whether a block has fetch operators
that match the info contained in fetch_targets and fetch_holder_name.
Return:
A boolean value that indicates whether a block has fetch operators
that match the info contained in fetch_targets and fetch_holder_name.
"""
fetch_count
=
0
...
...
@@ -146,6 +148,35 @@ def has_fetch_operators(block, fetch_targets, fetch_holder_name):
return
fetch_count
>
0
def
fetch_var
(
name
,
scope
=
None
,
return_numpy
=
True
):
"""
Fetch the value of the variable with the given name from the given scope
Args:
name(str): name of the variable. Typically, only persistable variables
can be found in the scope used for running the program.
scope(core.Scope|None): scope object. It should be the scope where
you pass to Executor.run() when running your program.
If None, global_scope() will be used.
return_numpy(bool): whether convert the tensor to numpy.ndarray
Returns:
LodTensor|numpy.ndarray
"""
assert
isinstance
(
name
,
str
)
if
scope
is
None
:
scope
=
global_scope
()
assert
isinstance
(
scope
,
core
.
Scope
)
var
=
global_scope
().
find_var
(
name
)
assert
var
is
not
None
,
(
"Cannot find "
+
name
+
" in scope. Perhaps you need to make the"
" variable persistable by using var.persistable = True in your"
" program."
)
tensor
=
var
.
get_tensor
()
if
return_numpy
:
tensor
=
as_numpy
(
tensor
)
return
tensor
class
Executor
(
object
):
def
__init__
(
self
,
places
):
if
not
isinstance
(
places
,
list
)
and
not
isinstance
(
places
,
tuple
):
...
...
python/paddle/v2/fluid/framework.py
浏览文件 @
b15c6755
...
...
@@ -31,6 +31,7 @@ __all__ = [
'program_guard'
,
'switch_startup_program'
,
'switch_main_program'
,
'get_var'
,
]
EMPTY_VAR_NAME
=
core
.
kEmptyVarName
()
...
...
@@ -1123,3 +1124,22 @@ def program_guard(main_program, startup_program=None):
switch_main_program
(
main_program
)
if
startup_program
is
not
None
:
switch_startup_program
(
startup_program
)
def
get_var
(
name
,
program
=
None
):
"""
Get a variable by name from the global block of a program
Args:
name(str): name of the variable
program(Program|None): program object.
If None, default_global_program() will be used.
Returns:
Variable
"""
if
program
is
None
:
program
=
default_main_program
()
assert
isinstance
(
name
,
str
)
assert
isinstance
(
name
,
Program
)
return
program
.
global_block
().
var
(
name
)
python/paddle/v2/fluid/layers/tensor.py
浏览文件 @
b15c6755
...
...
@@ -35,13 +35,15 @@ __all__ = [
]
def
create_tensor
(
dtype
,
name
=
None
):
def
create_tensor
(
dtype
,
name
=
None
,
persistable
=
False
):
helper
=
LayerHelper
(
"create_tensor"
,
**
locals
())
return
helper
.
create_variable
(
name
=
helper
.
name
,
dtype
=
dtype
)
return
helper
.
create_variable
(
name
=
helper
.
name
,
dtype
=
dtype
,
persistable
=
persistable
)
def
create_parameter
(
shape
,
dtype
,
name
=
None
,
attr
=
None
,
is_bias
=
False
,
default_initializer
=
None
):
...
...
@@ -62,7 +64,7 @@ def create_parameter(shape,
"""
helper
=
LayerHelper
(
"create_parameter"
,
**
locals
())
if
attr
is
None
:
attr
=
ParamAttr
()
attr
=
ParamAttr
(
name
=
name
)
return
helper
.
create_parameter
(
attr
,
shape
,
dtype
,
is_bias
,
default_initializer
)
...
...
python/paddle/v2/fluid/tests/test_fetch_var.py
0 → 100644
浏览文件 @
b15c6755
# 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.
import
paddle.v2.fluid
as
fluid
import
paddle.v2.fluid.layers
as
layers
import
op_test
import
numpy
import
unittest
class
TestFetchVar
(
op_test
.
OpTest
):
def
test_fetch_var
(
self
):
val
=
numpy
.
array
([
1
,
3
,
5
]).
astype
(
numpy
.
int32
)
x
=
layers
.
create_tensor
(
dtype
=
"int32"
,
persistable
=
True
,
name
=
"x"
)
layers
.
assign
(
input
=
val
,
output
=
x
)
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
{},
fetch_list
=
[])
fetched_x
=
fluid
.
fetch_var
(
"x"
)
self
.
assertTrue
(
numpy
.
array_equal
(
fetched_x
,
val
),
"fetch_x=%s val=%s"
%
(
fetched_x
,
val
))
self
.
assertEqual
(
fetched_x
.
dtype
,
val
.
dtype
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录