Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
085ebae0
提交
085ebae0
authored
5月 12, 2014
作者:
Frédéric Bastien
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1846 from rmcgibbo/geigvalsh
[ENH] Hermitian generalized eigenvalues and gradient
上级
daf7fc95
ad2d2050
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
134 行增加
和
2 行删除
+134
-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
+28
-1
没有找到文件。
theano/sandbox/linalg/__init__.py
浏览文件 @
085ebae0
...
...
@@ -2,5 +2,5 @@
from
kron
import
kron
from
ops
import
(
cholesky
,
matrix_inverse
,
solve
,
diag
,
extract_diag
,
alloc_diag
,
det
,
psd
,
eig
,
eigh
,
det
,
psd
,
eig
,
eigh
,
eigvalsh
,
trace
,
spectral_radius_bound
)
theano/sandbox/linalg/ops.py
浏览文件 @
085ebae0
...
...
@@ -1097,3 +1097,108 @@ class EighGrad(Op):
def
infer_shape
(
self
,
node
,
shapes
):
return
[
shapes
[
0
]]
class
Eigvalsh
(
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 Eigvalsh 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
,)):
w
[
0
]
=
scipy
.
linalg
.
eigvalsh
(
a
=
a
,
b
=
b
,
lower
=
self
.
lower
)
def
grad
(
self
,
inputs
,
g_outputs
):
a
,
b
=
inputs
gw
,
=
g_outputs
return
EigvalshGrad
(
self
.
lower
)(
a
,
b
,
gw
)
def
infer_shape
(
self
,
node
,
shapes
):
n
=
shapes
[
0
][
0
]
return
[(
n
,)]
class
EigvalshGrad
(
Op
):
"""Gradient of generalized eigenvalues of a Hermetian positive definite
eigensystem
"""
# Note: This Op (EigvalshGrad), should be removed and replaced with a graph
# of theano ops that is constructed directly in Eigvalsh.grad.
# But this can only be done once scipy.linalg.eigh is available as an Op
# (currently the Eigh uses numpy.linalg.eigh, which doesn't let you
# pass the right-hand-side matrix for a generalized eigenproblem.) See the
# discussion on github at
# https://github.com/Theano/Theano/pull/1846#discussion-diff-12486764
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
):
w
,
v
=
scipy
.
linalg
.
eigh
(
a
,
b
,
lower
=
self
.
lower
)
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
eigvalsh
(
a
,
b
,
lower
=
True
):
return
Eigvalsh
(
lower
)(
a
,
b
)
theano/sandbox/linalg/tests/test_linalg.py
浏览文件 @
085ebae0
...
...
@@ -32,7 +32,7 @@ from theano.sandbox.linalg.ops import (cholesky,
Eig
,
inv_as_solve
,
)
from
theano.sandbox.linalg
import
eig
,
eigh
from
theano.sandbox.linalg
import
eig
,
eigh
,
eigvalsh
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.attrib
import
attr
...
...
@@ -573,3 +573,30 @@ def test_matrix_inverse_solve():
node
=
matrix_inverse
(
A
)
.
dot
(
b
)
.
owner
[
out
]
=
inv_as_solve
.
transform
(
node
)
assert
isinstance
(
out
.
owner
.
op
,
Solve
)
def
test_eigvalsh
():
if
not
imported_scipy
:
raise
SkipTest
(
"Scipy needed for the geigvalsh op."
)
import
scipy.linalg
A
=
theano
.
tensor
.
dmatrix
(
'a'
)
B
=
theano
.
tensor
.
dmatrix
(
'b'
)
f
=
function
([
A
,
B
],
eigvalsh
(
A
,
B
))
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
a
=
rng
.
randn
(
5
,
5
)
a
=
a
+
a
.
T
for
b
in
[
10
*
numpy
.
eye
(
5
,
5
)
+
rng
.
randn
(
5
,
5
),
None
]:
w
=
f
(
a
,
b
)
refw
=
scipy
.
linalg
.
eigvalsh
(
a
,
b
)
numpy
.
testing
.
assert_array_almost_equal
(
w
,
refw
)
def
test_eigvalsh_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
:
eigvalsh
(
a
,
b
)
.
dot
([
1
,
2
,
3
,
4
,
5
]),
[
a
,
b
],
rng
=
numpy
.
random
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论