Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
026e8116
提交
026e8116
authored
6月 15, 2012
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
remove "import *"
上级
158c4b90
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
53 行增加
和
52 行删除
+53
-52
test_elemwise.py
theano/tensor/tests/test_elemwise.py
+53
-52
没有找到文件。
theano/tensor/tests/test_elemwise.py
浏览文件 @
026e8116
import
cPickle
,
time
,
unittest
from
itertools
import
imap
from
copy
import
copy
import
numpy
from
numpy.testing
import
dec
import
theano
from
theano.gof
import
Variable
,
Op
from
theano
import
gof
from
theano.scalar
import
*
from
theano
import
gof
,
scalar
,
config
from
theano
import
tensor
from
theano.tensor
import
TensorType
from
theano.compile.mode
import
get_default_mode
from
theano.tensor.elemwise
import
*
from
theano.tensor.elemwise
import
CAReduce
,
Elemwise
,
DimShuffle
,
Prod
,
ProdWithoutZeros
from
theano.tests
import
unittest_tools
...
...
@@ -83,7 +85,7 @@ class test_Broadcast(unittest.TestCase):
((),
())]:
x
=
TensorType
(
'float64'
,
[(
entry
==
1
)
for
entry
in
xsh
])(
'x'
)
y
=
TensorType
(
'float64'
,
[(
entry
==
1
)
for
entry
in
ysh
])(
'y'
)
e
=
Elemwise
(
add
)(
x
,
y
)
e
=
Elemwise
(
scalar
.
add
)(
x
,
y
)
f
=
copy
(
linker
)
.
accept
(
Env
([
x
,
y
],
[
e
]))
.
make_function
()
xv
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
*
xsh
))
yv
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
*
ysh
))
...
...
@@ -96,7 +98,7 @@ class test_Broadcast(unittest.TestCase):
if
isinstance
(
linker
,
gof
.
PerformLinker
):
x
=
TensorType
(
'float64'
,
[(
entry
==
1
)
for
entry
in
xsh
])(
'x'
)
y
=
TensorType
(
'float64'
,
[(
entry
==
1
)
for
entry
in
ysh
])(
'y'
)
e
=
Elemwise
(
add
)(
x
,
y
)
e
=
Elemwise
(
scalar
.
add
)(
x
,
y
)
f
=
copy
(
linker
)
.
accept
(
Env
([
x
,
y
],
[
e
.
shape
]))
.
make_function
()
assert
tuple
(
f
(
xv
,
yv
))
==
tuple
(
zv
.
shape
)
...
...
@@ -111,7 +113,7 @@ class test_Broadcast(unittest.TestCase):
((),
())]:
x
=
TensorType
(
'float64'
,
[(
entry
==
1
)
for
entry
in
xsh
])(
'x'
)
y
=
TensorType
(
'float64'
,
[(
entry
==
1
)
for
entry
in
ysh
])(
'y'
)
e
=
Elemwise
(
Add
(
transfer_type
(
0
)),
{
0
:
0
})(
x
,
y
)
e
=
Elemwise
(
scalar
.
Add
(
scalar
.
transfer_type
(
0
)),
{
0
:
0
})(
x
,
y
)
f
=
copy
(
linker
)
.
accept
(
Env
([
x
,
y
],
[
e
]))
.
make_function
()
xv
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
*
xsh
))
yv
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
*
ysh
))
...
...
@@ -125,7 +127,7 @@ class test_Broadcast(unittest.TestCase):
if
isinstance
(
linker
,
gof
.
PerformLinker
):
x
=
TensorType
(
'float64'
,
[(
entry
==
1
)
for
entry
in
xsh
])(
'x'
)
y
=
TensorType
(
'float64'
,
[(
entry
==
1
)
for
entry
in
ysh
])(
'y'
)
e
=
Elemwise
(
Add
(
transfer_type
(
0
)),
{
0
:
0
})(
x
,
y
)
e
=
Elemwise
(
scalar
.
Add
(
scalar
.
transfer_type
(
0
)),
{
0
:
0
})(
x
,
y
)
f
=
copy
(
linker
)
.
accept
(
Env
([
x
,
y
],
[
e
.
shape
]))
.
make_function
()
xv
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
*
xsh
))
yv
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
*
ysh
))
...
...
@@ -150,7 +152,7 @@ class test_Broadcast(unittest.TestCase):
def
test_fill
(
self
):
x
=
TensorType
(
'float64'
,
[
0
,
0
])(
'x'
)
y
=
TensorType
(
'float64'
,
[
1
,
1
])(
'y'
)
e
=
Elemwise
(
Second
(
transfer_type
(
0
)),
{
0
:
0
})(
x
,
y
)
e
=
Elemwise
(
scalar
.
Second
(
scalar
.
transfer_type
(
0
)),
{
0
:
0
})(
x
,
y
)
f
=
gof
.
CLinker
()
.
accept
(
Env
([
x
,
y
],
[
e
]))
.
make_function
()
xv
=
numpy
.
ones
((
5
,
5
))
yv
=
numpy
.
random
.
rand
(
1
,
1
)
...
...
@@ -160,7 +162,7 @@ class test_Broadcast(unittest.TestCase):
def
test_weird_strides
(
self
):
x
=
TensorType
(
'float64'
,
[
0
,
0
,
0
,
0
,
0
])(
'x'
)
y
=
TensorType
(
'float64'
,
[
0
,
0
,
0
,
0
,
0
])(
'y'
)
e
=
Elemwise
(
add
)(
x
,
y
)
e
=
Elemwise
(
scalar
.
add
)(
x
,
y
)
f
=
gof
.
CLinker
()
.
accept
(
Env
([
x
,
y
],
[
e
]))
.
make_function
()
xv
=
numpy
.
random
.
rand
(
2
,
2
,
2
,
2
,
2
)
yv
=
numpy
.
random
.
rand
(
2
,
2
,
2
,
2
,
2
)
.
transpose
(
4
,
0
,
3
,
1
,
2
)
...
...
@@ -169,7 +171,7 @@ class test_Broadcast(unittest.TestCase):
def
test_same_inputs
(
self
):
x
=
TensorType
(
'float64'
,
[
0
,
0
])(
'x'
)
e
=
Elemwise
(
add
)(
x
,
x
)
e
=
Elemwise
(
scalar
.
add
)(
x
,
x
)
f
=
gof
.
CLinker
()
.
accept
(
Env
([
x
],
[
e
]))
.
make_function
()
xv
=
numpy
.
random
.
rand
(
2
,
2
)
zv
=
xv
+
xv
...
...
@@ -180,7 +182,7 @@ class test_CAReduce(unittest.TestCase):
def
setUp
(
self
):
unittest_tools
.
seed_rng
()
def
with_linker
(
self
,
linker
,
scalar_op
=
add
,
dtype
=
"floatX"
,
def
with_linker
(
self
,
linker
,
scalar_op
=
scalar
.
add
,
dtype
=
"floatX"
,
test_nan
=
False
,
tensor_op
=
None
):
for
xsh
,
tosum
in
[((
5
,
6
),
None
),
((
5
,
6
),
(
0
,
1
)),
...
...
@@ -241,31 +243,31 @@ class test_CAReduce(unittest.TestCase):
zv
=
numpy
.
any
(
zv
,
axis
)
if
len
(
tosum
)
==
0
:
zv
=
zv
!=
0
elif
scalar_op
==
add
:
elif
scalar_op
==
scalar
.
add
:
for
axis
in
reversed
(
sorted
(
tosum
)):
zv
=
numpy
.
add
.
reduce
(
zv
,
axis
)
elif
scalar_op
==
mul
:
elif
scalar_op
==
scalar
.
mul
:
for
axis
in
reversed
(
sorted
(
tosum
)):
zv
=
numpy
.
multiply
.
reduce
(
zv
,
axis
)
elif
scalar_op
==
maximum
:
elif
scalar_op
==
scalar
.
maximum
:
try
:
for
axis
in
reversed
(
sorted
(
tosum
)):
zv
=
numpy
.
maximum
.
reduce
(
zv
,
axis
)
except
ValueError
:
numpy_raised
=
True
elif
scalar_op
==
minimum
:
elif
scalar_op
==
scalar
.
minimum
:
try
:
for
axis
in
reversed
(
sorted
(
tosum
)):
zv
=
numpy
.
minimum
.
reduce
(
zv
,
axis
)
except
ValueError
:
numpy_raised
=
True
elif
scalar_op
==
or_
:
elif
scalar_op
==
scalar
.
or_
:
for
axis
in
reversed
(
sorted
(
tosum
)):
zv
=
numpy
.
bitwise_or
.
reduce
(
zv
,
axis
)
elif
scalar_op
==
and_
:
elif
scalar_op
==
scalar
.
and_
:
for
axis
in
reversed
(
sorted
(
tosum
)):
zv
=
numpy
.
bitwise_and
.
reduce
(
zv
,
axis
)
elif
scalar_op
==
xor
:
elif
scalar_op
==
scalar
.
xor
:
# There is no identity value for the xor function
# So we can't support shape of dimensions 0.
if
numpy
.
prod
(
zv
.
shape
)
==
0
:
...
...
@@ -274,7 +276,7 @@ class test_CAReduce(unittest.TestCase):
zv
=
numpy
.
bitwise_xor
.
reduce
(
zv
,
axis
)
else
:
raise
Exception
(
"Test for CAReduce with scalar_op
%
s not implemented"
%
str
(
scalar_op
))
if
scalar_op
in
[
maximum
,
minimum
]
and
numpy_raised
:
if
scalar_op
in
[
scalar
.
maximum
,
scalar
.
minimum
]
and
numpy_raised
:
try
:
out
=
f
(
xv
)
assert
out
.
dtype
==
dtype
...
...
@@ -284,7 +286,7 @@ class test_CAReduce(unittest.TestCase):
self
.
fail
()
else
:
#numpy.{all,any} return bool type.
if
scalar_op
in
[
and_
,
or_
]:
if
scalar_op
in
[
scalar
.
and_
,
scalar
.
or_
]:
zv
=
numpy
.
asarray
(
zv
,
dtype
=
dtype
)
if
test_nan
:
self
.
assertTrue
(
theano
.
tensor
.
TensorType
.
values_eq
(
f
(
xv
),
zv
),
(
f
(
xv
),
zv
))
...
...
@@ -302,40 +304,40 @@ class test_CAReduce(unittest.TestCase):
e
=
tensor_op
(
x
,
axis
=
tosum
)
if
tosum
is
None
:
tosum
=
range
(
len
(
xsh
))
f
=
copy
(
linker
)
.
accept
(
Env
([
x
],
[
e
.
shape
]))
.
make_function
()
if
not
(
scalar_op
in
[
maximum
,
minimum
]
and
((
xsh
==
()
or
numpy
.
prod
(
xsh
)
==
0
))):
assert
all
(
f
(
xv
)
==
zv
.
shape
)
if
not
(
scalar_op
in
[
scalar
.
maximum
,
scalar
.
minimum
]
and
((
xsh
==
()
or
numpy
.
prod
(
xsh
)
==
0
))):
assert
all
(
f
(
xv
)
==
zv
.
shape
)
def
test_perform
(
self
):
for
dtype
in
[
"floatX"
,
"complex64"
,
"complex128"
,
"int8"
,
"uint8"
]:
self
.
with_linker
(
gof
.
PerformLinker
(),
add
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
PerformLinker
(),
mul
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
PerformLinker
(),
maximum
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
PerformLinker
(),
minimum
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
PerformLinker
(),
and_
,
dtype
=
dtype
,
self
.
with_linker
(
gof
.
PerformLinker
(),
scalar
.
add
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
PerformLinker
(),
scalar
.
mul
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
PerformLinker
(),
scalar
.
maximum
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
PerformLinker
(),
scalar
.
minimum
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
PerformLinker
(),
scalar
.
and_
,
dtype
=
dtype
,
tensor_op
=
tensor
.
all
)
self
.
with_linker
(
gof
.
PerformLinker
(),
or_
,
dtype
=
dtype
,
self
.
with_linker
(
gof
.
PerformLinker
(),
scalar
.
or_
,
dtype
=
dtype
,
tensor_op
=
tensor
.
any
)
for
dtype
in
[
"int8"
,
"uint8"
]:
self
.
with_linker
(
gof
.
PerformLinker
(),
or_
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
PerformLinker
(),
and_
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
PerformLinker
(),
xor
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
PerformLinker
(),
scalar
.
or_
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
PerformLinker
(),
scalar
.
and_
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
PerformLinker
(),
scalar
.
xor
,
dtype
=
dtype
)
@dec.knownfailureif
(
True
,
(
"When there is nan in the input of CAReduce, we don't have a good output. "
))
def
test_perform_nan
(
self
):
for
dtype
in
[
"floatX"
,
"complex64"
,
"complex128"
]:
self
.
with_linker
(
gof
.
PerformLinker
(),
add
,
dtype
=
dtype
,
self
.
with_linker
(
gof
.
PerformLinker
(),
scalar
.
add
,
dtype
=
dtype
,
test_nan
=
True
)
self
.
with_linker
(
gof
.
PerformLinker
(),
mul
,
dtype
=
dtype
,
self
.
with_linker
(
gof
.
PerformLinker
(),
scalar
.
mul
,
dtype
=
dtype
,
test_nan
=
True
)
self
.
with_linker
(
gof
.
PerformLinker
(),
maximum
,
dtype
=
dtype
,
self
.
with_linker
(
gof
.
PerformLinker
(),
scalar
.
maximum
,
dtype
=
dtype
,
test_nan
=
True
)
self
.
with_linker
(
gof
.
PerformLinker
(),
minimum
,
dtype
=
dtype
,
self
.
with_linker
(
gof
.
PerformLinker
(),
scalar
.
minimum
,
dtype
=
dtype
,
test_nan
=
True
)
self
.
with_linker
(
gof
.
PerformLinker
(),
or_
,
dtype
=
dtype
,
self
.
with_linker
(
gof
.
PerformLinker
(),
scalar
.
or_
,
dtype
=
dtype
,
test_nan
=
True
)
self
.
with_linker
(
gof
.
PerformLinker
(),
and_
,
dtype
=
dtype
,
self
.
with_linker
(
gof
.
PerformLinker
(),
scalar
.
and_
,
dtype
=
dtype
,
test_nan
=
True
)
self
.
with_linker
(
gof
.
PerformLinker
(),
or_
,
dtype
=
dtype
,
test_nan
=
True
,
tensor_op
=
tensor
.
any
)
...
...
@@ -344,34 +346,33 @@ class test_CAReduce(unittest.TestCase):
def
test_c
(
self
):
for
dtype
in
[
"floatX"
,
"complex64"
,
"complex128"
,
"int8"
,
"uint8"
]:
self
.
with_linker
(
gof
.
CLinker
(),
add
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
CLinker
(),
mul
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
CLinker
(),
scalar
.
add
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
CLinker
(),
scalar
.
mul
,
dtype
=
dtype
)
for
dtype
in
[
"floatX"
,
"int8"
,
"uint8"
]:
self
.
with_linker
(
gof
.
CLinker
(),
minimum
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
CLinker
(),
maximum
,
dtype
=
dtype
)
# all and any use neq that don't have c code for complex
self
.
with_linker
(
gof
.
CLinker
(),
and_
,
dtype
=
dtype
,
self
.
with_linker
(
gof
.
CLinker
(),
scalar
.
minimum
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
CLinker
(),
scalar
.
maximum
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
CLinker
(),
scalar
.
and_
,
dtype
=
dtype
,
tensor_op
=
tensor
.
all
)
self
.
with_linker
(
gof
.
CLinker
(),
or_
,
dtype
=
dtype
,
self
.
with_linker
(
gof
.
CLinker
(),
scalar
.
or_
,
dtype
=
dtype
,
tensor_op
=
tensor
.
any
)
for
dtype
in
[
"int8"
,
"uint8"
]:
self
.
with_linker
(
gof
.
CLinker
(),
or_
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
CLinker
(),
and_
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
CLinker
(),
xor
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
CLinker
(),
scalar
.
or_
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
CLinker
(),
scalar
.
and_
,
dtype
=
dtype
)
self
.
with_linker
(
gof
.
CLinker
(),
scalar
.
xor
,
dtype
=
dtype
)
@dec.knownfailureif
(
True
,
(
"When there is nan in the input of CAReduce, we don't have a good output. "
))
def
test_c_nan
(
self
):
for
dtype
in
[
"floatX"
,
"complex64"
,
"complex128"
]:
self
.
with_linker
(
gof
.
CLinker
(),
add
,
dtype
=
dtype
,
self
.
with_linker
(
gof
.
CLinker
(),
scalar
.
add
,
dtype
=
dtype
,
test_nan
=
True
)
self
.
with_linker
(
gof
.
CLinker
(),
mul
,
dtype
=
dtype
,
self
.
with_linker
(
gof
.
CLinker
(),
scalar
.
mul
,
dtype
=
dtype
,
test_nan
=
True
)
for
dtype
in
[
"floatX"
]:
self
.
with_linker
(
gof
.
CLinker
(),
minimum
,
dtype
=
dtype
,
self
.
with_linker
(
gof
.
CLinker
(),
scalar
.
minimum
,
dtype
=
dtype
,
test_nan
=
True
)
self
.
with_linker
(
gof
.
CLinker
(),
maximum
,
dtype
=
dtype
,
self
.
with_linker
(
gof
.
CLinker
(),
scalar
.
maximum
,
dtype
=
dtype
,
test_nan
=
True
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论