Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
514908e1
提交
514908e1
authored
5月 21, 2016
作者:
Ilya Kulikov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
tensorinv op added, test for tensorinv added
上级
74c0a4c7
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
72 行增加
和
1 行删除
+72
-1
nlinalg.py
theano/tensor/nlinalg.py
+56
-0
test_nlinalg.py
theano/tensor/tests/test_nlinalg.py
+16
-1
没有找到文件。
theano/tensor/nlinalg.py
浏览文件 @
514908e1
...
...
@@ -726,3 +726,59 @@ def norm(x, ord):
raise
ValueError
(
0
)
elif
ndim
>
2
:
raise
NotImplementedError
(
"We don't support norm witn ndim > 2"
)
class
TensorInv
(
Op
):
"""
Class wrapper for tensorinv() function;
Theano utilization of numpy.linalg.tensorinv;
"""
_numop
=
staticmethod
(
numpy
.
linalg
.
tensorinv
)
__props__
=
(
'ind'
,)
def
__init__
(
self
,
ind
=
2
):
self
.
ind
=
ind
def
make_node
(
self
,
a
):
a
=
as_tensor_variable
(
a
)
out_dtype
=
a
.
dtype
out
=
theano
.
tensor
.
matrix
(
dtype
=
out_dtype
)
return
Apply
(
self
,
[
a
],
[
out
])
def
perform
(
self
,
node
,
inputs
,
outputs
):
(
a
,)
=
inputs
(
x
,)
=
outputs
x
[
0
]
=
self
.
_numop
(
a
,
self
.
ind
)
def
tensorinv
(
a
,
ind
=
2
):
"""
Does not run on GPU;
Theano utilization of numpy.linalg.tensorinv;
Compute the 'inverse' of an N-dimensional array.
The result is an inverse for `a` relative to the tensordot operation
``tensordot(a, b, ind)``, i. e., up to floating-point accuracy,
``tensordot(tensorinv(a), a, ind)`` is the "identity" tensor for the
tensordot operation.
Parameters
----------
a : array_like
Tensor to 'invert'. Its shape must be 'square', i. e.,
``prod(a.shape[:ind]) == prod(a.shape[ind:])``.
ind : int, optional
Number of first indices that are involved in the inverse sum.
Must be a positive integer, default is 2.
Returns
-------
b : ndarray
`a`'s tensordot inverse, shape ``a.shape[ind:] + a.shape[:ind]``.
Raises
------
LinAlgError
If `a` is singular or not 'square' (in the above sense).
"""
return
TensorInv
(
ind
)(
a
)
theano/tensor/tests/test_nlinalg.py
浏览文件 @
514908e1
...
...
@@ -37,7 +37,8 @@ from theano.tensor.nlinalg import ( MatrixInverse,
qr
,
matrix_power
,
norm
,
svd
svd
,
tensorinv
)
from
nose.plugins.attrib
import
attr
...
...
@@ -517,3 +518,17 @@ class T_NormTests(unittest.TestCase):
t_n
=
f
(
A
[
2
][
i
])
n_n
=
numpy
.
linalg
.
norm
(
A
[
2
][
i
],
A
[
3
][
i
])
assert
_allclose
(
n_n
,
t_n
)
def
test_tensorinv
():
A
=
tensor
.
tensor4
(
"A"
,
dtype
=
theano
.
config
.
floatX
)
X
=
tensorinv
(
A
)
tf
=
function
([
A
],
[
X
])
a
=
numpy
.
eye
(
4
*
6
)
a
.
shape
=
(
4
,
6
,
8
,
3
)
n_ainv
=
numpy
.
linalg
.
tensorinv
(
a
)
t_ainv
=
tf
(
a
)
assert
_allclose
(
n_ainv
,
t_ainv
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论