Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
6384da65
提交
6384da65
authored
1月 13, 2012
作者:
nouiz
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #341 from jaberg/master
transpose method and tests
上级
bc2f50e5
01092d49
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
88 行增加
和
9 行删除
+88
-9
basic.py
theano/tensor/basic.py
+41
-6
test_basic.py
theano/tensor/tests/test_basic.py
+47
-3
没有找到文件。
theano/tensor/basic.py
浏览文件 @
6384da65
...
@@ -1237,7 +1237,31 @@ class _tensor_py_operators:
...
@@ -1237,7 +1237,31 @@ class _tensor_py_operators:
#TRANSPOSE
#TRANSPOSE
T
=
property
(
lambda
self
:
transpose
(
self
))
T
=
property
(
lambda
self
:
transpose
(
self
))
def
transpose
(
self
,
*
axes
):
"""
Return `tensor.transpose(self, axes)`
or `tensor.transpose(self, axes[0])`
If only one `axes` argument is provided and it is iterable, then it is
assumed to be the entire axes tuple, and passed intact to
tensor.transpose.
"""
if
len
(
axes
)
==
0
:
return
transpose
(
self
)
try
:
iter
(
axes
[
0
])
iterable
=
True
except
TypeError
:
iterable
=
False
if
len
(
axes
)
==
1
and
iterable
:
return
transpose
(
self
,
axes
[
0
])
else
:
return
transpose
(
self
,
axes
)
shape
=
property
(
lambda
self
:
shape
(
self
))
shape
=
property
(
lambda
self
:
shape
(
self
))
size
=
property
(
lambda
self
:
prod
(
self
.
shape
))
size
=
property
(
lambda
self
:
prod
(
self
.
shape
))
# We can't implement __len__ to provide a better error message.
# We can't implement __len__ to provide a better error message.
...
@@ -1347,18 +1371,23 @@ class _tensor_py_operators:
...
@@ -1347,18 +1371,23 @@ class _tensor_py_operators:
# CONVENIENT ACCESS TO TYPE PROPERTIES
# CONVENIENT ACCESS TO TYPE PROPERTIES
ndim
=
property
(
lambda
self
:
self
.
type
.
ndim
)
ndim
=
property
(
lambda
self
:
self
.
type
.
ndim
)
"""The rank of this tensor."""
"""The rank of this tensor."""
broadcastable
=
property
(
lambda
self
:
self
.
type
.
broadcastable
)
broadcastable
=
property
(
lambda
self
:
self
.
type
.
broadcastable
)
"""The broadcastable signature of this tensor.
"""The broadcastable signature of this tensor.
See :doc:`broadcasting` for details.
See :doc:`broadcasting` for details.
"""
"""
dtype
=
property
(
lambda
self
:
self
.
type
.
dtype
)
dtype
=
property
(
lambda
self
:
self
.
type
.
dtype
)
""" The dtype of this tensor. """
""" The dtype of this tensor. """
#extra pseudo-operator symbols
#extra pseudo-operator symbols
def
__dot__
(
left
,
right
):
return
dot
(
left
,
right
)
def
__dot__
(
left
,
right
):
def
__rdot__
(
right
,
left
):
return
dot
(
left
,
right
)
return
dot
(
left
,
right
)
def
__rdot__
(
right
,
left
):
return
dot
(
left
,
right
)
def
sum
(
self
,
axis
=
None
):
def
sum
(
self
,
axis
=
None
):
return
elemwise
.
Sum
(
axis
)(
self
)
return
elemwise
.
Sum
(
axis
)(
self
)
...
@@ -1390,7 +1419,6 @@ class _tensor_py_operators:
...
@@ -1390,7 +1419,6 @@ class _tensor_py_operators:
#TO TRUMP NUMPY OPERATORS
#TO TRUMP NUMPY OPERATORS
__array_priority__
=
1000
__array_priority__
=
1000
def
get_constant_value
(
self
):
def
get_constant_value
(
self
):
return
get_constant_value
(
self
)
return
get_constant_value
(
self
)
...
@@ -2934,10 +2962,17 @@ def get_canonical_form_slice(theslice, length):
...
@@ -2934,10 +2962,17 @@ def get_canonical_form_slice(theslice, length):
return
value
,
1
return
value
,
1
def
transpose
(
x
,
**
kwargs
):
def
transpose
(
x
,
axes
=
None
):
dims
=
range
(
x
.
ndim
-
1
,
-
1
,
-
1
)
"""
Reorder the dimensions of x. (Default: reverse them)
This is a macro around dimshuffle that matches the numpy.transpose
function.
return
DimShuffle
(
x
.
broadcastable
,
dims
,
inplace
=
False
)(
x
)
"""
if
axes
is
None
:
axes
=
range
(
x
.
ndim
-
1
,
-
1
,
-
1
)
return
DimShuffle
(
x
.
broadcastable
,
axes
,
inplace
=
False
)(
x
)
class
AdvancedIndexingError
(
TypeError
):
class
AdvancedIndexingError
(
TypeError
):
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
6384da65
...
@@ -5099,6 +5099,7 @@ def test_mod():
...
@@ -5099,6 +5099,7 @@ def test_mod():
):
):
assert
fn
(
a
,
b
)
==
a
%
b
,
(
a
,)
assert
fn
(
a
,
b
)
==
a
%
b
,
(
a
,)
def
test_mod_compile
():
def
test_mod_compile
():
"""
"""
This test generate an Elemwise of Composite as:
This test generate an Elemwise of Composite as:
...
@@ -5122,6 +5123,7 @@ def test_mod_compile():
...
@@ -5122,6 +5123,7 @@ def test_mod_compile():
f
=
theano
.
function
([
x
,
y
],
out
)
f
=
theano
.
function
([
x
,
y
],
out
)
def
test_unalign
():
def
test_unalign
():
if
config
.
floatX
==
'float64'
:
if
config
.
floatX
==
'float64'
:
dtype
=
"b1,f8"
dtype
=
"b1,f8"
...
@@ -5153,6 +5155,7 @@ def test_unalign():
...
@@ -5153,6 +5155,7 @@ def test_unalign():
if
not
should_raise
:
if
not
should_raise
:
raise
Exception
(
"Theano raised an exception when none was expected"
)
raise
Exception
(
"Theano raised an exception when none was expected"
)
def
test_dimshuffle_duplicate
():
def
test_dimshuffle_duplicate
():
x
=
tensor
.
vector
()
x
=
tensor
.
vector
()
...
@@ -5167,7 +5170,6 @@ def test_dimshuffle_duplicate():
...
@@ -5167,7 +5170,6 @@ def test_dimshuffle_duplicate():
assert
success
assert
success
class
T_get_constant_value
(
unittest
.
TestCase
):
class
T_get_constant_value
(
unittest
.
TestCase
):
def
test_get_constant_value
(
self
):
def
test_get_constant_value
(
self
):
a
=
tensor
.
stack
(
1
,
2
,
3
)
a
=
tensor
.
stack
(
1
,
2
,
3
)
assert
get_constant_value
(
a
[
0
])
==
1
assert
get_constant_value
(
a
[
0
])
==
1
...
@@ -5202,6 +5204,7 @@ class T_get_constant_value(unittest.TestCase):
...
@@ -5202,6 +5204,7 @@ class T_get_constant_value(unittest.TestCase):
for
j
in
range
(
c
.
value
.
shape
[
1
]):
for
j
in
range
(
c
.
value
.
shape
[
1
]):
assert
get_constant_value
(
c
[
i
,
j
])
==
c
.
value
[
i
,
j
]
assert
get_constant_value
(
c
[
i
,
j
])
==
c
.
value
[
i
,
j
]
class
T_as_tensor_variable
(
unittest
.
TestCase
):
class
T_as_tensor_variable
(
unittest
.
TestCase
):
"""
"""
We test that ticket #649 stay fixed.
We test that ticket #649 stay fixed.
...
@@ -5231,7 +5234,6 @@ class test_complex_mod(unittest.TestCase):
...
@@ -5231,7 +5234,6 @@ class test_complex_mod(unittest.TestCase):
class
test_size
(
unittest
.
TestCase
):
class
test_size
(
unittest
.
TestCase
):
"""
"""
Ensure the `size` attribute of tensors behaves as in numpy.
Ensure the `size` attribute of tensors behaves as in numpy.
"""
"""
...
@@ -5259,7 +5261,6 @@ class test_size(unittest.TestCase):
...
@@ -5259,7 +5261,6 @@ class test_size(unittest.TestCase):
class
test_numpy_assumptions
(
unittest
.
TestCase
):
class
test_numpy_assumptions
(
unittest
.
TestCase
):
"""
"""
Verify that some assumptions Theano makes on Numpy's behavior still hold.
Verify that some assumptions Theano makes on Numpy's behavior still hold.
"""
"""
...
@@ -5290,6 +5291,49 @@ class test_numpy_assumptions(unittest.TestCase):
...
@@ -5290,6 +5291,49 @@ class test_numpy_assumptions(unittest.TestCase):
assert
(
dtype1
==
dtype2
)
==
(
str
(
dtype1
)
==
str
(
dtype2
))
assert
(
dtype1
==
dtype2
)
==
(
str
(
dtype1
)
==
str
(
dtype2
))
def
test_transpose
():
x1
=
tensor
.
dvector
()
x2
=
tensor
.
dmatrix
()
x3
=
tensor
.
dtensor3
()
x1v
=
numpy
.
arange
(
24
)
x2v
=
numpy
.
arange
(
24
)
.
reshape
(
2
,
12
)
x3v
=
numpy
.
arange
(
24
)
.
reshape
(
2
,
3
,
4
)
f
=
theano
.
function
([
x1
,
x2
,
x3
],
[
tensor
.
transpose
(
x1
),
tensor
.
transpose
(
x2
),
tensor
.
transpose
(
x3
),
x1
.
transpose
(),
x2
.
transpose
(),
x3
.
transpose
(),
x2
.
transpose
(
0
,
1
),
x3
.
transpose
((
0
,
2
,
1
)),
tensor
.
transpose
(
x2
,
[
0
,
1
]),
tensor
.
transpose
(
x3
,
[
0
,
2
,
1
]),
])
t1
,
t2
,
t3
,
t1b
,
t2b
,
t3b
,
t2c
,
t3c
,
t2d
,
t3d
=
f
(
x1v
,
x2v
,
x3v
)
assert
t1
.
shape
==
numpy
.
transpose
(
x1v
)
.
shape
assert
t2
.
shape
==
numpy
.
transpose
(
x2v
)
.
shape
assert
t3
.
shape
==
numpy
.
transpose
(
x3v
)
.
shape
assert
numpy
.
all
(
t1
==
numpy
.
transpose
(
x1v
))
assert
numpy
.
all
(
t2
==
numpy
.
transpose
(
x2v
))
assert
numpy
.
all
(
t3
==
numpy
.
transpose
(
x3v
))
assert
numpy
.
all
(
t1b
==
x1v
.
transpose
())
assert
numpy
.
all
(
t2b
==
x2v
.
transpose
())
assert
numpy
.
all
(
t3b
==
x3v
.
transpose
())
assert
t2c
.
shape
==
(
2
,
12
)
assert
t3c
.
shape
==
(
2
,
4
,
3
)
assert
numpy
.
all
(
t2c
==
x2v
.
transpose
([
0
,
1
]))
assert
numpy
.
all
(
t3c
==
x3v
.
transpose
([
0
,
2
,
1
]))
assert
t2d
.
shape
==
(
2
,
12
)
assert
t3d
.
shape
==
(
2
,
4
,
3
)
assert
numpy
.
all
(
t2d
==
numpy
.
transpose
(
x2v
,
[
0
,
1
]))
assert
numpy
.
all
(
t3d
==
numpy
.
transpose
(
x3v
,
[
0
,
2
,
1
]))
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
if
0
:
if
0
:
unittest
.
main
()
unittest
.
main
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论