Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
244e24ec
提交
244e24ec
authored
4月 06, 2015
作者:
Seon-Wook Park
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Replace CompressOp with .take(.flatnonzero)
上级
a0bf02ba
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
36 行增加
和
79 行删除
+36
-79
extra_ops.py
theano/tensor/extra_ops.py
+2
-48
test_extra_ops.py
theano/tensor/tests/test_extra_ops.py
+34
-31
没有找到文件。
theano/tensor/extra_ops.py
浏览文件 @
244e24ec
...
@@ -511,53 +511,6 @@ def squeeze(x):
...
@@ -511,53 +511,6 @@ def squeeze(x):
return
view
return
view
class
CompressOp
(
theano
.
Op
):
# See the compress function for docstring
def
__init__
(
self
,
axis
=
None
):
self
.
axis
=
axis
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
)
and
self
.
axis
==
other
.
axis
)
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
axis
)
def
make_node
(
self
,
condition
,
x
):
x
=
basic
.
as_tensor_variable
(
x
)
condition
=
basic
.
as_tensor_variable
(
condition
)
if
condition
.
ndim
!=
1
:
raise
TypeError
(
"Conditions cannot have a number of "
"dimension different of 1."
)
return
theano
.
Apply
(
self
,
[
condition
,
x
],
[
x
.
type
()])
def
perform
(
self
,
node
,
inputs
,
output_storage
):
condition
=
inputs
[
0
]
x
=
inputs
[
1
]
z
=
output_storage
[
0
]
z
[
0
]
=
np
.
compress
(
condition
.
astype
(
bool
),
x
,
axis
=
self
.
axis
)
print
z
[
0
]
def
infer_shape
(
self
,
node
,
ins_shapes
):
condition
=
node
.
inputs
[
0
]
n
=
condition
.
ndim
# TODO: Find way to get condition vector shape
if
self
.
axis
is
None
:
out_shape
=
(
n
,)
else
:
out_shape
=
list
(
ins_shapes
[
1
])
out_shape
[
self
.
axis
]
-=
n
out_shape
=
tuple
(
out_shape
)
print
out_shape
return
[
out_shape
]
def
__str__
(
self
):
return
self
.
__class__
.
__name__
def
compress
(
condition
,
x
,
axis
=
None
,
out
=
None
):
def
compress
(
condition
,
x
,
axis
=
None
,
out
=
None
):
"""Return selected slices of an array along given axis.
"""Return selected slices of an array along given axis.
...
@@ -577,7 +530,8 @@ def compress(condition, x, axis=None, out=None):
...
@@ -577,7 +530,8 @@ def compress(condition, x, axis=None, out=None):
"""
"""
# This is done to keep the same function signature then NumPy.
# This is done to keep the same function signature then NumPy.
assert
out
is
None
assert
out
is
None
return
CompressOp
(
axis
=
axis
)(
condition
,
x
)
indices
=
theano
.
tensor
.
basic
.
flatnonzero
(
condition
)
return
x
.
take
(
indices
,
axis
=
axis
)
class
RepeatOp
(
theano
.
Op
):
class
RepeatOp
(
theano
.
Op
):
...
...
theano/tensor/tests/test_extra_ops.py
浏览文件 @
244e24ec
...
@@ -7,8 +7,8 @@ from theano.tests import unittest_tools as utt
...
@@ -7,8 +7,8 @@ from theano.tests import unittest_tools as utt
from
theano.tensor.extra_ops
import
(
CumsumOp
,
cumsum
,
CumprodOp
,
cumprod
,
from
theano.tensor.extra_ops
import
(
CumsumOp
,
cumsum
,
CumprodOp
,
cumprod
,
BinCountOp
,
bincount
,
DiffOp
,
diff
,
BinCountOp
,
bincount
,
DiffOp
,
diff
,
squeeze
,
CompressOp
,
compress
,
squeeze
,
compress
,
RepeatOp
,
repeat
,
RepeatOp
,
repeat
,
Bartlett
,
bartlett
,
Bartlett
,
bartlett
,
FillDiagonal
,
fill_diagonal
,
FillDiagonal
,
fill_diagonal
,
FillDiagonalOffset
,
fill_diagonal_offset
,
FillDiagonalOffset
,
fill_diagonal_offset
,
to_one_hot
)
to_one_hot
)
...
@@ -344,43 +344,46 @@ class SqueezeTester(utt.InferShapeTester):
...
@@ -344,43 +344,46 @@ class SqueezeTester(utt.InferShapeTester):
assert
numpy
.
allclose
(
tested
,
expected
)
assert
numpy
.
allclose
(
tested
,
expected
)
class
TestCompressOp
(
utt
.
InferShapeTester
):
class
CompressTester
(
utt
.
InferShapeTester
):
axis_list
=
[
None
,
0
,
1
]
cond_list
=
[[
1
,
0
,
1
,
0
,
0
,
1
],
[
0
,
1
,
1
,
0
],
[
1
,
1
,
0
,
1
,
0
]]
shape_list
=
[(
2
,
3
),
(
4
,
3
),
(
3
,
5
)]
def
setUp
(
self
):
def
setUp
(
self
):
super
(
TestCompressOp
,
self
)
.
setUp
()
super
(
CompressTester
,
self
)
.
setUp
()
self
.
op_class
=
CompressOp
self
.
op
=
compress
self
.
op
=
CompressOp
()
def
test_compressOp
(
self
):
def
test_op
(
self
):
x
=
T
.
dmatrix
()
for
axis
,
cond
,
shape
in
zip
(
self
.
axis_list
,
self
.
cond_list
,
self
.
shape_list
):
cond
=
T
.
dvector
()
cond_var
=
theano
.
tensor
.
ivector
()
data
=
numpy
.
random
.
random
(
size
=
shape
)
.
astype
(
theano
.
config
.
floatX
)
data_var
=
tensor
.
TensorType
(
theano
.
config
.
floatX
,
[
False
]
*
2
)()
cond_val
=
np
.
array
([
1
,
0
,
1
,
0
],
dtype
=
bool
)
f
=
theano
.
function
([
cond_var
,
data_var
],
self
.
op
(
cond_var
,
data_var
,
axis
=
axis
))
a
=
np
.
random
.
random
((
3
,
4
))
.
astype
(
config
.
floatX
)
f
=
theano
.
function
([
cond
,
x
],
compress
(
cond
,
x
)
)
expected
=
numpy
.
compress
(
cond
,
data
,
axis
=
axis
)
assert
np
.
allclose
(
np
.
compress
(
cond_val
,
a
),
f
(
cond_val
,
a
)
)
tested
=
f
(
cond
,
data
)
for
axis
in
range
(
len
(
a
.
shape
)):
assert
tested
.
shape
==
expected
.
shape
g
=
theano
.
function
([
cond
,
x
],
compress
(
cond
,
x
,
axis
=
axis
))
assert
numpy
.
allclose
(
tested
,
expected
)
assert
np
.
allclose
(
np
.
compress
(
cond_val
,
a
,
axis
=
axis
),
g
(
cond_val
,
a
))
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
x
=
T
.
dmatrix
()
for
axis
,
cond
,
shape
in
zip
(
self
.
axis_list
,
self
.
cond_list
,
self
.
shape_list
):
cond
=
T
.
dvector
()
cond_var
=
theano
.
tensor
.
ivector
()
data
=
numpy
.
random
.
random
(
size
=
shape
)
.
astype
(
theano
.
config
.
floatX
)
cond_val
=
np
.
array
([
1
,
0
,
1
,
0
],
dtype
=
bool
)
data_var
=
tensor
.
TensorType
(
theano
.
config
.
floatX
,
[
False
]
*
2
)()
a
=
np
.
random
.
random
((
3
,
4
))
.
astype
(
config
.
floatX
)
self
.
_compile_and_check
([
cond
,
x
],
[
compress
(
cond
,
x
)],
[
cond_val
,
a
],
self
.
op_class
)
for
axis
in
range
(
len
(
a
.
shape
)):
self
.
_compile_and_check
([
cond_var
,
data_var
],
self
.
_compile_and_check
([
cond
,
x
],
[
self
.
op
(
cond_var
,
data_var
,
axis
=
axis
)
],
[
co
mpress
(
cond
,
x
,
axis
=
axis
)
],
[
co
nd
,
data
],
[
cond_val
,
a
]
,
tensor
.
AdvancedSubtensor1
,
self
.
op_class
)
warn
=
False
)
class
TestRepeatOp
(
utt
.
InferShapeTester
):
class
TestRepeatOp
(
utt
.
InferShapeTester
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论