Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
85dad09c
提交
85dad09c
authored
3月 24, 2017
作者:
amrithasuresh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
1. Updated numpy as np
2. Fixed indentation
上级
c71d9d5c
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
53 行增加
和
54 行删除
+53
-54
test_extra_ops.py
theano/tensor/tests/test_extra_ops.py
+53
-54
没有找到文件。
theano/tensor/tests/test_extra_ops.py
浏览文件 @
85dad09c
...
...
@@ -2,7 +2,6 @@ from __future__ import absolute_import, print_function, division
from
functools
import
partial
import
numpy
as
np
import
numpy
import
theano
from
theano.tests
import
unittest_tools
as
utt
...
...
@@ -23,7 +22,7 @@ from theano.tests.unittest_tools import attr
def
test_cpu_contiguous
():
a
=
T
.
fmatrix
(
'a'
)
i
=
T
.
iscalar
(
'i'
)
a_val
=
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
4
,
5
),
dtype
=
'float32'
)
a_val
=
n
p
.
asarray
(
np
.
random
.
rand
(
4
,
5
),
dtype
=
'float32'
)
f
=
theano
.
function
([
a
,
i
],
cpu_contiguous
(
a
.
reshape
((
5
,
4
))[::
i
]))
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
any
([
isinstance
(
node
.
op
,
CpuContiguous
)
for
node
in
topo
])
...
...
@@ -33,7 +32,7 @@ def test_cpu_contiguous():
# Test the grad:
theano
.
tests
.
unittest_tools
.
verify_grad
(
cpu_contiguous
,
[
n
umpy
.
random
.
rand
(
5
,
7
,
2
)])
[
n
p
.
random
.
rand
(
5
,
7
,
2
)])
class
TestSearchsortedOp
(
utt
.
InferShapeTester
):
...
...
@@ -280,20 +279,20 @@ class SqueezeTester(utt.InferShapeTester):
def
test_op
(
self
):
for
shape
,
broadcast
in
zip
(
self
.
shape_list
,
self
.
broadcast_list
):
data
=
n
umpy
.
random
.
random
(
size
=
shape
)
.
astype
(
theano
.
config
.
floatX
)
data
=
n
p
.
random
.
random
(
size
=
shape
)
.
astype
(
theano
.
config
.
floatX
)
variable
=
tensor
.
TensorType
(
theano
.
config
.
floatX
,
broadcast
)()
f
=
theano
.
function
([
variable
],
self
.
op
(
variable
))
expected
=
n
umpy
.
squeeze
(
data
)
expected
=
n
p
.
squeeze
(
data
)
tested
=
f
(
data
)
assert
tested
.
shape
==
expected
.
shape
assert
n
umpy
.
allclose
(
tested
,
expected
)
assert
n
p
.
allclose
(
tested
,
expected
)
def
test_infer_shape
(
self
):
for
shape
,
broadcast
in
zip
(
self
.
shape_list
,
self
.
broadcast_list
):
data
=
n
umpy
.
random
.
random
(
size
=
shape
)
.
astype
(
theano
.
config
.
floatX
)
data
=
n
p
.
random
.
random
(
size
=
shape
)
.
astype
(
theano
.
config
.
floatX
)
variable
=
tensor
.
TensorType
(
theano
.
config
.
floatX
,
broadcast
)()
self
.
_compile_and_check
([
variable
],
...
...
@@ -304,23 +303,23 @@ class SqueezeTester(utt.InferShapeTester):
def
test_grad
(
self
):
for
shape
,
broadcast
in
zip
(
self
.
shape_list
,
self
.
broadcast_list
):
data
=
n
umpy
.
random
.
random
(
size
=
shape
)
.
astype
(
theano
.
config
.
floatX
)
data
=
n
p
.
random
.
random
(
size
=
shape
)
.
astype
(
theano
.
config
.
floatX
)
utt
.
verify_grad
(
self
.
op
,
[
data
])
def
test_var_interface
(
self
):
# same as test_op, but use a_theano_var.squeeze.
for
shape
,
broadcast
in
zip
(
self
.
shape_list
,
self
.
broadcast_list
):
data
=
n
umpy
.
random
.
random
(
size
=
shape
)
.
astype
(
theano
.
config
.
floatX
)
data
=
n
p
.
random
.
random
(
size
=
shape
)
.
astype
(
theano
.
config
.
floatX
)
variable
=
tensor
.
TensorType
(
theano
.
config
.
floatX
,
broadcast
)()
f
=
theano
.
function
([
variable
],
variable
.
squeeze
())
expected
=
n
umpy
.
squeeze
(
data
)
expected
=
n
p
.
squeeze
(
data
)
tested
=
f
(
data
)
assert
tested
.
shape
==
expected
.
shape
assert
n
umpy
.
allclose
(
tested
,
expected
)
assert
n
p
.
allclose
(
tested
,
expected
)
class
CompressTester
(
utt
.
InferShapeTester
):
...
...
@@ -351,17 +350,17 @@ class CompressTester(utt.InferShapeTester):
for
axis
,
cond
,
shape
in
zip
(
self
.
axis_list
,
self
.
cond_list
,
self
.
shape_list
):
cond_var
=
theano
.
tensor
.
ivector
()
data
=
n
umpy
.
random
.
random
(
size
=
shape
)
.
astype
(
theano
.
config
.
floatX
)
data
=
n
p
.
random
.
random
(
size
=
shape
)
.
astype
(
theano
.
config
.
floatX
)
data_var
=
theano
.
tensor
.
matrix
()
f
=
theano
.
function
([
cond_var
,
data_var
],
self
.
op
(
cond_var
,
data_var
,
axis
=
axis
))
expected
=
n
umpy
.
compress
(
cond
,
data
,
axis
=
axis
)
expected
=
n
p
.
compress
(
cond
,
data
,
axis
=
axis
)
tested
=
f
(
cond
,
data
)
assert
tested
.
shape
==
expected
.
shape
assert
n
umpy
.
allclose
(
tested
,
expected
)
assert
n
p
.
allclose
(
tested
,
expected
)
class
TestRepeatOp
(
utt
.
InferShapeTester
):
...
...
@@ -388,7 +387,7 @@ class TestRepeatOp(utt.InferShapeTester):
for
axis
in
self
.
_possible_axis
(
ndim
):
for
dtype
in
tensor
.
integer_dtypes
:
r_var
=
T
.
scalar
(
dtype
=
dtype
)
r
=
n
umpy
.
asarray
(
3
,
dtype
=
dtype
)
r
=
n
p
.
asarray
(
3
,
dtype
=
dtype
)
if
(
dtype
==
'uint64'
or
(
dtype
in
self
.
numpy_unsupported_dtypes
and
r_var
.
ndim
==
1
)):
...
...
@@ -441,13 +440,13 @@ class TestRepeatOp(utt.InferShapeTester):
def
test_infer_shape
(
self
):
for
ndim
in
range
(
4
):
x
=
T
.
TensorType
(
config
.
floatX
,
[
False
]
*
ndim
)()
shp
=
(
n
umpy
.
arange
(
ndim
)
+
1
)
*
5
shp
=
(
n
p
.
arange
(
ndim
)
+
1
)
*
5
a
=
np
.
random
.
random
(
shp
)
.
astype
(
config
.
floatX
)
for
axis
in
self
.
_possible_axis
(
ndim
):
for
dtype
in
tensor
.
integer_dtypes
:
r_var
=
T
.
scalar
(
dtype
=
dtype
)
r
=
n
umpy
.
asarray
(
3
,
dtype
=
dtype
)
r
=
n
p
.
asarray
(
3
,
dtype
=
dtype
)
if
dtype
in
self
.
numpy_unsupported_dtypes
:
r_var
=
T
.
vector
(
dtype
=
dtype
)
self
.
assertRaises
(
TypeError
,
repeat
,
x
,
r_var
)
...
...
@@ -501,17 +500,17 @@ class TestBartlett(utt.InferShapeTester):
def
test_perform
(
self
):
x
=
tensor
.
lscalar
()
f
=
function
([
x
],
self
.
op
(
x
))
M
=
n
umpy
.
random
.
randint
(
3
,
51
,
size
=
())
assert
n
umpy
.
allclose
(
f
(
M
),
numpy
.
bartlett
(
M
))
assert
n
umpy
.
allclose
(
f
(
0
),
numpy
.
bartlett
(
0
))
assert
n
umpy
.
allclose
(
f
(
-
1
),
numpy
.
bartlett
(
-
1
))
b
=
n
umpy
.
array
([
17
],
dtype
=
'uint8'
)
assert
n
umpy
.
allclose
(
f
(
b
[
0
]),
numpy
.
bartlett
(
b
[
0
]))
M
=
n
p
.
random
.
randint
(
3
,
51
,
size
=
())
assert
n
p
.
allclose
(
f
(
M
),
np
.
bartlett
(
M
))
assert
n
p
.
allclose
(
f
(
0
),
np
.
bartlett
(
0
))
assert
n
p
.
allclose
(
f
(
-
1
),
np
.
bartlett
(
-
1
))
b
=
n
p
.
array
([
17
],
dtype
=
'uint8'
)
assert
n
p
.
allclose
(
f
(
b
[
0
]),
np
.
bartlett
(
b
[
0
]))
def
test_infer_shape
(
self
):
x
=
tensor
.
lscalar
()
self
.
_compile_and_check
([
x
],
[
self
.
op
(
x
)],
[
n
umpy
.
random
.
randint
(
3
,
51
,
size
=
())],
[
n
p
.
random
.
randint
(
3
,
51
,
size
=
())],
self
.
op_class
)
self
.
_compile_and_check
([
x
],
[
self
.
op
(
x
)],
[
0
],
self
.
op_class
)
self
.
_compile_and_check
([
x
],
[
self
.
op
(
x
)],
[
1
],
self
.
op_class
)
...
...
@@ -519,7 +518,7 @@ class TestBartlett(utt.InferShapeTester):
class
TestFillDiagonal
(
utt
.
InferShapeTester
):
rng
=
n
umpy
.
random
.
RandomState
(
43
)
rng
=
n
p
.
random
.
RandomState
(
43
)
def
setUp
(
self
):
super
(
TestFillDiagonal
,
self
)
.
setUp
()
...
...
@@ -531,21 +530,21 @@ class TestFillDiagonal(utt.InferShapeTester):
y
=
tensor
.
scalar
()
f
=
function
([
x
,
y
],
fill_diagonal
(
x
,
y
))
for
shp
in
[(
8
,
8
),
(
5
,
8
),
(
8
,
5
)]:
a
=
n
umpy
.
random
.
rand
(
*
shp
)
.
astype
(
config
.
floatX
)
val
=
n
umpy
.
cast
[
config
.
floatX
](
numpy
.
random
.
rand
())
a
=
n
p
.
random
.
rand
(
*
shp
)
.
astype
(
config
.
floatX
)
val
=
n
p
.
cast
[
config
.
floatX
](
np
.
random
.
rand
())
out
=
f
(
a
,
val
)
# We can't use n
umpy
.fill_diagonal as it is bugged.
assert
n
umpy
.
allclose
(
numpy
.
diag
(
out
),
val
)
# We can't use n
p
.fill_diagonal as it is bugged.
assert
n
p
.
allclose
(
np
.
diag
(
out
),
val
)
assert
(
out
==
val
)
.
sum
()
==
min
(
a
.
shape
)
# test for 3d tensor
a
=
n
umpy
.
random
.
rand
(
3
,
3
,
3
)
.
astype
(
config
.
floatX
)
a
=
n
p
.
random
.
rand
(
3
,
3
,
3
)
.
astype
(
config
.
floatX
)
x
=
tensor
.
tensor3
()
y
=
tensor
.
scalar
()
f
=
function
([
x
,
y
],
fill_diagonal
(
x
,
y
))
val
=
n
umpy
.
cast
[
config
.
floatX
](
numpy
.
random
.
rand
()
+
10
)
val
=
n
p
.
cast
[
config
.
floatX
](
np
.
random
.
rand
()
+
10
)
out
=
f
(
a
,
val
)
# We can't use n
umpy
.fill_diagonal as it is bugged.
# We can't use n
p
.fill_diagonal as it is bugged.
assert
out
[
0
,
0
,
0
]
==
val
assert
out
[
1
,
1
,
1
]
==
val
assert
out
[
2
,
2
,
2
]
==
val
...
...
@@ -553,11 +552,11 @@ class TestFillDiagonal(utt.InferShapeTester):
@attr
(
'slow'
)
def
test_gradient
(
self
):
utt
.
verify_grad
(
fill_diagonal
,
[
n
umpy
.
random
.
rand
(
5
,
8
),
n
umpy
.
random
.
rand
()],
utt
.
verify_grad
(
fill_diagonal
,
[
n
p
.
random
.
rand
(
5
,
8
),
n
p
.
random
.
rand
()],
n_tests
=
1
,
rng
=
TestFillDiagonal
.
rng
)
utt
.
verify_grad
(
fill_diagonal
,
[
n
umpy
.
random
.
rand
(
8
,
5
),
n
umpy
.
random
.
rand
()],
utt
.
verify_grad
(
fill_diagonal
,
[
n
p
.
random
.
rand
(
8
,
5
),
n
p
.
random
.
rand
()],
n_tests
=
1
,
rng
=
TestFillDiagonal
.
rng
)
def
test_infer_shape
(
self
):
...
...
@@ -565,20 +564,20 @@ class TestFillDiagonal(utt.InferShapeTester):
x
=
tensor
.
dmatrix
()
y
=
tensor
.
dscalar
()
self
.
_compile_and_check
([
x
,
y
],
[
self
.
op
(
x
,
y
)],
[
n
umpy
.
random
.
rand
(
8
,
5
),
n
umpy
.
random
.
rand
()],
[
n
p
.
random
.
rand
(
8
,
5
),
n
p
.
random
.
rand
()],
self
.
op_class
)
self
.
_compile_and_check
([
z
,
y
],
[
self
.
op
(
z
,
y
)],
# must be square when nd>2
[
n
umpy
.
random
.
rand
(
8
,
8
,
8
),
n
umpy
.
random
.
rand
()],
[
n
p
.
random
.
rand
(
8
,
8
,
8
),
n
p
.
random
.
rand
()],
self
.
op_class
,
warn
=
False
)
class
TestFillDiagonalOffset
(
utt
.
InferShapeTester
):
rng
=
n
umpy
.
random
.
RandomState
(
43
)
rng
=
n
p
.
random
.
RandomState
(
43
)
def
setUp
(
self
):
super
(
TestFillDiagonalOffset
,
self
)
.
setUp
()
...
...
@@ -593,11 +592,11 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
f
=
function
([
x
,
y
,
z
],
fill_diagonal_offset
(
x
,
y
,
z
))
for
test_offset
in
(
-
5
,
-
4
,
-
1
,
0
,
1
,
4
,
5
):
for
shp
in
[(
8
,
8
),
(
5
,
8
),
(
8
,
5
),
(
5
,
5
)]:
a
=
n
umpy
.
random
.
rand
(
*
shp
)
.
astype
(
config
.
floatX
)
val
=
n
umpy
.
cast
[
config
.
floatX
](
numpy
.
random
.
rand
())
a
=
n
p
.
random
.
rand
(
*
shp
)
.
astype
(
config
.
floatX
)
val
=
n
p
.
cast
[
config
.
floatX
](
np
.
random
.
rand
())
out
=
f
(
a
,
val
,
test_offset
)
# We can't use n
umpy
.fill_diagonal as it is bugged.
assert
n
umpy
.
allclose
(
numpy
.
diag
(
out
,
test_offset
),
val
)
# We can't use n
p
.fill_diagonal as it is bugged.
assert
n
p
.
allclose
(
np
.
diag
(
out
,
test_offset
),
val
)
if
test_offset
>=
0
:
assert
(
out
==
val
)
.
sum
()
==
min
(
min
(
a
.
shape
),
a
.
shape
[
1
]
-
test_offset
)
...
...
@@ -612,13 +611,13 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
return
fill_diagonal_offset
(
a
,
val
,
test_offset
)
utt
.
verify_grad
(
fill_diagonal_with_fix_offset
,
[
n
umpy
.
random
.
rand
(
5
,
8
),
numpy
.
random
.
rand
()],
[
n
p
.
random
.
rand
(
5
,
8
),
np
.
random
.
rand
()],
n_tests
=
1
,
rng
=
TestFillDiagonalOffset
.
rng
)
utt
.
verify_grad
(
fill_diagonal_with_fix_offset
,
[
n
umpy
.
random
.
rand
(
8
,
5
),
numpy
.
random
.
rand
()],
[
n
p
.
random
.
rand
(
8
,
5
),
np
.
random
.
rand
()],
n_tests
=
1
,
rng
=
TestFillDiagonalOffset
.
rng
)
utt
.
verify_grad
(
fill_diagonal_with_fix_offset
,
[
n
umpy
.
random
.
rand
(
5
,
5
),
numpy
.
random
.
rand
()],
[
n
p
.
random
.
rand
(
5
,
5
),
np
.
random
.
rand
()],
n_tests
=
1
,
rng
=
TestFillDiagonalOffset
.
rng
)
def
test_infer_shape
(
self
):
...
...
@@ -627,13 +626,13 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
z
=
tensor
.
iscalar
()
for
test_offset
in
(
-
5
,
-
4
,
-
1
,
0
,
1
,
4
,
5
):
self
.
_compile_and_check
([
x
,
y
,
z
],
[
self
.
op
(
x
,
y
,
z
)],
[
n
umpy
.
random
.
rand
(
8
,
5
),
n
umpy
.
random
.
rand
(),
[
n
p
.
random
.
rand
(
8
,
5
),
n
p
.
random
.
rand
(),
test_offset
],
self
.
op_class
)
self
.
_compile_and_check
([
x
,
y
,
z
],
[
self
.
op
(
x
,
y
,
z
)],
[
n
umpy
.
random
.
rand
(
5
,
8
),
n
umpy
.
random
.
rand
(),
[
n
p
.
random
.
rand
(
5
,
8
),
n
p
.
random
.
rand
(),
test_offset
],
self
.
op_class
)
...
...
@@ -644,7 +643,7 @@ def test_to_one_hot():
f
=
theano
.
function
([
v
],
o
)
out
=
f
([
1
,
2
,
3
,
5
,
6
])
assert
out
.
dtype
==
theano
.
config
.
floatX
assert
n
umpy
.
allclose
(
assert
n
p
.
allclose
(
out
,
[[
0.
,
1.
,
0.
,
0.
,
0.
,
0.
,
0.
,
0.
,
0.
,
0.
],
[
0.
,
0.
,
1.
,
0.
,
0.
,
0.
,
0.
,
0.
,
0.
,
0.
],
...
...
@@ -657,7 +656,7 @@ def test_to_one_hot():
f
=
theano
.
function
([
v
],
o
)
out
=
f
([
1
,
2
,
3
,
5
,
6
])
assert
out
.
dtype
==
"int32"
assert
n
umpy
.
allclose
(
assert
n
p
.
allclose
(
out
,
[[
0.
,
1.
,
0.
,
0.
,
0.
,
0.
,
0.
,
0.
,
0.
,
0.
],
[
0.
,
0.
,
1.
,
0.
,
0.
,
0.
,
0.
,
0.
,
0.
,
0.
],
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论