Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
3c5cc644
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看板
提交
3c5cc644
编写于
7月 13, 2017
作者:
Y
Yu Yang
提交者:
GitHub
7月 13, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2808 from reyoung/feature/scope_default_wrapper
Default scope function
上级
305e21dc
d027f47d
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
118 addition
and
1 deletion
+118
-1
python/paddle/v2/framework/default_scope_funcs.py
python/paddle/v2/framework/default_scope_funcs.py
+83
-0
python/paddle/v2/framework/tests/CMakeLists.txt
python/paddle/v2/framework/tests/CMakeLists.txt
+2
-1
python/paddle/v2/framework/tests/test_default_scope_funcs.py
python/paddle/v2/framework/tests/test_default_scope_funcs.py
+33
-0
未找到文件。
python/paddle/v2/framework/default_scope_funcs.py
0 → 100644
浏览文件 @
3c5cc644
"""
Default scope function.
`Paddle` manages Scope as programming language's scope. It just a
thread-local stack of Scope. Top of that stack is current scope, the bottom
of that stack is all scopes' parent.
Invoking `create_var/get_var` can `create/get` variable in current scope.
Invoking `enter_local_scope/leave_local_scope` can create or destroy local
scope.
A `scoped_function` will take a `function` as input. That function will be
invoked in a new local scope.
"""
import
paddle.v2.framework.core
import
threading
__tl_scope__
=
threading
.
local
()
__all__
=
[
'get_cur_scope'
,
'enter_local_scope'
,
'leave_local_scope'
,
'create_var'
,
'get_var'
,
'scoped_function'
]
def
get_cur_scope
():
"""
Get current scope.
:rtype: paddle.v2.framework.core.Scope
"""
cur_scope_stack
=
getattr
(
__tl_scope__
,
'cur_scope'
,
None
)
if
cur_scope_stack
is
None
:
__tl_scope__
.
cur_scope
=
list
()
if
len
(
__tl_scope__
.
cur_scope
)
==
0
:
__tl_scope__
.
cur_scope
.
append
(
paddle
.
v2
.
framework
.
core
.
Scope
(
None
))
return
__tl_scope__
.
cur_scope
[
-
1
]
def
enter_local_scope
():
"""
Enter a new local scope
"""
cur_scope
=
get_cur_scope
()
new_scope
=
paddle
.
v2
.
framework
.
core
.
Scope
(
cur_scope
)
__tl_scope__
.
cur_scope
.
append
(
new_scope
)
def
leave_local_scope
():
"""
Leave local scope
"""
__tl_scope__
.
cur_scope
.
pop
()
def
create_var
(
name
):
"""
create variable in current scope.
"""
return
get_cur_scope
().
create_var
(
name
)
def
get_var
(
name
):
"""
get variable in current scope.
"""
return
get_cur_scope
().
get_var
(
name
)
def
scoped_function
(
func
):
"""
invoke `func` in new scope.
:param func: a callable function that will be run in new scope.
:type func: callable
"""
enter_local_scope
()
try
:
func
()
except
:
raise
finally
:
leave_local_scope
()
python/paddle/v2/framework/tests/CMakeLists.txt
浏览文件 @
3c5cc644
add_python_test
(
test_framework test_protobuf.py test_scope.py
)
add_python_test
(
test_framework test_protobuf.py test_scope.py
test_default_scope_funcs.py
)
python/paddle/v2/framework/tests/test_default_scope_funcs.py
0 → 100644
浏览文件 @
3c5cc644
from
paddle.v2.framework.default_scope_funcs
import
*
import
unittest
class
TestDefaultScopeFuncs
(
unittest
.
TestCase
):
def
test_cur_scope
(
self
):
self
.
assertIsNotNone
(
get_cur_scope
())
def
test_none_variable
(
self
):
self
.
assertIsNone
(
get_var
(
"test"
))
def
test_create_var_get_var
(
self
):
var_a
=
create_var
(
"var_a"
)
self
.
assertIsNotNone
(
var_a
)
self
.
assertIsNotNone
(
get_cur_scope
().
get_var
(
'var_a'
))
enter_local_scope
()
self
.
assertIsNotNone
(
get_cur_scope
().
get_var
(
'var_a'
))
leave_local_scope
()
def
test_var_get_int
(
self
):
def
__new_scope__
():
i
=
create_var
(
"var_i"
)
self
.
assertFalse
(
i
.
is_int
())
i
.
set_int
(
10
)
self
.
assertTrue
(
i
.
is_int
())
self
.
assertEqual
(
10
,
i
.
get_int
())
for
_
in
xrange
(
10
):
scoped_function
(
__new_scope__
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录