Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
5e9bf42a
提交
5e9bf42a
authored
5月 17, 2012
作者:
nouiz
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #646 from pascanur/fix_rop_shared_variables_rebase
Fix rop shared variables rebase
上级
9bd84310
003db6e7
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
150 行增加
和
17 行删除
+150
-17
gradient.py
theano/gradient.py
+12
-1
rng_mrg.py
theano/sandbox/rng_mrg.py
+3
-0
scan_op.py
theano/scan_module/scan_op.py
+51
-14
test_scan.py
theano/scan_module/tests/test_scan.py
+81
-2
raw_random.py
theano/tensor/raw_random.py
+3
-0
没有找到文件。
theano/gradient.py
浏览文件 @
5e9bf42a
...
...
@@ -250,10 +250,12 @@ def Rop(f, wrt, eval_points):
for
pack
in
enumerate
(
zip
(
wrt
,
eval_points
)):
i
=
pack
[
0
]
wrt_elem
,
eval_point
=
pack
[
1
]
if
not
isinstance
(
wrt_elem
,
gof
.
Variable
):
wrt_elem
=
as_tensor_variable
(
wrt_elem
)
if
not
isinstance
(
eval_point
,
gof
.
Variable
):
eval_point
=
as_tensor_variable
(
eval_point
)
try
:
wrt_dim
=
len
(
wrt_elem
.
type
.
broadcastable
)
eval_dim
=
len
(
eval_point
.
type
.
broadcastable
)
...
...
@@ -265,6 +267,10 @@ def Rop(f, wrt, eval_points):
str
(
wrt_dim
)
+
' versus '
+
str
(
eval_dim
))
except
:
# wrt_elem and eval_point can be non-tensor variable which do
# not have broadcastable flags
pass
seen_nodes
=
{}
...
...
@@ -283,7 +289,12 @@ def Rop(f, wrt, eval_points):
if
inp
in
wrt
:
local_eval_points
.
append
(
eval_points
[
wrt
.
index
(
inp
)])
elif
inp
.
owner
is
None
:
try
:
local_eval_points
.
append
(
inp
.
zeros_like
())
except
:
# None should be used for non-differentiable
# arguments, like for example random states
local_eval_points
.
append
(
None
)
elif
inp
.
owner
in
seen_nodes
:
local_eval_points
.
append
(
...
...
theano/sandbox/rng_mrg.py
浏览文件 @
5e9bf42a
...
...
@@ -175,6 +175,9 @@ class mrg_uniform_base(Op):
def
grad
(
self
,
inputs
,
ograd
):
return
[
None
for
i
in
inputs
]
def
R_op
(
self
,
inputs
,
eval_points
):
return
[
None
for
i
in
eval_points
]
class
mrg_uniform
(
mrg_uniform_base
):
#CPU VERSION
...
...
theano/scan_module/scan_op.py
浏览文件 @
5e9bf42a
...
...
@@ -1532,15 +1532,19 @@ class Scan(PureOp):
rval
=
scan_utils
.
reconstruct_graph
(
self
.
inputs
,
self
.
outputs
,
'_rop'
)
self_inputs
=
rval
[
0
]
rop_of_inputs
=
rval
[
0
][:
self
.
n_seqs
+
self
.
n_outs
]
+
\
rval
[
0
][
self
.
n_seqs
+
self
.
n_outs
+
self
.
n_shared_outs
:]
self_outputs
=
rval
[
1
]
# Step 1. Compute the R_op of the inner function
inner_eval_points
=
[
scan_utils
.
safe_new
(
x
,
'_evalpoint'
)
for
x
in
sel
f_inputs
]
for
x
in
rop_o
f_inputs
]
if
self
.
as_while
:
rop_self_outputs
=
self_outputs
[:
-
1
]
else
:
rop_self_outputs
=
self_outputs
rop_outs
=
tensor
.
Rop
(
rop_self_outputs
,
self_inputs
,
inner_eval_points
)
if
self
.
info
[
'n_shared_outs'
]
>
0
:
rop_self_outputs
=
rop_self_outputs
[:
-
self
.
info
[
'n_shared_outs'
]]
rop_outs
=
tensor
.
Rop
(
rop_self_outputs
,
rop_of_inputs
,
inner_eval_points
)
if
type
(
rop_outs
)
not
in
(
list
,
tuple
):
rop_outs
=
[
rop_outs
]
# Step 2. Figure out what corresponds to what in the scan
...
...
@@ -1559,7 +1563,7 @@ class Scan(PureOp):
info
[
'n_sit_sot'
]
=
self
.
n_sit_sot
*
2
info
[
'n_mit_mot'
]
=
self
.
n_mit_mot
*
2
info
[
'n_nit_sot'
]
=
self
.
n_nit_sot
*
2
info
[
'n_shared_outs'
]
=
self
.
n_shared_outs
*
2
info
[
'n_shared_outs'
]
=
self
.
n_shared_outs
info
[
'gpu'
]
=
False
info
[
'as_while'
]
=
self
.
as_while
info
[
'profile'
]
=
self
.
profile
...
...
@@ -1587,7 +1591,14 @@ class Scan(PureOp):
ib
=
0
e
=
1
+
self
.
n_seqs
ie
=
self
.
n_seqs
scan_seqs
=
inputs
[
b
:
e
]
+
eval_points
[
b
:
e
]
clean_eval_points
=
[]
for
inp
,
evp
in
zip
(
inputs
[
b
:
e
],
eval_points
[
b
:
e
]):
if
evp
is
not
None
:
clean_eval_points
.
append
(
evp
)
else
:
clean_eval_points
.
append
(
inp
.
zeros_like
())
scan_seqs
=
inputs
[
b
:
e
]
+
clean_eval_points
inner_seqs
=
self_inputs
[
ib
:
ie
]
+
inner_eval_points
[
ib
:
ie
]
# MIT_MOT sequences ...
...
...
@@ -1596,7 +1607,14 @@ class Scan(PureOp):
ib
=
ie
ie
=
ie
+
int
(
numpy
.
sum
([
len
(
x
)
for
x
in
self
.
tap_array
[:
self
.
n_mit_mot
]]))
scan_mit_mot
=
inputs
[
b
:
e
]
+
eval_points
[
b
:
e
]
clean_eval_points
=
[]
for
inp
,
evp
in
zip
(
inputs
[
b
:
e
],
eval_points
[
b
:
e
]):
if
evp
is
not
None
:
clean_eval_points
.
append
(
evp
)
else
:
clean_eval_points
.
append
(
inp
.
zeros_like
())
scan_mit_mot
=
inputs
[
b
:
e
]
+
clean_eval_points
inner_mit_mot
=
self_inputs
[
ib
:
ie
]
+
inner_eval_points
[
ib
:
ie
]
# MIT_SOT sequences ...
...
...
@@ -1606,6 +1624,13 @@ class Scan(PureOp):
ie
=
ie
+
int
(
numpy
.
sum
([
len
(
x
)
for
x
in
self
.
tap_array
[
self
.
n_mit_mot
:
\
self
.
n_mit_mot
+
self
.
n_mit_sot
]]))
clean_eval_points
=
[]
for
inp
,
evp
in
zip
(
inputs
[
b
:
e
],
eval_points
[
b
:
e
]):
if
evp
is
not
None
:
clean_eval_points
.
append
(
evp
)
else
:
clean_eval_points
.
append
(
inp
.
zeros_like
())
scan_mit_sot
=
inputs
[
b
:
e
]
+
eval_points
[
b
:
e
]
inner_mit_sot
=
self_inputs
[
ib
:
ie
]
+
inner_eval_points
[
ib
:
ie
]
...
...
@@ -1614,7 +1639,14 @@ class Scan(PureOp):
e
=
e
+
self
.
n_sit_sot
ib
=
ie
ie
=
ie
+
self
.
n_sit_sot
scan_sit_sot
=
inputs
[
b
:
e
]
+
eval_points
[
b
:
e
]
clean_eval_points
=
[]
for
inp
,
evp
in
zip
(
inputs
[
b
:
e
],
eval_points
[
b
:
e
]):
if
evp
is
not
None
:
clean_eval_points
.
append
(
evp
)
else
:
clean_eval_points
.
append
(
inp
.
zeros_like
())
scan_sit_sot
=
inputs
[
b
:
e
]
+
clean_eval_points
inner_sit_sot
=
self_inputs
[
ib
:
ie
]
+
inner_eval_points
[
ib
:
ie
]
#Shared outs ...
...
...
@@ -1622,8 +1654,8 @@ class Scan(PureOp):
e
=
e
+
self
.
n_shared_outs
ib
=
ie
ie
=
ie
+
self
.
n_shared_outs
scan_shared
=
inputs
[
b
:
e
]
+
eval_points
[
b
:
e
]
inner_shared
=
self_inputs
[
ib
:
ie
]
+
inner_eval_points
[
ib
:
ie
]
scan_shared
=
inputs
[
b
:
e
]
inner_shared
=
self_inputs
[
ib
:
ie
]
# NIT_SOT sequences
b
=
e
...
...
@@ -1631,8 +1663,15 @@ class Scan(PureOp):
scan_nit_sot
=
inputs
[
b
:
e
]
*
2
# All other arguments
scan_other
=
inputs
[
e
:]
+
eval_points
[
e
:]
inner_other
=
self_inputs
[
ie
:]
+
inner_eval_points
[
ie
:]
clean_eval_points
=
[]
for
inp
,
evp
in
zip
(
inputs
[
e
:],
eval_points
[
e
:]):
if
evp
is
not
None
:
clean_eval_points
.
append
(
evp
)
else
:
clean_eval_points
.
append
(
inp
.
zeros_like
())
scan_other
=
inputs
[
e
:]
+
clean_eval_points
# inner_eval_points do not have entries for shared variables
inner_other
=
self_inputs
[
ie
:]
+
inner_eval_points
[
ib
:]
# Outputs
n_mit_mot_outs
=
int
(
numpy
.
sum
([
len
(
x
)
for
x
in
...
...
@@ -1652,7 +1691,7 @@ class Scan(PureOp):
inner_out_nit_sot
=
self_outputs
[
b
:
e
]
+
rop_outs
[
b
:
e
]
b
=
e
e
=
e
+
self
.
n_shared_outs
inner_out_shared
=
self_outputs
[
b
:
e
]
+
rop_outs
[
b
:
e
]
inner_out_shared
=
self_outputs
[
b
:
e
]
inner_ins
=
(
inner_seqs
+
inner_mit_mot
+
...
...
@@ -1695,9 +1734,7 @@ class Scan(PureOp):
b
=
e
+
self
.
n_nit_sot
e
=
e
+
self
.
n_nit_sot
*
2
final_outs
+=
outputs
[
b
:
e
]
b
=
e
+
self
.
n_shared_outs
e
=
e
+
self
.
n_shared_outs
*
2
final_outs
+=
outputs
[
b
:
e
]
final_outs
+=
[
None
]
*
self
.
n_shared_outs
return
final_outs
...
...
theano/scan_module/tests/test_scan.py
浏览文件 @
5e9bf42a
...
...
@@ -2389,6 +2389,83 @@ class T_Scan(unittest.TestCase):
f2
=
theano
.
function
([],
gx
)
assert
numpy
.
allclose
(
f2
(),
numpy
.
ones
((
10
,)))
def
test_rop2
(
self
):
seed
=
utt
.
fetch_seed
()
rng
=
numpy
.
random
.
RandomState
(
seed
)
floatX
=
theano
.
config
.
floatX
v_u
=
numpy
.
array
(
rng
.
uniform
(
size
=
(
3
,
5
))
-
.
5
,
dtype
=
floatX
)
v_W
=
numpy
.
array
(
rng
.
uniform
(
size
=
(
5
,
5
))
-
.
5
,
dtype
=
floatX
)
v_h0
=
numpy
.
array
(
rng
.
uniform
(
size
=
(
5
,))
-
.
5
,
dtype
=
floatX
)
v_eu
=
numpy
.
array
(
rng
.
uniform
(
size
=
(
3
,
5
))
-
.
5
,
dtype
=
floatX
)
v_eW
=
numpy
.
array
(
rng
.
uniform
(
size
=
(
5
,
5
))
-
.
5
,
dtype
=
floatX
)
v_eh0
=
numpy
.
array
(
rng
.
uniform
(
size
=
(
5
,))
-
.
5
,
dtype
=
floatX
)
def
rnn_fn
(
_u
,
_y
,
_W
):
srng
=
theano
.
tensor
.
shared_randomstreams
.
RandomStreams
(
seed
)
sl_o
=
theano
.
tensor
.
tanh
(
theano
.
tensor
.
dot
(
_W
,
(
_u
+
_y
+
\
srng
.
uniform
(
size
=
v_h0
.
shape
)
*
numpy
.
float32
(
1e-6
))))
return
sl_o
u
=
theano
.
tensor
.
matrix
(
'U'
)
h0
=
theano
.
tensor
.
vector
(
'h0'
)
W
=
theano
.
tensor
.
matrix
(
'W'
)
_u
=
theano
.
tensor
.
specify_shape
(
u
,
v_u
.
shape
)
_u
.
name
=
'_U'
_h0
=
theano
.
tensor
.
specify_shape
(
h0
,
v_h0
.
shape
)
_h0
.
name
=
'_h0'
_W
=
theano
.
tensor
.
specify_shape
(
W
,
v_W
.
shape
)
_W
.
name
=
'_W'
o
,
_
=
theano
.
scan
(
rnn_fn
,
sequences
=
_u
,
outputs_info
=
_h0
,
non_sequences
=
_W
,
name
=
'rnn_fn'
)
o
=
o
[
-
1
]
eu
=
theano
.
tensor
.
matrix
(
'eu'
)
eh0
=
theano
.
tensor
.
vector
(
'eh0'
)
eW
=
theano
.
tensor
.
matrix
(
'eW'
)
nwo_u
=
theano
.
tensor
.
Rop
(
o
,
_u
,
eu
)
nwo_h0
=
theano
.
tensor
.
Rop
(
o
,
_h0
,
eh0
)
nwo_W
=
theano
.
tensor
.
Rop
(
o
,
_W
,
eW
)
fn_rop
=
theano
.
function
([
u
,
h0
,
W
,
eu
,
eh0
,
eW
],
[
nwo_u
,
nwo_h0
,
nwo_W
,
o
],
on_unused_input
=
'ignore'
)
n2o_u
,
_
=
theano
.
scan
(
lambda
i
,
o
,
u
,
h0
,
W
,
eu
:
\
(
theano
.
tensor
.
grad
(
o
[
i
],
u
)
*
eu
)
.
sum
(),
sequences
=
tensor
.
arange
(
o
.
shape
[
0
]),
non_sequences
=
[
o
,
u
,
h0
,
W
,
eu
],
name
=
'jacobU'
)
n2o_h0
,
_
=
theano
.
scan
(
lambda
i
,
o
,
u
,
h0
,
W
,
eh0
:
\
(
theano
.
tensor
.
grad
(
o
[
i
],
h0
)
*
eh0
)
.
sum
(),
sequences
=
tensor
.
arange
(
o
.
shape
[
0
]),
non_sequences
=
[
o
,
u
,
h0
,
W
,
eh0
],
name
=
'jacobh'
)
n2o_W
,
_
=
theano
.
scan
(
lambda
i
,
o
,
u
,
h0
,
W
,
eW
:
\
(
theano
.
tensor
.
grad
(
o
[
i
],
W
)
*
eW
)
.
sum
(),
sequences
=
tensor
.
arange
(
o
.
shape
[
0
]),
non_sequences
=
[
o
,
u
,
h0
,
W
,
eW
],
name
=
'jacobW'
)
fn_test
=
theano
.
function
([
u
,
h0
,
W
,
eu
,
eh0
,
eW
],
[
n2o_u
,
n2o_h0
,
n2o_W
,
o
],
on_unused_input
=
'ignore'
)
vnu
,
vnh0
,
vnW
,
vno
=
fn_rop
(
v_u
,
v_h0
,
v_W
,
v_eu
,
v_eh0
,
v_eW
)
tnu
,
tnh0
,
tnW
,
tno
=
fn_test
(
v_u
,
v_h0
,
v_W
,
v_eu
,
v_eh0
,
v_eW
)
assert
numpy
.
allclose
(
vnu
,
tnu
,
atol
=
1e-6
)
assert
numpy
.
allclose
(
vnh0
,
tnh0
,
atol
=
1e-6
)
assert
numpy
.
allclose
(
vnW
,
tnW
,
atol
=
1e-6
)
def
test_rop
(
self
):
seed
=
utt
.
fetch_seed
()
rng
=
numpy
.
random
.
RandomState
(
seed
)
...
...
@@ -2430,7 +2507,8 @@ class T_Scan(unittest.TestCase):
nwo_h0
=
theano
.
tensor
.
Rop
(
o
,
_h0
,
eh0
)
nwo_W
=
theano
.
tensor
.
Rop
(
o
,
_W
,
eW
)
fn_rop
=
theano
.
function
([
u
,
h0
,
W
,
eu
,
eh0
,
eW
],
[
nwo_u
,
nwo_h0
,
nwo_W
])
[
nwo_u
,
nwo_h0
,
nwo_W
],
on_unused_input
=
'ignore'
)
n2o_u
,
_
=
theano
.
scan
(
lambda
i
,
o
,
u
,
h0
,
W
,
eu
:
\
(
theano
.
tensor
.
grad
(
o
[
i
],
u
)
*
eu
)
.
sum
(),
...
...
@@ -2451,7 +2529,8 @@ class T_Scan(unittest.TestCase):
name
=
'jacobW'
)
fn_test
=
theano
.
function
([
u
,
h0
,
W
,
eu
,
eh0
,
eW
],
[
n2o_u
,
n2o_h0
,
n2o_W
])
[
n2o_u
,
n2o_h0
,
n2o_W
],
on_unused_input
=
'ignore'
)
vnu
,
vnh0
,
vnW
=
fn_rop
(
v_u
,
v_h0
,
v_W
,
v_eu
,
v_eh0
,
v_eW
)
tnu
,
tnh0
,
tnW
=
fn_test
(
v_u
,
v_h0
,
v_W
,
v_eu
,
v_eh0
,
v_eW
)
...
...
theano/tensor/raw_random.py
浏览文件 @
5e9bf42a
...
...
@@ -251,6 +251,9 @@ class RandomFunction(gof.Op):
def
grad
(
self
,
inputs
,
outputs
):
return
[
None
for
i
in
inputs
]
def
R_op
(
self
,
inputs
,
eval_points
):
return
[
None
for
i
in
eval_points
]
def
_infer_ndim_bcast
(
ndim
,
shape
,
*
args
):
"""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论