Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
17458063
提交
17458063
authored
11月 19, 2012
作者:
lamblin
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1074 from goodfeli/test_grad
Ready to merge: Adds a unit test of undefined gradients on integers
上级
51290164
23ae9a3f
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
67 行增加
和
23 行删除
+67
-23
gradient.py
theano/gradient.py
+43
-15
elemwise.py
theano/tensor/elemwise.py
+7
-8
test_gradient.py
theano/tests/test_gradient.py
+17
-0
没有找到文件。
theano/gradient.py
浏览文件 @
17458063
...
@@ -468,7 +468,7 @@ def grad(cost, wrt, consider_constant=None,
...
@@ -468,7 +468,7 @@ def grad(cost, wrt, consider_constant=None,
'Ambiguous whether
%
s should be made into tensor'
'Ambiguous whether
%
s should be made into tensor'
' or sparse theano variable'
%
str
(
type
(
g_var
)))
' or sparse theano variable'
%
str
(
type
(
g_var
)))
if
g_var
.
type
not
in
[
NullType
,
DisconnectedType
]
and
'float'
\
if
not
isinstance
(
g_var
.
type
,
(
NullType
,
DisconnectedType
))
and
'float'
\
not
in
str
(
g_var
.
type
.
dtype
):
not
in
str
(
g_var
.
type
.
dtype
):
raise
TypeError
(
"Gradients must always be NullType, "
raise
TypeError
(
"Gradients must always be NullType, "
"DisconnectedType, or continuous, but grad was "
"DisconnectedType, or continuous, but grad was "
...
@@ -776,8 +776,42 @@ def _populate_grad_dict(var_to_node_to_idx,
...
@@ -776,8 +776,42 @@ def _populate_grad_dict(var_to_node_to_idx,
input_to_outputs
in
connection_pattern
input_to_outputs
in
connection_pattern
]
]
if
True
in
inputs_connected
:
#List of bools indicating if each output is an integer dtype
# At least one input of this op is connected to the cost so we must
output_is_int
=
[
hasattr
(
output
.
type
,
'dtype'
)
and
output
.
type
.
dtype
in
theano
.
tensor
.
discrete_dtypes
for
output
in
node
.
outputs
]
#List of bools indicating if each output is NullType
ograd_is_nan
=
[
isinstance
(
output
.
type
,
NullType
)
for
output
in
output_grads
]
# List of bools indicating if each input only has NullType outputs
only_connected_to_nan
=
[(
True
not
in
[
in_to_out
and
out_to_cost
and
not
out_nan
for
in_to_out
,
out_to_cost
,
out_nan
in
zip
(
in_to_outs
,
outputs_connected
,
ograd_is_nan
)])
for
in_to_outs
in
connection_pattern
]
if
True
not
in
inputs_connected
:
# All outputs of this op are disconnected so we can skip
# Calling the op's grad method and report that the inputs
# are disconnected
# (The op's grad method could do this too, but this saves the
# implementer the trouble of worrying about this case)
input_grads
=
[
DisconnectedType
()()
for
ipt
in
inputs
]
elif
False
not
in
only_connected_to_nan
:
# All inputs are only connected to nan gradients, so we don't
# need to bother calling the grad method. We know the gradient
# with respect to all connected inputs is nan.
input_grads
=
[]
for
connected
in
inputs_connected
:
if
connected
:
input_grads
.
append
(
NullType
()())
else
:
input_grads
.
append
(
DisconnectedType
()())
else
:
# At least one input of this op is connected to the cost so and
# not all output gradients are undefined so we must
# call the op's grad method
# call the op's grad method
# Each Op's grad function requires inputs and output_grads
# Each Op's grad function requires inputs and output_grads
...
@@ -848,13 +882,6 @@ def _populate_grad_dict(var_to_node_to_idx,
...
@@ -848,13 +882,6 @@ def _populate_grad_dict(var_to_node_to_idx,
if
len
(
input_grads
)
!=
len
(
inputs
):
if
len
(
input_grads
)
!=
len
(
inputs
):
raise
ValueError
((
"
%
s returned the wrong number of"
+
\
raise
ValueError
((
"
%
s returned the wrong number of"
+
\
" gradient terms."
)
%
str
(
node
.
op
))
" gradient terms."
)
%
str
(
node
.
op
))
else
:
# All outputs of this op are disconnected so we can skip
# Calling the op's grad method and report that the inputs
# are disconnected
# (The op's grad method could do this too, but this saves the
# implementer the trouble of worrying about this case)
input_grads
=
[
DisconnectedType
()()
for
ipt
in
inputs
]
# must convert to list in case the op returns a tuple
# must convert to list in case the op returns a tuple
# we won't be able to post-process out the Nones if it does that
# we won't be able to post-process out the Nones if it does that
...
@@ -862,18 +889,15 @@ def _populate_grad_dict(var_to_node_to_idx,
...
@@ -862,18 +889,15 @@ def _populate_grad_dict(var_to_node_to_idx,
# Do type checking on the result
# Do type checking on the result
#List of bools indicating if each output is an integer dtype
output_is_int
=
[
hasattr
(
output
.
type
,
'dtype'
)
and
output
.
type
.
dtype
in
theano
.
tensor
.
discrete_dtypes
for
output
in
node
.
outputs
]
#List of bools indicating if each input only has integer outputs
#
List of bools indicating if each input only has integer outputs
only_connected_to_int
=
[(
True
not
in
only_connected_to_int
=
[(
True
not
in
[
in_to_out
and
out_to_cost
and
not
out_int
[
in_to_out
and
out_to_cost
and
not
out_int
for
in_to_out
,
out_to_cost
,
out_int
in
for
in_to_out
,
out_to_cost
,
out_int
in
zip
(
in_to_outs
,
outputs_connected
,
output_is_int
)])
zip
(
in_to_outs
,
outputs_connected
,
output_is_int
)])
for
in_to_outs
in
connection_pattern
]
for
in_to_outs
in
connection_pattern
]
for
i
,
term
in
enumerate
(
input_grads
):
for
i
,
term
in
enumerate
(
input_grads
):
# Disallow Nones
# Disallow Nones
...
@@ -898,6 +922,10 @@ def _populate_grad_dict(var_to_node_to_idx,
...
@@ -898,6 +922,10 @@ def _populate_grad_dict(var_to_node_to_idx,
' returned an integer-valued variable.'
' returned an integer-valued variable.'
' (Input index
%
d, dtype
%
s)'
%
(
i
,
' (Input index
%
d, dtype
%
s)'
%
(
i
,
term
.
type
.
dtype
))
term
.
type
.
dtype
))
if
only_connected_to_nan
[
i
]:
assert
isinstance
(
term
.
type
,
NullType
)
if
only_connected_to_int
[
i
]:
if
only_connected_to_int
[
i
]:
# This term has only integer outputs and we know
# This term has only integer outputs and we know
# it's not undefined or disconnected
# it's not undefined or disconnected
...
...
theano/tensor/elemwise.py
浏览文件 @
17458063
...
@@ -722,20 +722,19 @@ class Elemwise(Op):
...
@@ -722,20 +722,19 @@ class Elemwise(Op):
def
_bgrad
(
self
,
inputs
,
ograds
):
def
_bgrad
(
self
,
inputs
,
ograds
):
# returns grad, with respect to broadcasted versions of inputs
# returns grad, with respect to broadcasted versions of inputs
# Gradients (especially on the final costs) don't have to be symbolic
# e.g., ograds will be [ 1. ] if your objective is c and the output
# of the current apply node is c
ograds
=
map
(
as_tensor_variable
,
ograds
)
prev_setting
=
theano
.
config
.
compute_test_value
prev_setting
=
theano
.
config
.
compute_test_value
try
:
try
:
theano
.
config
.
compute_test_value
=
'off'
theano
.
config
.
compute_test_value
=
'off'
scalar_inputs
=
[
Scalar
(
dtype
=
t
.
type
.
dtype
)()
for
t
in
inputs
]
def
as_scalar
(
t
):
scalar_ograds
=
[
Scalar
(
dtype
=
ograd
.
type
.
dtype
)()
if
isinstance
(
t
.
type
,
(
NullType
,
DisconnectedType
)):
for
ograd
in
ograds
]
return
t
return
Scalar
(
t
.
type
.
dtype
)()
scalar_inputs
=
map
(
as_scalar
,
inputs
)
scalar_ograds
=
map
(
as_scalar
,
ograds
)
scalar_igrads
=
self
.
scalar_op
.
grad
(
scalar_inputs
,
scalar_ograds
)
scalar_igrads
=
self
.
scalar_op
.
grad
(
scalar_inputs
,
scalar_ograds
)
for
igrad
in
scalar_igrads
:
for
igrad
in
scalar_igrads
:
assert
igrad
is
not
None
assert
igrad
is
not
None
...
...
theano/tests/test_gradient.py
浏览文件 @
17458063
...
@@ -517,5 +517,22 @@ def test_known_grads_integers():
...
@@ -517,5 +517,22 @@ def test_known_grads_integers():
assert
np
.
allclose
(
g_actual
,
gv
)
assert
np
.
allclose
(
g_actual
,
gv
)
def
test_undefined_cost_grad
():
# Tests that if we say the cost is not differentiable via the
# known_grads mechanism, it is treated as such by the rest of the
# system.
x
=
theano
.
tensor
.
iscalar
()
y
=
theano
.
tensor
.
iscalar
()
cost
=
x
+
y
assert
cost
.
dtype
in
theano
.
tensor
.
discrete_dtypes
try
:
grads
=
theano
.
tensor
.
grad
(
cost
,
[
x
,
y
],
known_grads
=
{
cost
:
NullType
()()
})
except
theano
.
gradient
.
NullTypeGradError
:
return
raise
AssertionError
(
"An undefined gradient has been ignored."
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论