Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f9dd5a84
提交
f9dd5a84
authored
4月 21, 2011
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added unimplented_grad method to theano.gradient
上级
3dd4cf97
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
68 行增加
和
4 行删除
+68
-4
gradient.py
theano/gradient.py
+22
-4
raise_op.py
theano/raise_op.py
+36
-0
test_gradient.py
theano/tests/test_gradient.py
+10
-0
没有找到文件。
theano/gradient.py
浏览文件 @
f9dd5a84
"""Driver for general gradient calculations."""
"""Driver for gradient calculations."""
__authors__
=
"James Bergstra"
__copyright__
=
"(c) 2011, Universite de Montreal"
__license__
=
"3-clause BSD License"
__contact__
=
"theano-dev <theano-dev@googlegroups.com>"
__docformat__
=
"restructuredtext en"
__docformat__
=
"restructuredtext en"
import
logging
_logger
=
logging
.
getLogger
(
'theano.gradient'
)
import
sys
import
sys
import
gof
#, gof.variable
import
numpy
#for numeric_grad
import
numpy
#for numeric_grad
import
gof
#, gof.variable
from
gof.python25
import
all
from
gof.python25
import
all
import
gof.utils
import
gof.utils
import
logging
from
raise_op
import
Raise
_logger
=
logging
.
getLogger
(
'theano.gradient'
)
def
warning
(
*
msg
):
def
warning
(
*
msg
):
_logger
.
warning
(
'WARNING theano.gradient: '
+
' '
.
join
(
msg
))
_logger
.
warning
(
'WARNING theano.gradient: '
+
' '
.
join
(
msg
))
def
info
(
*
msg
):
def
info
(
*
msg
):
...
@@ -106,4 +114,14 @@ def grad_sources_inputs(sources, graph_inputs, warn_type=True):
...
@@ -106,4 +114,14 @@ def grad_sources_inputs(sources, graph_inputs, warn_type=True):
gmap
[
r
]
=
g_r
gmap
[
r
]
=
g_r
return
gmap
return
gmap
def
unimplemented_grad
(
op
,
x_pos
,
x
):
"""
Return an un-computable symbolic variable of type `x.type`.
If any function tries to compute this un-computable variable, an exception
(NotImplementedError) will be raised indicating that the gradient on the
`x_pos`'th input of `op` has not been implemented.
"""
msg
=
'
%
s.grad not implemented for input
%
i'
%
(
op
,
x_pos
)
return
Raise
(
msg
=
msg
)(
x
)
theano/raise_op.py
0 → 100644
浏览文件 @
f9dd5a84
"""Symbolic Op for raising an exception."""
__authors__
=
"James Bergstra"
__copyright__
=
"(c) 2011, Universite de Montreal"
__license__
=
"3-clause BSD License"
__contact__
=
"theano-dev <theano-dev@googlegroups.com>"
__docformat__
=
"restructuredtext en"
import
gof
class
Raise
(
gof
.
Op
):
"""Op whose perform() raises an exception.
"""
def
__init__
(
self
,
msg
=
""
,
exc
=
NotImplementedError
):
"""
msg - the argument to the exception
exc - an exception class to raise in self.perform
"""
self
.
msg
=
msg
self
.
exc
=
exc
def
__eq__
(
self
,
other
):
# Note: the msg does not technically have to be in the hash and eq
# because it doesn't affect the return value.
return
(
type
(
self
)
==
type
(
other
)
and
self
.
msg
==
other
.
msg
and
self
.
exc
==
other
.
exc
)
def
__hash__
(
self
):
return
hash
((
type
(
self
),
self
.
msg
,
self
.
exc
))
def
__str__
(
self
):
return
"Raise{
%
s(
%
s)}"
%
(
self
.
exc
,
self
.
msg
)
def
make_node
(
self
,
x
):
return
gof
.
Apply
(
self
,
[
x
],
[
x
.
type
()])
def
perform
(
self
,
node
,
inputs
,
out_storage
):
raise
self
.
exc
(
self
.
msg
)
theano/tests/test_gradient.py
浏览文件 @
f9dd5a84
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
#
#
import
unittest
import
unittest
import
numpy
import
numpy
import
theano
from
theano
import
gof
from
theano
import
gof
from
theano.gradient
import
*
from
theano.gradient
import
*
...
@@ -250,6 +251,15 @@ class test_grad_sources_inputs(unittest.TestCase):
...
@@ -250,6 +251,15 @@ class test_grad_sources_inputs(unittest.TestCase):
self
.
assertTrue
(
g
[
a1
.
inputs
[
0
]]
==
6
)
self
.
assertTrue
(
g
[
a1
.
inputs
[
0
]]
==
6
)
self
.
assertTrue
(
g
[
a1
.
inputs
[
1
]]
==
11
)
self
.
assertTrue
(
g
[
a1
.
inputs
[
1
]]
==
11
)
def
test_unimplemented_grad
():
a
=
theano
.
tensor
.
vector
()
b
=
theano
.
gradient
.
unimplemented_grad
(
theano
.
tensor
.
add
,
1
,
a
)
f
=
theano
.
function
([
a
],
b
)
try
:
f
([
1
,
2
,
3
])
assert
0
except
NotImplementedError
:
pass
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论