Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e7d72660
提交
e7d72660
authored
3月 24, 2017
作者:
amrithasuresh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
1. Updated numpy as np
2. Fixed indentation
上级
bcd5d52e
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
50 行增加
和
50 行删除
+50
-50
test_slinalg.py
theano/tensor/tests/test_slinalg.py
+50
-50
没有找到文件。
theano/tensor/tests/test_slinalg.py
浏览文件 @
e7d72660
from
__future__
import
absolute_import
,
print_function
,
division
import
unittest
import
numpy
import
numpy
as
np
import
numpy.linalg
from
numpy.testing
import
assert_array_almost_equal
from
numpy.testing
import
dec
,
assert_array_equal
,
assert_allclose
...
...
@@ -35,25 +35,25 @@ def check_lower_triangular(pd, ch_f):
ch
=
ch_f
(
pd
)
assert
ch
[
0
,
pd
.
shape
[
1
]
-
1
]
==
0
assert
ch
[
pd
.
shape
[
0
]
-
1
,
0
]
!=
0
assert
n
umpy
.
allclose
(
numpy
.
dot
(
ch
,
ch
.
T
),
pd
)
assert
not
n
umpy
.
allclose
(
numpy
.
dot
(
ch
.
T
,
ch
),
pd
)
assert
n
p
.
allclose
(
np
.
dot
(
ch
,
ch
.
T
),
pd
)
assert
not
n
p
.
allclose
(
np
.
dot
(
ch
.
T
,
ch
),
pd
)
def
check_upper_triangular
(
pd
,
ch_f
):
ch
=
ch_f
(
pd
)
assert
ch
[
4
,
0
]
==
0
assert
ch
[
0
,
4
]
!=
0
assert
n
umpy
.
allclose
(
numpy
.
dot
(
ch
.
T
,
ch
),
pd
)
assert
not
n
umpy
.
allclose
(
numpy
.
dot
(
ch
,
ch
.
T
),
pd
)
assert
n
p
.
allclose
(
np
.
dot
(
ch
.
T
,
ch
),
pd
)
assert
not
n
p
.
allclose
(
np
.
dot
(
ch
,
ch
.
T
),
pd
)
def
test_cholesky
():
if
not
imported_scipy
:
raise
SkipTest
(
"Scipy needed for the Cholesky op."
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
r
=
rng
.
randn
(
5
,
5
)
.
astype
(
config
.
floatX
)
pd
=
n
umpy
.
dot
(
r
,
r
.
T
)
pd
=
n
p
.
dot
(
r
,
r
.
T
)
x
=
tensor
.
matrix
()
chol
=
cholesky
(
x
)
# Check the default.
...
...
@@ -72,7 +72,7 @@ def test_cholesky():
def
test_cholesky_grad
():
if
not
imported_scipy
:
raise
SkipTest
(
"Scipy needed for the Cholesky op."
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
r
=
rng
.
randn
(
5
,
5
)
.
astype
(
config
.
floatX
)
# The dots are inside the graph since Cholesky needs separable matrices
...
...
@@ -93,7 +93,7 @@ def test_cholesky_and_cholesky_grad_shape():
if
not
imported_scipy
:
raise
SkipTest
(
"Scipy needed for the Cholesky op."
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x
=
tensor
.
matrix
()
for
l
in
(
cholesky
(
x
),
Cholesky
(
lower
=
True
)(
x
),
Cholesky
(
lower
=
False
)(
x
)):
f_chol
=
theano
.
function
([
x
],
l
.
shape
)
...
...
@@ -107,9 +107,9 @@ def test_cholesky_and_cholesky_grad_shape():
assert
sum
([
node
.
op
.
__class__
==
CholeskyGrad
for
node
in
topo_cholgrad
])
==
0
for
shp
in
[
2
,
3
,
5
]:
m
=
n
umpy
.
cov
(
rng
.
randn
(
shp
,
shp
+
10
))
.
astype
(
config
.
floatX
)
yield
n
umpy
.
testing
.
assert_equal
,
f_chol
(
m
),
(
shp
,
shp
)
yield
n
umpy
.
testing
.
assert_equal
,
f_cholgrad
(
m
),
(
shp
,
shp
)
m
=
n
p
.
cov
(
rng
.
randn
(
shp
,
shp
+
10
))
.
astype
(
config
.
floatX
)
yield
n
p
.
testing
.
assert_equal
,
f_chol
(
m
),
(
shp
,
shp
)
yield
n
p
.
testing
.
assert_equal
,
f_cholgrad
(
m
),
(
shp
,
shp
)
def
test_eigvalsh
():
...
...
@@ -121,13 +121,13 @@ def test_eigvalsh():
B
=
theano
.
tensor
.
dmatrix
(
'b'
)
f
=
function
([
A
,
B
],
eigvalsh
(
A
,
B
))
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
a
=
rng
.
randn
(
5
,
5
)
a
=
a
+
a
.
T
for
b
in
[
10
*
n
umpy
.
eye
(
5
,
5
)
+
rng
.
randn
(
5
,
5
)]:
for
b
in
[
10
*
n
p
.
eye
(
5
,
5
)
+
rng
.
randn
(
5
,
5
)]:
w
=
f
(
a
,
b
)
refw
=
scipy
.
linalg
.
eigvalsh
(
a
,
b
)
n
umpy
.
testing
.
assert_array_almost_equal
(
w
,
refw
)
n
p
.
testing
.
assert_array_almost_equal
(
w
,
refw
)
# We need to test None separatly, as otherwise DebugMode will
# complain, as this isn't a valid ndarray.
...
...
@@ -136,7 +136,7 @@ def test_eigvalsh():
f
=
function
([
A
],
eigvalsh
(
A
,
B
))
w
=
f
(
a
)
refw
=
scipy
.
linalg
.
eigvalsh
(
a
,
b
)
n
umpy
.
testing
.
assert_array_almost_equal
(
w
,
refw
)
n
p
.
testing
.
assert_array_almost_equal
(
w
,
refw
)
def
test_eigvalsh_grad
():
...
...
@@ -144,12 +144,12 @@ def test_eigvalsh_grad():
raise
SkipTest
(
"Scipy needed for the geigvalsh op."
)
import
scipy.linalg
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
a
=
rng
.
randn
(
5
,
5
)
a
=
a
+
a
.
T
b
=
10
*
n
umpy
.
eye
(
5
,
5
)
+
rng
.
randn
(
5
,
5
)
b
=
10
*
n
p
.
eye
(
5
,
5
)
+
rng
.
randn
(
5
,
5
)
tensor
.
verify_grad
(
lambda
a
,
b
:
eigvalsh
(
a
,
b
)
.
dot
([
1
,
2
,
3
,
4
,
5
]),
[
a
,
b
],
rng
=
n
umpy
.
random
)
[
a
,
b
],
rng
=
n
p
.
random
)
class
test_Solve
(
utt
.
InferShapeTester
):
...
...
@@ -161,27 +161,27 @@ class test_Solve(utt.InferShapeTester):
def
test_infer_shape
(
self
):
if
not
imported_scipy
:
raise
SkipTest
(
"Scipy needed for the Solve op."
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
A
=
theano
.
tensor
.
matrix
()
b
=
theano
.
tensor
.
matrix
()
self
.
_compile_and_check
([
A
,
b
],
# theano.function inputs
[
self
.
op
(
A
,
b
)],
# theano.function outputs
# A must be square
[
n
umpy
.
asarray
(
rng
.
rand
(
5
,
5
),
[
n
p
.
asarray
(
rng
.
rand
(
5
,
5
),
dtype
=
config
.
floatX
),
n
umpy
.
asarray
(
rng
.
rand
(
5
,
1
),
n
p
.
asarray
(
rng
.
rand
(
5
,
1
),
dtype
=
config
.
floatX
)],
self
.
op_class
,
warn
=
False
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
A
=
theano
.
tensor
.
matrix
()
b
=
theano
.
tensor
.
vector
()
self
.
_compile_and_check
([
A
,
b
],
# theano.function inputs
[
self
.
op
(
A
,
b
)],
# theano.function outputs
# A must be square
[
n
umpy
.
asarray
(
rng
.
rand
(
5
,
5
),
[
n
p
.
asarray
(
rng
.
rand
(
5
,
5
),
dtype
=
config
.
floatX
),
n
umpy
.
asarray
(
rng
.
rand
(
5
),
n
p
.
asarray
(
rng
.
rand
(
5
),
dtype
=
config
.
floatX
)],
self
.
op_class
,
warn
=
False
)
...
...
@@ -189,7 +189,7 @@ class test_Solve(utt.InferShapeTester):
def
test_solve_correctness
(
self
):
if
not
imported_scipy
:
raise
SkipTest
(
"Scipy needed for the Cholesky and Solve ops."
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
A
=
theano
.
tensor
.
matrix
()
b
=
theano
.
tensor
.
matrix
()
y
=
self
.
op
(
A
,
b
)
...
...
@@ -205,23 +205,23 @@ class test_Solve(utt.InferShapeTester):
y_upper
=
self
.
op
(
U
,
b
)
upper_solve_func
=
theano
.
function
([
U
,
b
],
y_upper
)
b_val
=
n
umpy
.
asarray
(
rng
.
rand
(
5
,
1
),
dtype
=
config
.
floatX
)
b_val
=
n
p
.
asarray
(
rng
.
rand
(
5
,
1
),
dtype
=
config
.
floatX
)
# 1-test general case
A_val
=
n
umpy
.
asarray
(
rng
.
rand
(
5
,
5
),
dtype
=
config
.
floatX
)
A_val
=
n
p
.
asarray
(
rng
.
rand
(
5
,
5
),
dtype
=
config
.
floatX
)
# positive definite matrix:
A_val
=
n
umpy
.
dot
(
A_val
.
transpose
(),
A_val
)
assert
n
umpy
.
allclose
(
scipy
.
linalg
.
solve
(
A_val
,
b_val
),
A_val
=
n
p
.
dot
(
A_val
.
transpose
(),
A_val
)
assert
n
p
.
allclose
(
scipy
.
linalg
.
solve
(
A_val
,
b_val
),
gen_solve_func
(
A_val
,
b_val
))
# 2-test lower traingular case
L_val
=
scipy
.
linalg
.
cholesky
(
A_val
,
lower
=
True
)
assert
n
umpy
.
allclose
(
scipy
.
linalg
.
solve_triangular
(
L_val
,
b_val
,
lower
=
True
),
assert
n
p
.
allclose
(
scipy
.
linalg
.
solve_triangular
(
L_val
,
b_val
,
lower
=
True
),
lower_solve_func
(
L_val
,
b_val
))
# 3-test upper traingular case
U_val
=
scipy
.
linalg
.
cholesky
(
A_val
,
lower
=
False
)
assert
n
umpy
.
allclose
(
scipy
.
linalg
.
solve_triangular
(
U_val
,
b_val
,
lower
=
False
),
assert
n
p
.
allclose
(
scipy
.
linalg
.
solve_triangular
(
U_val
,
b_val
,
lower
=
False
),
upper_solve_func
(
U_val
,
b_val
))
def
test_solve_dtype
(
self
):
...
...
@@ -232,8 +232,8 @@ class test_Solve(utt.InferShapeTester):
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'float16'
,
'float32'
,
'float64'
]
A_val
=
n
umpy
.
eye
(
2
)
b_val
=
n
umpy
.
ones
((
2
,
1
))
A_val
=
n
p
.
eye
(
2
)
b_val
=
n
p
.
ones
((
2
,
1
))
# try all dtype combinations
for
A_dtype
,
b_dtype
in
itertools
.
product
(
dtypes
,
dtypes
):
...
...
@@ -249,11 +249,11 @@ class test_Solve(utt.InferShapeTester):
# ensure diagonal elements of A relatively large to avoid numerical
# precision issues
A_val
=
(
rng
.
normal
(
size
=
(
m
,
m
))
*
0.5
+
n
umpy
.
eye
(
m
))
.
astype
(
config
.
floatX
)
n
p
.
eye
(
m
))
.
astype
(
config
.
floatX
)
if
A_structure
==
'lower_triangular'
:
A_val
=
n
umpy
.
tril
(
A_val
)
A_val
=
n
p
.
tril
(
A_val
)
elif
A_structure
==
'upper_triangular'
:
A_val
=
n
umpy
.
triu
(
A_val
)
A_val
=
n
p
.
triu
(
A_val
)
if
n
is
None
:
b_val
=
rng
.
normal
(
size
=
m
)
.
astype
(
config
.
floatX
)
else
:
...
...
@@ -267,7 +267,7 @@ class test_Solve(utt.InferShapeTester):
def
test_solve_grad
(
self
):
if
not
imported_scipy
:
raise
SkipTest
(
"Scipy needed for the Solve op."
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
structures
=
[
'general'
,
'lower_triangular'
,
'upper_triangular'
]
for
A_structure
in
structures
:
lower
=
(
A_structure
==
'lower_triangular'
)
...
...
@@ -282,7 +282,7 @@ class test_Solve(utt.InferShapeTester):
def
test_expm
():
if
not
imported_scipy
:
raise
SkipTest
(
"Scipy needed for the expm op."
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
A
=
rng
.
randn
(
5
,
5
)
.
astype
(
config
.
floatX
)
ref
=
scipy
.
linalg
.
expm
(
A
)
...
...
@@ -292,14 +292,14 @@ def test_expm():
expm_f
=
function
([
x
],
m
)
val
=
expm_f
(
A
)
n
umpy
.
testing
.
assert_array_almost_equal
(
val
,
ref
)
n
p
.
testing
.
assert_array_almost_equal
(
val
,
ref
)
def
test_expm_grad_1
():
# with symmetric matrix (real eigenvectors)
if
not
imported_scipy
:
raise
SkipTest
(
"Scipy needed for the expm op."
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# Always test in float64 for better numerical stability.
A
=
rng
.
randn
(
5
,
5
)
A
=
A
+
A
.
T
...
...
@@ -311,12 +311,12 @@ def test_expm_grad_2():
# with non-symmetric matrix with real eigenspecta
if
not
imported_scipy
:
raise
SkipTest
(
"Scipy needed for the expm op."
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# Always test in float64 for better numerical stability.
A
=
rng
.
randn
(
5
,
5
)
w
=
rng
.
randn
(
5
)
**
2
A
=
(
n
umpy
.
diag
(
w
**
0.5
))
.
dot
(
A
+
A
.
T
)
.
dot
(
numpy
.
diag
(
w
**
(
-
0.5
)))
assert
not
n
umpy
.
allclose
(
A
,
A
.
T
)
A
=
(
n
p
.
diag
(
w
**
0.5
))
.
dot
(
A
+
A
.
T
)
.
dot
(
np
.
diag
(
w
**
(
-
0.5
)))
assert
not
n
p
.
allclose
(
A
,
A
.
T
)
tensor
.
verify_grad
(
expm
,
[
A
],
rng
=
rng
)
...
...
@@ -325,7 +325,7 @@ def test_expm_grad_3():
# with non-symmetric matrix (complex eigenvectors)
if
not
imported_scipy
:
raise
SkipTest
(
"Scipy needed for the expm op."
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# Always test in float64 for better numerical stability.
A
=
rng
.
randn
(
5
,
5
)
...
...
@@ -334,7 +334,7 @@ def test_expm_grad_3():
class
TestKron
(
utt
.
InferShapeTester
):
rng
=
n
umpy
.
random
.
RandomState
(
43
)
rng
=
n
p
.
random
.
RandomState
(
43
)
def
setUp
(
self
):
super
(
TestKron
,
self
)
.
setUp
()
...
...
@@ -347,7 +347,7 @@ class TestKron(utt.InferShapeTester):
for
shp0
in
[(
2
,),
(
2
,
3
),
(
2
,
3
,
4
),
(
2
,
3
,
4
,
5
)]:
x
=
tensor
.
tensor
(
dtype
=
'floatX'
,
broadcastable
=
(
False
,)
*
len
(
shp0
))
a
=
n
umpy
.
asarray
(
self
.
rng
.
rand
(
*
shp0
))
.
astype
(
config
.
floatX
)
a
=
n
p
.
asarray
(
self
.
rng
.
rand
(
*
shp0
))
.
astype
(
config
.
floatX
)
for
shp1
in
[(
6
,),
(
6
,
7
),
(
6
,
7
,
8
),
(
6
,
7
,
8
,
9
)]:
if
len
(
shp0
)
+
len
(
shp1
)
==
2
:
continue
...
...
@@ -360,7 +360,7 @@ class TestKron(utt.InferShapeTester):
# so we have to add a dimension to a and flatten the result.
if
len
(
shp0
)
+
len
(
shp1
)
==
3
:
scipy_val
=
scipy
.
linalg
.
kron
(
a
[
n
umpy
.
newaxis
,
:],
b
)
.
flatten
()
a
[
n
p
.
newaxis
,
:],
b
)
.
flatten
()
else
:
scipy_val
=
scipy
.
linalg
.
kron
(
a
,
b
)
utt
.
assert_allclose
(
out
,
scipy_val
)
...
...
@@ -369,7 +369,7 @@ class TestKron(utt.InferShapeTester):
for
shp0
in
[(
2
,
3
)]:
x
=
tensor
.
tensor
(
dtype
=
'floatX'
,
broadcastable
=
(
False
,)
*
len
(
shp0
))
a
=
n
umpy
.
asarray
(
self
.
rng
.
rand
(
*
shp0
))
.
astype
(
config
.
floatX
)
a
=
n
p
.
asarray
(
self
.
rng
.
rand
(
*
shp0
))
.
astype
(
config
.
floatX
)
for
shp1
in
[(
6
,
7
)]:
if
len
(
shp0
)
+
len
(
shp1
)
==
2
:
continue
...
...
@@ -378,4 +378,4 @@ class TestKron(utt.InferShapeTester):
f
=
function
([
x
,
y
],
kron
(
x
,
y
))
b
=
self
.
rng
.
rand
(
*
shp1
)
.
astype
(
config
.
floatX
)
out
=
f
(
a
,
b
)
assert
n
umpy
.
allclose
(
out
,
numpy
.
kron
(
a
,
b
))
assert
n
p
.
allclose
(
out
,
np
.
kron
(
a
,
b
))
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论