Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
c0217dd1
提交
c0217dd1
authored
3月 06, 2015
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2558 from carriepl/fix_stochastic_tests
[WIP] Mechanism for running a test multiple times with different random seeds
上级
a0964ac0
a5e646f0
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
99 行增加
和
1 行删除
+99
-1
test_nlinalg.py
theano/tensor/tests/test_nlinalg.py
+9
-1
unittest_tools.py
theano/tests/unittest_tools.py
+90
-0
没有找到文件。
theano/tensor/tests/test_nlinalg.py
浏览文件 @
c0217dd1
...
...
@@ -419,6 +419,14 @@ class test_Eigh(test_Eig):
class
test_Eigh_float32
(
test_Eigh
):
dtype
=
'float32'
@utt.AttemptManyTimes
(
n_attempts
=
3
,
n_req_successes
=
2
)
def
test_uplo
(
self
):
super
(
test_Eigh_float32
,
self
)
.
test_uplo
()
@utt.AttemptManyTimes
(
n_attempts
=
3
,
n_req_successes
=
2
)
def
test_grad
(
self
):
super
(
test_Eigh_float32
,
self
)
.
test_grad
()
class
T_lstsq
(
unittest
.
TestCase
):
...
...
@@ -452,7 +460,7 @@ class T_lstsq(unittest.TestCase):
self
.
assertRaises
(
numpy
.
linalg
.
LinAlgError
,
f
,
[
2
,
1
],
[
2
,
1
],
[
2
,
1
])
class
Matrix_power
():
class
Matrix_power
(
unittest
.
TestCase
):
def
test_numpy_compare
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
...
...
theano/tests/unittest_tools.py
浏览文件 @
c0217dd1
...
...
@@ -341,3 +341,93 @@ class WrongValue(Exception):
def
assert_allclose
(
val1
,
val2
,
rtol
=
None
,
atol
=
None
):
if
not
T
.
basic
.
_allclose
(
val1
,
val2
,
rtol
,
atol
):
raise
WrongValue
(
val1
,
val2
,
rtol
,
atol
)
class
AttemptManyTimes
:
"""Decorator for unit tests that forces a unit test to be attempted
multiple times. The test needs to pass a certain number of times for it to
be considered to have succeeded. If it doesn't pass enough times, it is
considered to have failed.
Warning : care should be exercised when using this decorator. For some
tests, the fact that they fail randomly could point to important issues
such as race conditions, usage of uninitialized memory region, etc. and
using this decorator could hide these problems.
Usage:
@AttemptManyTimes(n_attempts=5, n_req_successes=3)
def fct(args):
...
"""
def
__init__
(
self
,
n_attempts
,
n_req_successes
=
1
):
assert
n_attempts
>=
n_req_successes
self
.
n_attempts
=
n_attempts
self
.
n_req_successes
=
n_req_successes
def
__call__
(
self
,
fct
):
# Wrap fct in a function that will attempt to run it multiple
# times and return the result if the test passes enough times
# of propagate the raised exception if it doesn't.
def
attempt_multiple_times
(
*
args
,
**
kwargs
):
# Keep a copy of the current seed for unittests so that we can use
# a different seed for every run of the decorated test and restore
# the original after
original_seed
=
config
.
unittests
.
rseed
current_seed
=
original_seed
# If the decorator has received only one, unnamed, argument
# and that argument has an atribute _testMethodName, it means
# that the unit test on which the decorator is used is in a test
# class. This means that the setup() method of that class will
# need to be called before any attempts to execute the test in
# case it relies on data randomly generated in the class' setup()
# method.
if
(
len
(
args
)
==
1
and
hasattr
(
args
[
0
],
"_testMethodName"
)):
test_in_class
=
True
class_instance
=
args
[
0
]
else
:
test_in_class
=
False
n_fail
=
0
n_success
=
0
# Attempt to call the test function multiple times. If it does
# raise any exception for at least one attempt, it passes. If it
# raises an exception at every attempt, it fails.
for
i
in
range
(
self
.
n_attempts
):
try
:
# Attempt to make the test use the current seed
config
.
unittests
.
rseed
=
current_seed
if
test_in_class
and
hasattr
(
class_instance
,
"setUp"
):
class_instance
.
setUp
()
fct
(
*
args
,
**
kwargs
)
n_success
+=
1
if
n_success
==
self
.
n_req_successes
:
break
except
Exception
:
n_fail
+=
1
# If there is not enough attempts remaining to achieve the
# required number of successes, propagate the original
# exception
if
n_fail
+
self
.
n_req_successes
>
self
.
n_attempts
:
raise
finally
:
# Clean up after the test
config
.
unittests
.
rseed
=
original_seed
if
test_in_class
and
hasattr
(
class_instance
,
"tearDown"
):
class_instance
.
tearDown
()
# Update the current_seed
if
current_seed
not
in
[
None
,
"random"
]:
current_seed
=
str
(
int
(
current_seed
)
+
1
)
return
attempt_multiple_times
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论