Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
6c853649
提交
6c853649
authored
5月 15, 2014
作者:
Hengjean
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implemented basic typed list
上级
47a812ab
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
264 行增加
和
0 行删除
+264
-0
__init__.py
theano/typed_list/__init__.py
+1
-0
__init__.py
theano/typed_list/tests/__init__.py
+0
-0
test_type.py
theano/typed_list/tests/test_type.py
+192
-0
type.py
theano/typed_list/type.py
+71
-0
没有找到文件。
theano/typed_list/__init__.py
0 → 100644
浏览文件 @
6c853649
from
type
import
TypedListType
theano/typed_list/tests/__init__.py
0 → 100644
浏览文件 @
6c853649
theano/typed_list/tests/test_type.py
0 → 100644
浏览文件 @
6c853649
import
unittest
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.attrib
import
attr
import
numpy
from
numpy.testing
import
dec
,
assert_array_equal
,
assert_allclose
from
numpy.testing.noseclasses
import
KnownFailureTest
import
theano
import
theano.typed_list
from
theano
import
tensor
as
T
from
theano.typed_list.type
import
TypedListType
from
theano.tests
import
unittest_tools
as
utt
#took from tensors/tests/test_basic.py
def
rand_ranged_matrix
(
minimum
,
maximum
,
shape
):
return
numpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
)
*
(
maximum
-
minimum
)
+
minimum
,
dtype
=
theano
.
config
.
floatX
)
class
test_typed_list_type
(
unittest
.
TestCase
):
def
setUp
(
self
):
utt
.
seed_rng
()
def
test_wrong_input_on_creation
(
self
):
"""
Typed list type should raises an
error if the argument passed for
type is not a valid theano type
"""
self
.
assertRaises
(
TypeError
,
TypedListType
,
None
)
def
test_wrong_input_on_filter
(
self
):
"""
Typed list type should raises an
error if the argument given to filter
isn't of the same type as the one
specified on creation
"""
#list of matrices
myType
=
TypedListType
(
T
.
TensorType
(
theano
.
config
.
floatX
,
(
False
,
False
)))
self
.
assertRaises
(
TypeError
,
myType
.
filter
,
[
4
])
def
test_not_a_list_on_filter
(
self
):
"""
Typed List Value should raises an error
if no iterable variable is given on input
"""
#list of matrices
myType
=
TypedListType
(
T
.
TensorType
(
theano
.
config
.
floatX
,
(
False
,
False
)))
self
.
assertRaises
(
TypeError
,
myType
.
filter
,
4
)
def
test_type_equality
(
self
):
"""
Typed list types should only be equal
when they contains the same theano
variables
"""
#list of matrices
myType1
=
TypedListType
(
T
.
TensorType
(
theano
.
config
.
floatX
,
(
False
,
False
)))
#list of matrices
myType2
=
TypedListType
(
T
.
TensorType
(
theano
.
config
.
floatX
,
(
False
,
False
)))
#list of scalars
myType3
=
TypedListType
(
T
.
TensorType
(
theano
.
config
.
floatX
,
()))
self
.
assertTrue
(
myType2
==
myType1
)
self
.
assertFalse
(
myType3
==
myType1
)
def
test_filter_sanity_check
(
self
):
"""
Simple test on typed list type filter
"""
myType
=
TypedListType
(
T
.
TensorType
(
theano
.
config
.
floatX
,
(
False
,
False
)))
x
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
100
])
self
.
assertTrue
(
numpy
.
array_equal
(
myType
.
filter
([
x
]),
[
x
]))
def
test_intern_filter
(
self
):
"""
(supposing theano.config.floatX = floatX)
Test checking if values contained are themselves
filtered. If they weren't this code would raise
an exception.
"""
myType
=
TypedListType
(
T
.
TensorType
(
theano
.
config
.
floatX
,
(
False
,
False
)))
x
=
numpy
.
asarray
([[
4
,
5
],
[
4
,
5
]],
dtype
=
'Float32'
)
self
.
assertTrue
(
numpy
.
array_equal
(
myType
.
filter
([
x
]),
[
x
]))
#Will fail for unknown reasons
#under search
"""
def test_load(self):
myType = TypedListType(T.TensorType(theano.config.floatX,
(False, False)))
x = rand_ranged_matrix(-1000, 1000, [100, 100])
testList = []
for i in range(10000):
testList.append(x)
self.assertTrue(numpy.array_equal(myType.filter(testList), testList))
"""
def
test_basic_nested_list
(
self
):
"""
Testing nested list with one level of depth
"""
myNestedType
=
TypedListType
(
T
.
TensorType
(
theano
.
config
.
floatX
,
(
False
,
False
)))
myType
=
TypedListType
(
myNestedType
)
x
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
100
])
self
.
assertTrue
(
numpy
.
array_equal
(
myType
.
filter
([[
x
]]),
[[
x
]]))
def
test_comparison_different_depth
(
self
):
"""
Nested list with different depth aren't the same
"""
myNestedType
=
TypedListType
(
T
.
TensorType
(
theano
.
config
.
floatX
,
(
False
,
False
)))
myNestedType2
=
TypedListType
(
myNestedType
)
myNestedType3
=
TypedListType
(
myNestedType2
)
self
.
assertFalse
(
myNestedType2
==
myNestedType3
)
def
test_nested_list_arg
(
self
):
"""
test for the 'depth' optionnal argument
"""
myNestedType
=
TypedListType
(
T
.
TensorType
(
theano
.
config
.
floatX
,
(
False
,
False
)),
3
)
myType
=
TypedListType
(
T
.
TensorType
(
theano
.
config
.
floatX
,
(
False
,
False
)))
myManualNestedType
=
TypedListType
(
TypedListType
(
TypedListType
(
myType
)))
self
.
assertTrue
(
myNestedType
==
myManualNestedType
)
def
test_get_depth
(
self
):
"""
test case for get_depth utilitary function
"""
myType
=
TypedListType
(
T
.
TensorType
(
theano
.
config
.
floatX
,
(
False
,
False
)))
myManualNestedType
=
TypedListType
(
TypedListType
(
TypedListType
(
myType
)))
self
.
assertTrue
(
myManualNestedType
.
get_depth
()
==
3
)
def
test_comparison_uneven_nested
(
self
):
"""
test for comparison between uneven nested list
"""
myType
=
TypedListType
(
T
.
TensorType
(
theano
.
config
.
floatX
,
(
False
,
False
)))
myManualNestedType1
=
TypedListType
(
TypedListType
(
TypedListType
(
myType
)))
myManualNestedType2
=
TypedListType
(
TypedListType
(
myType
))
self
.
assertFalse
(
myManualNestedType1
==
myManualNestedType2
)
self
.
assertFalse
(
myManualNestedType2
==
myManualNestedType1
)
\ No newline at end of file
theano/typed_list/type.py
0 → 100644
浏览文件 @
6c853649
from
theano
import
gof
class
TypedListType
(
gof
.
Type
):
def
__init__
(
self
,
ttype
,
depth
=
0
):
"""
:Parameters:
-'ttype' : Type of theano variable this list
will contains, can be another list.
-'depth' : Optionnal parameters, any value
above 0 will create a nested list of this
depth. (0-based)
"""
if
depth
<
0
:
raise
ValueError
(
'Please specify a depth superior or'
'equal to 0'
)
if
not
hasattr
(
ttype
,
'is_valid_value'
):
raise
TypeError
(
'Expected a Theano type'
)
if
depth
==
0
:
self
.
ttype
=
ttype
else
:
self
.
ttype
=
TypedListType
(
ttype
,
depth
-
1
)
def
filter
(
self
,
x
,
strict
=
False
,
allow_downcast
=
None
):
"""
:Parameters:
-'x' : value to filter
-'strict' : if true, only native python list will be accepted
-'allow_downcast' : does not have any utility at the moment
"""
if
strict
:
if
not
isinstance
(
x
,
list
):
raise
TypeError
(
'Expected a python list'
)
else
:
x
=
list
(
x
)
#check all member of list are of the same type
#for the moment only one dimension list accepted
x
=
[
self
.
ttype
.
filter
(
y
)
for
y
in
x
]
if
all
(
self
.
ttype
.
is_valid_value
(
y
)
for
y
in
x
):
return
x
else
:
raise
TypeError
(
'Expected all elements to'
' be
%
s'
%
str
(
self
.
ttype
))
def
__eq__
(
self
,
other
):
"""
two list are equals if they contains the same type.
"""
if
not
hasattr
(
other
,
'ttype'
):
return
False
return
(
self
.
ttype
==
other
.
ttype
)
def
__str__
(
self
):
return
'Typed List <'
+
str
(
self
.
ttype
)
+
'>'
def
get_depth
(
self
):
"""
utilitary function to get the 0 based
level of the list
"""
if
hasattr
(
self
.
ttype
,
'get_depth'
):
return
self
.
ttype
.
get_depth
()
+
1
else
:
return
0
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论