Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f0efd2a7
提交
f0efd2a7
authored
1月 20, 2015
作者:
abergeron
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2420 from lamblin/scan_grad_mitsot
Fix gradient of scan when some outputs are disconnected
上级
a72f34bc
f208a0c6
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
78 行增加
和
1 行删除
+78
-1
scan_op.py
theano/scan_module/scan_op.py
+4
-1
test_scan.py
theano/scan_module/tests/test_scan.py
+74
-0
没有找到文件。
theano/scan_module/scan_op.py
浏览文件 @
f0efd2a7
...
@@ -1720,9 +1720,12 @@ class Scan(PureOp):
...
@@ -1720,9 +1720,12 @@ class Scan(PureOp):
offset
=
self
.
n_mit_mot
offset
=
self
.
n_mit_mot
for
idx
in
xrange
(
self
.
n_mit_sot
):
for
idx
in
xrange
(
self
.
n_mit_sot
):
if
isinstance
(
dC_douts
[
idx
+
offset
]
.
type
,
DisconnectedType
):
outer_inp_mitmot
.
append
(
outs
[
idx
+
offset
]
.
zeros_like
())
else
:
outer_inp_mitmot
.
append
(
dC_douts
[
idx
+
offset
][::
-
1
])
mitmot_inp_taps
.
append
([])
mitmot_inp_taps
.
append
([])
mitmot_out_taps
.
append
([])
mitmot_out_taps
.
append
([])
outer_inp_mitmot
.
append
(
dC_douts
[
idx
+
offset
][::
-
1
])
idx_tap
=
idx
+
self
.
n_mit_mot
idx_tap
=
idx
+
self
.
n_mit_mot
inner_inp_mitmot
.
append
(
dC_dXts
[
out_pos
])
inner_inp_mitmot
.
append
(
dC_dXts
[
out_pos
])
out_pos
+=
1
out_pos
+=
1
...
...
theano/scan_module/tests/test_scan.py
浏览文件 @
f0efd2a7
...
@@ -1852,6 +1852,80 @@ class T_Scan(unittest.TestCase):
...
@@ -1852,6 +1852,80 @@ class T_Scan(unittest.TestCase):
analytic_grad
=
reset_rng_grad_fn
(
v_u
,
v_x0
,
vW_in
)
analytic_grad
=
reset_rng_grad_fn
(
v_u
,
v_x0
,
vW_in
)
utt
.
assert_allclose
(
analytic_grad
[
0
][:
2
],
numpy
.
zeros
((
2
,
2
)))
utt
.
assert_allclose
(
analytic_grad
[
0
][:
2
],
numpy
.
zeros
((
2
,
2
)))
def
test_grad_multiple_outs_some_disconnected
(
self
):
# Created on Tue Oct 07 13:28:51 2014
# @author: vaneetke
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
n_hid
=
3
n_in
=
1
n_out
=
1
W_hh_v
=
asarrayX
(
rng
.
uniform
(
size
=
(
n_hid
,
n_hid
),
low
=-.
01
,
high
=.
01
))
h0_v
=
asarrayX
(
rng
.
uniform
(
size
=
(
2
,
n_hid
),
low
=-.
01
,
high
=.
01
))
b_h_v
=
asarrayX
(
rng
.
uniform
(
size
=
(
n_hid
),
low
=-.
01
,
high
=.
01
))
W_ih_v
=
asarrayX
(
rng
.
uniform
(
size
=
(
n_in
,
n_hid
),
low
=-.
01
,
high
=.
01
))
W_ho_v
=
asarrayX
(
rng
.
uniform
(
size
=
(
n_hid
,
n_out
),
low
=-.
01
,
high
=.
01
))
b_o_v
=
asarrayX
(
rng
.
uniform
(
size
=
(
n_out
),
low
=-.
01
,
high
=.
01
))
# parameters of the rnn
b_h
=
theano
.
shared
(
b_h_v
)
h0
=
theano
.
shared
(
h0_v
)
W_ih
=
theano
.
shared
(
W_ih_v
)
W_hh
=
theano
.
shared
(
W_hh_v
)
W_ho
=
theano
.
shared
(
W_ho_v
)
b_o
=
theano
.
shared
(
b_o_v
)
params
=
[
W_ih
,
W_hh
,
b_h
,
W_ho
,
b_o
,
h0
]
# first dimension is time
x
=
tensor
.
matrix
()
# sequences: x_t
# prior results: h_tm2, h_tm1
# non-sequences: W_ih, W_hh, W_ho, b_h
def
one_step
(
x_t
,
h_tm2
,
h_tm1
,
W_ih
,
W_hh
,
b_h
,
W_ho
,
b_o
):
h_t
=
tensor
.
tanh
(
theano
.
dot
(
x_t
,
W_ih
)
+
theano
.
dot
(
h_tm2
,
W_hh
)
+
b_h
)
y_t
=
theano
.
dot
(
h_t
,
W_ho
)
+
b_o
return
[
h_t
,
y_t
]
# hidden and outputs of the entire sequence
[
h
,
y
],
_
=
theano
.
scan
(
fn
=
one_step
,
sequences
=
dict
(
input
=
x
),
# corresponds to the return type of one_step
outputs_info
=
[
dict
(
initial
=
h0
,
taps
=
[
-
2
,
-
1
]),
None
],
non_sequences
=
[
W_ih
,
W_hh
,
b_h
,
W_ho
,
b_o
])
# target values
t
=
tensor
.
matrix
()
# learning rate
lr
=
asarrayX
(
0.1
)
learning_rate
=
theano
.
shared
(
lr
)
cost
=
((
0.5
*
((
y
-
t
)
**
2.0
)
.
mean
())
+
(
0.5
*
(
y
.
std
()
-
t
.
std
())
**
2.0
))
gparams
=
theano
.
grad
(
cost
,
params
)
updates
=
[(
param
,
param
-
gparam
*
learning_rate
)
for
param
,
gparam
in
zip
(
params
,
gparams
)]
learn_rnn_fn
=
theano
.
function
(
inputs
=
[
x
,
t
],
outputs
=
cost
,
updates
=
updates
)
eval_rnn_fn
=
theano
.
function
(
inputs
=
[
x
],
outputs
=
y
)
# artificial data
x_v
=
numpy
.
arange
(
0.
,
100.
,
0.21
,
dtype
=
theano
.
config
.
floatX
)
x_v
=
x_v
.
reshape
(
len
(
x_v
),
1
)
s_v
=
numpy
.
sin
(
x_v
)
t_v
=
numpy
.
roll
(
s_v
,
-
1
)[:
-
1
]
s_v
=
s_v
[:
-
1
]
for
i
in
xrange
(
100
):
cost
=
learn_rnn_fn
(
s_v
,
t_v
)
pred
=
eval_rnn_fn
(
s_v
)
assert
cost
<
0.02
def
test_draw_as_input_to_scan
(
self
):
def
test_draw_as_input_to_scan
(
self
):
trng
=
theano
.
tensor
.
shared_randomstreams
.
RandomStreams
(
123
)
trng
=
theano
.
tensor
.
shared_randomstreams
.
RandomStreams
(
123
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论