Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
53756433
提交
53756433
authored
10月 26, 2014
作者:
Sigurd Spieckermann
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modified local_subtensor_make_vector to support advanced indexing
上级
6a6e9914
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
60 行增加
和
43 行删除
+60
-43
opt.py
theano/tensor/opt.py
+60
-43
没有找到文件。
theano/tensor/opt.py
浏览文件 @
53756433
...
...
@@ -28,7 +28,8 @@ from theano.tensor.elemwise import Elemwise, DimShuffle
from
theano.tensor.subtensor
import
(
get_idx_list
,
get_canonical_form_slice
,
Subtensor
,
IncSubtensor
,
make_constant
,
AdvancedIncSubtensor1
,
AdvancedIncSubtensor
)
AdvancedIncSubtensor
,
AdvancedSubtensor1
)
from
theano
import
scalar
from
theano.tensor
import
basic
as
T
from
theano
import
compile
# to register the optimizer built by this file
...
...
@@ -1330,52 +1331,68 @@ def local_track_shape_i(node):
@register_specialize
@register_canonicalize
(
'fast_compile_gpu'
)
@gof.local_optimizer
([
Subtensor
])
@gof.local_optimizer
([
Subtensor
,
AdvancedSubtensor1
])
def
local_subtensor_make_vector
(
node
):
# replace all subtensor(make_vector) like:
# [a,b,c][0] -> a
# [a,b,c][0:2] -> [a,b]
# we can do this for constant indexes
"""
replace all subtensor(make_vector) like:
[a,b,c][0] -> a
[a,b,c][0:2] -> [a,b]
replace all AdvancedSubtensor1(make_vector) like:
[a,b,c][[0,2]] -> [a,c]
we can do this for constant indexes
"""
x
=
node
.
inputs
[
0
]
if
not
x
.
owner
or
x
.
owner
.
op
!=
make_vector
:
return
if
isinstance
(
node
.
op
,
Subtensor
):
# This optimization needs ShapeOpt and fgraph.shape_feature
x
=
node
.
inputs
[
0
]
if
x
.
owner
and
x
.
owner
.
op
==
make_vector
:
try
:
idx
,
=
node
.
op
.
idx_list
except
Exception
:
#'how can you have multiple indexes into a shape?'
raise
if
isinstance
(
idx
,
(
scalar
.
Scalar
,
T
.
TensorType
)):
# The idx is a Scalar, ie a Type. This means the actual index
# is contained in node.inputs[1]
old_idx
,
idx
=
idx
,
node
.
inputs
[
1
]
assert
idx
.
type
==
old_idx
if
isinstance
(
idx
,
(
int
,
numpy
.
integer
)):
return
[
x
.
owner
.
inputs
[
idx
]]
elif
isinstance
(
idx
,
Variable
):
# if it is a constant we can do something with it
try
:
v
=
get_scalar_constant_value
(
idx
)
if
isinstance
(
v
,
numpy
.
integer
):
# Python 2.4 wants to index only with Python integers
v
=
int
(
v
)
return
[
x
.
owner
.
inputs
[
v
]]
except
NotScalarConstantError
:
pass
try
:
idx
,
=
node
.
op
.
idx_list
except
Exception
:
#'how can you have multiple indexes into a shape?'
raise
if
isinstance
(
idx
,
(
scalar
.
Scalar
,
T
.
TensorType
)):
# The idx is a Scalar, ie a Type. This means the actual index
# is contained in node.inputs[1]
old_idx
,
idx
=
idx
,
node
.
inputs
[
1
]
assert
idx
.
type
==
old_idx
elif
isinstance
(
node
.
op
,
AdvancedSubtensor1
):
idx
=
node
.
inputs
[
1
]
else
:
return
if
isinstance
(
idx
,
(
int
,
numpy
.
integer
)):
return
[
x
.
owner
.
inputs
[
idx
]]
elif
isinstance
(
idx
,
Variable
):
# if it is a constant we can do something with it
if
isinstance
(
idx
,
T
.
Constant
):
# make sure we have an ndarray to access the `ndim` attribute
idx
=
numpy
.
asarray
(
idx
.
value
)
if
idx
.
ndim
==
0
:
# Python 2.4 wants to index only with Python integers
return
[
x
.
owner
.
inputs
[
int
(
idx
)]]
elif
idx
.
ndim
==
1
:
values
=
map
(
int
,
list
(
idx
))
return
[
make_vector
(
*
[
x
.
owner
.
inputs
[
v
]
for
v
in
values
])]
else
:
# it is a slice of ints and/or Variables
#TODO: check subtensor to see if it can contain
# constant variables, and if it can, then try to
# unpack them.
try
:
return
[
make_vector
(
*
x
.
owner
.
inputs
.
__getitem__
(
idx
))]
except
TypeError
:
pass
except
Exception
:
_logger
.
error
(
'failed to index with "
%
s"'
%
str
(
idx
))
raise
raise
TypeError
else
:
# it is a slice of ints and/or Variables
#TODO: check subtensor to see if it can contain
# constant variables, and if it can, then try to
# unpack them.
try
:
return
[
make_vector
(
*
x
.
owner
.
inputs
.
__getitem__
(
idx
))]
except
TypeError
:
pass
except
Exception
:
_logger
.
error
(
'failed to index with "
%
s"'
%
str
(
idx
))
raise
#TODO: the other optimization for and, or, xor, le and ge see ticket #496.
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论