Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
d6dc0e71
提交
d6dc0e71
authored
3月 24, 2017
作者:
amrithasuresh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
1. Updated numpy as np
2. Fixed indentation
上级
0c36b353
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
72 行增加
和
72 行删除
+72
-72
test_nlinalg.py
theano/tensor/tests/test_nlinalg.py
+72
-72
没有找到文件。
theano/tensor/tests/test_nlinalg.py
浏览文件 @
d6dc0e71
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
unittest
import
unittest
import
numpy
import
numpy
as
np
import
numpy.linalg
import
numpy.linalg
from
numpy.testing
import
assert_array_almost_equal
from
numpy.testing
import
assert_array_almost_equal
from
numpy.testing
import
dec
,
assert_array_equal
,
assert_allclose
from
numpy.testing
import
dec
,
assert_array_equal
,
assert_allclose
...
@@ -28,7 +28,7 @@ from nose.tools import assert_raises
...
@@ -28,7 +28,7 @@ from nose.tools import assert_raises
def
test_pseudoinverse_correctness
():
def
test_pseudoinverse_correctness
():
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
d1
=
rng
.
randint
(
4
)
+
2
d1
=
rng
.
randint
(
4
)
+
2
d2
=
rng
.
randint
(
4
)
+
2
d2
=
rng
.
randint
(
4
)
+
2
r
=
rng
.
randn
(
d1
,
d2
)
.
astype
(
theano
.
config
.
floatX
)
r
=
rng
.
randn
(
d1
,
d2
)
.
astype
(
theano
.
config
.
floatX
)
...
@@ -41,8 +41,8 @@ def test_pseudoinverse_correctness():
...
@@ -41,8 +41,8 @@ def test_pseudoinverse_correctness():
assert
ri
.
shape
[
1
]
==
r
.
shape
[
0
]
assert
ri
.
shape
[
1
]
==
r
.
shape
[
0
]
assert
ri
.
dtype
==
r
.
dtype
assert
ri
.
dtype
==
r
.
dtype
# Note that pseudoinverse can be quite unprecise so I prefer to compare
# Note that pseudoinverse can be quite unprecise so I prefer to compare
# the result with what n
umpy
.linalg returns
# the result with what n
p
.linalg returns
assert
_allclose
(
ri
,
n
umpy
.
linalg
.
pinv
(
r
))
assert
_allclose
(
ri
,
n
p
.
linalg
.
pinv
(
r
))
class
test_MatrixInverse
(
utt
.
InferShapeTester
):
class
test_MatrixInverse
(
utt
.
InferShapeTester
):
...
@@ -50,7 +50,7 @@ class test_MatrixInverse(utt.InferShapeTester):
...
@@ -50,7 +50,7 @@ class test_MatrixInverse(utt.InferShapeTester):
super
(
test_MatrixInverse
,
self
)
.
setUp
()
super
(
test_MatrixInverse
,
self
)
.
setUp
()
self
.
op_class
=
MatrixInverse
self
.
op_class
=
MatrixInverse
self
.
op
=
matrix_inverse
self
.
op
=
matrix_inverse
self
.
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
self
.
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
def
test_inverse_correctness
(
self
):
def
test_inverse_correctness
(
self
):
...
@@ -63,11 +63,11 @@ class test_MatrixInverse(utt.InferShapeTester):
...
@@ -63,11 +63,11 @@ class test_MatrixInverse(utt.InferShapeTester):
assert
ri
.
shape
==
r
.
shape
assert
ri
.
shape
==
r
.
shape
assert
ri
.
dtype
==
r
.
dtype
assert
ri
.
dtype
==
r
.
dtype
rir
=
n
umpy
.
dot
(
ri
,
r
)
rir
=
n
p
.
dot
(
ri
,
r
)
rri
=
n
umpy
.
dot
(
r
,
ri
)
rri
=
n
p
.
dot
(
r
,
ri
)
assert
_allclose
(
n
umpy
.
identity
(
4
),
rir
),
rir
assert
_allclose
(
n
p
.
identity
(
4
),
rir
),
rir
assert
_allclose
(
n
umpy
.
identity
(
4
),
rri
),
rri
assert
_allclose
(
n
p
.
identity
(
4
),
rri
),
rri
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
...
@@ -81,7 +81,7 @@ class test_MatrixInverse(utt.InferShapeTester):
...
@@ -81,7 +81,7 @@ class test_MatrixInverse(utt.InferShapeTester):
def
test_matrix_dot
():
def
test_matrix_dot
():
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
n
=
rng
.
randint
(
4
)
+
2
n
=
rng
.
randint
(
4
)
+
2
rs
=
[]
rs
=
[]
xs
=
[]
xs
=
[]
...
@@ -93,26 +93,26 @@ def test_matrix_dot():
...
@@ -93,26 +93,26 @@ def test_matrix_dot():
theano_sol
=
function
(
xs
,
sol
)(
*
rs
)
theano_sol
=
function
(
xs
,
sol
)(
*
rs
)
numpy_sol
=
rs
[
0
]
numpy_sol
=
rs
[
0
]
for
r
in
rs
[
1
:]:
for
r
in
rs
[
1
:]:
numpy_sol
=
n
umpy
.
dot
(
numpy_sol
,
r
)
numpy_sol
=
n
p
.
dot
(
numpy_sol
,
r
)
assert
_allclose
(
numpy_sol
,
theano_sol
)
assert
_allclose
(
numpy_sol
,
theano_sol
)
def
test_qr_modes
():
def
test_qr_modes
():
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
A
=
tensor
.
matrix
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
A
=
tensor
.
matrix
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
a
=
rng
.
rand
(
4
,
4
)
.
astype
(
theano
.
config
.
floatX
)
a
=
rng
.
rand
(
4
,
4
)
.
astype
(
theano
.
config
.
floatX
)
f
=
function
([
A
],
qr
(
A
))
f
=
function
([
A
],
qr
(
A
))
t_qr
=
f
(
a
)
t_qr
=
f
(
a
)
n_qr
=
n
umpy
.
linalg
.
qr
(
a
)
n_qr
=
n
p
.
linalg
.
qr
(
a
)
assert
_allclose
(
n_qr
,
t_qr
)
assert
_allclose
(
n_qr
,
t_qr
)
for
mode
in
[
"reduced"
,
"r"
,
"raw"
]:
for
mode
in
[
"reduced"
,
"r"
,
"raw"
]:
f
=
function
([
A
],
qr
(
A
,
mode
))
f
=
function
([
A
],
qr
(
A
,
mode
))
t_qr
=
f
(
a
)
t_qr
=
f
(
a
)
n_qr
=
n
umpy
.
linalg
.
qr
(
a
,
mode
)
n_qr
=
n
p
.
linalg
.
qr
(
a
,
mode
)
if
isinstance
(
n_qr
,
(
list
,
tuple
)):
if
isinstance
(
n_qr
,
(
list
,
tuple
)):
assert
_allclose
(
n_qr
[
0
],
t_qr
[
0
])
assert
_allclose
(
n_qr
[
0
],
t_qr
[
0
])
assert
_allclose
(
n_qr
[
1
],
t_qr
[
1
])
assert
_allclose
(
n_qr
[
1
],
t_qr
[
1
])
...
@@ -120,7 +120,7 @@ def test_qr_modes():
...
@@ -120,7 +120,7 @@ def test_qr_modes():
assert
_allclose
(
n_qr
,
t_qr
)
assert
_allclose
(
n_qr
,
t_qr
)
try
:
try
:
n_qr
=
n
umpy
.
linalg
.
qr
(
a
,
"complete"
)
n_qr
=
n
p
.
linalg
.
qr
(
a
,
"complete"
)
f
=
function
([
A
],
qr
(
A
,
"complete"
))
f
=
function
([
A
],
qr
(
A
,
"complete"
))
t_qr
=
f
(
a
)
t_qr
=
f
(
a
)
assert
_allclose
(
n_qr
,
t_qr
)
assert
_allclose
(
n_qr
,
t_qr
)
...
@@ -129,12 +129,12 @@ def test_qr_modes():
...
@@ -129,12 +129,12 @@ def test_qr_modes():
def
test_svd
():
def
test_svd
():
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
A
=
tensor
.
matrix
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
A
=
tensor
.
matrix
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
U
,
V
,
T
=
svd
(
A
)
U
,
V
,
T
=
svd
(
A
)
fn
=
function
([
A
],
[
U
,
V
,
T
])
fn
=
function
([
A
],
[
U
,
V
,
T
])
a
=
rng
.
rand
(
4
,
4
)
.
astype
(
theano
.
config
.
floatX
)
a
=
rng
.
rand
(
4
,
4
)
.
astype
(
theano
.
config
.
floatX
)
n_u
,
n_v
,
n_t
=
n
umpy
.
linalg
.
svd
(
a
)
n_u
,
n_v
,
n_t
=
n
p
.
linalg
.
svd
(
a
)
t_u
,
t_v
,
t_t
=
fn
(
a
)
t_u
,
t_v
,
t_t
=
fn
(
a
)
assert
_allclose
(
n_u
,
t_u
)
assert
_allclose
(
n_u
,
t_u
)
...
@@ -143,19 +143,19 @@ def test_svd():
...
@@ -143,19 +143,19 @@ def test_svd():
def
test_tensorsolve
():
def
test_tensorsolve
():
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
A
=
tensor
.
tensor4
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
A
=
tensor
.
tensor4
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
B
=
tensor
.
matrix
(
"B"
,
dtype
=
theano
.
config
.
floatX
)
B
=
tensor
.
matrix
(
"B"
,
dtype
=
theano
.
config
.
floatX
)
X
=
tensorsolve
(
A
,
B
)
X
=
tensorsolve
(
A
,
B
)
fn
=
function
([
A
,
B
],
[
X
])
fn
=
function
([
A
,
B
],
[
X
])
# slightly modified example from n
umpy
.linalg.tensorsolve docstring
# slightly modified example from n
p
.linalg.tensorsolve docstring
a
=
n
umpy
.
eye
(
2
*
3
*
4
)
.
astype
(
theano
.
config
.
floatX
)
a
=
n
p
.
eye
(
2
*
3
*
4
)
.
astype
(
theano
.
config
.
floatX
)
a
.
shape
=
(
2
*
3
,
4
,
2
,
3
*
4
)
a
.
shape
=
(
2
*
3
,
4
,
2
,
3
*
4
)
b
=
rng
.
rand
(
2
*
3
,
4
)
.
astype
(
theano
.
config
.
floatX
)
b
=
rng
.
rand
(
2
*
3
,
4
)
.
astype
(
theano
.
config
.
floatX
)
n_x
=
n
umpy
.
linalg
.
tensorsolve
(
a
,
b
)
n_x
=
n
p
.
linalg
.
tensorsolve
(
a
,
b
)
t_x
=
fn
(
a
,
b
)
t_x
=
fn
(
a
,
b
)
assert
_allclose
(
n_x
,
t_x
)
assert
_allclose
(
n_x
,
t_x
)
...
@@ -165,10 +165,10 @@ def test_tensorsolve():
...
@@ -165,10 +165,10 @@ def test_tensorsolve():
Y
=
tensorsolve
(
C
,
D
)
Y
=
tensorsolve
(
C
,
D
)
fn
=
function
([
C
,
D
],
[
Y
])
fn
=
function
([
C
,
D
],
[
Y
])
c
=
n
umpy
.
eye
(
2
*
3
*
4
,
dtype
=
'float32'
)
c
=
n
p
.
eye
(
2
*
3
*
4
,
dtype
=
'float32'
)
c
.
shape
=
(
2
*
3
,
4
,
2
,
3
*
4
)
c
.
shape
=
(
2
*
3
,
4
,
2
,
3
*
4
)
d
=
rng
.
rand
(
2
*
3
,
4
)
.
astype
(
'float64'
)
d
=
rng
.
rand
(
2
*
3
,
4
)
.
astype
(
'float64'
)
n_y
=
n
umpy
.
linalg
.
tensorsolve
(
c
,
d
)
n_y
=
n
p
.
linalg
.
tensorsolve
(
c
,
d
)
t_y
=
fn
(
c
,
d
)
t_y
=
fn
(
c
,
d
)
assert
_allclose
(
n_y
,
t_y
)
assert
_allclose
(
n_y
,
t_y
)
assert
n_y
.
dtype
==
Y
.
dtype
assert
n_y
.
dtype
==
Y
.
dtype
...
@@ -179,68 +179,68 @@ def test_tensorsolve():
...
@@ -179,68 +179,68 @@ def test_tensorsolve():
Z
=
tensorsolve
(
E
,
F
)
Z
=
tensorsolve
(
E
,
F
)
fn
=
function
([
E
,
F
],
[
Z
])
fn
=
function
([
E
,
F
],
[
Z
])
e
=
n
umpy
.
eye
(
2
*
3
*
4
,
dtype
=
'int32'
)
e
=
n
p
.
eye
(
2
*
3
*
4
,
dtype
=
'int32'
)
e
.
shape
=
(
2
*
3
,
4
,
2
,
3
*
4
)
e
.
shape
=
(
2
*
3
,
4
,
2
,
3
*
4
)
f
=
rng
.
rand
(
2
*
3
,
4
)
.
astype
(
'float64'
)
f
=
rng
.
rand
(
2
*
3
,
4
)
.
astype
(
'float64'
)
n_z
=
n
umpy
.
linalg
.
tensorsolve
(
e
,
f
)
n_z
=
n
p
.
linalg
.
tensorsolve
(
e
,
f
)
t_z
=
fn
(
e
,
f
)
t_z
=
fn
(
e
,
f
)
assert
_allclose
(
n_z
,
t_z
)
assert
_allclose
(
n_z
,
t_z
)
assert
n_z
.
dtype
==
Z
.
dtype
assert
n_z
.
dtype
==
Z
.
dtype
def
test_inverse_singular
():
def
test_inverse_singular
():
singular
=
n
umpy
.
array
([[
1
,
0
,
0
]]
+
[[
0
,
1
,
0
]]
*
2
,
singular
=
n
p
.
array
([[
1
,
0
,
0
]]
+
[[
0
,
1
,
0
]]
*
2
,
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
a
=
tensor
.
matrix
()
a
=
tensor
.
matrix
()
f
=
function
([
a
],
matrix_inverse
(
a
))
f
=
function
([
a
],
matrix_inverse
(
a
))
try
:
try
:
f
(
singular
)
f
(
singular
)
except
n
umpy
.
linalg
.
LinAlgError
:
except
n
p
.
linalg
.
LinAlgError
:
return
return
assert
False
assert
False
def
test_inverse_grad
():
def
test_inverse_grad
():
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
r
=
rng
.
randn
(
4
,
4
)
r
=
rng
.
randn
(
4
,
4
)
tensor
.
verify_grad
(
matrix_inverse
,
[
r
],
rng
=
n
umpy
.
random
)
tensor
.
verify_grad
(
matrix_inverse
,
[
r
],
rng
=
n
p
.
random
)
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
r
=
rng
.
randn
(
4
,
4
)
r
=
rng
.
randn
(
4
,
4
)
tensor
.
verify_grad
(
matrix_inverse
,
[
r
],
rng
=
n
umpy
.
random
)
tensor
.
verify_grad
(
matrix_inverse
,
[
r
],
rng
=
n
p
.
random
)
def
test_det
():
def
test_det
():
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
r
=
rng
.
randn
(
5
,
5
)
.
astype
(
config
.
floatX
)
r
=
rng
.
randn
(
5
,
5
)
.
astype
(
config
.
floatX
)
x
=
tensor
.
matrix
()
x
=
tensor
.
matrix
()
f
=
theano
.
function
([
x
],
det
(
x
))
f
=
theano
.
function
([
x
],
det
(
x
))
assert
n
umpy
.
allclose
(
numpy
.
linalg
.
det
(
r
),
f
(
r
))
assert
n
p
.
allclose
(
np
.
linalg
.
det
(
r
),
f
(
r
))
def
test_det_grad
():
def
test_det_grad
():
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
r
=
rng
.
randn
(
5
,
5
)
.
astype
(
config
.
floatX
)
r
=
rng
.
randn
(
5
,
5
)
.
astype
(
config
.
floatX
)
tensor
.
verify_grad
(
det
,
[
r
],
rng
=
n
umpy
.
random
)
tensor
.
verify_grad
(
det
,
[
r
],
rng
=
n
p
.
random
)
def
test_det_shape
():
def
test_det_shape
():
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
r
=
rng
.
randn
(
5
,
5
)
.
astype
(
config
.
floatX
)
r
=
rng
.
randn
(
5
,
5
)
.
astype
(
config
.
floatX
)
x
=
tensor
.
matrix
()
x
=
tensor
.
matrix
()
f
=
theano
.
function
([
x
],
det
(
x
))
f
=
theano
.
function
([
x
],
det
(
x
))
f_shape
=
theano
.
function
([
x
],
det
(
x
)
.
shape
)
f_shape
=
theano
.
function
([
x
],
det
(
x
)
.
shape
)
assert
n
umpy
.
all
(
f
(
r
)
.
shape
==
f_shape
(
r
))
assert
n
p
.
all
(
f
(
r
)
.
shape
==
f_shape
(
r
))
class
test_diag
(
unittest
.
TestCase
):
class
test_diag
(
unittest
.
TestCase
):
"""
"""
Test that linalg.diag has the same behavior as n
umpy
.diag.
Test that linalg.diag has the same behavior as n
p
.diag.
n
umpy
.diag has two behaviors:
n
p
.diag has two behaviors:
(1) when given a vector, it returns a matrix with that vector as the
(1) when given a vector, it returns a matrix with that vector as the
diagonal.
diagonal.
(2) when given a matrix, returns a vector which is the diagonal of the
(2) when given a matrix, returns a vector which is the diagonal of the
...
@@ -263,7 +263,7 @@ class test_diag(unittest.TestCase):
...
@@ -263,7 +263,7 @@ class test_diag(unittest.TestCase):
super
(
test_diag
,
self
)
.
__init__
(
name
)
super
(
test_diag
,
self
)
.
__init__
(
name
)
def
test_alloc_diag
(
self
):
def
test_alloc_diag
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x
=
theano
.
tensor
.
vector
()
x
=
theano
.
tensor
.
vector
()
g
=
alloc_diag
(
x
)
g
=
alloc_diag
(
x
)
f
=
theano
.
function
([
x
],
g
)
f
=
theano
.
function
([
x
],
g
)
...
@@ -271,7 +271,7 @@ class test_diag(unittest.TestCase):
...
@@ -271,7 +271,7 @@ class test_diag(unittest.TestCase):
# test "normal" scenario (5x5 matrix) and special cases of 0x0 and 1x1
# test "normal" scenario (5x5 matrix) and special cases of 0x0 and 1x1
for
shp
in
[
5
,
0
,
1
]:
for
shp
in
[
5
,
0
,
1
]:
m
=
rng
.
rand
(
shp
)
.
astype
(
self
.
floatX
)
m
=
rng
.
rand
(
shp
)
.
astype
(
self
.
floatX
)
v
=
n
umpy
.
diag
(
m
)
v
=
n
p
.
diag
(
m
)
r
=
f
(
m
)
r
=
f
(
m
)
# The right matrix is created
# The right matrix is created
assert
(
r
==
v
)
.
all
()
assert
(
r
==
v
)
.
all
()
...
@@ -295,7 +295,7 @@ class test_diag(unittest.TestCase):
...
@@ -295,7 +295,7 @@ class test_diag(unittest.TestCase):
assert
(
f
(
m
)
==
m
.
shape
)
.
all
()
assert
(
f
(
m
)
==
m
.
shape
)
.
all
()
def
test_alloc_diag_grad
(
self
):
def
test_alloc_diag_grad
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x
=
rng
.
rand
(
5
)
x
=
rng
.
rand
(
5
)
tensor
.
verify_grad
(
alloc_diag
,
[
x
],
rng
=
rng
)
tensor
.
verify_grad
(
alloc_diag
,
[
x
],
rng
=
rng
)
...
@@ -322,7 +322,7 @@ class test_diag(unittest.TestCase):
...
@@ -322,7 +322,7 @@ class test_diag(unittest.TestCase):
# not testing the view=True case since it is not used anywhere.
# not testing the view=True case since it is not used anywhere.
def
test_extract_diag
(
self
):
def
test_extract_diag
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
m
=
rng
.
rand
(
2
,
3
)
.
astype
(
self
.
floatX
)
m
=
rng
.
rand
(
2
,
3
)
.
astype
(
self
.
floatX
)
x
=
self
.
shared
(
m
)
x
=
self
.
shared
(
m
)
g
=
extract_diag
(
x
)
g
=
extract_diag
(
x
)
...
@@ -334,7 +334,7 @@ class test_diag(unittest.TestCase):
...
@@ -334,7 +334,7 @@ class test_diag(unittest.TestCase):
for
shp
in
[(
2
,
3
),
(
3
,
2
),
(
3
,
3
),
(
1
,
1
),
(
0
,
0
)]:
for
shp
in
[(
2
,
3
),
(
3
,
2
),
(
3
,
3
),
(
1
,
1
),
(
0
,
0
)]:
m
=
rng
.
rand
(
*
shp
)
.
astype
(
self
.
floatX
)
m
=
rng
.
rand
(
*
shp
)
.
astype
(
self
.
floatX
)
x
.
set_value
(
m
)
x
.
set_value
(
m
)
v
=
n
umpy
.
diag
(
m
)
v
=
n
p
.
diag
(
m
)
r
=
f
()
r
=
f
()
# The right diagonal is extracted
# The right diagonal is extracted
assert
(
r
==
v
)
.
all
()
assert
(
r
==
v
)
.
all
()
...
@@ -360,13 +360,13 @@ class test_diag(unittest.TestCase):
...
@@ -360,13 +360,13 @@ class test_diag(unittest.TestCase):
assert
f
()
==
min
(
shp
)
assert
f
()
==
min
(
shp
)
def
test_extract_diag_grad
(
self
):
def
test_extract_diag_grad
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x
=
rng
.
rand
(
5
,
4
)
.
astype
(
self
.
floatX
)
x
=
rng
.
rand
(
5
,
4
)
.
astype
(
self
.
floatX
)
tensor
.
verify_grad
(
extract_diag
,
[
x
],
rng
=
rng
)
tensor
.
verify_grad
(
extract_diag
,
[
x
],
rng
=
rng
)
@attr
(
'slow'
)
@attr
(
'slow'
)
def
test_extract_diag_empty
(
self
):
def
test_extract_diag_empty
(
self
):
c
=
self
.
shared
(
n
umpy
.
array
([[],
[]],
self
.
floatX
))
c
=
self
.
shared
(
n
p
.
array
([[],
[]],
self
.
floatX
))
f
=
theano
.
function
([],
extract_diag
(
c
),
mode
=
self
.
mode
)
f
=
theano
.
function
([],
extract_diag
(
c
),
mode
=
self
.
mode
)
assert
[
isinstance
(
node
.
inputs
[
0
]
.
type
,
self
.
type
)
assert
[
isinstance
(
node
.
inputs
[
0
]
.
type
,
self
.
type
)
...
@@ -375,14 +375,14 @@ class test_diag(unittest.TestCase):
...
@@ -375,14 +375,14 @@ class test_diag(unittest.TestCase):
def
test_trace
():
def
test_trace
():
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x
=
theano
.
tensor
.
matrix
()
x
=
theano
.
tensor
.
matrix
()
g
=
trace
(
x
)
g
=
trace
(
x
)
f
=
theano
.
function
([
x
],
g
)
f
=
theano
.
function
([
x
],
g
)
for
shp
in
[(
2
,
3
),
(
3
,
2
),
(
3
,
3
)]:
for
shp
in
[(
2
,
3
),
(
3
,
2
),
(
3
,
3
)]:
m
=
rng
.
rand
(
*
shp
)
.
astype
(
config
.
floatX
)
m
=
rng
.
rand
(
*
shp
)
.
astype
(
config
.
floatX
)
v
=
n
umpy
.
trace
(
m
)
v
=
n
p
.
trace
(
m
)
assert
v
==
f
(
m
)
assert
v
==
f
(
m
)
xx
=
theano
.
tensor
.
vector
()
xx
=
theano
.
tensor
.
vector
()
...
@@ -401,9 +401,9 @@ class test_Eig(utt.InferShapeTester):
...
@@ -401,9 +401,9 @@ class test_Eig(utt.InferShapeTester):
def
setUp
(
self
):
def
setUp
(
self
):
super
(
test_Eig
,
self
)
.
setUp
()
super
(
test_Eig
,
self
)
.
setUp
()
self
.
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
self
.
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
self
.
A
=
theano
.
tensor
.
matrix
(
dtype
=
self
.
dtype
)
self
.
A
=
theano
.
tensor
.
matrix
(
dtype
=
self
.
dtype
)
self
.
X
=
n
umpy
.
asarray
(
self
.
rng
.
rand
(
5
,
5
),
self
.
X
=
n
p
.
asarray
(
self
.
rng
.
rand
(
5
,
5
),
dtype
=
self
.
dtype
)
dtype
=
self
.
dtype
)
self
.
S
=
self
.
X
.
dot
(
self
.
X
.
T
)
self
.
S
=
self
.
X
.
dot
(
self
.
X
.
T
)
...
@@ -423,7 +423,7 @@ class test_Eig(utt.InferShapeTester):
...
@@ -423,7 +423,7 @@ class test_Eig(utt.InferShapeTester):
[[
1.0
],
[[
1.0
]]])
[[
1.0
],
[[
1.0
]]])
x
=
[[
0
,
1
],
[
1
,
0
]]
x
=
[[
0
,
1
],
[
1
,
0
]]
w
,
v
=
[
e
.
eval
({
A
:
x
})
for
e
in
self
.
op
(
A
)]
w
,
v
=
[
e
.
eval
({
A
:
x
})
for
e
in
self
.
op
(
A
)]
assert_array_almost_equal
(
n
umpy
.
dot
(
x
,
v
),
w
*
v
)
assert_array_almost_equal
(
n
p
.
dot
(
x
,
v
),
w
*
v
)
class
test_Eigh
(
test_Eig
):
class
test_Eigh
(
test_Eig
):
...
@@ -435,8 +435,8 @@ class test_Eigh(test_Eig):
...
@@ -435,8 +435,8 @@ class test_Eigh(test_Eig):
wu
,
vu
=
[
out
.
eval
({
a
:
S
})
for
out
in
self
.
op
(
a
,
'U'
)]
wu
,
vu
=
[
out
.
eval
({
a
:
S
})
for
out
in
self
.
op
(
a
,
'U'
)]
wl
,
vl
=
[
out
.
eval
({
a
:
S
})
for
out
in
self
.
op
(
a
,
'L'
)]
wl
,
vl
=
[
out
.
eval
({
a
:
S
})
for
out
in
self
.
op
(
a
,
'L'
)]
assert_array_almost_equal
(
wu
,
wl
)
assert_array_almost_equal
(
wu
,
wl
)
assert_array_almost_equal
(
vu
*
n
umpy
.
sign
(
vu
[
0
,
:]),
assert_array_almost_equal
(
vu
*
n
p
.
sign
(
vu
[
0
,
:]),
vl
*
n
umpy
.
sign
(
vl
[
0
,
:]))
vl
*
n
p
.
sign
(
vl
[
0
,
:]))
def
test_grad
(
self
):
def
test_grad
(
self
):
X
=
self
.
X
X
=
self
.
X
...
@@ -466,12 +466,12 @@ class T_lstsq(unittest.TestCase):
...
@@ -466,12 +466,12 @@ class T_lstsq(unittest.TestCase):
z
=
tensor
.
lscalar
()
z
=
tensor
.
lscalar
()
b
=
theano
.
tensor
.
nlinalg
.
lstsq
()(
x
,
y
,
z
)
b
=
theano
.
tensor
.
nlinalg
.
lstsq
()(
x
,
y
,
z
)
f
=
function
([
x
,
y
,
z
],
b
)
f
=
function
([
x
,
y
,
z
],
b
)
TestMatrix1
=
n
umpy
.
asarray
([[
2
,
1
],
[
3
,
4
]])
TestMatrix1
=
n
p
.
asarray
([[
2
,
1
],
[
3
,
4
]])
TestMatrix2
=
n
umpy
.
asarray
([[
17
,
20
],
[
43
,
50
]])
TestMatrix2
=
n
p
.
asarray
([[
17
,
20
],
[
43
,
50
]])
TestScalar
=
n
umpy
.
asarray
(
1
)
TestScalar
=
n
p
.
asarray
(
1
)
f
=
function
([
x
,
y
,
z
],
b
)
f
=
function
([
x
,
y
,
z
],
b
)
m
=
f
(
TestMatrix1
,
TestMatrix2
,
TestScalar
)
m
=
f
(
TestMatrix1
,
TestMatrix2
,
TestScalar
)
self
.
assertTrue
(
n
umpy
.
allclose
(
TestMatrix2
,
numpy
.
dot
(
TestMatrix1
,
m
[
0
])))
self
.
assertTrue
(
n
p
.
allclose
(
TestMatrix2
,
np
.
dot
(
TestMatrix1
,
m
[
0
])))
def
test_wrong_coefficient_matrix
(
self
):
def
test_wrong_coefficient_matrix
(
self
):
x
=
tensor
.
vector
()
x
=
tensor
.
vector
()
...
@@ -479,7 +479,7 @@ class T_lstsq(unittest.TestCase):
...
@@ -479,7 +479,7 @@ class T_lstsq(unittest.TestCase):
z
=
tensor
.
scalar
()
z
=
tensor
.
scalar
()
b
=
theano
.
tensor
.
nlinalg
.
lstsq
()(
x
,
y
,
z
)
b
=
theano
.
tensor
.
nlinalg
.
lstsq
()(
x
,
y
,
z
)
f
=
function
([
x
,
y
,
z
],
b
)
f
=
function
([
x
,
y
,
z
],
b
)
self
.
assertRaises
(
n
umpy
.
linalg
.
linalg
.
LinAlgError
,
f
,
[
2
,
1
],
[
2
,
1
],
1
)
self
.
assertRaises
(
n
p
.
linalg
.
linalg
.
LinAlgError
,
f
,
[
2
,
1
],
[
2
,
1
],
1
)
def
test_wrong_rcond_dimension
(
self
):
def
test_wrong_rcond_dimension
(
self
):
x
=
tensor
.
vector
()
x
=
tensor
.
vector
()
...
@@ -487,24 +487,24 @@ class T_lstsq(unittest.TestCase):
...
@@ -487,24 +487,24 @@ class T_lstsq(unittest.TestCase):
z
=
tensor
.
vector
()
z
=
tensor
.
vector
()
b
=
theano
.
tensor
.
nlinalg
.
lstsq
()(
x
,
y
,
z
)
b
=
theano
.
tensor
.
nlinalg
.
lstsq
()(
x
,
y
,
z
)
f
=
function
([
x
,
y
,
z
],
b
)
f
=
function
([
x
,
y
,
z
],
b
)
self
.
assertRaises
(
n
umpy
.
linalg
.
LinAlgError
,
f
,
[
2
,
1
],
[
2
,
1
],
[
2
,
1
])
self
.
assertRaises
(
n
p
.
linalg
.
LinAlgError
,
f
,
[
2
,
1
],
[
2
,
1
],
[
2
,
1
])
class
Matrix_power
(
unittest
.
TestCase
):
class
Matrix_power
(
unittest
.
TestCase
):
def
test_numpy_compare
(
self
):
def
test_numpy_compare
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
A
=
tensor
.
matrix
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
A
=
tensor
.
matrix
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
Q
=
matrix_power
(
A
,
3
)
Q
=
matrix_power
(
A
,
3
)
fn
=
function
([
A
],
[
Q
])
fn
=
function
([
A
],
[
Q
])
a
=
rng
.
rand
(
4
,
4
)
.
astype
(
theano
.
config
.
floatX
)
a
=
rng
.
rand
(
4
,
4
)
.
astype
(
theano
.
config
.
floatX
)
n_p
=
n
umpy
.
linalg
.
matrix_power
(
a
,
3
)
n_p
=
n
p
.
linalg
.
matrix_power
(
a
,
3
)
t_p
=
fn
(
a
)
t_p
=
fn
(
a
)
assert
n
umpy
.
allclose
(
n_p
,
t_p
)
assert
n
p
.
allclose
(
n_p
,
t_p
)
def
test_non_square_matrix
(
self
):
def
test_non_square_matrix
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
A
=
tensor
.
matrix
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
A
=
tensor
.
matrix
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
Q
=
matrix_power
(
A
,
3
)
Q
=
matrix_power
(
A
,
3
)
f
=
function
([
A
],
[
Q
])
f
=
function
([
A
],
[
Q
])
...
@@ -524,10 +524,10 @@ class T_NormTests(unittest.TestCase):
...
@@ -524,10 +524,10 @@ class T_NormTests(unittest.TestCase):
self
.
assertRaises
(
ValueError
,
norm
,
3
,
None
)
self
.
assertRaises
(
ValueError
,
norm
,
3
,
None
)
def
test_tensor_input
(
self
):
def
test_tensor_input
(
self
):
self
.
assertRaises
(
NotImplementedError
,
norm
,
n
umpy
.
random
.
rand
(
3
,
4
,
5
),
None
)
self
.
assertRaises
(
NotImplementedError
,
norm
,
n
p
.
random
.
rand
(
3
,
4
,
5
),
None
)
def
test_numpy_compare
(
self
):
def
test_numpy_compare
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
M
=
tensor
.
matrix
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
M
=
tensor
.
matrix
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
V
=
tensor
.
vector
(
"V"
,
dtype
=
theano
.
config
.
floatX
)
V
=
tensor
.
vector
(
"V"
,
dtype
=
theano
.
config
.
floatX
)
...
@@ -543,7 +543,7 @@ class T_NormTests(unittest.TestCase):
...
@@ -543,7 +543,7 @@ class T_NormTests(unittest.TestCase):
for
i
in
range
(
0
,
14
):
for
i
in
range
(
0
,
14
):
f
=
function
([
A
[
1
][
i
]],
norm
(
A
[
1
][
i
],
A
[
0
][
i
]))
f
=
function
([
A
[
1
][
i
]],
norm
(
A
[
1
][
i
],
A
[
0
][
i
]))
t_n
=
f
(
A
[
2
][
i
])
t_n
=
f
(
A
[
2
][
i
])
n_n
=
n
umpy
.
linalg
.
norm
(
A
[
2
][
i
],
A
[
3
][
i
])
n_n
=
n
p
.
linalg
.
norm
(
A
[
2
][
i
],
A
[
3
][
i
])
assert
_allclose
(
n_n
,
t_n
)
assert
_allclose
(
n_n
,
t_n
)
...
@@ -552,9 +552,9 @@ class test_TensorInv(utt.InferShapeTester):
...
@@ -552,9 +552,9 @@ class test_TensorInv(utt.InferShapeTester):
super
(
test_TensorInv
,
self
)
.
setUp
()
super
(
test_TensorInv
,
self
)
.
setUp
()
self
.
A
=
tensor
.
tensor4
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
self
.
A
=
tensor
.
tensor4
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
self
.
B
=
tensor
.
tensor3
(
"B"
,
dtype
=
theano
.
config
.
floatX
)
self
.
B
=
tensor
.
tensor3
(
"B"
,
dtype
=
theano
.
config
.
floatX
)
self
.
a
=
n
umpy
.
random
.
rand
(
4
,
6
,
8
,
3
)
.
astype
(
theano
.
config
.
floatX
)
self
.
a
=
n
p
.
random
.
rand
(
4
,
6
,
8
,
3
)
.
astype
(
theano
.
config
.
floatX
)
self
.
b
=
n
umpy
.
random
.
rand
(
2
,
15
,
30
)
.
astype
(
theano
.
config
.
floatX
)
self
.
b
=
n
p
.
random
.
rand
(
2
,
15
,
30
)
.
astype
(
theano
.
config
.
floatX
)
self
.
b1
=
n
umpy
.
random
.
rand
(
30
,
2
,
15
)
.
astype
(
theano
.
config
.
floatX
)
# for ind=1 since we need prod(b1.shape[:ind]) == prod(b1.shape[ind:])
self
.
b1
=
n
p
.
random
.
rand
(
30
,
2
,
15
)
.
astype
(
theano
.
config
.
floatX
)
# for ind=1 since we need prod(b1.shape[:ind]) == prod(b1.shape[ind:])
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
A
=
self
.
A
A
=
self
.
A
...
@@ -567,7 +567,7 @@ class test_TensorInv(utt.InferShapeTester):
...
@@ -567,7 +567,7 @@ class test_TensorInv(utt.InferShapeTester):
def
test_eval
(
self
):
def
test_eval
(
self
):
A
=
self
.
A
A
=
self
.
A
Ai
=
tensorinv
(
A
)
Ai
=
tensorinv
(
A
)
n_ainv
=
n
umpy
.
linalg
.
tensorinv
(
self
.
a
)
n_ainv
=
n
p
.
linalg
.
tensorinv
(
self
.
a
)
tf_a
=
function
([
A
],
[
Ai
])
tf_a
=
function
([
A
],
[
Ai
])
t_ainv
=
tf_a
(
self
.
a
)
t_ainv
=
tf_a
(
self
.
a
)
assert
_allclose
(
n_ainv
,
t_ainv
)
assert
_allclose
(
n_ainv
,
t_ainv
)
...
@@ -575,8 +575,8 @@ class test_TensorInv(utt.InferShapeTester):
...
@@ -575,8 +575,8 @@ class test_TensorInv(utt.InferShapeTester):
B
=
self
.
B
B
=
self
.
B
Bi
=
tensorinv
(
B
)
Bi
=
tensorinv
(
B
)
Bi1
=
tensorinv
(
B
,
ind
=
1
)
Bi1
=
tensorinv
(
B
,
ind
=
1
)
n_binv
=
n
umpy
.
linalg
.
tensorinv
(
self
.
b
)
n_binv
=
n
p
.
linalg
.
tensorinv
(
self
.
b
)
n_binv1
=
n
umpy
.
linalg
.
tensorinv
(
self
.
b1
,
ind
=
1
)
n_binv1
=
n
p
.
linalg
.
tensorinv
(
self
.
b1
,
ind
=
1
)
tf_b
=
function
([
B
],
[
Bi
])
tf_b
=
function
([
B
],
[
Bi
])
tf_b1
=
function
([
B
],
[
Bi1
])
tf_b1
=
function
([
B
],
[
Bi1
])
t_binv
=
tf_b
(
self
.
b
)
t_binv
=
tf_b
(
self
.
b
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论