Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
7c4d91b4
提交
7c4d91b4
authored
8月 21, 2012
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ENH: BadGradFeature
上级
26dddb57
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
19 行增加
和
50 行删除
+19
-50
gradient.py
theano/gradient.py
+19
-50
没有找到文件。
theano/gradient.py
浏览文件 @
7c4d91b4
...
@@ -194,6 +194,7 @@ def grad_sources_inputs(sources, graph_inputs, warn_type=True):
...
@@ -194,6 +194,7 @@ def grad_sources_inputs(sources, graph_inputs, warn_type=True):
gmap
[
r
]
=
g_r
gmap
[
r
]
=
g_r
return
gmap
return
gmap
class
BadGradOp
(
gof
.
Op
):
class
BadGradOp
(
gof
.
Op
):
"""
"""
An Op representing a gradient that cannot be computed.
An Op representing a gradient that cannot be computed.
...
@@ -239,6 +240,7 @@ class BadGradOp(gof.Op):
...
@@ -239,6 +240,7 @@ class BadGradOp(gof.Op):
def
raise_exc
(
self
):
def
raise_exc
(
self
):
raise
self
.
exc
(
self
.
msg
)
raise
self
.
exc
(
self
.
msg
)
class
GradNotImplementedOp
(
BadGradOp
):
class
GradNotImplementedOp
(
BadGradOp
):
""" A BadGradOp representing a gradient that hasn't been implemented yet.
""" A BadGradOp representing a gradient that hasn't been implemented yet.
"""
"""
...
@@ -261,6 +263,7 @@ class GradNotImplementedOp(BadGradOp):
...
@@ -261,6 +263,7 @@ class GradNotImplementedOp(BadGradOp):
"
%
s does not implement its gradient with respect to input
%
d"
\
"
%
s does not implement its gradient with respect to input
%
d"
\
%
(
str
(
type
(
op
)),
x_pos
))
%
(
str
(
type
(
op
)),
x_pos
))
def
grad_not_implemented
(
op
,
x_pos
,
x
):
def
grad_not_implemented
(
op
,
x_pos
,
x
):
"""
"""
Return an un-computable symbolic variable of type `x.type`.
Return an un-computable symbolic variable of type `x.type`.
...
@@ -274,59 +277,20 @@ def grad_not_implemented(op, x_pos, x):
...
@@ -274,59 +277,20 @@ def grad_not_implemented(op, x_pos, x):
return
GradNotImplementedOp
(
op
,
x_pos
)(
x
)
return
GradNotImplementedOp
(
op
,
x_pos
)(
x
)
def
check_for_bad_grad
(
variables
):
"""
variables: A gof.Variable or list thereof
Raises an exception if any of the variables represents
an expression involving a BadGradOp
"""
#implemented using a deque rather than recursion because python recursion
#limit is set low by default
#handle the case where var is a theano.compile.io.SymbolicOutput
if
hasattr
(
variables
,
'variable'
):
variables
=
[
variables
.
variable
]
if
not
(
isinstance
(
variables
,
list
)
or
\
isinstance
(
variables
,
gof
.
Variable
)):
raise
TypeError
(
"Expected gof.Variable or list thereof, got "
+
\
str
(
type
(
variables
)))
if
not
isinstance
(
variables
,
list
):
variables
=
[
variables
]
vars_to_check
=
deque
(
variables
)
already_checked
=
set
([])
while
True
:
try
:
var
=
vars_to_check
.
pop
()
except
IndexError
:
break
if
var
not
in
already_checked
:
already_checked
.
update
([
var
])
#handle the case where var is a theano.compile.io.SymbolicOutput
def
raise_if_bad_grad
(
node
):
if
hasattr
(
var
,
'variable'
):
if
node
is
not
None
:
var
=
var
.
variable
op
=
node
.
op
if
isinstance
(
op
,
BadGradOp
):
op
.
raise_exc
()
vars_to_check
.
extendleft
(
node
.
inputs
)
if
not
isinstance
(
var
,
gof
.
Variable
):
raise
TypeError
(
"Expected gof.Variable, got "
+
str
(
type
(
var
)))
node
=
var
.
owner
class
BadGradFeature
(
gof
.
Feature
):
def
on_import
(
self
,
fgraph
,
node
):
raise_if_bad_grad
(
node
)
if
node
is
not
None
:
theano
.
compile
.
function_module
.
std_fgraph
.
features
.
append
(
BadGradFeature
)
op
=
node
.
op
if
isinstance
(
op
,
BadGradOp
):
op
.
raise_exc
()
vars_to_check
.
extendleft
(
node
.
inputs
)
#end if node is not None
#end if not already_checked
#end while
########################
########################
...
@@ -648,7 +612,12 @@ def grad(cost, wrt, g_cost=None, consider_constant=None, warn_type=False,
...
@@ -648,7 +612,12 @@ def grad(cost, wrt, g_cost=None, consider_constant=None, warn_type=False,
and
ret
[
-
1
]
.
name
is
None
:
and
ret
[
-
1
]
.
name
is
None
:
ret
[
-
1
]
.
name
=
'(d
%
s/d
%
s)'
%
(
cost
.
name
,
p
.
name
)
ret
[
-
1
]
.
name
=
'(d
%
s/d
%
s)'
%
(
cost
.
name
,
p
.
name
)
check_for_bad_grad
(
ret
)
# new_vars is meant to be a list of all variables created
# by this call to grad(), which will be visible to the caller
# after we return.
new_vars
=
graph
.
ancestors
(
ret
,
blockers
=
graph
.
ancestors
(
cost
)
+
list
(
wrt
))
map
(
raise_if_bad_grad
,
[
v
.
owner
for
v
in
new_vars
])
return
format_as
(
using_list
,
using_tuple
,
ret
)
return
format_as
(
using_list
,
using_tuple
,
ret
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论