Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
94148810
提交
94148810
authored
7月 21, 2014
作者:
Tanjay94
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fixed tests transfer for numpy based function.
上级
65908b64
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
109 行增加
和
111 行删除
+109
-111
ops.py
theano/sandbox/linalg/ops.py
+3
-52
test_linalg.py
theano/sandbox/linalg/tests/test_linalg.py
+0
-56
nlinalg.py
theano/tensor/nlinalg.py
+46
-2
test_nlinalg.py
theano/tensor/tests/test_nlinalg.py
+60
-1
没有找到文件。
theano/sandbox/linalg/ops.py
浏览文件 @
94148810
...
...
@@ -36,7 +36,9 @@ from theano.tensor.nlinalg import ( MatrixInverse,
_zero_disconnected
,
qr
,
svd
,
lstsq
lstsq
,
matrix_power
,
norm
)
from
theano.tensor.slinalg
import
(
Cholesky
,
...
...
@@ -378,54 +380,3 @@ def spectral_radius_bound(X, log2_exponent):
return
tensor
.
pow
(
trace
(
XX
),
2
**
(
-
log2_exponent
))
def
matrix_power
(
M
,
n
):
result
=
1
for
i
in
xrange
(
n
):
result
=
theano
.
dot
(
result
,
M
)
return
result
def
norm
(
x
,
ord
):
x
=
as_tensor_variable
(
x
)
ndim
=
x
.
ndim
if
ndim
==
0
:
raise
ValueError
(
"'axis' entry is out of bounds."
)
elif
ndim
==
1
:
if
ord
==
None
:
return
tensor
.
sum
(
x
**
2
)
**
0.5
elif
ord
==
'inf'
:
return
tensor
.
max
(
abs
(
x
))
elif
ord
==
'-inf'
:
return
tensor
.
min
(
abs
(
x
))
elif
ord
==
0
:
return
x
[
x
.
nonzero
()]
.
shape
[
0
]
else
:
try
:
z
=
tensor
.
sum
(
abs
(
x
**
ord
))
**
(
1.
/
ord
)
except
TypeError
:
raise
ValueError
(
"Invalid norm order for vectors."
)
return
z
elif
ndim
==
2
:
if
ord
==
None
or
ord
==
'fro'
:
return
tensor
.
sum
(
abs
(
x
**
2
))
**
(
0.5
)
elif
ord
==
'inf'
:
return
tensor
.
max
(
tensor
.
sum
(
abs
(
x
),
1
))
elif
ord
==
'-inf'
:
return
tensor
.
min
(
tensor
.
sum
(
abs
(
x
),
1
))
elif
ord
==
1
:
return
tensor
.
max
(
tensor
.
sum
(
abs
(
x
),
0
))
elif
ord
==
-
1
:
return
tensor
.
min
(
tensor
.
sum
(
abs
(
x
),
0
))
else
:
raise
ValueError
()
elif
ndim
>
2
:
raise
NotImplementedError
(
"We don't support norm witn ndim > 2"
)
theano/sandbox/linalg/tests/test_linalg.py
浏览文件 @
94148810
...
...
@@ -137,59 +137,3 @@ def test_spectral_radius_bound():
except
ValueError
:
ok
=
True
assert
ok
class
Matrix_power
():
def
test_numpy_compare
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
A
=
tensor
.
matrix
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
Q
=
matrix_power
(
A
,
3
)
fn
=
function
([
A
],
[
Q
])
a
=
rng
.
rand
(
4
,
4
)
.
astype
(
theano
.
config
.
floatX
)
n_p
=
numpy
.
linalg
.
matrix_power
(
a
,
3
)
t_p
=
fn
(
a
)
assert
numpy
.
allclose
(
n_p
,
t_p
)
def
test_non_square_matrix
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
A
=
tensor
.
matrix
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
Q
=
matrix_power
(
A
,
3
)
f
=
function
([
A
],
[
Q
])
a
=
rng
.
rand
(
4
,
3
)
.
astype
(
theano
.
config
.
floatX
)
self
.
assertRaises
(
ValueError
,
f
,
a
)
class
T_NormTests
(
unittest
.
TestCase
):
def
test_wrong_type_of_ord_for_vector
(
self
):
self
.
assertRaises
(
ValueError
,
norm
,
[
2
,
1
],
'fro'
)
def
test_wrong_type_of_ord_for_matrix
(
self
):
self
.
assertRaises
(
ValueError
,
norm
,
[[
2
,
1
],
[
3
,
4
]],
0
)
def
test_non_tensorial_input
(
self
):
self
.
assertRaises
(
ValueError
,
norm
,
3
,
None
)
def
test_tensor_input
(
self
):
self
.
assertRaises
(
NotImplementedError
,
norm
,
numpy
.
random
.
rand
(
3
,
4
,
5
),
None
)
def
test_numpy_compare
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
M
=
tensor
.
matrix
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
V
=
tensor
.
vector
(
"V"
,
dtype
=
theano
.
config
.
floatX
)
a
=
rng
.
rand
(
4
,
4
)
.
astype
(
theano
.
config
.
floatX
)
b
=
rng
.
rand
(
4
)
.
astype
(
theano
.
config
.
floatX
)
A
=
(
[
None
,
'fro'
,
'inf'
,
'-inf'
,
1
,
-
1
,
None
,
'inf'
,
'-inf'
,
0
,
1
,
-
1
,
2
,
-
2
],
[
M
,
M
,
M
,
M
,
M
,
M
,
V
,
V
,
V
,
V
,
V
,
V
,
V
,
V
],
[
a
,
a
,
a
,
a
,
a
,
a
,
b
,
b
,
b
,
b
,
b
,
b
,
b
,
b
],
[
None
,
'fro'
,
inf
,
-
inf
,
1
,
-
1
,
None
,
inf
,
-
inf
,
0
,
1
,
-
1
,
2
,
-
2
])
for
i
in
range
(
0
,
14
):
f
=
function
([
A
[
1
][
i
]],
norm
(
A
[
1
][
i
],
A
[
0
][
i
]))
t_n
=
f
(
A
[
2
][
i
])
n_n
=
numpy
.
linalg
.
norm
(
A
[
2
][
i
],
A
[
3
][
i
])
assert
_allclose
(
n_n
,
t_n
)
theano/tensor/nlinalg.py
浏览文件 @
94148810
...
...
@@ -727,4 +727,49 @@ class lstsq(theano.Op):
outputs
[
0
][
0
]
=
zz
[
0
]
outputs
[
1
][
0
]
=
zz
[
1
]
outputs
[
2
][
0
]
=
zz
[
2
]
outputs
[
3
][
0
]
=
zz
[
3
]
\ No newline at end of file
outputs
[
3
][
0
]
=
zz
[
3
]
def
matrix_power
(
M
,
n
):
result
=
1
for
i
in
xrange
(
n
):
result
=
theano
.
dot
(
result
,
M
)
return
result
def
norm
(
x
,
ord
):
x
=
as_tensor_variable
(
x
)
ndim
=
x
.
ndim
if
ndim
==
0
:
raise
ValueError
(
"'axis' entry is out of bounds."
)
elif
ndim
==
1
:
if
ord
==
None
:
return
tensor
.
sum
(
x
**
2
)
**
0.5
elif
ord
==
'inf'
:
return
tensor
.
max
(
abs
(
x
))
elif
ord
==
'-inf'
:
return
tensor
.
min
(
abs
(
x
))
elif
ord
==
0
:
return
x
[
x
.
nonzero
()]
.
shape
[
0
]
else
:
try
:
z
=
tensor
.
sum
(
abs
(
x
**
ord
))
**
(
1.
/
ord
)
except
TypeError
:
raise
ValueError
(
"Invalid norm order for vectors."
)
return
z
elif
ndim
==
2
:
if
ord
==
None
or
ord
==
'fro'
:
return
tensor
.
sum
(
abs
(
x
**
2
))
**
(
0.5
)
elif
ord
==
'inf'
:
return
tensor
.
max
(
tensor
.
sum
(
abs
(
x
),
1
))
elif
ord
==
'-inf'
:
return
tensor
.
min
(
tensor
.
sum
(
abs
(
x
),
1
))
elif
ord
==
1
:
return
tensor
.
max
(
tensor
.
sum
(
abs
(
x
),
0
))
elif
ord
==
-
1
:
return
tensor
.
min
(
tensor
.
sum
(
abs
(
x
),
0
))
else
:
raise
ValueError
(
0
)
elif
ndim
>
2
:
raise
NotImplementedError
(
"We don't support norm witn ndim > 2"
)
theano/tensor/tests/test_nlinalg.py
浏览文件 @
94148810
...
...
@@ -32,7 +32,9 @@ from theano.tensor.nlinalg import ( MatrixInverse,
eigh
,
matrix_dot
,
_zero_disconnected
,
qr
qr
,
matrix_power
,
norm
)
from
nose.plugins.skip
import
SkipTest
...
...
@@ -432,3 +434,59 @@ class T_lstsq(unittest.TestCase):
f
=
function
([
x
,
y
,
z
],
b
)
self
.
assertRaises
(
numpy
.
linalg
.
LinAlgError
,
f
,
[
2
,
1
],
[
2
,
1
],
[
2
,
1
])
class
Matrix_power
():
def
test_numpy_compare
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
A
=
tensor
.
matrix
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
Q
=
matrix_power
(
A
,
3
)
fn
=
function
([
A
],
[
Q
])
a
=
rng
.
rand
(
4
,
4
)
.
astype
(
theano
.
config
.
floatX
)
n_p
=
numpy
.
linalg
.
matrix_power
(
a
,
3
)
t_p
=
fn
(
a
)
assert
numpy
.
allclose
(
n_p
,
t_p
)
def
test_non_square_matrix
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
A
=
tensor
.
matrix
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
Q
=
matrix_power
(
A
,
3
)
f
=
function
([
A
],
[
Q
])
a
=
rng
.
rand
(
4
,
3
)
.
astype
(
theano
.
config
.
floatX
)
self
.
assertRaises
(
ValueError
,
f
,
a
)
class
T_NormTests
(
unittest
.
TestCase
):
def
test_wrong_type_of_ord_for_vector
(
self
):
self
.
assertRaises
(
ValueError
,
norm
,
[
2
,
1
],
'fro'
)
def
test_wrong_type_of_ord_for_matrix
(
self
):
self
.
assertRaises
(
ValueError
,
norm
,
[[
2
,
1
],
[
3
,
4
]],
0
)
def
test_non_tensorial_input
(
self
):
self
.
assertRaises
(
ValueError
,
norm
,
3
,
None
)
def
test_tensor_input
(
self
):
self
.
assertRaises
(
NotImplementedError
,
norm
,
numpy
.
random
.
rand
(
3
,
4
,
5
),
None
)
def
test_numpy_compare
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
M
=
tensor
.
matrix
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
V
=
tensor
.
vector
(
"V"
,
dtype
=
theano
.
config
.
floatX
)
a
=
rng
.
rand
(
4
,
4
)
.
astype
(
theano
.
config
.
floatX
)
b
=
rng
.
rand
(
4
)
.
astype
(
theano
.
config
.
floatX
)
A
=
(
[
None
,
'fro'
,
'inf'
,
'-inf'
,
1
,
-
1
,
None
,
'inf'
,
'-inf'
,
0
,
1
,
-
1
,
2
,
-
2
],
[
M
,
M
,
M
,
M
,
M
,
M
,
V
,
V
,
V
,
V
,
V
,
V
,
V
,
V
],
[
a
,
a
,
a
,
a
,
a
,
a
,
b
,
b
,
b
,
b
,
b
,
b
,
b
,
b
],
[
None
,
'fro'
,
inf
,
-
inf
,
1
,
-
1
,
None
,
inf
,
-
inf
,
0
,
1
,
-
1
,
2
,
-
2
])
for
i
in
range
(
0
,
14
):
f
=
function
([
A
[
1
][
i
]],
norm
(
A
[
1
][
i
],
A
[
0
][
i
]))
t_n
=
f
(
A
[
2
][
i
])
n_n
=
numpy
.
linalg
.
norm
(
A
[
2
][
i
],
A
[
3
][
i
])
assert
_allclose
(
n_n
,
t_n
)
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论