Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
4336f227
提交
4336f227
authored
3月 13, 2008
作者:
bergstrj@iro.umontreal.ca
浏览文件
操作
浏览文件
下载
差异文件
gradient.py written and tested
上级
30d14420
c1d9d010
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
41 行增加
和
33 行删除
+41
-33
_test_gradient.py
_test_gradient.py
+0
-0
gradient.py
gradient.py
+41
-33
没有找到文件。
_test_gradient.py
浏览文件 @
4336f227
差异被折叠。
点击展开。
gradient.py
浏览文件 @
4336f227
import
gof
import
gof
,
gof
.
result
_msg_retNone
=
'op.grad(...) returned None, consider returning [None]'
_msg_badlen
=
'op.grad(...) returned wrong number of gradients'
def
_unpack_result
(
lst
):
def
_unpack_result
(
lst
):
if
len
(
lst
)
>
1
:
if
len
(
lst
)
>
1
:
return
lst
return
lst
else
else
:
return
lst
[
0
]
return
lst
[
0
]
def
_pack_result
(
arg
):
def
_pack_result
(
arg
):
if
gof
.
result
.
is_result
(
arg
):
return
[
arg
]
if
isinstance
(
arg
,
gof
.
result
.
ResultBase
):
return
arg
return
[
arg
]
else
:
return
arg
def
grad_sources_inputs
(
sources
,
inputs
):
def
grad_sources_inputs
(
sources
,
graph_
inputs
):
"""Return a dictionary mapping each result necessary for a source to its gradient
"""Return a dictionary mapping each result necessary for a source to its gradient
sources - a list of gradient sources (explained below)
sources - a list of gradient sources (explained below)
inputs - a list of results considered to be constant
graph_
inputs - a list of results considered to be constant
A gradient source is a pair (r, g_r), in which r is a result, and g_r is a
A gradient source is a pair (r, g_r), in which r is a result, and g_r is a
result that is a gradient wrt r.
result that is a gradient wrt r.
...
@@ -49,33 +54,37 @@ def grad_sources_inputs(sources, inputs):
...
@@ -49,33 +54,37 @@ def grad_sources_inputs(sources, inputs):
None instead of a result instance.
None instead of a result instance.
"""
"""
gmap
=
{}
gmap
=
{}
for
(
r
,
g_r
)
in
self
.
sources
:
for
(
r
,
g_r
)
in
sources
:
if
r
in
gmap
:
if
g_r
is
not
None
:
gmap
[
r
]
=
gmap
[
r
]
+
dr
if
r
in
gmap
:
else
:
gmap
[
r
]
=
gmap
[
r
]
+
g_r
gmap
[
r
]
=
dr
else
:
gmap
[
r
]
=
g_r
outputs
=
gmap
.
keys
()
graph_outputs
=
gmap
.
keys
()
if
inputs
is
None
:
if
graph_
inputs
is
None
:
inputs
=
gof
.
graph
.
inputs
(
outputs
)
graph_inputs
=
gof
.
graph
.
inputs
(
graph_
outputs
)
for
op
in
gof
.
graph
.
io_toposort
(
inputs
,
outputs
)
.
__reversed__
():
for
op
in
gof
.
graph
.
io_toposort
(
graph_inputs
,
graph_outputs
)
.
__reversed__
():
g_outputs
=
[
gmap
[
o
]
for
o
in
self
.
outputs
]
g_outputs
=
[
gmap
.
get
(
o
,
None
)
for
o
in
op
.
outputs
]
if
all
(
map
(
lambda
x
:
x
is
None
,
g_outputs
)):
continue
#if all output gradients are None, continue
output_arg
=
unpack_singleton
(
g_outputs
)
if
all
(
map
(
lambda
x
:
x
is
None
,
g_outputs
)):
continue
input_arg
=
unpack_singleton
(
op
.
inputs
)
output_arg
=
_unpack_result
(
g_outputs
)
input_arg
=
_unpack_result
(
op
.
inputs
)
op_grad
=
op
.
grad
(
input_arg
,
output_arg
)
op_grad
=
op
.
grad
(
input_arg
,
output_arg
)
if
op_grad
is
None
:
if
op_grad
is
None
:
raise
Exception
(
'If you really mean for grad(...) to return None,
raise
ValueError
(
_msg_retNone
,
op
.
__class__
)
please return [None]'
,
op
.
__class__
)
g_inputs
=
_pack_result
(
op_grad
)
g_inputs
=
pack_singleton
(
op_grad
)
if
len
(
g_inputs
)
!=
len
(
op
.
inputs
):
assert
len
(
g_inputs
)
==
len
(
op
.
inputs
)
raise
ValueError
(
_msg_badlen
,
op
.
__class__
,
for
r
,
g_r
in
zip
(
self
.
inputs
,
g_inputs
):
len
(
g_inputs
),
len
(
op
.
inputs
))
for
r
,
g_r
in
zip
(
op
.
inputs
,
g_inputs
):
if
g_r
is
not
None
:
if
g_r
is
not
None
:
if
r
in
gmap
:
if
r
in
gmap
:
gmap
[
r
]
=
gmap
[
r
]
+
g_r
gmap
[
r
]
=
gmap
[
r
]
+
g_r
...
@@ -83,17 +92,16 @@ def grad_sources_inputs(sources, inputs):
...
@@ -83,17 +92,16 @@ def grad_sources_inputs(sources, inputs):
gmap
[
r
]
=
g_r
gmap
[
r
]
=
g_r
return
gmap
return
gmap
def
diff
(
cost
,
param
):
def
grad
(
cost
,
param
):
"""Return symbolic expression of gradient of <cost> wrt <param>.
"""Return symbolic expression of gradient of <cost> wrt <param>.
If <param> is a list, then return a list containing the gradient of cost wrt
If <param> is a list, then return a list containing the gradient of cost wrt
each element of the list.
each element of the list.
"""
"""
inputs
=
gof
.
graph
.
inputs
([
cost
])
inputs
=
gof
.
graph
.
inputs
([
cost
])
gmap
=
grad_sources_inputs
([(
cost
,
1.0
)],
inputs
)
gmap
=
grad_sources_inputs
([(
cost
,
1.0
)],
inputs
)
if
isinstance
(
param
,
lst
):
if
isinstance
(
param
,
l
i
st
):
return
[
gmap
[
p
]
for
p
in
param
]
return
[
gmap
.
get
(
p
,
None
)
for
p
in
param
]
else
:
else
:
return
gmap
[
param
]
return
gmap
.
get
(
param
,
None
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论