Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
763586fa
提交
763586fa
authored
9月 16, 2015
作者:
carriepl
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add method compute_all_gradients
上级
9bdef90d
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
63 行增加
和
1 行删除
+63
-1
scan_op.py
theano/scan_module/scan_op.py
+63
-1
没有找到文件。
theano/scan_module/scan_op.py
浏览文件 @
763586fa
...
@@ -1552,7 +1552,7 @@ class Scan(PureOp):
...
@@ -1552,7 +1552,7 @@ class Scan(PureOp):
return
connection_pattern
return
connection_pattern
def
get_oinp_iinp_iout_oout_mappings
(
self
):
def
get_oinp_iinp_iout_oout_mappings
(
self
):
"""
"""
Compute and return dictionary mappings between the inputs and
Compute and return dictionary mappings between the inputs and
outputs of the inner function and the inputs and outputs of the Scan
outputs of the inner function and the inputs and outputs of the Scan
node in the outer graph.
node in the outer graph.
...
@@ -1783,6 +1783,42 @@ class Scan(PureOp):
...
@@ -1783,6 +1783,42 @@ class Scan(PureOp):
iidx
-=
len
(
taps
)
iidx
-=
len
(
taps
)
return
oidx
+
iidx
return
oidx
+
iidx
def
compute_all_gradients
(
known_grads
):
y_s
=
known_grads
.
keys
()
g_y_s
=
known_grads
.
values
()
for
g_y
in
g_y_s
:
if
'int'
in
str
(
g_y
.
dtype
):
raise
TypeError
(
"Gradients may never be integers but g_y "
"has type "
+
str
(
g_y
.
type
))
out_indices
=
[
get_out_idx
(
self_outputs
.
index
(
y
))
for
y
in
y_s
]
connected_inputs
=
[
i
for
i
in
range
(
len
(
scan_node
.
inputs
))
if
any
([
connection_pattern
[
i
][
odx
]
for
odx
in
out_indices
])]
wrt
=
[
x
for
x
in
theano
.
gof
.
graph
.
inputs
(
y_s
)
if
(
x
in
diff_inputs
)
and
get_inp_idx
(
self_inputs
.
index
(
x
))
in
connected_inputs
]
gmp
=
OrderedDict
()
known_grads
=
dict
([(
k
*
1
,
v
)
for
(
k
,
v
)
in
known_grads
.
items
()])
grads
=
gradient
.
grad
(
cost
=
None
,
known_grads
=
known_grads
,
wrt
=
wrt
,
consider_constant
=
wrt
,
disconnected_inputs
=
'ignore'
,
return_disconnected
=
'None'
,
null_gradients
=
'return'
)
for
i
in
range
(
len
(
wrt
)):
gmp
[
wrt
[
i
]]
=
grads
[
i
]
rval
=
[
gmp
.
get
(
p
,
None
)
for
p
in
diff_inputs
]
return
rval
def
compute_gradient
(
y
,
g_y
):
def
compute_gradient
(
y
,
g_y
):
if
'int'
in
str
(
g_y
.
dtype
):
if
'int'
in
str
(
g_y
.
dtype
):
raise
TypeError
(
"Gradients may never be integers but g_y "
raise
TypeError
(
"Gradients may never be integers but g_y "
...
@@ -1871,6 +1907,7 @@ class Scan(PureOp):
...
@@ -1871,6 +1907,7 @@ class Scan(PureOp):
continue
continue
dC_dXt
=
safe_new
(
dC_douts
[
idx
][
0
])
dC_dXt
=
safe_new
(
dC_douts
[
idx
][
0
])
dC_dXts
.
append
(
dC_dXt
)
dC_dXts
.
append
(
dC_dXt
)
"""
_dC_dinps_t = compute_gradient(Xt, dC_dXt)
_dC_dinps_t = compute_gradient(Xt, dC_dXt)
for jdx in xrange(len(_dC_dinps_t)):
for jdx in xrange(len(_dC_dinps_t)):
if dC_dinps_t[jdx] is None:
if dC_dinps_t[jdx] is None:
...
@@ -1885,6 +1922,31 @@ class Scan(PureOp):
...
@@ -1885,6 +1922,31 @@ class Scan(PureOp):
dC_dinps_t[jdx] = _dC_dinps_t[jdx]
dC_dinps_t[jdx] = _dC_dinps_t[jdx]
else:
else:
dC_dinps_t[jdx] += _dC_dinps_t[jdx]
dC_dinps_t[jdx] += _dC_dinps_t[jdx]
"""
known_grads
=
{}
dc_dxts_idx
=
0
for
i
in
range
(
len
(
diff_outputs
)):
if
i
<
idx_nitsot_start
or
i
>=
idx_nitsot_end
:
if
diff_outputs
[
i
]
in
known_grads
:
known_grads
[
diff_outputs
[
i
]]
+=
dC_dXts
[
dc_dxts_idx
]
else
:
known_grads
[
diff_outputs
[
i
]]
=
dC_dXts
[
dc_dxts_idx
]
dc_dxts_idx
+=
1
else
:
if
isinstance
(
dC_douts
[
i
]
.
type
,
DisconnectedType
):
c_dxts_idx
+=
1
continue
else
:
if
diff_outputs
[
i
]
in
known_grads
:
known_grads
[
diff_outputs
[
i
]]
+=
dC_dXts
[
dc_dxts_idx
]
else
:
known_grads
[
diff_outputs
[
i
]]
=
dC_dXts
[
dc_dxts_idx
]
dc_dxts_idx
+=
1
dC_dinps_t
=
compute_all_gradients
(
known_grads
)
# mask inputs that get no gradients
# mask inputs that get no gradients
for
dx
in
xrange
(
len
(
dC_dinps_t
)):
for
dx
in
xrange
(
len
(
dC_dinps_t
)):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论