Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
7fd2e38f
提交
7fd2e38f
authored
10月 24, 2011
作者:
Razvan Pascanu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
rop for matrix inverse plus test
上级
ab1c7862
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
70 行增加
和
1 行删除
+70
-1
ops.py
theano/sandbox/linalg/ops.py
+22
-0
test_linalg.py
theano/sandbox/linalg/tests/test_linalg.py
+48
-1
没有找到文件。
theano/sandbox/linalg/ops.py
浏览文件 @
7fd2e38f
...
@@ -392,6 +392,28 @@ class MatrixInverse(Op):
...
@@ -392,6 +392,28 @@ class MatrixInverse(Op):
#TT.dot(gz.T,xi)
#TT.dot(gz.T,xi)
return
[
-
matrix_dot
(
xi
,
gz
.
T
,
xi
)
.
T
]
return
[
-
matrix_dot
(
xi
,
gz
.
T
,
xi
)
.
T
]
def
R_op
(
self
,
inputs
,
eval_points
):
"""The gradient function should return:
:math:`
\\
frac{
\
partial X^{-1}}{
\
partial X}V`
where :math:`V` corresponds to ``g_outputs`` and :math:`X` to
``inputs``. Using the matrix cookbook
``http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=3274``,
once can deduce that the relation corresponds to :
:math:`X^{-1}
\
cdot V
\
cdot X^{-1}`
"""
x
,
=
inputs
xi
=
self
(
x
)
ev
,
=
eval_points
if
ev
is
None
:
return
[
None
]
#TT.dot(gz.T,xi)
return
[
-
matrix_dot
(
xi
,
ev
,
xi
)]
def
__str__
(
self
):
def
__str__
(
self
):
return
"MatrixInverse"
return
"MatrixInverse"
...
...
theano/sandbox/linalg/tests/test_linalg.py
浏览文件 @
7fd2e38f
...
@@ -5,6 +5,7 @@ import numpy
...
@@ -5,6 +5,7 @@ import numpy
import
theano
import
theano
from
theano
import
tensor
,
function
from
theano
import
tensor
,
function
from
theano.tensor.basic
import
_allclose
from
theano.tensor.basic
import
_allclose
from
theano.tensor.tests.test_rop
import
break_op
from
theano.tests
import
unittest_tools
as
utt
from
theano.tests
import
unittest_tools
as
utt
from
theano
import
config
from
theano
import
config
...
@@ -81,11 +82,57 @@ def test_inverse_correctness():
...
@@ -81,11 +82,57 @@ def test_inverse_correctness():
def
test_inverse_grad
():
def
test_inverse_grad
():
rng
=
numpy
.
random
.
RandomState
(
1234
)
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
()
)
r
=
rng
.
randn
(
4
,
4
)
r
=
rng
.
randn
(
4
,
4
)
tensor
.
verify_grad
(
matrix_inverse
,
[
r
],
rng
=
numpy
.
random
)
tensor
.
verify_grad
(
matrix_inverse
,
[
r
],
rng
=
numpy
.
random
)
def
test_rop_lop
():
mx
=
tensor
.
matrix
(
'mx'
)
mv
=
tensor
.
matrix
(
'mv'
)
v
=
tensor
.
vector
(
'v'
)
y
=
matrix_inverse
(
mx
)
.
sum
(
axis
=
0
)
yv
=
tensor
.
Rop
(
y
,
mx
,
mv
)
rop_f
=
function
([
mx
,
mv
],
yv
)
sy
,
_
=
theano
.
scan
(
lambda
i
,
y
,
x
,
v
:
(
tensor
.
grad
(
y
[
i
],
x
)
*
v
)
.
sum
(),
sequences
=
tensor
.
arange
(
y
.
shape
[
0
]),
non_sequences
=
[
y
,
mx
,
mv
])
scan_f
=
function
([
mx
,
mv
],
sy
)
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
vx
=
numpy
.
asarray
(
rng
.
randn
(
4
,
4
),
theano
.
config
.
floatX
)
vv
=
numpy
.
asarray
(
rng
.
randn
(
4
,
4
),
theano
.
config
.
floatX
)
v1
=
rop_f
(
vx
,
vv
)
v2
=
scan_f
(
vx
,
vv
)
assert
numpy
.
allclose
(
v1
,
v2
),
(
'ROP mismatch:
%
s
%
s'
%
(
v1
,
v2
))
raised
=
False
try
:
tmp
=
tensor
.
Rop
(
theano
.
clone
(
y
,
replace
=
{
mx
:
break_op
(
mx
)}),
mx
,
mv
)
except
ValueError
:
raised
=
True
if
not
raised
:
raise
Exception
((
'Op did not raised an error even though the function'
' is not differentiable'
))
vv
=
numpy
.
asarray
(
rng
.
uniform
(
size
=
(
4
,)),
theano
.
config
.
floatX
)
yv
=
tensor
.
Lop
(
y
,
mx
,
v
)
lop_f
=
function
([
mx
,
v
],
yv
)
sy
=
tensor
.
grad
((
v
*
y
)
.
sum
(),
mx
)
scan_f
=
function
([
mx
,
v
],
sy
)
v1
=
lop_f
(
vx
,
vv
)
v2
=
scan_f
(
vx
,
vv
)
assert
numpy
.
allclose
(
v1
,
v2
),
(
'LOP mismatch:
%
s
%
s'
%
(
v1
,
v2
))
def
test_det_grad
():
def
test_det_grad
():
# If scipy is not available, this test will fail, thus we skip it.
# If scipy is not available, this test will fail, thus we skip it.
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论