Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
4e0a1674
提交
4e0a1674
authored
9月 27, 2010
作者:
Frederic Bastien
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
make TensorDot and TensorDotGrad work with scalar axes. Add tests for that and…
make TensorDot and TensorDotGrad work with scalar axes. Add tests for that and test that is raise error.
上级
5b5b21a6
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
82 行增加
和
3 行删除
+82
-3
basic.py
theano/tensor/basic.py
+12
-3
test_basic.py
theano/tensor/tests/test_basic.py
+70
-0
没有找到文件。
theano/tensor/basic.py
浏览文件 @
4e0a1674
...
@@ -3787,7 +3787,11 @@ class TensorDotGrad(Op):
...
@@ -3787,7 +3787,11 @@ class TensorDotGrad(Op):
assert
isinstance
(
gz
,
Variable
)
assert
isinstance
(
gz
,
Variable
)
gx
=
x
.
type
()
gx
=
x
.
type
()
gy
=
y
.
type
()
gy
=
y
.
type
()
return
Apply
(
self
,
[
x
,
y
,
gz
],
[
gx
,
gy
])
op
=
self
if
isinstance
(
self
.
axes
,
int
):
axes
=
[
range
(
x
.
ndim
-
self
.
axes
,
x
.
ndim
),
range
(
self
.
axes
)]
op
=
TensorDotGrad
(
axes
)
return
Apply
(
op
,
[
x
,
y
,
gz
],
[
gx
,
gy
])
def
perform
(
self
,
node
,
(
x
,
y
,
gz
),
(
gx
,
gy
)):
def
perform
(
self
,
node
,
(
x
,
y
,
gz
),
(
gx
,
gy
)):
...
@@ -3841,8 +3845,13 @@ class TensorDot(Op):
...
@@ -3841,8 +3845,13 @@ class TensorDot(Op):
return
hashtype
(
self
)
^
hash
(
self
.
axes
)
^
89234
return
hashtype
(
self
)
^
hash
(
self
.
axes
)
^
89234
def
make_node
(
self
,
x
,
y
):
def
make_node
(
self
,
x
,
y
):
op
=
self
if
isinstance
(
self
.
axes
,
int
):
axes
=
[
range
(
x
.
ndim
-
self
.
axes
,
x
.
ndim
),
range
(
self
.
axes
)]
op
=
TensorDot
(
axes
)
axesdim
=
numpy
.
size
(
op
.
axes
)
/
2
axesdim
=
numpy
.
size
(
self
.
axes
)
/
2
x
,
y
=
map
(
as_tensor_variable
,
[
x
,
y
])
x
,
y
=
map
(
as_tensor_variable
,
[
x
,
y
])
if
axesdim
>
x
.
type
.
ndim
or
axesdim
>
y
.
type
.
ndim
:
if
axesdim
>
x
.
type
.
ndim
or
axesdim
>
y
.
type
.
ndim
:
...
@@ -3851,7 +3860,7 @@ class TensorDot(Op):
...
@@ -3851,7 +3860,7 @@ class TensorDot(Op):
outdim
=
x
.
type
.
ndim
+
y
.
type
.
ndim
-
2
*
axesdim
outdim
=
x
.
type
.
ndim
+
y
.
type
.
ndim
-
2
*
axesdim
output
=
tensor
(
dtype
=
x
.
dtype
,
broadcastable
=
[
False
]
*
outdim
);
output
=
tensor
(
dtype
=
x
.
dtype
,
broadcastable
=
[
False
]
*
outdim
);
return
Apply
(
self
,
inputs
=
[
x
,
y
],
outputs
=
[
output
,])
return
Apply
(
op
,
inputs
=
[
x
,
y
],
outputs
=
[
output
,])
def
perform
(
self
,
node
,
(
x
,
y
),
(
z
,)):
def
perform
(
self
,
node
,
(
x
,
y
),
(
z
,)):
try
:
try
:
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
4e0a1674
...
@@ -2513,6 +2513,35 @@ class test_tensordot(unittest.TestCase):
...
@@ -2513,6 +2513,35 @@ class test_tensordot(unittest.TestCase):
f6
(
bval
,
aval
)))
f6
(
bval
,
aval
)))
utt
.
verify_grad
(
TensorDot
(
axes
),
[
bval
,
aval
])
utt
.
verify_grad
(
TensorDot
(
axes
),
[
bval
,
aval
])
def
test_raise_error
(
self
):
# test vector-vector
avec
=
dvector
()
bvec
=
dvector
()
axes
=
((
0
,),())
try
:
c
=
tensordot
(
axes
)(
avec
,
bvec
)
assert
False
except
ValueError
:
pass
# test matrix-vector
bmat
=
dmatrix
()
axes
=
((
0
,),())
try
:
c
=
tensordot
(
axes
)(
avec
,
bmat
)
assert
False
except
ValueError
:
pass
# test matrix-matrix
amat
=
dmatrix
()
axes
=
((
1
,),())
try
:
c
=
tensordot
(
axes
)(
amat
,
bmat
)
assert
False
except
ValueError
:
pass
def
test_list
(
self
):
def
test_list
(
self
):
# test matrix-matrix
# test matrix-matrix
amat
=
dmatrix
()
amat
=
dmatrix
()
...
@@ -2526,6 +2555,47 @@ class test_tensordot(unittest.TestCase):
...
@@ -2526,6 +2555,47 @@ class test_tensordot(unittest.TestCase):
f3
(
aval
,
bval
)))
f3
(
aval
,
bval
)))
utt
.
verify_grad
(
TensorDot
(
axes
),
[
aval
,
bval
])
utt
.
verify_grad
(
TensorDot
(
axes
),
[
aval
,
bval
])
def
test_scalar
(
self
):
# test matrix-matrix
amat
=
dmatrix
()
bmat
=
dmatrix
()
axes
=
1
aval
=
numpy
.
random
.
rand
(
4
,
5
)
bval
=
numpy
.
random
.
rand
(
5
,
3
)
c
=
tensordot
(
axes
)(
amat
,
bmat
)
f3
=
inplace_func
([
amat
,
bmat
],
c
)
self
.
failUnless
(
numpy
.
all
(
numpy
.
tensordot
(
aval
,
bval
,
axes
)
==
\
f3
(
aval
,
bval
)))
utt
.
verify_grad
(
TensorDot
(
axes
),
[
aval
,
bval
])
# test tensor-tensor
amat
=
dtensor3
()
bmat
=
dtensor3
()
axes
=
2
aval
=
numpy
.
random
.
rand
(
3
,
4
,
5
)
bval
=
numpy
.
random
.
rand
(
4
,
5
,
3
)
c
=
tensordot
(
axes
)(
amat
,
bmat
)
f3
=
inplace_func
([
amat
,
bmat
],
c
)
self
.
failUnless
(
numpy
.
all
(
numpy
.
tensordot
(
aval
,
bval
,
axes
)
==
\
f3
(
aval
,
bval
)))
utt
.
verify_grad
(
TensorDot
(
axes
),
[
aval
,
bval
])
def
test_tensordot_grad
(
self
):
#We test it manually as we recreate the op in the make_node
amat
=
dmatrix
()
bmat
=
dmatrix
()
gzmat
=
dmatrix
()
axes
=
1
aval
=
numpy
.
random
.
rand
(
4
,
5
)
bval
=
numpy
.
random
.
rand
(
5
,
3
)
gzval
=
numpy
.
random
.
rand
(
4
,
3
)
f1
=
inplace_func
([
amat
,
bmat
,
gzmat
],
tensordot_grad
(
axes
)(
amat
,
bmat
,
gzmat
))
f2
=
inplace_func
([
amat
,
bmat
,
gzmat
],
tensordot_grad
(((
1
,),(
0
,)))(
amat
,
bmat
,
gzmat
))
o1
=
f1
(
aval
,
bval
,
gzval
)
o2
=
f2
(
aval
,
bval
,
gzval
)
self
.
failUnless
(
numpy
.
all
(
o1
[
0
]
==
o2
[
0
]))
self
.
failUnless
(
numpy
.
all
(
o1
[
1
]
==
o2
[
1
]))
def
test_smallest_stack
():
def
test_smallest_stack
():
sx
,
sy
=
dscalar
(),
dscalar
()
sx
,
sy
=
dscalar
(),
dscalar
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论