Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e0d81475
提交
e0d81475
authored
6月 10, 2008
作者:
Olivier Breuleux
浏览文件
操作
浏览文件
下载
差异文件
merge
上级
c1a5c7ed
4fc8b90f
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
129 行增加
和
66 行删除
+129
-66
__init__.py
__init__.py
+32
-29
_test_tensor.py
_test_tensor.py
+26
-19
wraplinker.py
sandbox/wraplinker.py
+1
-1
tensor.py
tensor.py
+70
-17
没有找到文件。
__init__.py
浏览文件 @
e0d81475
...
@@ -52,33 +52,36 @@ def __src_version__():
...
@@ -52,33 +52,36 @@ def __src_version__():
#
#
#print 'name:', __name__
if
not
hasattr
(
__src_version__
,
'rval'
):
location
=
_imp
.
find_module
(
__name__
)[
1
]
#print 'name:', __name__
#print 'location:', location
location
=
_imp
.
find_module
(
__name__
)[
1
]
#print 'location:', location
status
=
_subprocess
.
Popen
((
'hg'
,
'st'
),
cwd
=
location
,
stdout
=
_subprocess
.
PIPE
)
.
communicate
()[
0
]
#status_codes = [line[0] for line in if line and line[0] != '?']
status
=
_subprocess
.
Popen
((
'hg'
,
'st'
),
cwd
=
location
,
stdout
=
_subprocess
.
PIPE
)
.
communicate
()[
0
]
for
line
in
status
.
split
(
'
\n
'
):
#status_codes = [line[0] for line in if line and line[0] != '?']
if
not
line
:
continue
for
line
in
status
.
split
(
'
\n
'
):
if
line
[
0
]
!=
'?'
:
if
not
line
:
continue
raise
Exception
(
'Uncommitted modification to "
%
s" in
%
s (
%
s)'
if
line
[
0
]
!=
'?'
:
%
(
line
[
2
:],
__name__
,
location
))
raise
Exception
(
'Uncommitted modification to "
%
s" in
%
s (
%
s)'
if
line
[
0
]
==
'?'
and
line
[
-
3
:]
==
'.py'
:
%
(
line
[
2
:],
__name__
,
location
))
raise
Exception
(
'Untracked file "
%
s" in
%
s (
%
s)'
if
line
[
0
]
==
'?'
and
line
[
-
3
:]
==
'.py'
:
%
(
line
[
2
:],
__name__
,
location
))
raise
Exception
(
'Untracked file "
%
s" in
%
s (
%
s)'
%
(
line
[
2
:],
__name__
,
location
))
hg_id
=
_subprocess
.
Popen
((
'hg'
,
'id'
),
cwd
=
location
,
stdout
=
_subprocess
.
PIPE
)
.
communicate
()[
0
]
hg_id
=
_subprocess
.
Popen
((
'hg'
,
'id'
),
cwd
=
location
,
stdout
=
_subprocess
.
PIPE
)
.
communicate
()[
0
]
#This asserts my understanding of hg id return values
# There is mention in the doc that it might return two parent hash codes
#This asserts my understanding of hg id return values
# but I've never seen it, and I dont' know what it means or how it is
# There is mention in the doc that it might return two parent hash codes
# formatted.
# but I've never seen it, and I dont' know what it means or how it is
tokens
=
hg_id
.
split
(
' '
)
# formatted.
assert
len
(
tokens
)
<=
2
tokens
=
hg_id
.
split
(
' '
)
assert
len
(
tokens
)
>=
1
assert
len
(
tokens
)
<=
2
assert
tokens
[
0
][
-
1
]
!=
'+'
# the trailing + indicates uncommitted changes
assert
len
(
tokens
)
>=
1
if
len
(
tokens
)
==
2
:
assert
tokens
[
0
][
-
1
]
!=
'+'
# the trailing + indicates uncommitted changes
assert
tokens
[
1
]
==
'tip
\n
'
if
len
(
tokens
)
==
2
:
assert
tokens
[
1
]
==
'tip
\n
'
return
tokens
[
0
]
__src_version__
.
rval
=
tokens
[
0
]
return
__src_version__
.
rval
_test_tensor.py
浏览文件 @
e0d81475
...
@@ -1282,22 +1282,22 @@ class t_dot(unittest.TestCase):
...
@@ -1282,22 +1282,22 @@ class t_dot(unittest.TestCase):
self
.
failUnless
(
tz
.
shape
==
nz
.
shape
)
self
.
failUnless
(
tz
.
shape
==
nz
.
shape
)
self
.
failUnless
(
_approx_eq
(
nz
,
tz
))
self
.
failUnless
(
_approx_eq
(
nz
,
tz
))
def
test_dot_0d_0d
(
self
):
self
.
cmp_dot
(
1.1
,
2.2
)
#
def test_dot_0d_0d(self): self.cmp_dot(1.1, 2.2)
def
test_dot_0d_1d
(
self
):
self
.
cmp_dot
(
1.1
,
self
.
rand
(
5
))
#
def test_dot_0d_1d(self): self.cmp_dot(1.1, self.rand(5))
def
test_dot_0d_2d
(
self
):
self
.
cmp_dot
(
3.0
,
self
.
rand
(
6
,
7
))
#
def test_dot_0d_2d(self): self.cmp_dot(3.0, self.rand(6,7))
def
test_dot_0d_3d
(
self
):
self
.
cmp_dot
(
3.0
,
self
.
rand
(
8
,
6
,
7
))
#
def test_dot_0d_3d(self): self.cmp_dot(3.0, self.rand(8,6,7))
def
test_dot_1d_0d
(
self
):
self
.
cmp_dot
(
self
.
rand
(
5
),
1.1
)
#
def test_dot_1d_0d(self): self.cmp_dot(self.rand(5), 1.1 )
def
test_dot_1d_1d
(
self
):
self
.
cmp_dot
(
self
.
rand
(
5
),
self
.
rand
(
5
))
def
test_dot_1d_1d
(
self
):
self
.
cmp_dot
(
self
.
rand
(
5
),
self
.
rand
(
5
))
def
test_dot_1d_2d
(
self
):
self
.
cmp_dot
(
self
.
rand
(
6
),
self
.
rand
(
6
,
7
))
def
test_dot_1d_2d
(
self
):
self
.
cmp_dot
(
self
.
rand
(
6
),
self
.
rand
(
6
,
7
))
def
test_dot_1d_3d
(
self
):
self
.
cmp_dot
(
self
.
rand
(
6
),
self
.
rand
(
8
,
6
,
7
))
#
def test_dot_1d_3d(self): self.cmp_dot(self.rand(6), self.rand(8,6,7))
def
test_dot_2d_0d
(
self
):
self
.
cmp_dot
(
self
.
rand
(
5
,
6
),
1.0
)
#
def test_dot_2d_0d(self): self.cmp_dot(self.rand(5,6), 1.0)
def
test_dot_2d_1d
(
self
):
self
.
cmp_dot
(
self
.
rand
(
5
,
6
),
self
.
rand
(
6
))
def
test_dot_2d_1d
(
self
):
self
.
cmp_dot
(
self
.
rand
(
5
,
6
),
self
.
rand
(
6
))
def
test_dot_2d_2d
(
self
):
self
.
cmp_dot
(
self
.
rand
(
5
,
6
),
self
.
rand
(
6
,
7
))
def
test_dot_2d_2d
(
self
):
self
.
cmp_dot
(
self
.
rand
(
5
,
6
),
self
.
rand
(
6
,
7
))
def
test_dot_2d_3d
(
self
):
self
.
cmp_dot
(
self
.
rand
(
5
,
6
),
self
.
rand
(
8
,
6
,
7
))
#
def test_dot_2d_3d(self): self.cmp_dot(self.rand(5,6), self.rand(8,6,7))
def
test_dot_3d_0d
(
self
):
self
.
cmp_dot
(
self
.
rand
(
4
,
5
,
6
),
1.0
)
#
def test_dot_3d_0d(self): self.cmp_dot(self.rand(4,5,6), 1.0)
def
test_dot_3d_1d
(
self
):
self
.
cmp_dot
(
self
.
rand
(
4
,
5
,
6
),
self
.
rand
(
6
))
#
def test_dot_3d_1d(self): self.cmp_dot(self.rand(4,5,6), self.rand(6))
def
test_dot_3d_2d
(
self
):
self
.
cmp_dot
(
self
.
rand
(
4
,
5
,
6
),
self
.
rand
(
6
,
7
))
#
def test_dot_3d_2d(self): self.cmp_dot(self.rand(4,5,6), self.rand(6,7))
def
test_dot_3d_3d
(
self
):
self
.
cmp_dot
(
self
.
rand
(
4
,
5
,
6
),
self
.
rand
(
8
,
6
,
7
))
#
def test_dot_3d_3d(self): self.cmp_dot(self.rand(4,5,6), self.rand(8,6,7))
def
not_aligned
(
self
,
x
,
y
):
def
not_aligned
(
self
,
x
,
y
):
z
=
dot
(
x
,
y
)
z
=
dot
(
x
,
y
)
...
@@ -1310,16 +1310,22 @@ class t_dot(unittest.TestCase):
...
@@ -1310,16 +1310,22 @@ class t_dot(unittest.TestCase):
def
test_align_1_1
(
self
):
self
.
not_aligned
(
self
.
rand
(
5
),
self
.
rand
(
6
))
def
test_align_1_1
(
self
):
self
.
not_aligned
(
self
.
rand
(
5
),
self
.
rand
(
6
))
def
test_align_1_2
(
self
):
self
.
not_aligned
(
self
.
rand
(
5
),
self
.
rand
(
6
,
4
))
def
test_align_1_2
(
self
):
self
.
not_aligned
(
self
.
rand
(
5
),
self
.
rand
(
6
,
4
))
def
test_align_1_3
(
self
):
self
.
not_aligned
(
self
.
rand
(
5
),
self
.
rand
(
6
,
4
,
7
))
#
def test_align_1_3(self): self.not_aligned(self.rand(5), self.rand(6,4,7))
def
test_align_2_1
(
self
):
self
.
not_aligned
(
self
.
rand
(
5
,
4
),
self
.
rand
(
6
))
def
test_align_2_1
(
self
):
self
.
not_aligned
(
self
.
rand
(
5
,
4
),
self
.
rand
(
6
))
def
test_align_2_1
(
self
):
self
.
not_aligned
(
self
.
rand
(
5
,
4
),
self
.
rand
(
6
,
7
))
def
test_align_2_1
(
self
):
self
.
not_aligned
(
self
.
rand
(
5
,
4
),
self
.
rand
(
6
,
7
))
def
test_align_2_3
(
self
):
self
.
not_aligned
(
self
.
rand
(
5
,
4
),
self
.
rand
(
6
,
7
,
8
))
#
def test_align_2_3(self): self.not_aligned(self.rand(5,4), self.rand(6,7,8))
def
test_align_3_1
(
self
):
self
.
not_aligned
(
self
.
rand
(
5
,
4
,
3
),
self
.
rand
(
6
))
#
def test_align_3_1(self): self.not_aligned(self.rand(5,4,3), self.rand(6))
def
test_align_3_2
(
self
):
self
.
not_aligned
(
self
.
rand
(
5
,
4
,
3
),
self
.
rand
(
6
,
7
))
#
def test_align_3_2(self): self.not_aligned(self.rand(5,4,3), self.rand(6,7))
def
test_align_3_3
(
self
):
self
.
not_aligned
(
self
.
rand
(
5
,
4
,
3
),
self
.
rand
(
6
,
7
,
8
))
#
def test_align_3_3(self): self.not_aligned(self.rand(5,4,3), self.rand(6,7,8))
def
test_grad
(
self
):
def
test_grad
(
self
):
#verify_grad(self, dot, [self.rand(2,3,4), self.rand(4)])
verify_grad
(
self
,
dot
,
[
self
.
rand
(
2
,
3
),
self
.
rand
(
3
,
2
)])
verify_grad
(
self
,
dot
,
[
self
.
rand
(
2
,
3
),
self
.
rand
(
3
,
2
)])
verify_grad
(
self
,
dot
,
[
self
.
rand
(
2
),
self
.
rand
(
2
,
3
)])
verify_grad
(
self
,
dot
,
[
self
.
rand
(
3
,
2
),
self
.
rand
(
2
)])
verify_grad
(
self
,
dot
,
[
self
.
rand
(
2
),
self
.
rand
(
2
)])
#verify_grad(self, dot, [self.rand(), self.rand(2)])
#verify_grad(self, dot, [self.rand(), self.rand(2,5)])
class
t_gemm
(
unittest
.
TestCase
):
class
t_gemm
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
...
@@ -1703,7 +1709,8 @@ if __name__ == '__main__':
...
@@ -1703,7 +1709,8 @@ if __name__ == '__main__':
if
1
:
if
1
:
unittest
.
main
()
unittest
.
main
()
else
:
else
:
testcase
=
t_dot
suite
=
unittest
.
TestLoader
()
suite
=
unittest
.
TestLoader
()
#suite = suite.loadTestsFromTestCase(T_subtensor)
suite
=
suite
.
loadTestsFromTestCase
(
testcase
)
suite
=
suite
.
loadTestsFromTestCase
(
T_Stack
)
unittest
.
TextTestRunner
(
verbosity
=
2
)
.
run
(
suite
)
unittest
.
TextTestRunner
(
verbosity
=
2
)
.
run
(
suite
)
sandbox/wraplinker.py
浏览文件 @
e0d81475
...
@@ -69,7 +69,7 @@ def numpy_wrapper(f):
...
@@ -69,7 +69,7 @@ def numpy_wrapper(f):
for
thunk
in
thunks
:
for
thunk
in
thunks
:
for
output
in
thunk
.
outputs
:
for
output
in
thunk
.
outputs
:
if
hasattr
(
output
,
'dtype'
):
if
hasattr
(
output
,
'dtype'
):
if
f
(
output
)
)
:
if
f
(
output
):
raise
Exception
(
'uh oh'
,
(
thunk
,
output
))
raise
Exception
(
'uh oh'
,
(
thunk
,
output
))
return
wrapper
return
wrapper
...
...
tensor.py
浏览文件 @
e0d81475
...
@@ -912,37 +912,90 @@ def horizontal_stack(x, y):
...
@@ -912,37 +912,90 @@ def horizontal_stack(x, y):
#########################
#########################
class
Dot
(
Op
):
class
Dot
(
Op
):
"""Compute matrix-matrix, matrix-vector products and vector inner-products.
"""
def
make_node
(
self
,
*
inputs
):
def
make_node
(
self
,
*
inputs
):
inputs
=
map
(
as_tensor
,
inputs
)
inputs
=
map
(
as_tensor
,
inputs
)
if
len
(
inputs
)
!=
2
:
raise
TypeError
(
"Wrong number of inputs for
%
s (got
%
i, expected 2)"
%
self
)
i_broadcastables
=
[
input
.
type
.
broadcastable
for
input
in
inputs
]
i_dtypes
=
[
input
.
type
.
dtype
for
input
in
inputs
]
bx
,
by
=
i_broadcastables
numpy_semantics
=
0
if
len
(
bx
)
==
0
:
# x is a scalar
if
numpy_semantics
:
bz
=
by
#numpy defines dot for tensor pairs with any rank
if
len
(
inputs
)
!=
2
:
raise
TypeError
(
"Wrong number of inputs for
%
s (got
%
i, expected 2)"
%
self
)
i_broadcastables
=
[
input
.
type
.
broadcastable
for
input
in
inputs
]
bx
,
by
=
i_broadcastables
if
len
(
bx
)
==
0
:
# x is a scalar
bz
=
by
else
:
if
len
(
by
)
>=
2
:
#y is a matrix or tensor
bz
=
bx
[:
-
1
]
+
by
[:
-
2
]
+
by
[
-
1
:]
elif
len
(
by
)
==
1
:
#y is vector
bz
=
bx
[:
-
1
]
else
:
#y is a scalar
bz
=
bx
else
:
else
:
if
len
(
by
)
>=
2
:
#y is a matrix or tensor
x
,
y
=
inputs
bz
=
bx
[:
-
1
]
+
by
[:
-
2
]
+
by
[
-
1
:]
nx
=
x
.
type
.
ndim
elif
len
(
by
)
==
1
:
#y is vector
ny
=
y
.
type
.
ndim
bz
=
bx
[:
-
1
]
else
:
#y is a scalar
if
nx
not
in
(
1
,
2
):
raise
TypeError
(
'not matrix or vector'
,
x
)
bz
=
bx
if
ny
not
in
(
1
,
2
):
raise
TypeError
(
'not matrix or vector'
,
y
)
o_broadcastables
=
[
bz
]
o_dtypes
=
[
scal
.
upcast
(
*
i_dtypes
)]
if
nx
==
2
and
ny
==
2
:
bz
=
[
x
.
type
.
broadcastable
[
0
],
y
.
type
.
broadcastable
[
1
]]
outputs
=
[
tensor
(
t
,
b
)
for
b
,
t
in
zip
(
o_broadcastables
,
o_dtypes
)]
elif
nx
==
1
and
ny
==
2
:
bz
=
[
y
.
type
.
broadcastable
[
1
]]
elif
nx
==
2
and
ny
==
1
:
bz
=
[
x
.
type
.
broadcastable
[
0
]]
else
:
bz
=
[]
i_dtypes
=
[
input
.
type
.
dtype
for
input
in
inputs
]
outputs
=
[
tensor
(
scal
.
upcast
(
*
i_dtypes
),
bz
)]
return
Apply
(
self
,
inputs
,
outputs
)
return
Apply
(
self
,
inputs
,
outputs
)
def
perform
(
self
,
node
,
(
x
,
y
),
(
z
,
)):
def
perform
(
self
,
node
,
(
x
,
y
),
(
z
,
)):
z
[
0
]
=
numpy
.
dot
(
x
,
y
)
z
[
0
]
=
numpy
.
dot
(
x
,
y
)
def
grad
(
self
,
(
x
,
y
),
(
gz
,)):
def
grad
(
self
,
(
x
,
y
),
(
gz
,)):
if
gz
.
type
.
ndim
==
0
:
return
gz
*
y
,
gz
*
x
if
x
.
type
.
ndim
==
1
and
y
.
type
.
ndim
>
1
:
return
dot
(
gz
,
y
.
T
),
outer
(
x
.
T
,
gz
)
if
x
.
type
.
ndim
>
1
and
y
.
type
.
ndim
==
1
:
return
outer
(
gz
,
y
.
T
),
dot
(
x
.
T
,
gz
)
return
dot
(
gz
,
y
.
T
),
dot
(
x
.
T
,
gz
)
return
dot
(
gz
,
y
.
T
),
dot
(
x
.
T
,
gz
)
def
__str__
(
self
):
def
__str__
(
self
):
return
"dot"
return
"dot"
dot
=
Dot
()
dot
=
Dot
()
class
Outer
(
Op
):
""" Compute vector-vector outer product
"""
def
make_node
(
self
,
*
inputs
):
inputs
=
map
(
as_tensor
,
inputs
)
x
,
y
=
inputs
nx
=
x
.
type
.
ndim
ny
=
y
.
type
.
ndim
if
nx
!=
1
:
raise
TypeError
(
'not vector'
,
x
)
if
ny
!=
1
:
raise
TypeError
(
'not vector'
,
y
)
bz
=
[
x
.
type
.
broadcastable
[
0
],
y
.
type
.
broadcastable
[
0
]]
i_dtypes
=
[
input
.
type
.
dtype
for
input
in
inputs
]
outputs
=
[
tensor
(
scal
.
upcast
(
*
i_dtypes
),
bz
)]
return
Apply
(
self
,
inputs
,
outputs
)
def
perform
(
self
,
node
,
(
x
,
y
),
(
z
,
)):
z
[
0
]
=
numpy
.
outer
(
x
,
y
)
def
grad
(
self
,
(
x
,
y
),
(
gz
,)):
return
dot
(
gz
,
y
),
dot
(
x
,
gz
)
#no transposing necessary
def
__str__
(
self
):
return
"outer"
outer
=
Outer
()
class
Gemm
(
Op
):
class
Gemm
(
Op
):
E_rank
=
'gemm only works for rank 2'
E_rank
=
'gemm only works for rank 2'
E_scalar
=
'gemm requires scalar argument'
E_scalar
=
'gemm requires scalar argument'
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论