Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
b3278229
MegEngine
项目概览
MegEngine 天元
/
MegEngine
1 年多 前同步成功
通知
404
Star
4705
Fork
582
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
MegEngine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
b3278229
编写于
10月 29, 2020
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(mge/grad_manager): add `clear_grad` method for GradManager
GitOrigin-RevId: aa9540e09018697110b672f3772473b68305751c
上级
2627e1f7
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
10 addition
and
4 deletion
+10
-4
imperative/python/megengine/autodiff/grad_manager.py
imperative/python/megengine/autodiff/grad_manager.py
+8
-0
imperative/python/megengine/optimizer/optimizer.py
imperative/python/megengine/optimizer/optimizer.py
+1
-2
imperative/python/test/unit/autodiff/test_grad_manger.py
imperative/python/test/unit/autodiff/test_grad_manger.py
+1
-2
未找到文件。
imperative/python/megengine/autodiff/grad_manager.py
浏览文件 @
b3278229
...
@@ -112,6 +112,14 @@ class GradManager:
...
@@ -112,6 +112,14 @@ class GradManager:
else
:
else
:
logger
.
warning
(
"params with index {} is not attached."
.
format
(
idx
))
logger
.
warning
(
"params with index {} is not attached."
.
format
(
idx
))
def
clear_grad
(
self
):
r
"""
For advanced usage: set the grad attribute to None for registered parameters.
It could be more convenient when there is more than one Optimizer.
"""
for
param
in
self
.
_param_dict
.
values
():
param
.
grad
=
None
def
_register_after_backward_callback
(
self
,
callback
):
def
_register_after_backward_callback
(
self
,
callback
):
self
.
_after_backward_callback
.
append
(
callback
)
self
.
_after_backward_callback
.
append
(
callback
)
return
self
return
self
...
...
imperative/python/megengine/optimizer/optimizer.py
浏览文件 @
b3278229
...
@@ -91,7 +91,7 @@ class Optimizer(metaclass=ABCMeta):
...
@@ -91,7 +91,7 @@ class Optimizer(metaclass=ABCMeta):
if
not
isinstance
(
param
,
Parameter
):
if
not
isinstance
(
param
,
Parameter
):
raise
TypeError
(
raise
TypeError
(
"optimizer can only optimize Parameters, but one of the params is "
"optimizer can only optimize Parameters, but one of the params is "
+
type
(
param
)
+
str
(
type
(
param
)
)
)
)
for
name
,
default
in
self
.
_defaults
.
items
():
for
name
,
default
in
self
.
_defaults
.
items
():
...
@@ -159,7 +159,6 @@ class Optimizer(metaclass=ABCMeta):
...
@@ -159,7 +159,6 @@ class Optimizer(metaclass=ABCMeta):
def
clear_grad
(
self
):
def
clear_grad
(
self
):
r
"""Set the grad attribute to None for all parameters.
r
"""Set the grad attribute to None for all parameters.
"""
"""
for
param_group
in
self
.
param_groups
:
for
param_group
in
self
.
param_groups
:
for
param
in
param_group
[
"params"
]:
for
param
in
param_group
[
"params"
]:
...
...
imperative/python/test/unit/autodiff/test_grad_manger.py
浏览文件 @
b3278229
...
@@ -29,8 +29,7 @@ def test_basic():
...
@@ -29,8 +29,7 @@ def test_basic():
np
.
testing
.
assert_equal
(
w
.
grad
.
numpy
(),
[[
1
],
[
3
],
[
5
]])
np
.
testing
.
assert_equal
(
w
.
grad
.
numpy
(),
[[
1
],
[
3
],
[
5
]])
np
.
testing
.
assert_equal
(
b
.
grad
.
numpy
(),
[
1
])
np
.
testing
.
assert_equal
(
b
.
grad
.
numpy
(),
[
1
])
w
.
grad
=
None
gm
.
clear_grad
()
b
.
grad
=
None
with
gm
:
with
gm
:
p
=
F
.
matmul
(
x
,
w
)
p
=
F
.
matmul
(
x
,
w
)
y
=
p
+
b
y
=
p
+
b
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录