Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
bf226158
提交
bf226158
authored
6月 10, 2008
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix #158
上级
4fc8b90f
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
25 行增加
和
7 行删除
+25
-7
_test_tensor.py
_test_tensor.py
+13
-4
tensor.py
tensor.py
+12
-3
没有找到文件。
_test_tensor.py
浏览文件 @
bf226158
...
@@ -1692,16 +1692,24 @@ class _test_grad(unittest.TestCase):
...
@@ -1692,16 +1692,24 @@ class _test_grad(unittest.TestCase):
"""grad: Test returning a single None from grad"""
"""grad: Test returning a single None from grad"""
o
=
_test_grad
.
O
()
o
=
_test_grad
.
O
()
a1
=
o
.
make_node
()
a1
=
o
.
make_node
()
self
.
failUnless
(
None
is
grad
(
a1
.
outputs
[
0
],
a1
.
outputs
[
1
]))
g
=
grad
(
a1
.
outputs
[
0
],
a1
.
outputs
[
1
])
self
.
failUnless
(
None
is
grad
(
a1
.
outputs
[
0
],
'wtf'
))
self
.
failUnless
(
isinstance
(
g
,
TensorConstant
))
self
.
failUnless
(
g
.
data
==
0
)
try
:
grad
(
a1
.
outputs
[
0
],
'wtf'
)
except
AttributeError
,
e
:
return
self
.
fail
()
def
test_NNone_rval
(
self
):
def
test_NNone_rval
(
self
):
"""grad: Test returning some Nones from grad"""
"""grad: Test returning some Nones from grad"""
o
=
_test_grad
.
O
()
o
=
_test_grad
.
O
()
a1
=
o
.
make_node
()
a1
=
o
.
make_node
()
g0
,
g1
,
g2
=
grad
(
a1
.
outputs
[
0
],
a1
.
inputs
+
[
'wtf'
])
g0
,
g1
,
g2
=
grad
(
a1
.
outputs
[
0
],
a1
.
inputs
+
[
scalar
(
'z'
)
])
self
.
failUnless
(
o
.
gval0
is
g0
)
self
.
failUnless
(
o
.
gval0
is
g0
)
self
.
failUnless
(
o
.
gval1
is
g1
)
self
.
failUnless
(
o
.
gval1
is
g1
)
self
.
failUnless
(
None
is
g2
)
self
.
failUnless
(
isinstance
(
g2
,
TensorConstant
))
self
.
failUnless
(
g2
.
data
==
0
)
...
@@ -1714,3 +1722,4 @@ if __name__ == '__main__':
...
@@ -1714,3 +1722,4 @@ if __name__ == '__main__':
suite
=
unittest
.
TestLoader
()
suite
=
unittest
.
TestLoader
()
suite
=
suite
.
loadTestsFromTestCase
(
testcase
)
suite
=
suite
.
loadTestsFromTestCase
(
testcase
)
unittest
.
TextTestRunner
(
verbosity
=
2
)
.
run
(
suite
)
unittest
.
TextTestRunner
(
verbosity
=
2
)
.
run
(
suite
)
tensor.py
浏览文件 @
bf226158
...
@@ -1232,14 +1232,23 @@ def grad(cost, wrt, g_cost=None):
...
@@ -1232,14 +1232,23 @@ def grad(cost, wrt, g_cost=None):
@rtype: L{Result} or list of L{Result}s (depending upon I{wrt})
@rtype: L{Result} or list of L{Result}s (depending upon I{wrt})
@return: symbolic expression of gradient of I{cost} with respect to I{wrt}.
@return: symbolic expression of gradient of I{cost} with respect to I{wrt}.
If I{wrt} is a list, then return a list containing the gradient of I{cost} wrt
If I{wrt} is a list, then return a list containing the gradient of I{cost} wrt
each element of the list.
each element of the list. If an element of I{wrt} is not differentiable
with respect to the output, then a L{TensorConstant} with an appropriate
kind of zero is returned.
"""
"""
if
g_cost
is
None
:
if
g_cost
is
None
:
g_cost
=
ones_like
(
cost
)
g_cost
=
ones_like
(
cost
)
inputs
=
gof
.
graph
.
inputs
([
cost
])
inputs
=
gof
.
graph
.
inputs
([
cost
])
gmap
=
gradient
.
grad_sources_inputs
([(
cost
,
g_cost
)],
inputs
)
gmap
=
gradient
.
grad_sources_inputs
([(
cost
,
g_cost
)],
inputs
)
def
zero
(
p
):
return
TensorConstant
(
Tensor
(
dtype
=
p
.
type
.
dtype
,
broadcastable
=
[]),
numpy
.
asarray
(
0
,
dtype
=
p
.
type
.
dtype
))
if
isinstance
(
wrt
,
list
):
if
isinstance
(
wrt
,
list
):
return
[
gmap
.
get
(
p
,
None
)
for
p
in
wrt
]
return
[
gmap
.
get
(
p
,
zero
(
p
)
)
for
p
in
wrt
]
else
:
else
:
return
gmap
.
get
(
wrt
,
None
)
return
gmap
.
get
(
wrt
,
zero
(
wrt
)
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论