Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
5a14c0c4
提交
5a14c0c4
authored
2月 24, 2017
作者:
notoraptor
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rewrite hash methods (still too heavy, I think).
Add new tests. Fix typos.
上级
be97f3ee
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
139 行增加
和
18 行删除
+139
-18
test_wrapper.py
theano/common/tests/test_wrapper.py
+104
-14
wrapper.py
theano/common/wrapper.py
+35
-4
没有找到文件。
theano/common/tests/test_wrapper.py
浏览文件 @
5a14c0c4
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
theano
import
theano
import
numpy
import
numpy
from
unittest
import
TestCase
from
theano.gof
import
Op
,
Apply
from
theano.gof
import
Op
,
Apply
from
theano
import
Generic
from
theano.tensor
import
TensorType
from
theano.tensor
import
TensorType
from
theano.common
import
Wrapper
,
Wrap
from
theano.common
import
Wrapper
,
Wrap
from
theano
import
config
from
theano
import
config
...
@@ -98,17 +100,105 @@ class QuadraticFunction(Op):
...
@@ -98,17 +100,105 @@ class QuadraticFunction(Op):
"""
%
locals
()
"""
%
locals
()
def
test_wrapper
():
class
TestWrapper
(
TestCase
):
a
,
b
,
c
=
2
,
3
,
-
7
x
=
tensor
.
matrix
()
def
test_wrap_instances
(
self
):
y
=
QuadraticFunction
(
a
,
b
,
c
)(
x
)
w1
=
Wrap
(
a
=
1
,
b
=
'test string'
,
array
=
numpy
.
asarray
([
1
,
2
,
4
,
5
,
7
]),
floatting
=-
4.5
,
npy_scalar
=
numpy
.
asarray
(
12
))
f
=
theano
.
function
([
x
],
y
)
w2
=
Wrap
(
a
=
1
,
b
=
'test string'
,
array
=
numpy
.
asarray
([
1
,
2
,
4
,
5
,
7
]),
floatting
=-
4.5
,
npy_scalar
=
numpy
.
asarray
(
12
))
shape
=
(
100
,
100
)
assert
w1
==
w2
# The for-loop is here just to force profiling print something interesting.
assert
hash
(
w1
)
==
hash
(
w2
)
# When running this test without this loop, profiling does not print neither list of classes nor list of ops
assert
all
(
hasattr
(
w1
,
key
)
for
key
in
(
'a'
,
'b'
,
'array'
,
'floatting'
,
'npy_scalar'
))
# (maybe because the function is extremely fast ?).
# Changing attributes names only.
for
i
in
range
(
50
):
w2
=
Wrap
(
other_name
=
1
,
b
=
'test string'
,
array
=
numpy
.
asarray
([
1
,
2
,
4
,
5
,
7
]),
floatting
=-
4.5
,
npy_scalar
=
numpy
.
asarray
(
12
))
vx
=
numpy
.
random
.
normal
(
size
=
shape
[
0
]
*
shape
[
1
])
.
astype
(
dtype
)
.
reshape
(
*
shape
)
assert
w1
!=
w2
vy
=
f
(
vx
)
# Changing attributes types only.
ref
=
a
*
(
vx
**
2
)
+
b
*
vx
+
c
w2
=
Wrap
(
a
=
1
,
b
=
'test string'
,
array
=
[
1
,
2
,
4
,
5
,
7
],
floatting
=-
4.5
,
npy_scalar
=
numpy
.
asarray
(
12
))
utt
.
assert_allclose
(
ref
,
vy
)
assert
w1
!=
w2
# Changing attributes values only.
w2
=
Wrap
(
a
=
1
,
b
=
'string'
,
array
=
numpy
.
asarray
([
1
,
2
,
4
,
5
,
7
]),
floatting
=-
4.5
,
npy_scalar
=
numpy
.
asarray
(
12
))
assert
w1
!=
w2
# Changing NumPy array values.
w2
=
Wrap
(
a
=
1
,
b
=
'test string'
,
array
=
numpy
.
asarray
([
1
,
2
,
4
,
-
5
,
7
]),
floatting
=-
4.5
,
npy_scalar
=
numpy
.
asarray
(
12
))
assert
w1
!=
w2
def
test_wrapper_instances
(
self
):
w1
=
Wrapper
(
a1
=
TensorType
(
'int64'
,
(
False
,
False
)),
a2
=
TensorType
(
'int64'
,
(
False
,
True
,
False
,
False
,
True
)),
a3
=
Generic
())
w2
=
Wrapper
(
a1
=
TensorType
(
'int64'
,
(
False
,
False
)),
a2
=
TensorType
(
'int64'
,
(
False
,
True
,
False
,
False
,
True
)),
a3
=
Generic
())
assert
w1
==
w2
assert
hash
(
w1
)
==
hash
(
w2
)
# Changing attributes names only.
w2
=
Wrapper
(
a1
=
TensorType
(
'int64'
,
(
False
,
False
)),
other_name
=
TensorType
(
'int64'
,
(
False
,
True
,
False
,
False
,
True
)),
a3
=
Generic
())
assert
w1
!=
w2
# Changing attributes types only.
w2
=
Wrapper
(
a1
=
TensorType
(
'int64'
,
(
False
,
False
)),
a2
=
Generic
(),
# changing class
a3
=
Generic
())
assert
w1
!=
w2
# Changing attributes types characteristics only.
w2
=
Wrapper
(
a1
=
TensorType
(
'int64'
,
(
False
,
True
)),
# changing broadcasting
a2
=
TensorType
(
'int64'
,
(
False
,
True
,
False
,
False
,
True
)),
a3
=
Generic
())
assert
w1
!=
w2
def
test_wrapper_filtering
(
self
):
shape_tensor5
=
(
1
,
2
,
2
,
3
,
2
)
size_tensor5
=
reduce
(
lambda
x
,
y
:
x
*
y
,
shape_tensor5
,
1
)
random_tensor
=
numpy
.
random
.
normal
(
size
=
size_tensor5
)
.
astype
(
'float64'
)
.
reshape
(
shape_tensor5
)
# With a wrapper that does not match the value.
w
=
Wrapper
(
a1
=
TensorType
(
'int64'
,
(
False
,
False
)),
a2
=
TensorType
(
'float32'
,
(
False
,
False
,
False
,
False
,
False
)),
a3
=
Generic
())
o
=
Wrap
(
a1
=
numpy
.
asarray
([[
1
,
2
,
3
,
4
,
5
,
6
],
[
7
,
8
,
9
,
10
,
11
,
12
]])
.
astype
(
'int64'
),
a2
=
random_tensor
,
a3
=
2000
)
# should fail (a2 is not float32)
self
.
assertRaises
(
TypeError
,
w
.
filter
,
o
,
True
)
# should fail (a2 is float64, but downcast to float32 is disallowed)
self
.
assertRaises
(
TypeError
,
w
.
filter
,
o
,
False
,
False
)
# Should pass.
w
.
filter
(
o
,
strict
=
False
,
allow_downcast
=
True
)
# With a wrapper that matches the value.
w
=
Wrapper
(
a1
=
TensorType
(
'int64'
,
(
False
,
False
)),
a2
=
TensorType
(
'float64'
,
(
False
,
False
,
False
,
False
,
False
)),
a3
=
Generic
())
# All should pass.
w
.
filter
(
o
,
strict
=
True
)
w
.
filter
(
o
,
strict
=
False
,
allow_downcast
=
False
)
w
.
filter
(
o
,
strict
=
False
,
allow_downcast
=
True
)
# Check value_eq and value_eq_approx.
o2
=
Wrap
(
a1
=
numpy
.
asarray
([[
1
,
2
,
3
,
4
,
5
,
6
],
[
7
,
8
,
9
,
10
,
11
,
12
]])
.
astype
(
'int64'
),
a2
=
random_tensor
,
a3
=
2000
)
assert
w
.
values_eq
(
o
,
o2
)
assert
w
.
values_eq_approx
(
o
,
o2
)
# Check value_eq_approx.
o3
=
Wrap
(
a1
=
numpy
.
asarray
([[
1
,
2.0
,
3.000
,
4
,
5.0
,
6
],
[
7
,
8
,
9
,
10
,
11
,
12
]])
.
astype
(
'int32'
),
a2
=
random_tensor
.
astype
(
'float32'
),
a3
=
2000.0
)
assert
w
.
values_eq_approx
(
o
,
o3
)
def
test_wrapper
(
self
):
a
,
b
,
c
=
2
,
3
,
-
7
x
=
tensor
.
matrix
()
y
=
QuadraticFunction
(
a
,
b
,
c
)(
x
)
f
=
theano
.
function
([
x
],
y
)
shape
=
(
100
,
100
)
# The for-loop is here just to force profiling print something interesting.
# When running this test without this loop, profiling does not print neither list of classes nor list of ops
# (maybe because the function is extremely fast ?).
for
i
in
range
(
50
):
vx
=
numpy
.
random
.
normal
(
size
=
shape
[
0
]
*
shape
[
1
])
.
astype
(
dtype
)
.
reshape
(
*
shape
)
vy
=
f
(
vx
)
ref
=
a
*
(
vx
**
2
)
+
b
*
vx
+
c
utt
.
assert_allclose
(
ref
,
vy
)
theano/common/wrapper.py
浏览文件 @
5a14c0c4
...
@@ -29,6 +29,7 @@ See theano/common/tests/test_wrapper.py for a complete working example.
...
@@ -29,6 +29,7 @@ See theano/common/tests/test_wrapper.py for a complete working example.
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
re
import
re
import
hashlib
import
hashlib
import
numpy
from
theano.gof.utils
import
MethodNotDefined
from
theano.gof.utils
import
MethodNotDefined
from
theano.gof
import
Type
from
theano.gof
import
Type
from
theano.gof.cmodule
import
GCC_compiler
as
compiler
from
theano.gof.cmodule
import
GCC_compiler
as
compiler
...
@@ -71,10 +72,40 @@ class Wrap(object):
...
@@ -71,10 +72,40 @@ class Wrap(object):
self
.
data
[
key
]
=
value
self
.
data
[
key
]
=
value
def
__hash__
(
self
):
def
__hash__
(
self
):
return
hash
(
frozenset
(
self
.
data
.
items
()))
keys
=
sorted
(
self
.
data
.
keys
())
types
=
[]
attributes
=
[]
for
k
in
keys
:
types
+=
(
type
(
self
.
data
[
k
]),)
if
isinstance
(
self
.
data
[
k
],
numpy
.
ndarray
):
if
len
(
self
.
data
[
k
]
.
shape
)
==
0
:
attributes
+=
(
numpy
.
asscalar
(
self
.
data
[
k
]),)
else
:
attributes
+=
tuple
(
self
.
data
[
k
])
else
:
try
:
iter
(
self
.
data
[
k
])
except
TypeError
:
attributes
+=
(
self
.
data
[
k
],)
else
:
attributes
+=
tuple
(
self
.
data
[
k
])
return
hash
((
type
(
self
),)
+
tuple
(
keys
)
+
tuple
(
types
)
+
tuple
(
attributes
))
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
and
self
.
data
==
other
.
data
if
type
(
self
)
!=
type
(
other
):
return
False
for
k
in
self
.
data
:
if
(
k
not
in
other
.
data
or
not
isinstance
(
self
.
data
[
k
],
type
(
other
.
data
[
k
]))
or
not
isinstance
(
other
.
data
[
k
],
type
(
self
.
data
[
k
]))):
return
False
if
isinstance
(
self
.
data
[
k
],
numpy
.
ndarray
):
if
not
numpy
.
allclose
(
self
.
data
[
k
],
other
.
data
[
k
]):
return
False
elif
self
.
data
[
k
]
!=
other
.
data
[
k
]:
return
False
return
True
# return type(self) == type(other) and self.data == other.data
class
Wrapper
(
Type
):
class
Wrapper
(
Type
):
...
@@ -178,7 +209,7 @@ class Wrapper(Type):
...
@@ -178,7 +209,7 @@ class Wrapper(Type):
a
=
self
.
filter
(
a
,
strict
=
False
)
a
=
self
.
filter
(
a
,
strict
=
False
)
b
=
self
.
filter
(
b
,
strict
=
False
)
b
=
self
.
filter
(
b
,
strict
=
False
)
for
i
in
range
(
self
.
length
):
for
i
in
range
(
self
.
length
):
if
not
self
.
types
[
i
]
.
value_eq
(
getattr
(
a
,
self
.
fields
[
i
]),
getattr
(
b
,
self
.
fields
[
i
])):
if
not
self
.
types
[
i
]
.
value
s
_eq
(
getattr
(
a
,
self
.
fields
[
i
]),
getattr
(
b
,
self
.
fields
[
i
])):
return
False
return
False
return
True
return
True
...
@@ -186,7 +217,7 @@ class Wrapper(Type):
...
@@ -186,7 +217,7 @@ class Wrapper(Type):
a
=
self
.
filter
(
a
,
strict
=
False
)
a
=
self
.
filter
(
a
,
strict
=
False
)
b
=
self
.
filter
(
b
,
strict
=
False
)
b
=
self
.
filter
(
b
,
strict
=
False
)
for
i
in
range
(
self
.
length
):
for
i
in
range
(
self
.
length
):
if
not
self
.
types
[
i
]
.
value_eq_approx
(
getattr
(
a
,
self
.
fields
[
i
]),
getattr
(
b
,
self
.
fields
[
i
])):
if
not
self
.
types
[
i
]
.
value
s
_eq_approx
(
getattr
(
a
,
self
.
fields
[
i
]),
getattr
(
b
,
self
.
fields
[
i
])):
return
False
return
False
return
True
return
True
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论