Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
6fc0f59a
提交
6fc0f59a
authored
5月 05, 2016
作者:
Frederic Bastien
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix IfElse.grad when true or false condition contain ints. fix gh-4471
上级
200babca
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
24 行增加
和
5 行删除
+24
-5
ifelse.py
theano/ifelse.py
+9
-5
test_ifelse.py
theano/tests/test_ifelse.py
+15
-0
没有找到文件。
theano/ifelse.py
浏览文件 @
6fc0f59a
...
@@ -213,17 +213,21 @@ class IfElse(PureOp):
...
@@ -213,17 +213,21 @@ class IfElse(PureOp):
gpu
=
self
.
gpu
,
gpu
=
self
.
gpu
,
name
=
nw_name_f
)
name
=
nw_name_f
)
if_true
=
([
ins
[
0
]]
+
grads
+
[
theano
.
tensor
.
zeros_like
(
t
)
# The grads can have a different type then the inputs.
for
t
in
ts
])
# As all condition must have the same dtype, we must
if_false
=
([
ins
[
0
]]
+
[
theano
.
tensor
.
zeros_like
(
f
)
dtype
=
grads
[
0
]
.
dtype
for
f
in
fs
]
+
grads
)
if_true
=
([
ins
[
0
]]
+
grads
+
[
theano
.
tensor
.
zeros_like
(
t
,
dtype
=
dtype
)
for
t
in
ts
])
if_false
=
([
ins
[
0
]]
+
[
theano
.
tensor
.
zeros_like
(
f
,
dtype
=
dtype
)
for
f
in
fs
]
+
grads
)
condition
=
ins
[
0
]
condition
=
ins
[
0
]
# condition does affect the elements of the output so it is connected.
# condition does affect the elements of the output so it is connected.
# For the sake of making the gradient convenient we assume that
# For the sake of making the gradient convenient we assume that
# condition + epsilon always triggers the same branch as condition
# condition + epsilon always triggers the same branch as condition
condition_grad
=
condition
.
zeros_like
()
.
astype
(
theano
.
config
.
floatX
)
condition_grad
=
condition
.
zeros_like
()
.
astype
(
theano
.
config
.
floatX
)
return
([
condition_grad
]
+
return
([
condition_grad
]
+
if_true_op
(
*
if_true
,
**
dict
(
return_list
=
True
))
+
if_true_op
(
*
if_true
,
**
dict
(
return_list
=
True
))
+
if_false_op
(
*
if_false
,
**
dict
(
return_list
=
True
)))
if_false_op
(
*
if_false
,
**
dict
(
return_list
=
True
)))
...
...
theano/tests/test_ifelse.py
浏览文件 @
6fc0f59a
...
@@ -482,6 +482,21 @@ class test_ifelse(unittest.TestCase, utt.TestOptimizationMixin):
...
@@ -482,6 +482,21 @@ class test_ifelse(unittest.TestCase, utt.TestOptimizationMixin):
finally
:
finally
:
theano
.
config
.
compute_test_value
=
backup
theano
.
config
.
compute_test_value
=
backup
def
test_grad_int_value
(
self
):
w
=
theano
.
shared
(
numpy
.
random
.
rand
(
10
))
b
=
theano
.
shared
(
numpy
.
random
.
rand
())
params
=
[
w
,
b
]
x
=
tensor
.
vector
()
y
=
tensor
.
scalar
()
score
=
w
.
dot
(
x
)
+
b
correct
=
(
score
*
y
>
0
)
loss
=
ifelse
(
correct
,
0
,
1
)
updates
=
[(
param
,
param
-
0.5
*
tensor
.
grad
(
cost
=
loss
,
wrt
=
param
))
for
param
in
params
]
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
print
(
' Use nosetests to run these tests '
)
print
(
' Use nosetests to run these tests '
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论