Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
2060b8f7
提交
2060b8f7
authored
11月 29, 2012
作者:
lamblin
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1102 from nouiz/py24
Fix tests
上级
d22d8720
929bb073
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
39 行增加
和
24 行删除
+39
-24
ops.py
theano/sandbox/linalg/ops.py
+24
-17
test_linalg.py
theano/sandbox/linalg/tests/test_linalg.py
+15
-7
没有找到文件。
theano/sandbox/linalg/ops.py
浏览文件 @
2060b8f7
...
@@ -662,10 +662,10 @@ class Solve(Op):
...
@@ -662,10 +662,10 @@ class Solve(Op):
def
infer_shape
(
self
,
node
,
shapes
):
def
infer_shape
(
self
,
node
,
shapes
):
Ashape
,
Bshape
=
shapes
Ashape
,
Bshape
=
shapes
rows
=
Ashape
[
1
]
rows
=
Ashape
[
1
]
if
len
(
Bshape
)
==
1
:
# b is a Vector
if
len
(
Bshape
)
==
1
:
# b is a Vector
return
[(
rows
,)]
return
[(
rows
,)]
else
:
else
:
cols
=
Bshape
[
1
]
# b is a Matrix
cols
=
Bshape
[
1
]
# b is a Matrix
return
[(
rows
,
cols
)]
return
[(
rows
,
cols
)]
solve
=
Solve
()
# general solve
solve
=
Solve
()
# general solve
...
@@ -879,6 +879,7 @@ class A_Xinv_b(Op):
...
@@ -879,6 +879,7 @@ class A_Xinv_b(Op):
gb
=
matrix_dot
(
ix
.
T
,
a
.
T
,
gz
)
gb
=
matrix_dot
(
ix
.
T
,
a
.
T
,
gz
)
return
[
ga
,
gX
,
gb
]
return
[
ga
,
gX
,
gb
]
class
Eig
(
Op
):
class
Eig
(
Op
):
"""Compute the eigenvalues and right eigenvectors of a square array.
"""Compute the eigenvalues and right eigenvectors of a square array.
...
@@ -916,24 +917,31 @@ class Eig(Op):
...
@@ -916,24 +917,31 @@ class Eig(Op):
def
infer_shape
(
self
,
node
,
shapes
):
def
infer_shape
(
self
,
node
,
shapes
):
n
=
shapes
[
0
][
0
]
n
=
shapes
[
0
][
0
]
return
[(
n
,),
(
n
,
n
)]
return
[(
n
,),
(
n
,
n
)]
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
_numop
.
__name__
.
capitalize
()
return
self
.
_numop
.
__name__
.
capitalize
()
eig
=
Eig
()
eig
=
Eig
()
def
_zero_disconnected
(
outputs
,
grads
):
def
_zero_disconnected
(
outputs
,
grads
):
return
[
o
.
zeros_like
()
l
=
[]
if
isinstance
(
g
.
type
,
DisconnectedType
)
else
g
for
o
,
g
in
zip
(
outputs
,
grads
):
for
o
,
g
in
zip
(
outputs
,
grads
)]
if
isinstance
(
g
.
type
,
DisconnectedType
):
l
.
append
(
o
.
zeros_like
())
else
:
l
.
append
(
g
)
return
l
class
Eigh
(
Eig
):
class
Eigh
(
Eig
):
"""
"""
Return the eigenvalues and eigenvectors of a Hermitian or symmetric matrix.
Return the eigenvalues and eigenvectors of a Hermitian or symmetric matrix.
"""
"""
_numop
=
staticmethod
(
numpy
.
linalg
.
eigh
)
_numop
=
staticmethod
(
numpy
.
linalg
.
eigh
)
def
__init__
(
self
,
UPLO
=
'L'
):
def
__init__
(
self
,
UPLO
=
'L'
):
self
.
UPLO
=
UPLO
self
.
UPLO
=
UPLO
...
@@ -962,9 +970,8 @@ class Eigh(Eig):
...
@@ -962,9 +970,8 @@ class Eigh(Eig):
except
numpy
.
linalg
.
LinAlgError
:
except
numpy
.
linalg
.
LinAlgError
:
logger
.
debug
(
'Failed to find
%
s of
%
s'
%
(
self
.
_numop
.
__name__
,
logger
.
debug
(
'Failed to find
%
s of
%
s'
%
(
self
.
_numop
.
__name__
,
node
.
inputs
[
0
]))
node
.
inputs
[
0
]))
raise
raise
def
grad
(
self
,
inputs
,
g_outputs
):
def
grad
(
self
,
inputs
,
g_outputs
):
r"""The gradient function should return
r"""The gradient function should return
...
@@ -972,7 +979,7 @@ class Eigh(Eig):
...
@@ -972,7 +979,7 @@ class Eigh(Eig):
{\partial a_{ij}} +
{\partial a_{ij}} +
\sum_k V_{nk}\frac{\partial\,v_{nk}}
\sum_k V_{nk}\frac{\partial\,v_{nk}}
{\partial a_{ij}}\right),
{\partial a_{ij}}\right),
where [:math:`W`, :math:`V`] corresponds to ``g_outputs``,
where [:math:`W`, :math:`V`] corresponds to ``g_outputs``,
:math:`a` to ``inputs``, and :math:`(w, v)=\mbox{eig}(a)`.
:math:`a` to ``inputs``, and :math:`(w, v)=\mbox{eig}(a)`.
...
@@ -984,7 +991,7 @@ class Eigh(Eig):
...
@@ -984,7 +991,7 @@ class Eigh(Eig):
.. math:: \frac{\partial\,v_{kn}}
.. math:: \frac{\partial\,v_{kn}}
{\partial a_{ij}} =
{\partial a_{ij}} =
\sum_{m\ne n}\frac{v_{km}v_{jn}}{w_n-w_m}
\sum_{m\ne n}\frac{v_{km}v_{jn}}{w_n-w_m}
"""
"""
x
,
=
inputs
x
,
=
inputs
...
@@ -994,8 +1001,10 @@ class Eigh(Eig):
...
@@ -994,8 +1001,10 @@ class Eigh(Eig):
gw
,
gv
=
_zero_disconnected
([
w
,
v
],
g_outputs
)
gw
,
gv
=
_zero_disconnected
([
w
,
v
],
g_outputs
)
return
[
EighGrad
(
self
.
UPLO
)(
x
,
w
,
v
,
gw
,
gv
)]
return
[
EighGrad
(
self
.
UPLO
)(
x
,
w
,
v
,
gw
,
gv
)]
def
eigh
(
a
,
UPLO
=
'L'
):
def
eigh
(
a
,
UPLO
=
'L'
):
return
Eigh
(
UPLO
)(
a
)
return
Eigh
(
UPLO
)(
a
)
class
EighGrad
(
Op
):
class
EighGrad
(
Op
):
"""Gradient of an eigensystem of a Hermitian matrix.
"""Gradient of an eigensystem of a Hermitian matrix.
...
@@ -1009,7 +1018,7 @@ class EighGrad(Op):
...
@@ -1009,7 +1018,7 @@ class EighGrad(Op):
else
:
else
:
self
.
tri0
=
numpy
.
triu
self
.
tri0
=
numpy
.
triu
self
.
tri1
=
lambda
a
:
numpy
.
tril
(
a
,
-
1
)
self
.
tri1
=
lambda
a
:
numpy
.
tril
(
a
,
-
1
)
def
props
(
self
):
def
props
(
self
):
return
()
return
()
...
@@ -1021,7 +1030,6 @@ class EighGrad(Op):
...
@@ -1021,7 +1030,6 @@ class EighGrad(Op):
def
__str__
(
self
):
def
__str__
(
self
):
return
'EighGrad{
%
s}'
%
self
.
UPLO
return
'EighGrad{
%
s}'
%
self
.
UPLO
def
make_node
(
self
,
x
,
w
,
v
,
gw
,
gv
):
def
make_node
(
self
,
x
,
w
,
v
,
gw
,
gv
):
x
,
w
,
v
,
gw
,
gv
=
map
(
as_tensor_variable
,
(
x
,
w
,
v
,
gw
,
gv
))
x
,
w
,
v
,
gw
,
gv
=
map
(
as_tensor_variable
,
(
x
,
w
,
v
,
gw
,
gv
))
...
@@ -1036,9 +1044,9 @@ class EighGrad(Op):
...
@@ -1036,9 +1044,9 @@ class EighGrad(Op):
N
=
x
.
shape
[
0
]
N
=
x
.
shape
[
0
]
outer
=
numpy
.
outer
outer
=
numpy
.
outer
G
=
lambda
n
:
sum
(
v
[:,
m
]
*
V
.
T
[
n
]
.
dot
(
v
[:,
m
])
/
(
w
[
n
]
-
w
[
m
])
G
=
lambda
n
:
sum
(
v
[:,
m
]
*
V
.
T
[
n
]
.
dot
(
v
[:,
m
])
/
(
w
[
n
]
-
w
[
m
])
for
m
in
xrange
(
N
)
if
m
!=
n
)
for
m
in
xrange
(
N
)
if
m
!=
n
)
g
=
sum
(
outer
(
v
[:,
n
],
v
[:,
n
]
*
W
[
n
]
+
G
(
n
))
g
=
sum
(
outer
(
v
[:,
n
],
v
[:,
n
]
*
W
[
n
]
+
G
(
n
))
for
n
in
xrange
(
N
))
for
n
in
xrange
(
N
))
# Numpy's eigh(a, 'L') (eigh(a, 'U')) is a function of tril(a)
# Numpy's eigh(a, 'L') (eigh(a, 'U')) is a function of tril(a)
...
@@ -1053,4 +1061,3 @@ class EighGrad(Op):
...
@@ -1053,4 +1061,3 @@ class EighGrad(Op):
def
infer_shape
(
self
,
node
,
shapes
):
def
infer_shape
(
self
,
node
,
shapes
):
return
[
shapes
[
0
]]
return
[
shapes
[
0
]]
theano/sandbox/linalg/tests/test_linalg.py
浏览文件 @
2060b8f7
...
@@ -185,6 +185,7 @@ def test_inverse_grad():
...
@@ -185,6 +185,7 @@ def test_inverse_grad():
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
():
def
test_rop_lop
():
mx
=
tensor
.
matrix
(
'mx'
)
mx
=
tensor
.
matrix
(
'mx'
)
mv
=
tensor
.
matrix
(
'mv'
)
mv
=
tensor
.
matrix
(
'mv'
)
...
@@ -442,6 +443,7 @@ def test_spectral_radius_bound():
...
@@ -442,6 +443,7 @@ def test_spectral_radius_bound():
ok
=
True
ok
=
True
assert
ok
assert
ok
class
test_Solve
(
utt
.
InferShapeTester
):
class
test_Solve
(
utt
.
InferShapeTester
):
def
setUp
(
self
):
def
setUp
(
self
):
super
(
test_Solve
,
self
)
.
setUp
()
super
(
test_Solve
,
self
)
.
setUp
()
...
@@ -474,10 +476,12 @@ class test_Solve(utt.InferShapeTester):
...
@@ -474,10 +476,12 @@ class test_Solve(utt.InferShapeTester):
dtype
=
config
.
floatX
)],
dtype
=
config
.
floatX
)],
self
.
op_class
)
self
.
op_class
)
class
test_Eig
(
utt
.
InferShapeTester
):
class
test_Eig
(
utt
.
InferShapeTester
):
op_class
=
Eig
op_class
=
Eig
op
=
eig
op
=
eig
dtype
=
'float64'
dtype
=
'float64'
def
setUp
(
self
):
def
setUp
(
self
):
super
(
test_Eig
,
self
)
.
setUp
()
super
(
test_Eig
,
self
)
.
setUp
()
self
.
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
self
.
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
...
@@ -485,7 +489,7 @@ class test_Eig(utt.InferShapeTester):
...
@@ -485,7 +489,7 @@ class test_Eig(utt.InferShapeTester):
X
=
numpy
.
asarray
(
self
.
rng
.
rand
(
5
,
5
),
X
=
numpy
.
asarray
(
self
.
rng
.
rand
(
5
,
5
),
dtype
=
self
.
dtype
)
dtype
=
self
.
dtype
)
self
.
S
=
X
.
dot
(
X
.
T
)
self
.
S
=
X
.
dot
(
X
.
T
)
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
A
=
self
.
A
A
=
self
.
A
S
=
self
.
S
S
=
self
.
S
...
@@ -494,26 +498,29 @@ class test_Eig(utt.InferShapeTester):
...
@@ -494,26 +498,29 @@ class test_Eig(utt.InferShapeTester):
# S must be square
# S must be square
[
S
],
[
S
],
self
.
op_class
)
self
.
op_class
)
def
test_eval
(
self
):
def
test_eval
(
self
):
import
math
import
math
A
=
theano
.
tensor
.
matrix
(
dtype
=
self
.
dtype
)
A
=
theano
.
tensor
.
matrix
(
dtype
=
self
.
dtype
)
self
.
assertEquals
([
e
.
eval
({
A
:
[[
1
]]})
for
e
in
self
.
op
(
A
)],
self
.
assertEquals
([
e
.
eval
({
A
:
[[
1
]]})
for
e
in
self
.
op
(
A
)],
[[
1.0
],
[[
1.0
]]])
[[
1.0
],
[[
1.0
]]])
x
=
[[
0
,
1
],
[
1
,
0
]]
x
=
[[
0
,
1
],
[
1
,
0
]]
w
,
v
=
[
e
.
eval
({
A
:
x
})
for
e
in
self
.
op
(
A
)]
w
,
v
=
[
e
.
eval
({
A
:
x
})
for
e
in
self
.
op
(
A
)]
assert_array_almost_equal
(
numpy
.
dot
(
x
,
v
),
w
*
v
)
assert_array_almost_equal
(
numpy
.
dot
(
x
,
v
),
w
*
v
)
class
test_Eigh
(
test_Eig
):
class
test_Eigh
(
test_Eig
):
op
=
staticmethod
(
eigh
)
op
=
staticmethod
(
eigh
)
def
test_uplo
(
self
):
def
test_uplo
(
self
):
S
=
self
.
S
S
=
self
.
S
a
=
theano
.
tensor
.
matrix
()
a
=
theano
.
tensor
.
matrix
(
dtype
=
self
.
dtype
)
wu
,
vu
=
[
out
.
eval
({
a
:
S
})
for
out
in
self
.
op
(
a
,
'U'
)]
wu
,
vu
=
[
out
.
eval
({
a
:
S
})
for
out
in
self
.
op
(
a
,
'U'
)]
wl
,
vl
=
[
out
.
eval
({
a
:
S
})
for
out
in
self
.
op
(
a
,
'L'
)]
wl
,
vl
=
[
out
.
eval
({
a
:
S
})
for
out
in
self
.
op
(
a
,
'L'
)]
assert_array_almost_equal
(
wu
,
wl
)
assert_array_almost_equal
(
wu
,
wl
)
assert_array_almost_equal
(
vu
*
numpy
.
sign
(
vu
[
0
,
:]),
assert_array_almost_equal
(
vu
*
numpy
.
sign
(
vu
[
0
,
:]),
vl
*
numpy
.
sign
(
vl
[
0
,
:]))
vl
*
numpy
.
sign
(
vl
[
0
,
:]))
def
test_grad
(
self
):
def
test_grad
(
self
):
S
=
self
.
S
S
=
self
.
S
utt
.
verify_grad
(
lambda
x
:
self
.
op
(
x
)[
0
],
[
S
],
rng
=
self
.
rng
)
utt
.
verify_grad
(
lambda
x
:
self
.
op
(
x
)[
0
],
[
S
],
rng
=
self
.
rng
)
...
@@ -521,5 +528,6 @@ class test_Eigh(test_Eig):
...
@@ -521,5 +528,6 @@ class test_Eigh(test_Eig):
utt
.
verify_grad
(
lambda
x
:
self
.
op
(
x
,
'U'
)[
0
],
[
S
],
rng
=
self
.
rng
)
utt
.
verify_grad
(
lambda
x
:
self
.
op
(
x
,
'U'
)[
0
],
[
S
],
rng
=
self
.
rng
)
utt
.
verify_grad
(
lambda
x
:
self
.
op
(
x
,
'U'
)[
1
],
[
S
],
rng
=
self
.
rng
)
utt
.
verify_grad
(
lambda
x
:
self
.
op
(
x
,
'U'
)[
1
],
[
S
],
rng
=
self
.
rng
)
class
test_Eigh_float32
(
test_Eigh
):
class
test_Eigh_float32
(
test_Eigh
):
dtype
=
'float32'
dtype
=
'float32'
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论