Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
19a501bb
提交
19a501bb
authored
12月 27, 2011
作者:
Razvan Pascanu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
code that prepares arguments for the scan op
上级
b9344a3c
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
139 行增加
和
1 行删除
+139
-1
scan.py
theano/sandbox/scan_module/scan.py
+139
-1
没有找到文件。
theano/sandbox/scan_module/scan.py
浏览文件 @
19a501bb
...
@@ -319,7 +319,145 @@ def scan(fn,
...
@@ -319,7 +319,145 @@ def scan(fn,
"""
"""
# Note : see the internal documentation of the scan op for naming
# Note : see the internal documentation of the scan op for naming
# conventions and all other details
# conventions and all other details
us
,
xys_info
,
ws
,
T
=
scan_utils
.
canonical_arguments
(
sequences
,
outputs_info
,
non_sequences
,
go_backwards
,
n_steps
)
# If we provided a known number of steps ( before compilation)
# and if that number is 1 or -1, then we can skip the Scan Op,
# and just apply the inner function once
# To do that we check here to see the nature of n_steps
T_value
=
None
if
isinstance
(
n_steps
,
(
float
,
int
)):
T_value
=
int
(
n_steps
)
else
:
try
:
T_value
=
opt
.
get_constant_value
(
n_steps
)
except
(
TypeError
,
AttributeError
):
T_value
=
None
if
T_value
in
(
1
,
-
1
):
return
one_step_scan
(
fn
,
us
,
xys_info
,
ws
,
T_value
,
truncate_gradient
)
# 1. Variable representing the current time step
t
=
scalar_shared
(
numpy
.
int64
(
0
))
# 2. Allocate memory for the states of scan.
mintaps
=
[]
lengths
=
[]
for
xy
in
xys_info
:
if
xy
.
get
(
'taps'
,
None
)
==
[
-
1
]:
mintaps
.
append
(
1
)
lengths
.
append
(
scalar_shared
(
numpy
.
int64
(
0
)))
xy
[
'initial'
]
=
scan_utils
.
expand
(
tensor
.
unbroadcast
(
tensor
.
shape_padfelt
(
xy
[
'initial'
],
0
),
T
))
elif
xy
.
get
(
'taps'
,
None
):
if
numpy
.
any
(
numpy
.
array
(
xy
.
get
(
'taps'
,
[]))
>
0
):
# Make sure we do not have requests for future values of a
# sequence we can not provide such values
raise
ValueError
(
'Can not use future taps of outputs'
,
init_out
)
mintap
=
abs
(
numpy
.
min
(
xy
[
'taps'
]))
lengths
.
append
(
scalar_shared
(
numpy
.
int64
(
0
)))
mintaps
.
append
(
mintap
)
xy
[
'initial'
]
=
scan_utils
.
expand
(
xy
[
'initial'
][:
mintap
],
T
)
else
:
mintaps
.
append
(
0
)
lengths
.
append
(
scalar_shared
(
numpy
.
int64
(
0
)))
# 3. Generate arguments for the function passed to scan. This will
# function will return the outputs that need to be computed at every
# timesteps
us_slices
=
[
u
[
t
]
for
u
in
us
]
xs_slices
=
[]
for
n
,
xy
in
enumerate
(
xys_info
):
if
mintaps
[
n
]
!=
0
:
for
k
in
init_out
[
'taps'
]:
xs_slices
.
append
(
xy
[
'initial'
][(
t
+
mintaps
[
n
]
-
k
)
%
lengths
[
n
]])
# 4. Construct outputs that are to be computed by the inner
# function of scan
args
=
us_slices
+
xs_slices
+
ws
cond
,
xys_results
,
updates
=
scan_utils
.
get_updates_and_outputs
(
fn
(
*
args
))
if
cond
is
not
None
:
as_while
=
True
else
:
as_while
=
False
# User is allowed to provide no information if it only behaves like a
# map
if
len
(
xys_outputs
)
!=
len
(
xys_info
)
and
len
(
xys_info
)
==
0
:
xys_info
=
[
None
]
*
len
(
xys_outputs
)
# 5. Construct the scan op
# 5.1 Construct list of shared variables with updates (those that
# can be treated as states (i.e. of TensorType) and those that can not
# (like Random States)
rvals
=
rebuild_collect_shared
(
xys_results
+
[
cond
],
updates
=
updates
,
rebuild_strict
=
True
,
copy_inputs_over
=
True
,
no_default_updates
=
False
)
# extracting the arguments
input_variables
,
cloned_outputs
,
other_rval
=
rvals
clone_d
,
update_d
,
update_expr
,
shared_inputs
=
other_rval
additional_xs_outer
=
[]
additional_xs_inner
=
[]
additional_xs_results
=
[]
additional_lengths
=
[]
zs_outer
=
[]
zs_inner
=
[]
zs_results
=
[]
for
sv
in
shared_inputs
:
if
sv
in
update_d
:
if
isinstance
(
sv
,
TensorType
):
# We can treat it as a sit sot
nw_x
=
scan_utils
.
expand
(
tensor
.
unbroadcast
(
tensor
.
shape_padleft
(
sv
,
0
),
actual_n_steps
))
additional_lengths
.
append
(
scalar_shared
(
numpy
.
int64
(
0
)))
additional_xs_outer
.
append
(
nw_x
)
additional_xs_inner
.
append
(
nw_x
.
type
())
additional_xs_results
.
append
(
scan_utils
.
clone
(
tensor
.
set_subtensor
(
nw_x
[(
t
+
1
)
%
additional_lengths
[
-
1
]],
update_d
[
sv
])))
else
:
zs_outer
.
append
(
sv
)
zs_inner
.
append
(
sv
.
type
())
zs_results
.
append
(
update_d
[
sv
])
# 5.2 Collect and order inputs of the inner function
xs_outer
=
[]
xs_results
=
[]
ys_outer
=
[]
ys_results
=
[]
for
n
,
mintap
in
enumerate
(
mintaps
):
if
mintap
!=
0
:
x
=
xys_info
[
n
][
'initial'
]
xs_outer
.
append
(
x
)
xs_results
.
append
(
tensor
.
set_subtensor
(
x
[(
t
+
1
)
%
lengths
[
n
]],
xys_results
[
n
]))
else
:
y
=
scan_utils
.
allocate_memory
(
T
,
xys_info
[
n
],
xys_results
[
n
])
ys_outer
.
append
(
y
)
ys_results
.
append
(
tensor
.
set_subtensor
(
y
[
t
%
lengths
[
n
]],
xys_results
[
n
])
# 5.3 Construct the scan op
def
one_step_scan
(
fn
,
us
,
xys_info
,
ws
,
T
,
truncate_gradient
):
def
one_step_scan
(
fn
,
us
,
xys_info
,
ws
,
T
,
truncate_gradient
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论