Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
c7ba692d
提交
c7ba692d
authored
3月 10, 2017
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make tests more robust to random samples
上级
99e32f41
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
63 行增加
和
38 行删除
+63
-38
test_basic.py
theano/tensor/tests/test_basic.py
+63
-38
没有找到文件。
theano/tensor/tests/test_basic.py
浏览文件 @
c7ba692d
...
@@ -73,8 +73,34 @@ if config.mode == "FAST_COMPILE":
...
@@ -73,8 +73,34 @@ if config.mode == "FAST_COMPILE":
else
:
else
:
mode_opt
=
get_default_mode
()
mode_opt
=
get_default_mode
()
### seed random number generator so that unittests are deterministic ###
# Use a seeded random number generator so that unittests are deterministic
utt
.
seed_rng
()
utt
.
seed_rng
()
test_rng
=
numpy
.
random
.
RandomState
(
seed
=
utt
.
fetch_seed
())
# A helpful class to check random values close to the boundaries
# when designing new tests
class
MockRandomState
:
def
__init__
(
self
,
val
):
self
.
val
=
val
def
rand
(
self
,
*
shape
):
return
numpy
.
zeros
(
shape
,
dtype
=
'float64'
)
+
self
.
val
def
randint
(
self
,
minval
,
maxval
=
None
,
size
=
1
):
if
maxval
is
None
:
minval
,
maxval
=
0
,
minval
out
=
numpy
.
zeros
(
size
,
dtype
=
'int64'
)
if
self
.
val
==
0
:
return
out
+
minval
else
:
return
out
+
maxval
-
1
# Examples of use:
# test_rng = MockRandomState(0)
# test_rng = MockRandomState(0.99999982)
# test_rng = MockRandomState(1)
if
PY3
:
if
PY3
:
def
L
(
i
):
def
L
(
i
):
...
@@ -83,6 +109,7 @@ else:
...
@@ -83,6 +109,7 @@ else:
def
L
(
i
):
def
L
(
i
):
return
long
(
i
)
return
long
(
i
)
def
inplace_func
(
inputs
,
outputs
,
mode
=
None
,
allow_input_downcast
=
False
,
def
inplace_func
(
inputs
,
outputs
,
mode
=
None
,
allow_input_downcast
=
False
,
on_unused_input
=
'raise'
,
name
=
None
):
on_unused_input
=
'raise'
,
name
=
None
):
if
mode
is
None
:
if
mode
is
None
:
...
@@ -546,14 +573,14 @@ def makeTester(name, op, expected, checks=None, good=None, bad_build=None,
...
@@ -546,14 +573,14 @@ def makeTester(name, op, expected, checks=None, good=None, bad_build=None,
def
rand
(
*
shape
):
def
rand
(
*
shape
):
r
=
numpy
.
random
.
rand
(
*
shape
)
*
2
-
1
r
=
test_rng
.
rand
(
*
shape
)
*
2
-
1
return
numpy
.
asarray
(
r
,
dtype
=
config
.
floatX
)
return
numpy
.
asarray
(
r
,
dtype
=
config
.
floatX
)
def
rand_nonzero
(
shape
,
eps
=
3e-4
):
def
rand_nonzero
(
shape
,
eps
=
3e-4
):
"""Like rand, but the absolute value has to be at least eps"""
"""Like rand, but the absolute value has to be at least eps"""
# covers [0, 1)
# covers [0, 1)
r
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
config
.
floatX
)
r
=
numpy
.
asarray
(
test_rng
.
rand
(
*
shape
),
dtype
=
config
.
floatX
)
# covers [0, (1 - eps) / 2) U [(1 + eps) / 2, 1)
# covers [0, (1 - eps) / 2) U [(1 + eps) / 2, 1)
r
=
r
*
(
1
-
eps
)
+
eps
*
(
r
>=
0.5
)
r
=
r
*
(
1
-
eps
)
+
eps
*
(
r
>=
0.5
)
# covers [-1, -eps) U [eps, 1)
# covers [-1, -eps) U [eps, 1)
...
@@ -562,17 +589,20 @@ def rand_nonzero(shape, eps=3e-4):
...
@@ -562,17 +589,20 @@ def rand_nonzero(shape, eps=3e-4):
def
randint
(
*
shape
):
def
randint
(
*
shape
):
return
numpy
.
random
.
randint
(
-
5
,
6
,
shape
)
return
test_rng
.
randint
(
-
5
,
6
,
shape
)
def
randuint32
(
*
shape
):
def
randuint32
(
*
shape
):
return
numpy
.
array
(
numpy
.
random
.
randint
(
5
,
size
=
shape
),
dtype
=
numpy
.
uint32
)
return
numpy
.
array
(
test_rng
.
randint
(
5
,
size
=
shape
),
dtype
=
numpy
.
uint32
)
def
randuint16
(
*
shape
):
def
randuint16
(
*
shape
):
return
numpy
.
array
(
numpy
.
random
.
randint
(
5
,
size
=
shape
),
dtype
=
numpy
.
uint16
)
return
numpy
.
array
(
test_rng
.
randint
(
5
,
size
=
shape
),
dtype
=
numpy
.
uint16
)
# XXX: this so-called complex random array as all-zero imaginary parts
# XXX: this so-called complex random array as all-zero imaginary parts
def
randcomplex
(
*
shape
):
def
randcomplex
(
*
shape
):
r
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
config
.
floatX
)
r
=
numpy
.
asarray
(
test_rng
.
rand
(
*
shape
),
dtype
=
config
.
floatX
)
return
numpy
.
complex128
(
2
*
r
-
1
)
return
numpy
.
complex128
(
2
*
r
-
1
)
...
@@ -581,21 +611,21 @@ def randcomplex_nonzero(shape, eps=1e-4):
...
@@ -581,21 +611,21 @@ def randcomplex_nonzero(shape, eps=1e-4):
def
randint_nonzero
(
*
shape
):
def
randint_nonzero
(
*
shape
):
r
=
numpy
.
random
.
randint
(
-
5
,
5
,
shape
)
r
=
test_rng
.
randint
(
-
5
,
5
,
shape
)
return
r
+
(
r
==
0
)
*
5
return
r
+
(
r
==
0
)
*
5
def
rand_ranged
(
min
,
max
,
shape
):
def
rand_ranged
(
min
,
max
,
shape
):
return
numpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
)
*
(
max
-
min
)
+
min
,
return
numpy
.
asarray
(
test_rng
.
rand
(
*
shape
)
*
(
max
-
min
)
+
min
,
dtype
=
config
.
floatX
)
dtype
=
config
.
floatX
)
def
randint_ranged
(
min
,
max
,
shape
):
def
randint_ranged
(
min
,
max
,
shape
):
return
numpy
.
random
.
randint
(
min
,
max
+
1
,
shape
)
return
test_rng
.
randint
(
min
,
max
+
1
,
shape
)
def
randc128_ranged
(
min
,
max
,
shape
):
def
randc128_ranged
(
min
,
max
,
shape
):
return
numpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
)
*
(
max
-
min
)
+
min
,
return
numpy
.
asarray
(
test_rng
.
rand
(
*
shape
)
*
(
max
-
min
)
+
min
,
dtype
=
'complex128'
)
dtype
=
'complex128'
)
...
@@ -609,6 +639,8 @@ def rand_of_dtype(shape, dtype):
...
@@ -609,6 +639,8 @@ def rand_of_dtype(shape, dtype):
else
:
else
:
raise
TypeError
()
raise
TypeError
()
# Used to exclude random numbers too close to certain values
_eps
=
1e-2
def
makeBroadcastTester
(
op
,
expected
,
checks
=
None
,
name
=
None
,
**
kwargs
):
def
makeBroadcastTester
(
op
,
expected
,
checks
=
None
,
name
=
None
,
**
kwargs
):
if
checks
is
None
:
if
checks
is
None
:
...
@@ -1144,6 +1176,11 @@ _grad_broadcast_unary_normal = dict(
...
@@ -1144,6 +1176,11 @@ _grad_broadcast_unary_normal = dict(
# empty = [numpy.asarray([])] # XXX: should this be included?
# empty = [numpy.asarray([])] # XXX: should this be included?
)
)
# Avoid epsilon around integer values
_grad_broadcast_unary_normal_noint
=
dict
(
normal
=
[(
rand_ranged
(
_eps
,
1
-
_eps
,
(
2
,
3
))
+
randint
(
2
,
3
))
.
astype
(
floatX
)])
_grad_broadcast_unary_normal_small_neg_range
=
dict
(
_grad_broadcast_unary_normal_small_neg_range
=
dict
(
normal
=
[
numpy
.
asarray
(
rand_ranged
(
-
2
,
5
,
(
2
,
3
)),
dtype
=
floatX
)],
normal
=
[
numpy
.
asarray
(
rand_ranged
(
-
2
,
5
,
(
2
,
3
)),
dtype
=
floatX
)],
corner_case
=
[
corner_case_grad
])
corner_case
=
[
corner_case_grad
])
...
@@ -1153,12 +1190,12 @@ _grad_broadcast_unary_normal_no_complex_no_corner_case = copymod(
...
@@ -1153,12 +1190,12 @@ _grad_broadcast_unary_normal_no_complex_no_corner_case = copymod(
without
=
[
'corner_case'
])
without
=
[
'corner_case'
])
_grad_broadcast_unary_abs1_no_complex
=
dict
(
_grad_broadcast_unary_abs1_no_complex
=
dict
(
normal
=
[
numpy
.
asarray
(
rand_ranged
(
-
1
,
1
,
(
2
,
3
)),
dtype
=
floatX
)],
normal
=
[
numpy
.
asarray
(
rand_ranged
(
-
1
+
_eps
,
1
-
_eps
,
(
2
,
3
)),
dtype
=
floatX
)],
)
)
_grad_broadcast_unary_0_2_no_complex
=
dict
(
_grad_broadcast_unary_0_2_no_complex
=
dict
(
# Don't go too close to 2 for tests in float32
# Don't go too close to
0 or
2 for tests in float32
normal
=
[
numpy
.
asarray
(
rand_ranged
(
0
,
1.9
,
(
2
,
3
)),
dtype
=
floatX
)],
normal
=
[
numpy
.
asarray
(
rand_ranged
(
_eps
,
1
-
_eps
,
(
2
,
3
)),
dtype
=
floatX
)],
)
)
# inplace ops when the input is integer and the output is float*
# inplace ops when the input is integer and the output is float*
...
@@ -1222,9 +1259,7 @@ IntDivInplaceTester = makeBroadcastTester(
...
@@ -1222,9 +1259,7 @@ IntDivInplaceTester = makeBroadcastTester(
CeilTester
=
makeBroadcastTester
(
op
=
tensor
.
ceil
,
CeilTester
=
makeBroadcastTester
(
op
=
tensor
.
ceil
,
expected
=
upcast_float16_ufunc
(
numpy
.
ceil
),
expected
=
upcast_float16_ufunc
(
numpy
.
ceil
),
good
=
_good_broadcast_unary_normal_no_complex
,
good
=
_good_broadcast_unary_normal_no_complex
,
grad
=
copymod
(
_grad_broadcast_unary_normal
,
grad
=
copymod
(
_grad_broadcast_unary_normal_noint
,
without
=
[
'corner_case'
],
# corner_case includes ints where ceil is not differentiable
extra
=
[
numpy
.
asarray
([
-
2.5
,
-
1.5
,
-
1.51
,
0.49
,
.
98
,
1.02
],
extra
=
[
numpy
.
asarray
([
-
2.5
,
-
1.5
,
-
1.51
,
0.49
,
.
98
,
1.02
],
dtype
=
floatX
)]))
dtype
=
floatX
)]))
...
@@ -1233,9 +1268,7 @@ CeilInplaceTester = makeBroadcastTester(op=inplace.ceil_inplace,
...
@@ -1233,9 +1268,7 @@ CeilInplaceTester = makeBroadcastTester(op=inplace.ceil_inplace,
good
=
_good_broadcast_unary_normal_no_complex
,
good
=
_good_broadcast_unary_normal_no_complex
,
# corner cases includes a lot of integers: points where Ceil is not
# corner cases includes a lot of integers: points where Ceil is not
# continuous (not differentiable)
# continuous (not differentiable)
grad
=
copymod
(
_grad_broadcast_unary_normal
,
grad
=
copymod
(
_grad_broadcast_unary_normal_noint
,
without
=
[
'corner_case'
],
# corner_case includes ints where ceil is not differentiable
extra
=
[
numpy
.
asarray
([
-
2.5
,
-
1.5
,
-
1.51
,
0.49
,
.
98
,
1.02
],
extra
=
[
numpy
.
asarray
([
-
2.5
,
-
1.5
,
-
1.51
,
0.49
,
.
98
,
1.02
],
dtype
=
floatX
)]),
dtype
=
floatX
)]),
inplace
=
True
)
inplace
=
True
)
...
@@ -1243,16 +1276,12 @@ CeilInplaceTester = makeBroadcastTester(op=inplace.ceil_inplace,
...
@@ -1243,16 +1276,12 @@ CeilInplaceTester = makeBroadcastTester(op=inplace.ceil_inplace,
FloorTester
=
makeBroadcastTester
(
op
=
tensor
.
floor
,
FloorTester
=
makeBroadcastTester
(
op
=
tensor
.
floor
,
expected
=
upcast_float16_ufunc
(
numpy
.
floor
),
expected
=
upcast_float16_ufunc
(
numpy
.
floor
),
good
=
_good_broadcast_unary_normal_no_complex
,
good
=
_good_broadcast_unary_normal_no_complex
,
# XXX: why does grad of floor not give huge values at
grad
=
_grad_broadcast_unary_normal_noint
)
# the integer points in the 'corner_case' in
# _grad_broadcast_unary_normal? It seems this test should fail,
# yet it does not...
grad
=
_grad_broadcast_unary_normal
)
FloorInplaceTester
=
makeBroadcastTester
(
op
=
inplace
.
floor_inplace
,
FloorInplaceTester
=
makeBroadcastTester
(
op
=
inplace
.
floor_inplace
,
expected
=
upcast_float16_ufunc
(
numpy
.
floor
),
expected
=
upcast_float16_ufunc
(
numpy
.
floor
),
good
=
_good_broadcast_unary_normal_no_complex
,
good
=
_good_broadcast_unary_normal_no_complex
,
grad
=
_grad_broadcast_unary_normal
,
grad
=
_grad_broadcast_unary_normal
_noint
,
inplace
=
True
)
inplace
=
True
)
TruncInplaceTester
=
makeBroadcastTester
(
TruncInplaceTester
=
makeBroadcastTester
(
...
@@ -1362,7 +1391,7 @@ _good_broadcast_unary_positive_float = copymod(
...
@@ -1362,7 +1391,7 @@ _good_broadcast_unary_positive_float = copymod(
_good_broadcast_unary_positive
,
_good_broadcast_unary_positive
,
without
=
[
'integers'
,
'uint8'
])
without
=
[
'integers'
,
'uint8'
])
_grad_broadcast_unary_positive
=
dict
(
normal
=
(
rand_ranged
(
0.001
,
5
,
(
2
,
3
)),),)
_grad_broadcast_unary_positive
=
dict
(
normal
=
(
rand_ranged
(
_eps
,
5
,
(
2
,
3
)),),)
LogTester
=
makeBroadcastTester
(
op
=
tensor
.
log
,
LogTester
=
makeBroadcastTester
(
op
=
tensor
.
log
,
expected
=
upcast_float16_ufunc
(
numpy
.
log
),
expected
=
upcast_float16_ufunc
(
numpy
.
log
),
...
@@ -1629,7 +1658,7 @@ _good_broadcast_unary_arccosh = dict(
...
@@ -1629,7 +1658,7 @@ _good_broadcast_unary_arccosh = dict(
uint8
=
[
numpy
.
arange
(
1
,
256
,
dtype
=
'uint8'
)],
uint8
=
[
numpy
.
arange
(
1
,
256
,
dtype
=
'uint8'
)],
complex
=
(
randc128_ranged
(
1
,
1000
,
(
2
,
3
)),),
complex
=
(
randc128_ranged
(
1
,
1000
,
(
2
,
3
)),),
empty
=
(
numpy
.
asarray
([],
dtype
=
config
.
floatX
),),)
empty
=
(
numpy
.
asarray
([],
dtype
=
config
.
floatX
),),)
_grad_broadcast_unary_arccosh
=
dict
(
normal
=
(
rand_ranged
(
1
,
1000
,
(
2
,
3
)),),)
_grad_broadcast_unary_arccosh
=
dict
(
normal
=
(
rand_ranged
(
1
+
_eps
,
1000
,
(
2
,
3
)),),)
ArccoshTester
=
makeBroadcastTester
(
ArccoshTester
=
makeBroadcastTester
(
op
=
tensor
.
arccosh
,
op
=
tensor
.
arccosh
,
...
@@ -1681,7 +1710,6 @@ TanhInplaceTester = makeBroadcastTester(
...
@@ -1681,7 +1710,6 @@ TanhInplaceTester = makeBroadcastTester(
grad
=
_grad_broadcast_unary_normal
,
grad
=
_grad_broadcast_unary_normal
,
inplace
=
True
)
inplace
=
True
)
_eps
=
1e-2
_good_broadcast_unary_arctanh
=
dict
(
_good_broadcast_unary_arctanh
=
dict
(
normal
=
(
rand_ranged
(
-
1
+
_eps
,
1
-
_eps
,
(
2
,
3
)),),
normal
=
(
rand_ranged
(
-
1
+
_eps
,
1
-
_eps
,
(
2
,
3
)),),
integers
=
(
randint_ranged
(
-
1
+
_eps
,
1
-
_eps
,
(
2
,
3
)),),
integers
=
(
randint_ranged
(
-
1
+
_eps
,
1
-
_eps
,
(
2
,
3
)),),
...
@@ -4768,9 +4796,6 @@ class T_mean(unittest.TestCase):
...
@@ -4768,9 +4796,6 @@ class T_mean(unittest.TestCase):
class
test_matinv
(
unittest
.
TestCase
):
class
test_matinv
(
unittest
.
TestCase
):
def
setUp
(
self
):
utt
.
seed_rng
()
def
mat_reciprocal
(
self
,
dim
):
def
mat_reciprocal
(
self
,
dim
):
# symbolic program
# symbolic program
# broadcastable=[False,False] means that the shape of matrix is two dimensional,
# broadcastable=[False,False] means that the shape of matrix is two dimensional,
...
@@ -4778,7 +4803,7 @@ class test_matinv(unittest.TestCase):
...
@@ -4778,7 +4803,7 @@ class test_matinv(unittest.TestCase):
# Note that TensorType's constructor does not actually allocate any memory.
# Note that TensorType's constructor does not actually allocate any memory.
# TODO: Make TensorType syntax more explicit, and maybe give shape or number of dimensions.
# TODO: Make TensorType syntax more explicit, and maybe give shape or number of dimensions.
utt
.
seed_rng
(
)
rng
=
numpy
.
random
.
RandomState
(
seed
=
utt
.
fetch_seed
()
)
a
,
b
=
matrices
(
'ab'
)
a
,
b
=
matrices
(
'ab'
)
ab
=
a
*
b
ab
=
a
*
b
...
@@ -4795,8 +4820,8 @@ class test_matinv(unittest.TestCase):
...
@@ -4795,8 +4820,8 @@ class test_matinv(unittest.TestCase):
fn
=
inplace_func
([
a
,
b
],
[
ssdiff
,
g_b
])
fn
=
inplace_func
([
a
,
b
],
[
ssdiff
,
g_b
])
# use the function
# use the function
x
=
rand
(
dim
,
dim
)
+
0.1
# Initialized s.t. x is not too tiny
x
=
r
ng
.
r
and
(
dim
,
dim
)
+
0.1
# Initialized s.t. x is not too tiny
w
=
rand
(
dim
,
dim
)
w
=
r
ng
.
r
and
(
dim
,
dim
)
x
=
numpy
.
asarray
(
x
,
dtype
=
config
.
floatX
)
x
=
numpy
.
asarray
(
x
,
dtype
=
config
.
floatX
)
w
=
numpy
.
asarray
(
w
,
dtype
=
config
.
floatX
)
w
=
numpy
.
asarray
(
w
,
dtype
=
config
.
floatX
)
...
@@ -4813,10 +4838,10 @@ class test_matinv(unittest.TestCase):
...
@@ -4813,10 +4838,10 @@ class test_matinv(unittest.TestCase):
"""Matrix reciprocal by gradient descent"""
"""Matrix reciprocal by gradient descent"""
ssd0
,
ssd
=
self
.
mat_reciprocal
(
3
)
ssd0
,
ssd
=
self
.
mat_reciprocal
(
3
)
utt
.
seed_rng
(
)
rng
=
numpy
.
random
.
RandomState
(
seed
=
utt
.
fetch_seed
()
)
# hand-coded numpy implementation for verification
# hand-coded numpy implementation for verification
x
=
rand
(
3
,
3
)
+
0.1
x
=
r
ng
.
r
and
(
3
,
3
)
+
0.1
w
=
rand
(
3
,
3
)
w
=
r
ng
.
r
and
(
3
,
3
)
x
=
numpy
.
asarray
(
x
,
dtype
=
config
.
floatX
)
x
=
numpy
.
asarray
(
x
,
dtype
=
config
.
floatX
)
w
=
numpy
.
asarray
(
w
,
dtype
=
config
.
floatX
)
w
=
numpy
.
asarray
(
w
,
dtype
=
config
.
floatX
)
ones
=
numpy
.
ones
((
3
,
3
),
dtype
=
config
.
floatX
)
ones
=
numpy
.
ones
((
3
,
3
),
dtype
=
config
.
floatX
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论