Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
eabd4641
提交
eabd4641
authored
1月 14, 2015
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add GradClip op
上级
370953c5
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
69 行增加
和
1 行删除
+69
-1
gradient.py
theano/gradient.py
+43
-0
opt.py
theano/tensor/opt.py
+7
-0
test_gradient.py
theano/tests/test_gradient.py
+19
-1
没有找到文件。
theano/gradient.py
浏览文件 @
eabd4641
...
@@ -1888,3 +1888,46 @@ def consider_constant(x):
...
@@ -1888,3 +1888,46 @@ def consider_constant(x):
.. versionadded:: 0.6.1
.. versionadded:: 0.6.1
"""
"""
return
consider_constant_
(
x
)
return
consider_constant_
(
x
)
class
GradClip
(
theano
.
compile
.
ViewOp
):
# See doc in user fct grad_clip
__props__
=
()
def
__init__
(
self
,
clip_lower_bound
,
clip_upper_bound
):
# We do not put those member in __eq__ or __hash__
# as they do not influence the perform of this op.
self
.
clip_lower_bound
=
clip_lower_bound
self
.
clip_upper_bound
=
clip_upper_bound
assert
(
self
.
clip_upper_bound
>=
self
.
clip_lower_bound
)
def
grad
(
self
,
args
,
g_outs
):
return
[
theano
.
tensor
.
clip
(
g_out
,
self
.
clip_lower_bound
,
self
.
clip_upper_bound
)
for
g_out
in
g_outs
]
def
grad_clip
(
x
,
lower_bound
,
upper_bound
):
"""
This op do a view in the forward, but clip the gradient.
This is an elemwise operation.
:param x: the variable we want its gradient inputs clipped
:param lower_bound: The lower bound of the gradient value
:param upper_bound: The upper bound of the gradient value.
:examples:
x = theano.tensor.scalar()
z = theano.tensor.grad(grad_clip(x)**2, x)
z2 = theano.tensor.grad(x**2, x)
f = theano.function([x], outputs = [z, z2])
print f(2.0) # output (1.0, 4.0)
:note: We register an opt in tensor/opt.py that remove the GradClip.
So it have 0 cost in the forward and only do work in the grad.
"""
return
GradClip
(
lower_bound
,
upper_bound
)(
x
)
theano/tensor/opt.py
浏览文件 @
eabd4641
...
@@ -5551,3 +5551,10 @@ else:
...
@@ -5551,3 +5551,10 @@ else:
# the graph to make sure all possible optimizations can be applied.
# the graph to make sure all possible optimizations can be applied.
register_canonicalize
(
gof
.
OpRemove
(
theano
.
gradient
.
consider_constant_
),
register_canonicalize
(
gof
.
OpRemove
(
theano
.
gradient
.
consider_constant_
),
'fast_compile'
,
'fast_run'
,
name
=
'remove_consider_constant'
)
'fast_compile'
,
'fast_run'
,
name
=
'remove_consider_constant'
)
@register_canonicalize
@gof.local_optimizer
([
theano
.
gradient
.
GradClip
])
def
local_grad_clip
(
node
):
if
isinstance
(
node
.
op
,
theano
.
gradient
.
GradClip
):
return
node
.
inputs
theano/tests/test_gradient.py
浏览文件 @
eabd4641
...
@@ -3,6 +3,9 @@
...
@@ -3,6 +3,9 @@
# UNIT TEST
# UNIT TEST
#
#
import
unittest
import
unittest
import
numpy
as
np
import
theano
import
theano
from
theano
import
gof
from
theano
import
gof
from
theano.tests
import
unittest_tools
as
utt
from
theano.tests
import
unittest_tools
as
utt
...
@@ -10,7 +13,6 @@ from theano.tests import unittest_tools as utt
...
@@ -10,7 +13,6 @@ from theano.tests import unittest_tools as utt
from
theano
import
gradient
from
theano
import
gradient
from
theano.tensor.nnet.Conv3D
import
conv3D
from
theano.tensor.nnet.Conv3D
import
conv3D
from
theano
import
config
from
theano
import
config
import
numpy
as
np
from
theano.gof.null_type
import
NullType
from
theano.gof.null_type
import
NullType
one
=
theano
.
tensor
.
as_tensor_variable
(
1.
)
one
=
theano
.
tensor
.
as_tensor_variable
(
1.
)
...
@@ -641,5 +643,21 @@ class TestConsiderConstant(unittest.TestCase):
...
@@ -641,5 +643,21 @@ class TestConsiderConstant(unittest.TestCase):
assert
np
.
allclose
(
f
(
a
),
f2
(
a
))
assert
np
.
allclose
(
f
(
a
),
f2
(
a
))
def
test_grad_clip
():
x
=
theano
.
tensor
.
scalar
()
z
=
theano
.
tensor
.
grad
(
gradient
.
grad_clip
(
x
,
-
1
,
1
)
**
2
,
x
)
z2
=
theano
.
tensor
.
grad
(
x
**
2
,
x
)
f
=
theano
.
function
([
x
],
outputs
=
[
z
,
z2
])
if
theano
.
config
.
mode
!=
"FAST_COMPILE"
:
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
not
any
([
isinstance
(
node
.
op
,
gradient
.
GradClip
)
for
node
in
topo
])
out
=
f
(
2.
)
assert
np
.
allclose
(
out
,
(
1
,
4
))
assert
not
np
.
allclose
(
out
[
0
],
out
[
1
])
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论