Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
3a6ead24
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
3a6ead24
编写于
6月 03, 2019
作者:
Z
Zeng Jinle
提交者:
GitHub
6月 03, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add no_grad decorator to dygraph (#17790)
* add no_grad decorator to dygraph, test=develop * add unittest,test=develop
上级
53920f5e
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
86 addition
and
2 deletion
+86
-2
python/paddle/fluid/dygraph/base.py
python/paddle/fluid/dygraph/base.py
+31
-2
python/paddle/fluid/dygraph_grad_clip.py
python/paddle/fluid/dygraph_grad_clip.py
+5
-0
python/paddle/fluid/optimizer.py
python/paddle/fluid/optimizer.py
+2
-0
python/paddle/fluid/tests/unittests/test_imperative_decorator.py
...paddle/fluid/tests/unittests/test_imperative_decorator.py
+48
-0
未找到文件。
python/paddle/fluid/dygraph/base.py
浏览文件 @
3a6ead24
...
...
@@ -11,20 +11,49 @@
# 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
..wrapped_decorator
import
signature_safe_contextmanager
from
..wrapped_decorator
import
signature_safe_contextmanager
,
wrap_decorator
import
contextlib
import
numpy
as
np
from
paddle.fluid
import
core
from
paddle.fluid
import
framework
from
.tracer
import
Tracer
__all__
=
[
'enabled'
,
'guard'
,
'to_variable'
]
__all__
=
[
'enabled'
,
'no_grad'
,
'guard'
,
'to_variable'
,
]
def
enabled
():
return
framework
.
in_dygraph_mode
()
@
contextlib
.
contextmanager
def
_switch_tracer_mode_guard_
(
is_train
=
True
):
tracer
=
framework
.
_dygraph_tracer
()
if
tracer
:
mode
=
tracer
.
_train_mode
tracer
.
_train_mode
=
is_train
yield
tracer
.
_train_mode
=
mode
else
:
yield
def
_no_grad_
(
func
):
def
__impl__
(
*
args
,
**
kwargs
):
with
_switch_tracer_mode_guard_
(
is_train
=
False
):
return
func
(
*
args
,
**
kwargs
)
return
__impl__
no_grad
=
wrap_decorator
(
_no_grad_
)
@
signature_safe_contextmanager
def
guard
(
place
=
None
):
train
=
framework
.
Program
()
...
...
python/paddle/fluid/dygraph_grad_clip.py
浏览文件 @
3a6ead24
...
...
@@ -22,6 +22,7 @@ import functools
from
.
import
layers
from
.
import
framework
from
.
import
core
from
.dygraph
import
base
as
imperative_base
__all__
=
[
'GradClipByValue'
,
...
...
@@ -37,6 +38,7 @@ class GradClipBase(object):
def
_clip
(
self
,
para_and_grad
):
raise
NotImplementedError
@
imperative_base
.
no_grad
def
__call__
(
self
,
para_and_grad
):
return
self
.
_clip
(
para_and_grad
)
...
...
@@ -86,6 +88,7 @@ class GradClipByValue(GradClipBase):
"""
@
imperative_base
.
no_grad
def
__init__
(
self
,
min_value
,
max_value
=
None
):
if
min_value
is
None
:
...
...
@@ -164,6 +167,7 @@ class GradClipByNorm(GradClipBase):
"""
@
imperative_base
.
no_grad
def
__init__
(
self
,
clip_norm
):
self
.
clip_norm
=
clip_norm
...
...
@@ -243,6 +247,7 @@ class GradClipByGlobalNorm(GradClipBase):
"""
@
imperative_base
.
no_grad
def
__init__
(
self
,
max_global_norm
):
self
.
max_global_norm
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'float32'
,
value
=
max_global_norm
)
...
...
python/paddle/fluid/optimizer.py
浏览文件 @
3a6ead24
...
...
@@ -55,6 +55,7 @@ class Optimizer(object):
but need to use one of it's implementation.
"""
@
imperative_base
.
no_grad
def
__init__
(
self
,
learning_rate
,
regularization
=
None
,
name
=
None
):
if
framework
.
in_dygraph_mode
():
if
not
isinstance
(
learning_rate
,
float
)
and
\
...
...
@@ -472,6 +473,7 @@ class Optimizer(object):
optimize_ops
=
self
.
apply_gradients
(
params_grads
)
return
optimize_ops
@
imperative_base
.
no_grad
def
minimize
(
self
,
loss
,
startup_program
=
None
,
...
...
python/paddle/fluid/tests/unittests/test_imperative_decorator.py
0 → 100644
浏览文件 @
3a6ead24
# Copyright (c) 2019 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.fluid
as
fluid
import
paddle.fluid.framework
as
framework
import
unittest
class
TestTracerMode
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
init_mode
=
True
def
get_tracer_mode
(
self
):
assert
fluid
.
dygraph
.
enabled
(),
"Dygraph mode must be enabled"
@
fluid
.
dygraph
.
no_grad
def
no_grad_func
(
self
,
a
):
self
.
assertEqual
(
self
.
tracer
.
_train_mode
,
False
)
return
a
def
test_main
(
self
):
with
fluid
.
dygraph
.
guard
():
self
.
tracer
=
framework
.
_dygraph_tracer
()
self
.
tracer
.
_train_mode
=
self
.
init_mode
self
.
assertEqual
(
self
.
no_grad_func
(
1
),
1
)
self
.
assertEqual
(
self
.
tracer
.
_train_mode
,
self
.
init_mode
)
class
TestTracerMode2
(
TestTracerMode
):
def
setUp
(
self
):
self
.
init_mode
=
False
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录