Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
b4b3079d
提交
b4b3079d
authored
3月 03, 2014
作者:
abergeron
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1755 from benanne/consider_constant
Add consider_constant op
上级
9bda81cc
be80c421
显示空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
82 行增加
和
3 行删除
+82
-3
builders.py
theano/compile/builders.py
+2
-2
gradient.py
theano/gradient.py
+27
-0
__init__.py
theano/tensor/__init__.py
+1
-1
opt.py
theano/tensor/opt.py
+10
-0
test_extra_ops.py
theano/tensor/tests/test_extra_ops.py
+1
-0
test_gradient.py
theano/tests/test_gradient.py
+41
-0
没有找到文件。
theano/compile/builders.py
浏览文件 @
b4b3079d
import
theano
from
theano
import
gof
from
theano
import
gradient
as
G
from
theano.compile.function_module
import
orig_function
from
theano.compile
import
SharedVariable
,
rebuild_collect_shared
from
theano.gof
import
ops_with_inner_function
...
...
@@ -142,7 +142,7 @@ class OpFromGraph(gof.Op):
if
hasattr
(
self
,
"grad_ops"
):
grad_ops
=
self
.
grad_ops
else
:
gs
=
G
.
grad
(
cost
=
None
,
gs
=
theano
.
gradient
.
grad
(
cost
=
None
,
known_grads
=
dict
(
zip
(
self
.
new_outputs
,
output_grads
)),
wrt
=
self
.
new_inputs
,
disconnected_inputs
=
'ignore'
)
...
...
theano/gradient.py
浏览文件 @
b4b3079d
...
...
@@ -23,6 +23,7 @@ from theano.gof import Variable
from
theano.gof.python25
import
OrderedDict
from
theano.gof.null_type
import
NullType
from
theano.gof.op
import
get_debug_values
from
theano.compile
import
ViewOp
# we can't do "import theano.tensor"
# tensor depends on theano.compile
...
...
@@ -1685,3 +1686,29 @@ def _is_zero(x):
return
'no'
return
'yes'
class
ConsiderConstant
(
ViewOp
):
def
grad
(
self
,
args
,
g_outs
):
return
[
g_out
.
zeros_like
(
g_out
)
for
g_out
in
g_outs
]
consider_constant_
=
ConsiderConstant
()
#I create a function only to have the doc show well.
def
consider_constant
(
x
):
""" Consider an expression constant when computing gradients.
The expression itself is unaffected, but when its gradient is
computed, or the gradient of another expression that this
expression is a subexpression of, it will not be backpropagated
through. In other words, the gradient of the expression is
truncated to 0.
:param x: A Theano expression whose gradient should be truncated.
:return: The expression is returned unmodified, but its gradient
is now truncated to 0.
.. versionadded:: 0.6.1
"""
return
consider_constant_
(
x
)
theano/tensor/__init__.py
浏览文件 @
b4b3079d
...
...
@@ -58,7 +58,7 @@ def shared(*args, **kw):
from
theano.tensor
import
nnet
# used for softmax, sigmoid, etc.
from
theano.gradient
import
Rop
,
Lop
,
grad
,
numeric_grad
,
verify_grad
,
\
jacobian
,
hessian
jacobian
,
hessian
,
consider_constant
from
theano.tensor.sort
import
sort
,
argsort
from
theano.tensor.extra_ops
import
(
DiffOp
,
bincount
,
squeeze
,
...
...
theano/tensor/opt.py
浏览文件 @
b4b3079d
...
...
@@ -4866,3 +4866,13 @@ else:
FusionOptimizer
(
local_elemwise_fusion
),
49
,
'fusion'
,
'local_elemwise_fusion'
,
'FusionOptimizer'
)
# ############################
# # Remove consider_constant #
# ############################
# Although the op just returns its input, it should be removed from
# the graph to make sure all possible optimizations can be applied.
register_canonicalize
(
gof
.
OpRemove
(
theano
.
gradient
.
consider_constant_
),
'fast_compile'
,
name
=
'remove_consider_constant'
)
theano/tensor/tests/test_extra_ops.py
浏览文件 @
b4b3079d
import
numpy
as
np
import
numpy
import
unittest
import
theano
from
theano.tests
import
unittest_tools
as
utt
...
...
theano/tests/test_gradient.py
浏览文件 @
b4b3079d
...
...
@@ -5,6 +5,7 @@
import
unittest
import
theano
from
theano
import
gof
from
theano.tests
import
unittest_tools
as
utt
from
theano
import
gradient
from
theano.tensor.nnet.Conv3D
import
conv3D
...
...
@@ -554,5 +555,45 @@ def test_disconnected_cost_grad():
return
raise
AssertionError
(
"A disconnected gradient has been ignored."
)
class
TestConsiderConstant
(
unittest
.
TestCase
):
def
setUp
(
self
):
utt
.
seed_rng
()
self
.
rng
=
np
.
random
.
RandomState
(
seed
=
utt
.
fetch_seed
())
def
test_op_removed
(
self
):
x
=
theano
.
tensor
.
matrix
(
'x'
)
y
=
x
*
gradient
.
consider_constant
(
x
)
f
=
theano
.
function
([
x
],
y
)
# need to refer to theano.gradient.consider_constant_ here,
# theano.gradient.consider_constant is a wrapper function!
assert
gradient
.
consider_constant_
not
in
\
[
node
.
op
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
def
test_grad
(
self
):
T
=
theano
.
tensor
a
=
np
.
asarray
(
self
.
rng
.
randn
(
5
,
5
),
dtype
=
config
.
floatX
)
x
=
T
.
matrix
(
'x'
)
expressions_gradients
=
[
(
x
*
gradient
.
consider_constant
(
x
),
x
),
(
x
*
gradient
.
consider_constant
(
T
.
exp
(
x
)),
T
.
exp
(
x
)),
(
gradient
.
consider_constant
(
x
),
T
.
constant
(
0.
)),
(
x
**
2
*
gradient
.
consider_constant
(
x
),
2
*
x
**
2
),
]
for
expr
,
expr_grad
in
expressions_gradients
:
g
=
gradient
.
grad
(
expr
.
sum
(),
x
)
# gradient according to theano
f
=
theano
.
function
([
x
],
g
,
on_unused_input
=
'ignore'
)
# desired gradient
f2
=
theano
.
function
([
x
],
expr_grad
,
on_unused_input
=
'ignore'
)
assert
np
.
allclose
(
f
(
a
),
f2
(
a
))
if
__name__
==
'__main__'
:
unittest
.
main
()
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论