Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
1394b39b
提交
1394b39b
authored
11月 22, 2014
作者:
Robert McGibbon
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Clean stuff up, no matrix inverse
上级
e412b2bd
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
33 行增加
和
30 行删除
+33
-30
slinalg.py
theano/tensor/slinalg.py
+10
-5
test_slinalg.py
theano/tensor/tests/test_slinalg.py
+23
-25
没有找到文件。
theano/tensor/slinalg.py
浏览文件 @
1394b39b
import
logging
import
warnings
logger
=
logging
.
getLogger
(
__name__
)
import
numpy
...
...
@@ -370,17 +371,21 @@ class ExpmGrad(Op):
return
[
shapes
[
0
]]
def
perform
(
self
,
node
,
(
A
,
gw
),
(
out
,)):
w
,
M
=
scipy
.
linalg
.
eig
(
A
)
# Kalbfleisch and Lawless, J. Am. Stat. Assoc. 80 (1985) Equation 3.4
w
,
UL
,
UR
=
scipy
.
linalg
.
eig
(
A
,
left
=
True
,
right
=
True
)
G
=
scipy
.
linalg
.
solve
(
M
,
gw
)
.
dot
(
M
)
if
numpy
.
linalg
.
norm
(
w
-
numpy
.
real
(
w
))
>
numpy
.
finfo
(
A
.
dtype
)
.
eps
:
warnings
.
warn
(
"ExpmGrad not correct for matrices with complex "
"eigenvalues"
)
G
=
(
UR
.
conj
()
.
T
)
.
dot
(
gw
)
.
dot
(
UL
)
exp_w
=
numpy
.
exp
(
w
)
V
=
numpy
.
subtract
.
outer
(
exp_w
,
exp_w
)
/
numpy
.
subtract
.
outer
(
w
,
w
)
V
[
numpy
.
diag_indices_from
(
V
)]
=
exp_w
numpy
.
multiply
(
V
,
G
,
V
)
V
=
numpy
.
multiply
(
V
,
G
,
V
)
Mi
=
scipy
.
linalg
.
inv
(
M
)
out
[
0
]
=
numpy
.
real
(
M
.
dot
(
V
)
.
dot
(
Mi
))
out
[
0
]
=
numpy
.
real
(
UL
.
dot
(
V
)
.
dot
(
UR
.
conj
()
.
T
))
.
astype
(
A
.
dtype
)
def
expm
(
A
):
...
...
theano/tensor/tests/test_slinalg.py
浏览文件 @
1394b39b
...
...
@@ -224,31 +224,29 @@ def test_expm_grad_2():
raise
SkipTest
(
"Scipy needed for the expm op."
)
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
A
=
rng
.
randn
(
3
,
3
)
.
astype
(
config
.
floatX
)
A
=
A
tensor
.
verify_grad
(
expm
,
[
A
,],
rng
=
rng
)
def
test_expm_grad_3
():
if
not
imported_scipy
:
raise
SkipTest
(
"Scipy needed for the expm op."
)
from
theano.gradient
import
grad
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
A
=
rng
.
randn
(
3
,
3
)
.
astype
(
config
.
floatX
)
h
=
1e-7
def
e
(
i
,
j
):
v
=
numpy
.
zeros
((
3
,
3
))
v
[
i
,
j
]
=
1
return
v
x
=
tensor
.
matrix
()
grad_expm_f
=
function
([
x
],
grad
(
expm
(
x
)[
0
,
1
],
x
))
expm_f
=
function
([
x
],
expm
(
x
)[
0
,
1
])
g
=
lambda
i
,
j
:
(
expm_f
(
A
+
h
*
e
(
i
,
j
))
-
expm_f
(
A
))
/
h
numgrad
=
numpy
.
array
([[
g
(
i
,
j
)
for
i
in
range
(
3
)]
for
j
in
range
(
3
)])
print
(
grad_expm_f
(
A
))
print
(
numgrad
)
# def test_expm_grad_3():
# if not imported_scipy:
# raise SkipTest("Scipy needed for the expm op.")
# from theano.gradient import grad
# rng = numpy.random.RandomState(utt.fetch_seed())
# A = rng.randn(3, 3).astype(config.floatX)
#
# h = 1e-7
# def e(i,j):
# v = numpy.zeros((3, 3))
# v[i, j] = 1
# return v
#
# x = tensor.matrix()
# grad_expm_f = function([x], grad(expm(x)[0,0], x))
# expm_f = function([x], expm(x)[0,0])
#
# g = lambda i, j: (expm_f(A + h*e(i,j)) - expm_f(A)) / h
# numgrad = numpy.array([[g(i,j) for i in range(3)] for j in range(3)])
#
# print(grad_expm_f(A))
# print(numgrad)
#
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论