Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
66d82e36
提交
66d82e36
authored
5月 09, 2014
作者:
Robert McGibbon
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
initial commit
上级
daf7fc95
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
135 行增加
和
2 行删除
+135
-2
__init__.py
theano/sandbox/linalg/__init__.py
+1
-1
ops.py
theano/sandbox/linalg/ops.py
+105
-0
test_linalg.py
theano/sandbox/linalg/tests/test_linalg.py
+29
-1
没有找到文件。
theano/sandbox/linalg/__init__.py
浏览文件 @
66d82e36
...
@@ -2,5 +2,5 @@
...
@@ -2,5 +2,5 @@
from
kron
import
kron
from
kron
import
kron
from
ops
import
(
cholesky
,
matrix_inverse
,
solve
,
from
ops
import
(
cholesky
,
matrix_inverse
,
solve
,
diag
,
extract_diag
,
alloc_diag
,
diag
,
extract_diag
,
alloc_diag
,
det
,
psd
,
eig
,
eigh
,
det
,
psd
,
eig
,
eigh
,
geigvalsh
,
trace
,
spectral_radius_bound
)
trace
,
spectral_radius_bound
)
theano/sandbox/linalg/ops.py
浏览文件 @
66d82e36
...
@@ -1097,3 +1097,108 @@ class EighGrad(Op):
...
@@ -1097,3 +1097,108 @@ class EighGrad(Op):
def
infer_shape
(
self
,
node
,
shapes
):
def
infer_shape
(
self
,
node
,
shapes
):
return
[
shapes
[
0
]]
return
[
shapes
[
0
]]
class
GEigvalsh
(
Op
):
"""Generalized eigenvalues of a Hermetian positive definite eigensystem
"""
def
__init__
(
self
,
lower
=
True
):
assert
lower
in
[
True
,
False
]
self
.
lower
=
lower
def
props
(
self
):
return
(
self
.
lower
,)
def
__hash__
(
self
):
return
hash
((
type
(
self
),
self
.
props
()))
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
)
and
self
.
props
()
==
other
.
props
())
def
make_node
(
self
,
a
,
b
):
assert
imported_scipy
,
(
"Scipy not available. Scipy is needed for the GEigvalsh op"
)
a
,
b
=
map
(
as_tensor_variable
,
(
a
,
b
))
assert
a
.
ndim
==
2
assert
b
.
ndim
==
2
out_dtype
=
theano
.
scalar
.
upcast
(
a
.
dtype
,
b
.
dtype
)
w
=
theano
.
tensor
.
vector
(
dtype
=
out_dtype
)
return
Apply
(
self
,
[
a
,
b
],
[
w
])
def
perform
(
self
,
node
,
(
a
,
b
),
(
w
,)):
try
:
w
[
0
]
=
scipy
.
linalg
.
eigvalsh
(
a
=
a
,
b
=
b
,
lower
=
self
.
lower
)
except
numpy
.
linalg
.
LinAlgError
:
logger
.
debug
(
'Failed to find generalized eigs of
%
s'
%
(
node
.
inputs
[
0
]))
raise
def
grad
(
self
,
inputs
,
g_outputs
):
a
,
b
=
inputs
gw
,
=
g_outputs
return
GEigvalshGrad
(
self
.
lower
)(
a
,
b
,
gw
)
def
infer_shape
(
self
,
node
,
shapes
):
n
=
shapes
[
0
][
0
]
return
[(
n
,)]
class
GEigvalshGrad
(
Op
):
"""Gradient of generalized eigenvalues of a Hermetian positive definite
eigensystem
"""
def
__init__
(
self
,
lower
=
True
):
assert
lower
in
[
True
,
False
]
self
.
lower
=
lower
if
lower
:
self
.
tri0
=
numpy
.
tril
self
.
tri1
=
lambda
a
:
numpy
.
triu
(
a
,
1
)
else
:
self
.
tri0
=
numpy
.
triu
self
.
tri1
=
lambda
a
:
numpy
.
tril
(
a
,
-
1
)
def
props
(
self
):
return
(
self
.
lower
,)
def
__hash__
(
self
):
return
hash
((
type
(
self
),
self
.
props
()))
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
)
and
self
.
props
()
==
other
.
props
())
def
make_node
(
self
,
a
,
b
,
gw
):
assert
imported_scipy
,
(
"Scipy not available. Scipy is needed for the GEigvalsh op"
)
a
,
b
,
gw
=
map
(
as_tensor_variable
,
(
a
,
b
,
gw
))
assert
a
.
ndim
==
2
assert
b
.
ndim
==
2
assert
gw
.
ndim
==
1
out_dtype
=
theano
.
scalar
.
upcast
(
a
.
dtype
,
b
.
dtype
,
gw
.
dtype
)
out1
=
theano
.
tensor
.
matrix
(
dtype
=
out_dtype
)
out2
=
theano
.
tensor
.
matrix
(
dtype
=
out_dtype
)
return
Apply
(
self
,
[
a
,
b
,
gw
],
[
out1
,
out2
])
def
perform
(
self
,
node
,
(
a
,
b
,
gw
),
outputs
):
N
=
a
.
shape
[
0
]
w
,
v
=
scipy
.
linalg
.
eigh
(
a
,
b
,
lower
=
True
)
gA
=
v
.
dot
(
numpy
.
diag
(
gw
)
.
dot
(
v
.
T
))
gB
=
-
v
.
dot
(
numpy
.
diag
(
gw
*
w
)
.
dot
(
v
.
T
))
# See EighGrad comments for an explanation of these lines
out1
=
self
.
tri0
(
gA
)
+
self
.
tri1
(
gA
)
.
T
out2
=
self
.
tri0
(
gB
)
+
self
.
tri1
(
gB
)
.
T
outputs
[
0
][
0
]
=
numpy
.
asarray
(
out1
,
dtype
=
node
.
outputs
[
0
]
.
dtype
)
outputs
[
1
][
0
]
=
numpy
.
asarray
(
out2
,
dtype
=
node
.
outputs
[
1
]
.
dtype
)
def
infer_shape
(
self
,
node
,
shapes
):
return
[
shapes
[
0
],
shapes
[
1
]]
def
geigvalsh
(
a
,
b
,
lower
=
True
):
return
GEigvalsh
(
lower
)(
a
,
b
)
theano/sandbox/linalg/tests/test_linalg.py
浏览文件 @
66d82e36
...
@@ -32,7 +32,7 @@ from theano.sandbox.linalg.ops import (cholesky,
...
@@ -32,7 +32,7 @@ from theano.sandbox.linalg.ops import (cholesky,
Eig
,
Eig
,
inv_as_solve
,
inv_as_solve
,
)
)
from
theano.sandbox.linalg
import
eig
,
eigh
from
theano.sandbox.linalg
import
eig
,
eigh
,
geigvalsh
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.attrib
import
attr
from
nose.plugins.attrib
import
attr
...
@@ -573,3 +573,31 @@ def test_matrix_inverse_solve():
...
@@ -573,3 +573,31 @@ def test_matrix_inverse_solve():
node
=
matrix_inverse
(
A
)
.
dot
(
b
)
.
owner
node
=
matrix_inverse
(
A
)
.
dot
(
b
)
.
owner
[
out
]
=
inv_as_solve
.
transform
(
node
)
[
out
]
=
inv_as_solve
.
transform
(
node
)
assert
isinstance
(
out
.
owner
.
op
,
Solve
)
assert
isinstance
(
out
.
owner
.
op
,
Solve
)
def
test_geigvalsh
():
if
not
imported_scipy
:
raise
SkipTest
(
"Scipy needed for the Solve op."
)
import
scipy.linalg
A
=
theano
.
tensor
.
dmatrix
(
'a'
)
B
=
theano
.
tensor
.
dmatrix
(
'b'
)
f
=
function
([
A
,
B
],
geigvalsh
(
A
,
B
))
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
a
=
rng
.
randn
(
5
,
5
)
a
=
a
+
a
.
T
b
=
10
*
numpy
.
eye
(
5
,
5
)
+
rng
.
randn
(
5
,
5
)
w
=
f
(
a
,
b
)
refw
=
scipy
.
linalg
.
eigvalsh
(
a
,
b
)
numpy
.
testing
.
assert_array_almost_equal
(
w
,
refw
)
def
test_geigvalsh_grad
():
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
a
=
rng
.
randn
(
5
,
5
)
a
=
a
+
a
.
T
b
=
10
*
numpy
.
eye
(
5
,
5
)
+
rng
.
randn
(
5
,
5
)
tensor
.
verify_grad
(
lambda
a
,
b
:
geigvalsh
(
a
,
b
)
.
dot
([
1
,
2
,
3
,
4
,
5
]),
[
a
,
b
],
rng
=
numpy
.
random
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论