Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
92e2e0a0
提交
92e2e0a0
authored
7月 31, 2012
作者:
nouiz
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #701 from larseeri/kron
Kron
上级
96b7c725
b71bfbc4
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
122 行增加
和
0 行删除
+122
-0
kron.py
theano/sandbox/linalg/kron.py
+70
-0
test_kron.py
theano/sandbox/linalg/tests/test_kron.py
+52
-0
没有找到文件。
theano/sandbox/linalg/kron.py
0 → 100644
浏览文件 @
92e2e0a0
import
numpy
import
theano
from
theano.gof
import
Op
,
Apply
from
theano
import
tensor
try
:
import
scipy.linalg
imported_scipy
=
True
except
ImportError
:
imported_scipy
=
False
class
Kron
(
Op
):
"""
Kronecker product of a and b.
Parameters:
a: array, shape (M, N)
b: array, shape (P, Q)
Returns:
A: array, shape (M*P, N*Q)
The result is the block matrix:
(notice that a[i,j]*b is itself a matrix of the same shape as b)
a[0,0]*b a[0,1]*b ... a[0,-1]*b
a[1,0]*b a[1,1]*b ... a[1,-1]*b
...
a[-1,0]*b a[-1,1]*b ... a[-1,-1]*b
"""
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
def
__hash__
(
self
):
return
hash
(
type
(
self
))
def
__str__
(
self
):
return
"
%
s"
%
self
.
__class__
.
__name__
def
make_node
(
self
,
a
,
b
):
assert
imported_scipy
,
(
"Scipy not available. Scipy is needed for the Kron op"
)
a
=
tensor
.
as_tensor_variable
(
a
)
b
=
tensor
.
as_tensor_variable
(
b
)
if
(
not
a
.
ndim
==
2
or
not
b
.
ndim
==
2
):
raise
TypeError
(
'
%
s: inputs must have two dimensions'
%
self
.
__class__
.
__name__
)
out_var
=
tensor
.
TensorType
(
dtype
=
theano
.
scalar
.
upcast
(
a
,
b
),
broadcastable
=
(
False
,
False
))()
return
Apply
(
self
,
[
a
,
b
],
[
out_var
])
def
infer_shape
(
self
,
node
,
in_shapes
):
shape_a
,
shape_b
=
in_shapes
return
[[
shape_a
[
0
]
*
shape_b
[
0
],
shape_a
[
1
]
*
shape_b
[
1
]]]
def
perform
(
self
,
node
,
inputs
,
output_storage
):
a
,
b
=
inputs
output_storage
[
0
][
0
]
=
scipy
.
linalg
.
kron
(
a
,
b
)
def
grad
(
self
,
inputs
,
cost_grad
):
raise
NotImplementedError
(
'
%
s: gradient is not currently'
' implemented'
%
self
.
__class__
.
__name__
)
kron
=
Kron
()
theano/sandbox/linalg/tests/test_kron.py
0 → 100644
浏览文件 @
92e2e0a0
import
numpy
from
theano
import
tensor
,
function
from
theano.tests
import
unittest_tools
as
utt
from
theano.sandbox.linalg.kron
import
Kron
,
kron
try
:
import
scipy.linalg
imported_scipy
=
True
except
ImportError
:
imported_scipy
=
False
class
TestKron
(
utt
.
InferShapeTester
):
rng
=
numpy
.
random
.
RandomState
(
43
)
def
setUp
(
self
):
super
(
TestKron
,
self
)
.
setUp
()
self
.
op_class
=
Kron
self
.
op
=
kron
def
test_perform
(
self
):
assert
imported_scipy
,
(
"Scipy not available. Scipy is needed for TestKron"
)
x
=
tensor
.
dmatrix
()
y
=
tensor
.
dmatrix
()
f
=
function
([
x
,
y
],
kron
(
x
,
y
))
for
shp0
in
[(
8
,
6
),
(
5
,
8
)]:
for
shp1
in
[(
5
,
7
),
(
3
,
3
)]:
a
=
numpy
.
random
.
rand
(
*
shp0
)
b
=
numpy
.
random
.
rand
(
*
shp1
)
out
=
f
(
a
,
b
)
assert
numpy
.
allclose
(
out
,
scipy
.
linalg
.
kron
(
a
,
b
))
def
test_infer_shape
(
self
):
x
=
tensor
.
dmatrix
()
y
=
tensor
.
dmatrix
()
self
.
_compile_and_check
([
x
,
y
],
[
self
.
op
(
x
,
y
)],
[
numpy
.
random
.
rand
(
8
,
5
),
numpy
.
random
.
rand
(
3
,
7
)],
self
.
op_class
)
self
.
_compile_and_check
([
x
,
y
],
[
self
.
op
(
x
,
y
)],
[
numpy
.
random
.
rand
(
2
,
5
),
numpy
.
random
.
rand
(
6
,
3
)],
self
.
op_class
)
if
__name__
==
"__main__"
:
t
=
TestKron
(
'setUp'
)
t
.
setUp
()
t
.
test_perform
()
t
.
test_infer_shape
()
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论