Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
429a8fae
提交
429a8fae
authored
4月 12, 2017
作者:
Frédéric Bastien
提交者:
GitHub
4月 12, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5843 from Amrithasuresh/master
Updated numpy as np #4218
上级
76cfad25
7a67459b
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
10 个修改的文件
包含
205 行增加
和
206 行删除
+205
-206
test_abstract_conv.py
theano/tensor/nnet/tests/test_abstract_conv.py
+34
-35
test_blocksparse.py
theano/tensor/nnet/tests/test_blocksparse.py
+19
-19
test_bn.py
theano/tensor/nnet/tests/test_bn.py
+44
-44
test_conv.py
theano/tensor/nnet/tests/test_conv.py
+19
-19
test_conv3d2d.py
theano/tensor/nnet/tests/test_conv3d2d.py
+18
-18
test_corr.py
theano/tensor/nnet/tests/test_corr.py
+27
-27
test_corr3d.py
theano/tensor/nnet/tests/test_corr3d.py
+28
-28
test_neighbours.py
theano/tensor/nnet/tests/test_neighbours.py
+0
-0
test_nnet.py
theano/tensor/nnet/tests/test_nnet.py
+0
-0
test_sigm.py
theano/tensor/nnet/tests/test_sigm.py
+16
-16
没有找到文件。
theano/tensor/nnet/tests/test_abstract_conv.py
浏览文件 @
429a8fae
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
unittest
import
unittest
import
numpy
import
numpy
as
np
import
numpy
as
np
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
from
nose.tools
import
assert_raises
,
assert_true
from
nose.tools
import
assert_raises
,
assert_true
...
@@ -238,8 +237,8 @@ class TestAssertShape(unittest.TestCase):
...
@@ -238,8 +237,8 @@ class TestAssertShape(unittest.TestCase):
expected_shape
=
[
None
,
s1
,
s2
,
None
]
expected_shape
=
[
None
,
s1
,
s2
,
None
]
f
=
theano
.
function
([
x
,
s1
,
s2
],
assert_shape
(
x
,
expected_shape
))
f
=
theano
.
function
([
x
,
s1
,
s2
],
assert_shape
(
x
,
expected_shape
))
v
=
n
umpy
.
zeros
((
3
,
5
,
7
,
11
),
dtype
=
'float32'
)
v
=
n
p
.
zeros
((
3
,
5
,
7
,
11
),
dtype
=
'float32'
)
self
.
assertEqual
(
0
,
n
umpy
.
sum
(
f
(
v
,
5
,
7
)))
self
.
assertEqual
(
0
,
n
p
.
sum
(
f
(
v
,
5
,
7
)))
assert_raises
(
AssertionError
,
f
,
v
,
5
,
0
)
assert_raises
(
AssertionError
,
f
,
v
,
5
,
0
)
assert_raises
(
AssertionError
,
f
,
v
,
5
,
9
)
assert_raises
(
AssertionError
,
f
,
v
,
5
,
9
)
...
@@ -257,12 +256,12 @@ class TestAssertShape(unittest.TestCase):
...
@@ -257,12 +256,12 @@ class TestAssertShape(unittest.TestCase):
f
=
theano
.
function
([
input
,
filters
],
out
)
f
=
theano
.
function
([
input
,
filters
],
out
)
# mismatched input_shape
# mismatched input_shape
assert_raises
(
AssertionError
,
f
,
assert_raises
(
AssertionError
,
f
,
n
umpy
.
zeros
((
3
,
5
,
9
,
11
),
dtype
=
'float32'
),
n
p
.
zeros
((
3
,
5
,
9
,
11
),
dtype
=
'float32'
),
n
umpy
.
zeros
((
7
,
5
,
3
,
3
),
dtype
=
'float32'
))
n
p
.
zeros
((
7
,
5
,
3
,
3
),
dtype
=
'float32'
))
# mismatched filter_shape
# mismatched filter_shape
assert_raises
(
AssertionError
,
f
,
assert_raises
(
AssertionError
,
f
,
n
umpy
.
zeros
((
3
,
5
,
7
,
11
),
dtype
=
'float32'
),
n
p
.
zeros
((
3
,
5
,
7
,
11
),
dtype
=
'float32'
),
n
umpy
.
zeros
((
7
,
5
,
2
,
2
),
dtype
=
'float32'
))
n
p
.
zeros
((
7
,
5
,
2
,
2
),
dtype
=
'float32'
))
@change_flags
([(
"conv.assert_shape"
,
True
)])
@change_flags
([(
"conv.assert_shape"
,
True
)])
def
test_shape_check_conv3d
(
self
):
def
test_shape_check_conv3d
(
self
):
...
@@ -275,12 +274,12 @@ class TestAssertShape(unittest.TestCase):
...
@@ -275,12 +274,12 @@ class TestAssertShape(unittest.TestCase):
f
=
theano
.
function
([
input
,
filters
],
out
)
f
=
theano
.
function
([
input
,
filters
],
out
)
# mismatched input_shape
# mismatched input_shape
assert_raises
(
AssertionError
,
f
,
assert_raises
(
AssertionError
,
f
,
n
umpy
.
zeros
((
3
,
5
,
9
,
11
,
13
),
dtype
=
'float32'
),
n
p
.
zeros
((
3
,
5
,
9
,
11
,
13
),
dtype
=
'float32'
),
n
umpy
.
zeros
((
7
,
5
,
3
,
3
,
3
),
dtype
=
'float32'
))
n
p
.
zeros
((
7
,
5
,
3
,
3
,
3
),
dtype
=
'float32'
))
# mismatched filter_shape
# mismatched filter_shape
assert_raises
(
AssertionError
,
f
,
assert_raises
(
AssertionError
,
f
,
n
umpy
.
zeros
((
3
,
5
,
7
,
11
,
13
),
dtype
=
'float32'
),
n
p
.
zeros
((
3
,
5
,
7
,
11
,
13
),
dtype
=
'float32'
),
n
umpy
.
zeros
((
7
,
5
,
2
,
2
,
2
),
dtype
=
'float32'
))
n
p
.
zeros
((
7
,
5
,
2
,
2
,
2
),
dtype
=
'float32'
))
@change_flags
([(
"conv.assert_shape"
,
True
)])
@change_flags
([(
"conv.assert_shape"
,
True
)])
def
test_shape_check_conv2d_grad_wrt_inputs
(
self
):
def
test_shape_check_conv2d_grad_wrt_inputs
(
self
):
...
@@ -293,8 +292,8 @@ class TestAssertShape(unittest.TestCase):
...
@@ -293,8 +292,8 @@ class TestAssertShape(unittest.TestCase):
f
=
theano
.
function
([
output_grad
,
filters
],
out
)
f
=
theano
.
function
([
output_grad
,
filters
],
out
)
# mismatched filter_shape
# mismatched filter_shape
assert_raises
(
AssertionError
,
f
,
assert_raises
(
AssertionError
,
f
,
n
umpy
.
zeros
((
3
,
6
,
5
,
9
),
dtype
=
'float32'
),
n
p
.
zeros
((
3
,
6
,
5
,
9
),
dtype
=
'float32'
),
n
umpy
.
zeros
((
7
,
6
,
3
,
3
),
dtype
=
'float32'
))
n
p
.
zeros
((
7
,
6
,
3
,
3
),
dtype
=
'float32'
))
@change_flags
([(
"conv.assert_shape"
,
True
)])
@change_flags
([(
"conv.assert_shape"
,
True
)])
def
test_shape_check_conv3d_grad_wrt_inputs
(
self
):
def
test_shape_check_conv3d_grad_wrt_inputs
(
self
):
...
@@ -307,8 +306,8 @@ class TestAssertShape(unittest.TestCase):
...
@@ -307,8 +306,8 @@ class TestAssertShape(unittest.TestCase):
f
=
theano
.
function
([
output_grad
,
filters
],
out
)
f
=
theano
.
function
([
output_grad
,
filters
],
out
)
# mismatched filter_shape
# mismatched filter_shape
assert_raises
(
AssertionError
,
f
,
assert_raises
(
AssertionError
,
f
,
n
umpy
.
zeros
((
3
,
6
,
5
,
9
,
11
),
dtype
=
'float32'
),
n
p
.
zeros
((
3
,
6
,
5
,
9
,
11
),
dtype
=
'float32'
),
n
umpy
.
zeros
((
7
,
6
,
3
,
3
,
3
),
dtype
=
'float32'
))
n
p
.
zeros
((
7
,
6
,
3
,
3
,
3
),
dtype
=
'float32'
))
@change_flags
([(
"conv.assert_shape"
,
True
)])
@change_flags
([(
"conv.assert_shape"
,
True
)])
def
test_shape_check_conv2d_grad_wrt_weights
(
self
):
def
test_shape_check_conv2d_grad_wrt_weights
(
self
):
...
@@ -321,8 +320,8 @@ class TestAssertShape(unittest.TestCase):
...
@@ -321,8 +320,8 @@ class TestAssertShape(unittest.TestCase):
f
=
theano
.
function
([
input
,
output_grad
],
out
)
f
=
theano
.
function
([
input
,
output_grad
],
out
)
# mismatched filter_shape
# mismatched filter_shape
assert_raises
(
AssertionError
,
f
,
assert_raises
(
AssertionError
,
f
,
n
umpy
.
zeros
((
3
,
6
,
7
,
11
),
dtype
=
'float32'
),
n
p
.
zeros
((
3
,
6
,
7
,
11
),
dtype
=
'float32'
),
n
umpy
.
zeros
((
3
,
7
,
5
,
9
),
dtype
=
'float32'
))
n
p
.
zeros
((
3
,
7
,
5
,
9
),
dtype
=
'float32'
))
@change_flags
([(
"conv.assert_shape"
,
True
)])
@change_flags
([(
"conv.assert_shape"
,
True
)])
def
test_shape_check_conv3d_grad_wrt_weights
(
self
):
def
test_shape_check_conv3d_grad_wrt_weights
(
self
):
...
@@ -335,8 +334,8 @@ class TestAssertShape(unittest.TestCase):
...
@@ -335,8 +334,8 @@ class TestAssertShape(unittest.TestCase):
f
=
theano
.
function
([
input
,
output_grad
],
out
)
f
=
theano
.
function
([
input
,
output_grad
],
out
)
# mismatched filter_shape
# mismatched filter_shape
assert_raises
(
AssertionError
,
f
,
assert_raises
(
AssertionError
,
f
,
n
umpy
.
zeros
((
3
,
6
,
7
,
11
,
13
),
dtype
=
'float32'
),
n
p
.
zeros
((
3
,
6
,
7
,
11
,
13
),
dtype
=
'float32'
),
n
umpy
.
zeros
((
3
,
7
,
5
,
9
,
11
),
dtype
=
'float32'
))
n
p
.
zeros
((
3
,
7
,
5
,
9
,
11
),
dtype
=
'float32'
))
class
BaseTestConv
(
object
):
class
BaseTestConv
(
object
):
...
@@ -371,8 +370,8 @@ class BaseTestConv(object):
...
@@ -371,8 +370,8 @@ class BaseTestConv(object):
if
filter_dilation
is
None
:
if
filter_dilation
is
None
:
filter_dilation
=
(
1
,)
*
(
len
(
inputs_shape
)
-
2
)
filter_dilation
=
(
1
,)
*
(
len
(
inputs_shape
)
-
2
)
inputs_val
=
n
umpy
.
random
.
random
(
inputs_shape
)
.
astype
(
'float32'
)
inputs_val
=
n
p
.
random
.
random
(
inputs_shape
)
.
astype
(
'float32'
)
filters_val
=
n
umpy
.
random
.
random
(
filters_shape
)
.
astype
(
'float32'
)
filters_val
=
n
p
.
random
.
random
(
filters_shape
)
.
astype
(
'float32'
)
# scale down values to prevent rounding errors
# scale down values to prevent rounding errors
inputs_val
/=
10
inputs_val
/=
10
...
@@ -414,8 +413,8 @@ class BaseTestConv(object):
...
@@ -414,8 +413,8 @@ class BaseTestConv(object):
if
check_trace
:
if
check_trace
:
assert_true
(
check_stack_trace
(
f
,
ops_to_check
=
target_op
))
assert_true
(
check_stack_trace
(
f
,
ops_to_check
=
target_op
))
res_ref
=
n
umpy
.
array
(
f_ref
())
res_ref
=
n
p
.
array
(
f_ref
())
res
=
n
umpy
.
array
(
f
())
res
=
n
p
.
array
(
f
())
utt
.
assert_allclose
(
res_ref
,
res
)
utt
.
assert_allclose
(
res_ref
,
res
)
if
verify_grad
and
inputs_val
.
size
>
0
and
filters_val
.
size
>
0
and
res
.
size
>
0
:
if
verify_grad
and
inputs_val
.
size
>
0
and
filters_val
.
size
>
0
and
res
.
size
>
0
:
utt
.
verify_grad
(
conv_op
(
border_mode
=
border_mode
,
utt
.
verify_grad
(
conv_op
(
border_mode
=
border_mode
,
...
@@ -436,8 +435,8 @@ class BaseTestConv(object):
...
@@ -436,8 +435,8 @@ class BaseTestConv(object):
if
filter_dilation
is
None
:
if
filter_dilation
is
None
:
filter_dilation
=
(
1
,)
*
(
len
(
inputs_shape
)
-
2
)
filter_dilation
=
(
1
,)
*
(
len
(
inputs_shape
)
-
2
)
inputs_val
=
n
umpy
.
random
.
random
(
inputs_shape
)
.
astype
(
'float32'
)
inputs_val
=
n
p
.
random
.
random
(
inputs_shape
)
.
astype
(
'float32'
)
output_val
=
n
umpy
.
random
.
random
(
output_shape
)
.
astype
(
'float32'
)
output_val
=
n
p
.
random
.
random
(
output_shape
)
.
astype
(
'float32'
)
inputs
=
self
.
shared
(
inputs_val
)
inputs
=
self
.
shared
(
inputs_val
)
output
=
self
.
shared
(
output_val
)
output
=
self
.
shared
(
output_val
)
...
@@ -473,8 +472,8 @@ class BaseTestConv(object):
...
@@ -473,8 +472,8 @@ class BaseTestConv(object):
if
check_trace
:
if
check_trace
:
assert_true
(
check_stack_trace
(
f
,
ops_to_check
=
target_op
))
assert_true
(
check_stack_trace
(
f
,
ops_to_check
=
target_op
))
res_ref
=
n
umpy
.
array
(
f_ref
())
res_ref
=
n
p
.
array
(
f_ref
())
res
=
n
umpy
.
array
(
f
())
res
=
n
p
.
array
(
f
())
utt
.
assert_allclose
(
res_ref
,
res
)
utt
.
assert_allclose
(
res_ref
,
res
)
def
abstract_conv_gradweight
(
inputs_val
,
output_val
):
def
abstract_conv_gradweight
(
inputs_val
,
output_val
):
...
@@ -499,8 +498,8 @@ class BaseTestConv(object):
...
@@ -499,8 +498,8 @@ class BaseTestConv(object):
if
filter_dilation
is
None
:
if
filter_dilation
is
None
:
filter_dilation
=
(
1
,)
*
(
len
(
inputs_shape
)
-
2
)
filter_dilation
=
(
1
,)
*
(
len
(
inputs_shape
)
-
2
)
output_val
=
n
umpy
.
random
.
random
(
output_shape
)
.
astype
(
'float32'
)
output_val
=
n
p
.
random
.
random
(
output_shape
)
.
astype
(
'float32'
)
filters_val
=
n
umpy
.
random
.
random
(
filters_shape
)
.
astype
(
'float32'
)
filters_val
=
n
p
.
random
.
random
(
filters_shape
)
.
astype
(
'float32'
)
output
=
self
.
shared
(
output_val
)
output
=
self
.
shared
(
output_val
)
filters
=
self
.
shared
(
filters_val
)
filters
=
self
.
shared
(
filters_val
)
...
@@ -537,10 +536,10 @@ class BaseTestConv(object):
...
@@ -537,10 +536,10 @@ class BaseTestConv(object):
if
check_trace
:
if
check_trace
:
assert_true
(
check_stack_trace
(
f
,
ops_to_check
=
target_op
))
assert_true
(
check_stack_trace
(
f
,
ops_to_check
=
target_op
))
res
=
n
umpy
.
array
(
f
())
res
=
n
p
.
array
(
f
())
if
ref
is
not
None
:
if
ref
is
not
None
:
res_ref
=
n
umpy
.
array
(
f_ref
())
res_ref
=
n
p
.
array
(
f_ref
())
utt
.
assert_allclose
(
res_ref
,
res
)
utt
.
assert_allclose
(
res_ref
,
res
)
def
abstract_conv_gradinputs
(
filters_val
,
output_val
):
def
abstract_conv_gradinputs
(
filters_val
,
output_val
):
...
@@ -1272,7 +1271,7 @@ class TestConvTypes(unittest.TestCase):
...
@@ -1272,7 +1271,7 @@ class TestConvTypes(unittest.TestCase):
self
.
filters
=
tensor
.
ftensor4
()
self
.
filters
=
tensor
.
ftensor4
()
self
.
topgrad
=
tensor
.
ftensor4
()
self
.
topgrad
=
tensor
.
ftensor4
()
self
.
constant_tensor
=
n
umpy
.
zeros
((
3
,
5
,
7
,
11
),
dtype
=
'float32'
)
self
.
constant_tensor
=
n
p
.
zeros
((
3
,
5
,
7
,
11
),
dtype
=
'float32'
)
def
test_grad_types
(
self
):
def
test_grad_types
(
self
):
# This function simply tests the behaviour of the AbstractConv
# This function simply tests the behaviour of the AbstractConv
...
@@ -1582,7 +1581,7 @@ class TestConv2dTranspose(unittest.TestCase):
...
@@ -1582,7 +1581,7 @@ class TestConv2dTranspose(unittest.TestCase):
output_shape
=
(
2
,
1
,
10
,
10
),
output_shape
=
(
2
,
1
,
10
,
10
),
input_dilation
=
(
2
,
2
)),
input_dilation
=
(
2
,
2
)),
mode
=
mode
)()
mode
=
mode
)()
expected_output
=
n
umpy
.
array
(
expected_output
=
n
p
.
array
(
[[[[
2
,
2
,
4
,
4
,
4
,
4
,
4
,
4
,
2
,
2
],
[[[[
2
,
2
,
4
,
4
,
4
,
4
,
4
,
4
,
2
,
2
],
[
2
,
2
,
4
,
4
,
4
,
4
,
4
,
4
,
2
,
2
],
[
2
,
2
,
4
,
4
,
4
,
4
,
4
,
4
,
2
,
2
],
[
4
,
4
,
8
,
8
,
8
,
8
,
8
,
8
,
4
,
4
],
[
4
,
4
,
8
,
8
,
8
,
8
,
8
,
8
,
4
,
4
],
...
@@ -1593,7 +1592,7 @@ class TestConv2dTranspose(unittest.TestCase):
...
@@ -1593,7 +1592,7 @@ class TestConv2dTranspose(unittest.TestCase):
[
4
,
4
,
8
,
8
,
8
,
8
,
8
,
8
,
4
,
4
],
[
4
,
4
,
8
,
8
,
8
,
8
,
8
,
8
,
4
,
4
],
[
2
,
2
,
4
,
4
,
4
,
4
,
4
,
4
,
2
,
2
],
[
2
,
2
,
4
,
4
,
4
,
4
,
4
,
4
,
2
,
2
],
[
2
,
2
,
4
,
4
,
4
,
4
,
4
,
4
,
2
,
2
]]]]
*
2
)
[
2
,
2
,
4
,
4
,
4
,
4
,
4
,
4
,
2
,
2
]]]]
*
2
)
n
umpy
.
testing
.
assert_equal
(
output
,
expected_output
)
n
p
.
testing
.
assert_equal
(
output
,
expected_output
)
class
TestConv2dGrads
(
unittest
.
TestCase
):
class
TestConv2dGrads
(
unittest
.
TestCase
):
...
@@ -1604,7 +1603,7 @@ class TestConv2dGrads(unittest.TestCase):
...
@@ -1604,7 +1603,7 @@ class TestConv2dGrads(unittest.TestCase):
theano
.
config
.
mode
==
"FAST_COMPILE"
):
theano
.
config
.
mode
==
"FAST_COMPILE"
):
raise
SkipTest
(
"Need blas to test conv2d"
)
raise
SkipTest
(
"Need blas to test conv2d"
)
self
.
random_stream
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
self
.
random_stream
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
self
.
inputs_shapes
=
[(
8
,
1
,
12
,
12
),
(
1
,
1
,
5
,
5
),
(
1
,
1
,
5
,
6
),
(
1
,
1
,
6
,
6
)]
self
.
inputs_shapes
=
[(
8
,
1
,
12
,
12
),
(
1
,
1
,
5
,
5
),
(
1
,
1
,
5
,
6
),
(
1
,
1
,
6
,
6
)]
self
.
filters_shapes
=
[(
5
,
1
,
2
,
2
),
(
1
,
1
,
3
,
3
)]
self
.
filters_shapes
=
[(
5
,
1
,
2
,
2
),
(
1
,
1
,
3
,
3
)]
...
...
theano/tensor/nnet/tests/test_blocksparse.py
浏览文件 @
429a8fae
...
@@ -3,7 +3,7 @@
...
@@ -3,7 +3,7 @@
"""
"""
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
from
numpy.random
import
randn
from
numpy.random
import
randn
import
theano
import
theano
...
@@ -41,10 +41,10 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
...
@@ -41,10 +41,10 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
batchSize
=
2
batchSize
=
2
input
=
randn
(
batchSize
,
inputWindowSize
,
inputSize
)
.
astype
(
'float32'
)
input
=
randn
(
batchSize
,
inputWindowSize
,
inputSize
)
.
astype
(
'float32'
)
permutation
=
n
umpy
.
random
.
permutation
permutation
=
n
p
.
random
.
permutation
inputIndice
=
n
umpy
.
vstack
(
permutation
(
nInputBlock
)[:
inputWindowSize
]
inputIndice
=
n
p
.
vstack
(
permutation
(
nInputBlock
)[:
inputWindowSize
]
for
_
in
range
(
batchSize
))
.
astype
(
'int32'
)
for
_
in
range
(
batchSize
))
.
astype
(
'int32'
)
outputIndice
=
n
umpy
.
vstack
(
outputIndice
=
n
p
.
vstack
(
permutation
(
nOutputBlock
)[:
outputWindowSize
]
permutation
(
nOutputBlock
)[:
outputWindowSize
]
for
_
in
range
(
batchSize
))
.
astype
(
'int32'
)
for
_
in
range
(
batchSize
))
.
astype
(
'int32'
)
weight
=
randn
(
nInputBlock
,
nOutputBlock
,
weight
=
randn
(
nInputBlock
,
nOutputBlock
,
...
@@ -66,11 +66,11 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
...
@@ -66,11 +66,11 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
o
=
randn
(
nInputBlock
,
nOutputBlock
,
xSize
,
ySize
)
.
astype
(
'float32'
)
o
=
randn
(
nInputBlock
,
nOutputBlock
,
xSize
,
ySize
)
.
astype
(
'float32'
)
x
=
randn
(
batchSize
,
xWindowSize
,
xSize
)
.
astype
(
'float32'
)
x
=
randn
(
batchSize
,
xWindowSize
,
xSize
)
.
astype
(
'float32'
)
y
=
randn
(
batchSize
,
yWindowSize
,
ySize
)
.
astype
(
'float32'
)
y
=
randn
(
batchSize
,
yWindowSize
,
ySize
)
.
astype
(
'float32'
)
randint
=
n
umpy
.
random
.
randint
randint
=
n
p
.
random
.
randint
xIdx
=
n
umpy
.
vstack
(
randint
(
0
,
nInputBlock
,
size
=
xWindowSize
)
xIdx
=
n
p
.
vstack
(
randint
(
0
,
nInputBlock
,
size
=
xWindowSize
)
for
_
in
range
(
batchSize
))
.
astype
(
'int32'
)
for
_
in
range
(
batchSize
))
.
astype
(
'int32'
)
yIdx
=
n
umpy
.
vstack
(
randint
(
0
,
nOutputBlock
,
size
=
yWindowSize
)
yIdx
=
n
p
.
vstack
(
randint
(
0
,
nOutputBlock
,
size
=
yWindowSize
)
for
_
in
range
(
batchSize
))
.
astype
(
'int32'
)
for
_
in
range
(
batchSize
))
.
astype
(
'int32'
)
return
o
,
x
,
y
,
xIdx
,
yIdx
return
o
,
x
,
y
,
xIdx
,
yIdx
...
@@ -82,7 +82,7 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
...
@@ -82,7 +82,7 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
for
i
in
range
(
h
.
shape
[
1
]):
for
i
in
range
(
h
.
shape
[
1
]):
inputIdx
=
iIdx
[
b
,
i
]
inputIdx
=
iIdx
[
b
,
i
]
w
=
W
[
inputIdx
,
outputIdx
]
w
=
W
[
inputIdx
,
outputIdx
]
o
[
b
,
j
,
:]
+=
n
umpy
.
dot
(
h
[
b
,
i
],
w
)
o
[
b
,
j
,
:]
+=
n
p
.
dot
(
h
[
b
,
i
],
w
)
return
o
return
o
@staticmethod
@staticmethod
...
@@ -94,7 +94,7 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
...
@@ -94,7 +94,7 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
for
b
in
range
(
o
.
shape
[
0
]):
for
b
in
range
(
o
.
shape
[
0
]):
w
=
W
[
ix_
(
iIdx
[
b
],
oIdx
[
b
])]
.
swapaxes
(
1
,
2
)
w
=
W
[
ix_
(
iIdx
[
b
],
oIdx
[
b
])]
.
swapaxes
(
1
,
2
)
w
=
w
.
reshape
((
w
.
shape
[
0
]
*
w
.
shape
[
1
],
w
.
shape
[
2
]
*
w
.
shape
[
3
]))
w
=
w
.
reshape
((
w
.
shape
[
0
]
*
w
.
shape
[
1
],
w
.
shape
[
2
]
*
w
.
shape
[
3
]))
o
[
b
]
+=
n
umpy
.
dot
(
h
[
b
]
.
ravel
(),
w
)
.
reshape
(
o
.
shape
[
1
:])
o
[
b
]
+=
n
p
.
dot
(
h
[
b
]
.
ravel
(),
w
)
.
reshape
(
o
.
shape
[
1
:])
return
o
return
o
@staticmethod
@staticmethod
...
@@ -108,8 +108,8 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
...
@@ -108,8 +108,8 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
# The next three lines do the same operation. The last one is the
# The next three lines do the same operation. The last one is the
# fastest
# fastest
# o[b] += (h[b][:, None, :, None] * w).sum(axis=(0, 2))
# o[b] += (h[b][:, None, :, None] * w).sum(axis=(0, 2))
# o[b] += n
umpy
.tensordot(h[b], w, [(0,1),(0,2)])
# o[b] += n
p
.tensordot(h[b], w, [(0,1),(0,2)])
o
[
b
]
+=
n
umpy
.
einsum
(
'ik,ijkl'
,
h
[
b
],
w
)
o
[
b
]
+=
n
p
.
einsum
(
'ik,ijkl'
,
h
[
b
],
w
)
return
o
return
o
@staticmethod
@staticmethod
...
@@ -117,8 +117,8 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
...
@@ -117,8 +117,8 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
for
b
in
range
(
x
.
shape
[
0
]):
for
b
in
range
(
x
.
shape
[
0
]):
for
i
in
range
(
xIdx
.
shape
[
1
]):
for
i
in
range
(
xIdx
.
shape
[
1
]):
for
j
in
range
(
yIdx
.
shape
[
1
]):
for
j
in
range
(
yIdx
.
shape
[
1
]):
o
[
xIdx
[
b
,
i
],
yIdx
[
b
,
j
]]
+=
n
umpy
.
outer
(
x
[
b
,
i
,
:],
o
[
xIdx
[
b
,
i
],
yIdx
[
b
,
j
]]
+=
n
p
.
outer
(
x
[
b
,
i
,
:],
y
[
b
,
j
,
:])
y
[
b
,
j
,
:])
return
o
return
o
def
test_sparseblockdot
(
self
):
def
test_sparseblockdot
(
self
):
...
@@ -190,7 +190,7 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
...
@@ -190,7 +190,7 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
W_val
,
h_val
,
iIdx_val
,
b_val
,
oIdx_val
=
\
W_val
,
h_val
,
iIdx_val
,
b_val
,
oIdx_val
=
\
BlockSparse_Gemv_and_Outer
.
gemv_data
()
BlockSparse_Gemv_and_Outer
.
gemv_data
()
th_out
=
f
(
n
umpy
.
swapaxes
(
W_val
,
2
,
3
),
h_val
,
iIdx_val
,
b_val
,
th_out
=
f
(
n
p
.
swapaxes
(
W_val
,
2
,
3
),
h_val
,
iIdx_val
,
b_val
,
oIdx_val
)
oIdx_val
)
ref_out
=
BlockSparse_Gemv_and_Outer
.
gemv_numpy
(
ref_out
=
BlockSparse_Gemv_and_Outer
.
gemv_numpy
(
b_val
.
take
(
oIdx_val
,
axis
=
0
),
W_val
,
h_val
,
iIdx_val
,
oIdx_val
)
b_val
.
take
(
oIdx_val
,
axis
=
0
),
W_val
,
h_val
,
iIdx_val
,
oIdx_val
)
...
@@ -218,8 +218,8 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
...
@@ -218,8 +218,8 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
def
test_sparseblockgemv_grad_1
(
self
):
def
test_sparseblockgemv_grad_1
(
self
):
# Test that we correctly handle cases where dimensions are 1.
# Test that we correctly handle cases where dimensions are 1.
h_val
=
randn
(
1
,
1
,
1
)
.
astype
(
'float32'
)
h_val
=
randn
(
1
,
1
,
1
)
.
astype
(
'float32'
)
iIdx_val
=
n
umpy
.
random
.
permutation
(
1
)[:
1
][
None
,
:]
iIdx_val
=
n
p
.
random
.
permutation
(
1
)[:
1
][
None
,
:]
oIdx_val
=
n
umpy
.
random
.
permutation
(
1
)[:
1
][
None
,
:]
oIdx_val
=
n
p
.
random
.
permutation
(
1
)[:
1
][
None
,
:]
W_val
=
randn
(
1
,
1
,
1
,
1
)
.
astype
(
'float32'
)
W_val
=
randn
(
1
,
1
,
1
,
1
)
.
astype
(
'float32'
)
b_val
=
randn
(
1
,
1
)
.
astype
(
'float32'
)
b_val
=
randn
(
1
,
1
)
.
astype
(
'float32'
)
...
...
theano/tensor/nnet/tests/test_bn.py
浏览文件 @
429a8fae
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
import
theano
import
theano
import
theano.tensor
as
T
import
theano.tensor
as
T
from
theano.tests
import
unittest_tools
as
utt
from
theano.tests
import
unittest_tools
as
utt
import
numpy
import
numpy
as
np
from
theano.tensor.nnet
import
bn
from
theano.tensor.nnet
import
bn
...
@@ -17,12 +17,12 @@ def test_BNComposite():
...
@@ -17,12 +17,12 @@ def test_BNComposite():
n
=
(
x
-
M
)
/
V
n
=
(
x
-
M
)
/
V
return
n
*
G
+
B
return
n
*
G
+
B
n
umpy
.
random
.
seed
(
1234
)
n
p
.
random
.
seed
(
1234
)
X
=
1
+
n
umpy
.
random
.
random
([
10
,
20
])
.
astype
(
'float32'
)
X
=
1
+
n
p
.
random
.
random
([
10
,
20
])
.
astype
(
'float32'
)
B
=
1
+
n
umpy
.
random
.
random
([
20
])
.
astype
(
'float32'
)
B
=
1
+
n
p
.
random
.
random
([
20
])
.
astype
(
'float32'
)
G
=
1
+
n
umpy
.
random
.
random
([
20
])
.
astype
(
'float32'
)
G
=
1
+
n
p
.
random
.
random
([
20
])
.
astype
(
'float32'
)
M
=
1
+
n
umpy
.
random
.
random
([
20
])
.
astype
(
'float32'
)
M
=
1
+
n
p
.
random
.
random
([
20
])
.
astype
(
'float32'
)
V
=
1
+
n
umpy
.
random
.
random
([
20
])
.
astype
(
'float32'
)
V
=
1
+
n
p
.
random
.
random
([
20
])
.
astype
(
'float32'
)
x
=
theano
.
tensor
.
matrix
(
'x'
)
x
=
theano
.
tensor
.
matrix
(
'x'
)
b
=
theano
.
tensor
.
vector
(
'b'
)
b
=
theano
.
tensor
.
vector
(
'b'
)
...
@@ -30,11 +30,11 @@ def test_BNComposite():
...
@@ -30,11 +30,11 @@ def test_BNComposite():
m
=
theano
.
tensor
.
vector
(
'm'
)
m
=
theano
.
tensor
.
vector
(
'm'
)
v
=
theano
.
tensor
.
vector
(
'v'
)
v
=
theano
.
tensor
.
vector
(
'v'
)
x
.
tag
.
test_value
=
n
umpy
.
random
.
rand
(
2
,
2
)
.
astype
(
theano
.
config
.
floatX
)
x
.
tag
.
test_value
=
n
p
.
random
.
rand
(
2
,
2
)
.
astype
(
theano
.
config
.
floatX
)
b
.
tag
.
test_value
=
n
umpy
.
random
.
rand
(
2
)
.
astype
(
theano
.
config
.
floatX
)
b
.
tag
.
test_value
=
n
p
.
random
.
rand
(
2
)
.
astype
(
theano
.
config
.
floatX
)
g
.
tag
.
test_value
=
n
umpy
.
random
.
rand
(
2
)
.
astype
(
theano
.
config
.
floatX
)
g
.
tag
.
test_value
=
n
p
.
random
.
rand
(
2
)
.
astype
(
theano
.
config
.
floatX
)
m
.
tag
.
test_value
=
n
umpy
.
random
.
rand
(
2
)
.
astype
(
theano
.
config
.
floatX
)
m
.
tag
.
test_value
=
n
p
.
random
.
rand
(
2
)
.
astype
(
theano
.
config
.
floatX
)
v
.
tag
.
test_value
=
n
umpy
.
random
.
rand
(
2
)
.
astype
(
theano
.
config
.
floatX
)
v
.
tag
.
test_value
=
n
p
.
random
.
rand
(
2
)
.
astype
(
theano
.
config
.
floatX
)
bn_ref_op
=
bn_ref
(
x
,
g
,
b
,
m
,
v
)
bn_ref_op
=
bn_ref
(
x
,
g
,
b
,
m
,
v
)
f_ref
=
theano
.
function
([
x
,
b
,
g
,
m
,
v
],
[
bn_ref_op
])
f_ref
=
theano
.
function
([
x
,
b
,
g
,
m
,
v
],
[
bn_ref_op
])
...
@@ -54,12 +54,12 @@ def test_batch_normalization():
...
@@ -54,12 +54,12 @@ def test_batch_normalization():
n
=
(
x
-
M
)
/
V
n
=
(
x
-
M
)
/
V
return
n
*
G
+
B
return
n
*
G
+
B
n
umpy
.
random
.
seed
(
1234
)
n
p
.
random
.
seed
(
1234
)
X
=
1
+
n
umpy
.
random
.
random
([
10
,
20
])
.
astype
(
'float32'
)
X
=
1
+
n
p
.
random
.
random
([
10
,
20
])
.
astype
(
'float32'
)
B
=
1
+
n
umpy
.
random
.
random
([
20
])
.
astype
(
'float32'
)
B
=
1
+
n
p
.
random
.
random
([
20
])
.
astype
(
'float32'
)
G
=
1
+
n
umpy
.
random
.
random
([
20
])
.
astype
(
'float32'
)
G
=
1
+
n
p
.
random
.
random
([
20
])
.
astype
(
'float32'
)
M
=
1
+
n
umpy
.
random
.
random
([
20
])
.
astype
(
'float32'
)
M
=
1
+
n
p
.
random
.
random
([
20
])
.
astype
(
'float32'
)
V
=
1
+
n
umpy
.
random
.
random
([
20
])
.
astype
(
'float32'
)
V
=
1
+
n
p
.
random
.
random
([
20
])
.
astype
(
'float32'
)
x
=
theano
.
tensor
.
matrix
(
'x'
)
x
=
theano
.
tensor
.
matrix
(
'x'
)
b
=
theano
.
tensor
.
vector
(
'b'
)
b
=
theano
.
tensor
.
vector
(
'b'
)
...
@@ -92,7 +92,7 @@ def test_batch_normalization():
...
@@ -92,7 +92,7 @@ def test_batch_normalization():
def
bn_f
(
inputs
,
gamma
,
beta
,
mean
,
std
):
def
bn_f
(
inputs
,
gamma
,
beta
,
mean
,
std
):
return
bn
.
batch_normalization
(
inputs
,
gamma
,
beta
,
mean
,
std
,
mode
=
mode
)
return
bn
.
batch_normalization
(
inputs
,
gamma
,
beta
,
mean
,
std
,
mode
=
mode
)
utt
.
verify_grad
(
bn_f
,
[
X
,
G
,
B
,
utt
.
verify_grad
(
bn_f
,
[
X
,
G
,
B
,
X
.
mean
(
axis
=
0
)[
n
umpy
.
newaxis
],
X
.
std
(
axis
=
0
)[
numpy
.
newaxis
]])
X
.
mean
(
axis
=
0
)[
n
p
.
newaxis
],
X
.
std
(
axis
=
0
)[
np
.
newaxis
]])
def
test_bn_feature_maps
():
def
test_bn_feature_maps
():
...
@@ -101,12 +101,12 @@ def test_bn_feature_maps():
...
@@ -101,12 +101,12 @@ def test_bn_feature_maps():
n
=
(
x
-
M
)
/
V
n
=
(
x
-
M
)
/
V
return
n
*
G
+
B
return
n
*
G
+
B
n
umpy
.
random
.
seed
(
1234
)
n
p
.
random
.
seed
(
1234
)
X
=
1
+
n
umpy
.
random
.
random
([
2
,
3
,
4
,
4
])
.
astype
(
'float32'
)
X
=
1
+
n
p
.
random
.
random
([
2
,
3
,
4
,
4
])
.
astype
(
'float32'
)
B
=
1
+
n
umpy
.
random
.
random
([
3
])
.
astype
(
'float32'
)
B
=
1
+
n
p
.
random
.
random
([
3
])
.
astype
(
'float32'
)
G
=
1
+
n
umpy
.
random
.
random
([
3
])
.
astype
(
'float32'
)
G
=
1
+
n
p
.
random
.
random
([
3
])
.
astype
(
'float32'
)
M
=
1
+
n
umpy
.
random
.
random
([
3
])
.
astype
(
'float32'
)
M
=
1
+
n
p
.
random
.
random
([
3
])
.
astype
(
'float32'
)
V
=
1
+
n
umpy
.
random
.
random
([
3
])
.
astype
(
'float32'
)
V
=
1
+
n
p
.
random
.
random
([
3
])
.
astype
(
'float32'
)
x
=
theano
.
tensor
.
tensor4
(
'x'
)
x
=
theano
.
tensor
.
tensor4
(
'x'
)
b
=
theano
.
tensor
.
vector
(
'b'
)
b
=
theano
.
tensor
.
vector
(
'b'
)
...
@@ -205,20 +205,20 @@ def test_batch_normalization_train():
...
@@ -205,20 +205,20 @@ def test_batch_normalization_train():
data_shape
=
data_shape
[:
ndim
]
data_shape
=
data_shape
[:
ndim
]
param_shape
=
tuple
(
1
if
d
in
axes2
else
s
param_shape
=
tuple
(
1
if
d
in
axes2
else
s
for
d
,
s
in
enumerate
(
data_shape
))
for
d
,
s
in
enumerate
(
data_shape
))
X
=
4
+
3
*
n
umpy
.
random
.
randn
(
*
data_shape
)
.
astype
(
theano
.
config
.
floatX
)
X
=
4
+
3
*
n
p
.
random
.
randn
(
*
data_shape
)
.
astype
(
theano
.
config
.
floatX
)
Dy
=
-
1
+
2
*
n
umpy
.
random
.
randn
(
*
data_shape
)
.
astype
(
theano
.
config
.
floatX
)
Dy
=
-
1
+
2
*
n
p
.
random
.
randn
(
*
data_shape
)
.
astype
(
theano
.
config
.
floatX
)
Scale
=
n
umpy
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
Scale
=
n
p
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
Bias
=
n
umpy
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
Bias
=
n
p
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
Running_mean
=
n
umpy
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
Running_mean
=
n
p
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
Running_var
=
n
umpy
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
Running_var
=
n
p
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
outputs
=
f
(
X
,
Scale
,
Bias
,
Running_mean
,
Running_var
,
Dy
)
outputs
=
f
(
X
,
Scale
,
Bias
,
Running_mean
,
Running_var
,
Dy
)
# compare outputs
# compare outputs
utt
.
assert_allclose
(
outputs
[
0
],
outputs
[
0
+
5
])
# out
utt
.
assert_allclose
(
outputs
[
0
],
outputs
[
0
+
5
])
# out
utt
.
assert_allclose
(
outputs
[
1
],
outputs
[
1
+
5
])
# mean
utt
.
assert_allclose
(
outputs
[
1
],
outputs
[
1
+
5
])
# mean
utt
.
assert_allclose
(
outputs
[
2
],
outputs
[
2
+
5
])
# invstd
utt
.
assert_allclose
(
outputs
[
2
],
outputs
[
2
+
5
])
# invstd
utt
.
assert_allclose
(
outputs
[
3
],
outputs
[
3
+
5
])
# running_mean
utt
.
assert_allclose
(
outputs
[
3
],
outputs
[
3
+
5
])
# running_mean
utt
.
assert_allclose
(
n
umpy
.
nan_to_num
(
outputs
[
4
]),
utt
.
assert_allclose
(
n
p
.
nan_to_num
(
outputs
[
4
]),
n
umpy
.
nan_to_num
(
outputs
[
4
+
5
]))
# running_var
n
p
.
nan_to_num
(
outputs
[
4
+
5
]))
# running_var
# compare gradients
# compare gradients
utt
.
assert_allclose
(
outputs
[
10
],
outputs
[
10
+
3
],
atol
=
1e-4
)
# dx
utt
.
assert_allclose
(
outputs
[
10
],
outputs
[
10
+
3
],
atol
=
1e-4
)
# dx
utt
.
assert_allclose
(
outputs
[
11
],
outputs
[
11
+
3
],
rtol
=
2e-4
,
atol
=
1e-4
)
# dscale
utt
.
assert_allclose
(
outputs
[
11
],
outputs
[
11
+
3
],
rtol
=
2e-4
,
atol
=
1e-4
)
# dscale
...
@@ -245,10 +245,10 @@ def test_batch_normalization_train_without_running_averages():
...
@@ -245,10 +245,10 @@ def test_batch_normalization_train_without_running_averages():
bn
.
AbstractBatchNormTrainGrad
))
bn
.
AbstractBatchNormTrainGrad
))
for
n
in
f
.
maker
.
fgraph
.
toposort
()])
for
n
in
f
.
maker
.
fgraph
.
toposort
()])
# run
# run
X
=
4
+
3
*
n
umpy
.
random
.
randn
(
*
data_shape
)
.
astype
(
theano
.
config
.
floatX
)
X
=
4
+
3
*
n
p
.
random
.
randn
(
*
data_shape
)
.
astype
(
theano
.
config
.
floatX
)
Dy
=
-
1
+
2
*
n
umpy
.
random
.
randn
(
*
data_shape
)
.
astype
(
theano
.
config
.
floatX
)
Dy
=
-
1
+
2
*
n
p
.
random
.
randn
(
*
data_shape
)
.
astype
(
theano
.
config
.
floatX
)
Scale
=
n
umpy
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
Scale
=
n
p
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
Bias
=
n
umpy
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
Bias
=
n
p
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
f
(
X
,
Scale
,
Bias
,
Dy
)
f
(
X
,
Scale
,
Bias
,
Dy
)
...
@@ -330,7 +330,7 @@ def test_batch_normalization_train_broadcast():
...
@@ -330,7 +330,7 @@ def test_batch_normalization_train_broadcast():
if
theano
.
config
.
mode
!=
"FAST_COMPILE"
:
if
theano
.
config
.
mode
!=
"FAST_COMPILE"
:
assert
len
(
nodes
)
==
1
assert
len
(
nodes
)
==
1
assert
isinstance
(
nodes
[
0
]
.
op
,
theano
.
compile
.
DeepCopyOp
)
assert
isinstance
(
nodes
[
0
]
.
op
,
theano
.
compile
.
DeepCopyOp
)
inputs
=
[
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
*
((
4
,)
*
n
)),
x
.
dtype
)
inputs
=
[
n
p
.
asarray
(
np
.
random
.
rand
(
*
((
4
,)
*
n
)),
x
.
dtype
)
for
n
in
[
x
.
ndim
,
scale
.
ndim
,
bias
.
ndim
,
for
n
in
[
x
.
ndim
,
scale
.
ndim
,
bias
.
ndim
,
running_mean
.
ndim
,
running_var
.
ndim
]]
running_mean
.
ndim
,
running_var
.
ndim
]]
assert
0.0
==
f
(
*
inputs
)
assert
0.0
==
f
(
*
inputs
)
...
@@ -381,12 +381,12 @@ def test_batch_normalization_test():
...
@@ -381,12 +381,12 @@ def test_batch_normalization_test():
data_shape
=
data_shape
[:
ndim
]
data_shape
=
data_shape
[:
ndim
]
param_shape
=
tuple
(
1
if
d
in
axes2
else
s
param_shape
=
tuple
(
1
if
d
in
axes2
else
s
for
d
,
s
in
enumerate
(
data_shape
))
for
d
,
s
in
enumerate
(
data_shape
))
X
=
4
+
3
*
n
umpy
.
random
.
randn
(
*
data_shape
)
.
astype
(
theano
.
config
.
floatX
)
X
=
4
+
3
*
n
p
.
random
.
randn
(
*
data_shape
)
.
astype
(
theano
.
config
.
floatX
)
Dy
=
-
1
+
2
*
n
umpy
.
random
.
randn
(
*
data_shape
)
.
astype
(
theano
.
config
.
floatX
)
Dy
=
-
1
+
2
*
n
p
.
random
.
randn
(
*
data_shape
)
.
astype
(
theano
.
config
.
floatX
)
Scale
=
n
umpy
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
Scale
=
n
p
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
Bias
=
n
umpy
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
Bias
=
n
p
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
Mean
=
n
umpy
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
Mean
=
n
p
.
random
.
randn
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
Var
=
n
umpy
.
random
.
rand
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
Var
=
n
p
.
random
.
rand
(
*
param_shape
)
.
astype
(
theano
.
config
.
floatX
)
outputs
=
f
(
X
,
Scale
,
Bias
,
Mean
,
Var
,
Dy
)
outputs
=
f
(
X
,
Scale
,
Bias
,
Mean
,
Var
,
Dy
)
# compare outputs
# compare outputs
utt
.
assert_allclose
(
outputs
[
0
],
outputs
[
1
])
# out
utt
.
assert_allclose
(
outputs
[
0
],
outputs
[
1
])
# out
...
...
theano/tensor/nnet/tests/test_conv.py
浏览文件 @
429a8fae
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
import
time
import
time
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
import
numpy
import
numpy
as
np
import
theano
import
theano
import
theano.tensor
as
T
import
theano.tensor
as
T
from
theano.tests
import
unittest_tools
as
utt
from
theano.tests
import
unittest_tools
as
utt
...
@@ -79,8 +79,8 @@ class TestConv2D(utt.InferShapeTester):
...
@@ -79,8 +79,8 @@ class TestConv2D(utt.InferShapeTester):
theano_conv
=
theano
.
function
([
input
,
filters
],
output
,
mode
=
self
.
mode
)
theano_conv
=
theano
.
function
([
input
,
filters
],
output
,
mode
=
self
.
mode
)
# initialize input and compute result
# initialize input and compute result
image_data
=
n
umpy
.
random
.
random
(
N_image_shape
)
.
astype
(
self
.
dtype
)
image_data
=
n
p
.
random
.
random
(
N_image_shape
)
.
astype
(
self
.
dtype
)
filter_data
=
n
umpy
.
random
.
random
(
N_filter_shape
)
.
astype
(
self
.
dtype
)
filter_data
=
n
p
.
random
.
random
(
N_filter_shape
)
.
astype
(
self
.
dtype
)
try
:
try
:
theano_output
=
theano_conv
(
image_data
,
filter_data
)
theano_output
=
theano_conv
(
image_data
,
filter_data
)
except
ValueError
:
except
ValueError
:
...
@@ -97,20 +97,20 @@ class TestConv2D(utt.InferShapeTester):
...
@@ -97,20 +97,20 @@ class TestConv2D(utt.InferShapeTester):
orig_image_data
=
image_data
orig_image_data
=
image_data
if
border_mode
is
not
'full'
:
if
border_mode
is
not
'full'
:
s
=
-
1.
s
=
-
1.
out_shape2d
=
n
umpy
.
array
(
N_image_shape
[
-
2
:])
+
\
out_shape2d
=
n
p
.
array
(
N_image_shape
[
-
2
:])
+
\
s
*
n
umpy
.
array
(
N_filter_shape
[
-
2
:])
-
s
s
*
n
p
.
array
(
N_filter_shape
[
-
2
:])
-
s
out_shape2d
=
n
umpy
.
ceil
(
out_shape2d
/
numpy
.
array
(
subsample
))
out_shape2d
=
n
p
.
ceil
(
out_shape2d
/
np
.
array
(
subsample
))
# avoid numpy deprecation
# avoid numpy deprecation
out_shape2d
=
out_shape2d
.
astype
(
'int32'
)
out_shape2d
=
out_shape2d
.
astype
(
'int32'
)
out_shape
=
(
N_image_shape
[
0
],
N_filter_shape
[
0
])
+
tuple
(
out_shape2d
)
out_shape
=
(
N_image_shape
[
0
],
N_filter_shape
[
0
])
+
tuple
(
out_shape2d
)
ref_output
=
n
umpy
.
zeros
(
out_shape
)
ref_output
=
n
p
.
zeros
(
out_shape
)
# loop over output feature maps
# loop over output feature maps
ref_output
.
fill
(
0
)
ref_output
.
fill
(
0
)
if
border_mode
==
'full'
:
if
border_mode
==
'full'
:
image_data2
=
n
umpy
.
zeros
((
N_image_shape
[
0
],
N_image_shape
[
1
],
image_data2
=
n
p
.
zeros
((
N_image_shape
[
0
],
N_image_shape
[
1
],
N_image_shape
[
2
]
+
2
*
N_filter_shape
[
2
]
-
2
,
N_image_shape
[
2
]
+
2
*
N_filter_shape
[
2
]
-
2
,
N_image_shape
[
3
]
+
2
*
N_filter_shape
[
3
]
-
2
))
N_image_shape
[
3
]
+
2
*
N_filter_shape
[
3
]
-
2
))
image_data2
[
image_data2
[
:,
:,
N_filter_shape
[
2
]
-
1
:
N_filter_shape
[
2
]
-
1
+
N_image_shape
[
2
],
:,
:,
N_filter_shape
[
2
]
-
1
:
N_filter_shape
[
2
]
-
1
+
N_image_shape
[
2
],
N_filter_shape
[
3
]
-
1
:
N_filter_shape
[
3
]
-
1
+
N_image_shape
[
3
]]
=
image_data
N_filter_shape
[
3
]
-
1
:
N_filter_shape
[
3
]
-
1
+
N_image_shape
[
3
]]
=
image_data
...
@@ -160,17 +160,17 @@ class TestConv2D(utt.InferShapeTester):
...
@@ -160,17 +160,17 @@ class TestConv2D(utt.InferShapeTester):
def
test_uint_image_shape_datatype
(
self
):
def
test_uint_image_shape_datatype
(
self
):
"""Tests for uint datatype in image_shape.
"""Tests for uint datatype in image_shape.
"""
"""
self
.
validate
((
2
,
2
,
3
,
n
umpy
.
uint8
(
3
)),
(
3
,
2
,
3
,
3
),
'valid'
,
verify_grad
=
False
)
self
.
validate
((
2
,
2
,
3
,
n
p
.
uint8
(
3
)),
(
3
,
2
,
3
,
3
),
'valid'
,
verify_grad
=
False
)
self
.
validate
((
n
umpy
.
uint16
(
2
),
2
,
3
,
3
),
(
3
,
2
,
3
,
3
),
'valid'
,
verify_grad
=
False
)
self
.
validate
((
n
p
.
uint16
(
2
),
2
,
3
,
3
),
(
3
,
2
,
3
,
3
),
'valid'
,
verify_grad
=
False
)
self
.
validate
((
2
,
n
umpy
.
uint32
(
2
),
3
,
3
),
(
3
,
2
,
3
,
3
),
'valid'
,
verify_grad
=
False
)
self
.
validate
((
2
,
n
p
.
uint32
(
2
),
3
,
3
),
(
3
,
2
,
3
,
3
),
'valid'
,
verify_grad
=
False
)
def
test_uint_filter_shape_datatype
(
self
):
def
test_uint_filter_shape_datatype
(
self
):
"""Tests for uint datatype in filter_shape
"""Tests for uint datatype in filter_shape
"""
"""
self
.
validate
((
3
,
2
,
3
,
3
),
(
2
,
2
,
3
,
n
umpy
.
uint8
(
3
)),
'valid'
,
verify_grad
=
False
)
self
.
validate
((
3
,
2
,
3
,
3
),
(
2
,
2
,
3
,
n
p
.
uint8
(
3
)),
'valid'
,
verify_grad
=
False
)
self
.
validate
((
3
,
2
,
3
,
3
),
(
n
umpy
.
uint16
(
2
),
2
,
3
,
3
),
'valid'
,
verify_grad
=
False
)
self
.
validate
((
3
,
2
,
3
,
3
),
(
n
p
.
uint16
(
2
),
2
,
3
,
3
),
'valid'
,
verify_grad
=
False
)
self
.
validate
((
3
,
2
,
3
,
3
),
(
2
,
n
umpy
.
uint32
(
2
),
3
,
3
),
'valid'
,
verify_grad
=
False
)
self
.
validate
((
3
,
2
,
3
,
3
),
(
2
,
n
p
.
uint32
(
2
),
3
,
3
),
'valid'
,
verify_grad
=
False
)
def
test_img_kernel_same_shape
(
self
):
def
test_img_kernel_same_shape
(
self
):
self
.
validate
((
3
,
2
,
3
,
3
),
(
4
,
2
,
3
,
3
),
'full'
)
self
.
validate
((
3
,
2
,
3
,
3
),
(
4
,
2
,
3
,
3
),
'full'
)
...
@@ -474,8 +474,8 @@ class TestConv2D(utt.InferShapeTester):
...
@@ -474,8 +474,8 @@ class TestConv2D(utt.InferShapeTester):
print
(
"filter_shapes"
,
filter_shapes
)
print
(
"filter_shapes"
,
filter_shapes
)
for
filter_shape
in
filter_shapes
:
for
filter_shape
in
filter_shapes
:
input
=
theano
.
shared
(
n
umpy
.
random
.
random
(
image_shape
))
input
=
theano
.
shared
(
n
p
.
random
.
random
(
image_shape
))
filters
=
theano
.
shared
(
n
umpy
.
random
.
random
(
filter_shape
))
filters
=
theano
.
shared
(
n
p
.
random
.
random
(
filter_shape
))
output
=
self
.
conv2d
(
output
=
self
.
conv2d
(
input
,
filters
,
input
,
filters
,
...
@@ -498,7 +498,7 @@ class TestConv2D(utt.InferShapeTester):
...
@@ -498,7 +498,7 @@ class TestConv2D(utt.InferShapeTester):
# must be provided explicitly
# must be provided explicitly
def
rand
(
*
shape
):
def
rand
(
*
shape
):
r
=
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
'float64'
)
r
=
n
p
.
asarray
(
np
.
random
.
rand
(
*
shape
),
dtype
=
'float64'
)
return
r
*
2
-
1
return
r
*
2
-
1
adtens
=
T
.
dtensor4
()
adtens
=
T
.
dtensor4
()
...
...
theano/tensor/nnet/tests/test_conv3d2d.py
浏览文件 @
429a8fae
...
@@ -3,7 +3,7 @@ import time
...
@@ -3,7 +3,7 @@ import time
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
from
nose_parameterized
import
parameterized
from
nose_parameterized
import
parameterized
import
numpy
import
numpy
as
np
try
:
try
:
from
scipy
import
ndimage
from
scipy
import
ndimage
except
ImportError
:
except
ImportError
:
...
@@ -17,14 +17,14 @@ import theano.tests.unittest_tools as utt
...
@@ -17,14 +17,14 @@ import theano.tests.unittest_tools as utt
def
test_get_diagonal_subtensor_view
(
wrap
=
lambda
a
:
a
):
def
test_get_diagonal_subtensor_view
(
wrap
=
lambda
a
:
a
):
x
=
n
umpy
.
arange
(
20
)
.
reshape
(
5
,
4
)
.
astype
(
'float32'
)
x
=
n
p
.
arange
(
20
)
.
reshape
(
5
,
4
)
.
astype
(
'float32'
)
x
=
wrap
(
x
)
x
=
wrap
(
x
)
xv01
=
get_diagonal_subtensor_view
(
x
,
0
,
1
)
xv01
=
get_diagonal_subtensor_view
(
x
,
0
,
1
)
# test that it works in 2d
# test that it works in 2d
assert
n
umpy
.
all
(
numpy
.
asarray
(
xv01
)
==
[[
12
,
9
,
6
,
3
],
[
16
,
13
,
10
,
7
]])
assert
n
p
.
all
(
np
.
asarray
(
xv01
)
==
[[
12
,
9
,
6
,
3
],
[
16
,
13
,
10
,
7
]])
x
=
n
umpy
.
arange
(
24
)
.
reshape
(
4
,
3
,
2
)
x
=
n
p
.
arange
(
24
)
.
reshape
(
4
,
3
,
2
)
xv01
=
get_diagonal_subtensor_view
(
x
,
0
,
1
)
xv01
=
get_diagonal_subtensor_view
(
x
,
0
,
1
)
xv02
=
get_diagonal_subtensor_view
(
x
,
0
,
2
)
xv02
=
get_diagonal_subtensor_view
(
x
,
0
,
2
)
xv12
=
get_diagonal_subtensor_view
(
x
,
1
,
2
)
xv12
=
get_diagonal_subtensor_view
(
x
,
1
,
2
)
...
@@ -32,11 +32,11 @@ def test_get_diagonal_subtensor_view(wrap=lambda a: a):
...
@@ -32,11 +32,11 @@ def test_get_diagonal_subtensor_view(wrap=lambda a: a):
# print 'x', x
# print 'x', x
# print 'xv01', xv01
# print 'xv01', xv01
# print 'xv02', xv02
# print 'xv02', xv02
assert
n
umpy
.
all
(
numpy
.
asarray
(
xv01
)
==
[
assert
n
p
.
all
(
np
.
asarray
(
xv01
)
==
[
[[
12
,
13
],
[
8
,
9
],
[
4
,
5
]],
[[
12
,
13
],
[
8
,
9
],
[
4
,
5
]],
[[
18
,
19
],
[
14
,
15
],
[
10
,
11
]]])
[[
18
,
19
],
[
14
,
15
],
[
10
,
11
]]])
assert
n
umpy
.
all
(
numpy
.
asarray
(
xv02
)
==
[
assert
n
p
.
all
(
np
.
asarray
(
xv02
)
==
[
[[
6
,
1
],
[
8
,
3
],
[
10
,
5
]],
[[
6
,
1
],
[
8
,
3
],
[
10
,
5
]],
[[
12
,
7
],
[
14
,
9
],
[
16
,
11
]],
[[
12
,
7
],
[
14
,
9
],
[
16
,
11
]],
[[
18
,
13
],
[
20
,
15
],
[
22
,
17
]],
[[
18
,
13
],
[
20
,
15
],
[
22
,
17
]],
...
@@ -45,7 +45,7 @@ def test_get_diagonal_subtensor_view(wrap=lambda a: a):
...
@@ -45,7 +45,7 @@ def test_get_diagonal_subtensor_view(wrap=lambda a: a):
# diagonal views of each leading matrix is the same
# diagonal views of each leading matrix is the same
# as the slices out of the diagonal view of the entire 3d tensor
# as the slices out of the diagonal view of the entire 3d tensor
for
xi
,
xvi
in
zip
(
x
,
xv12
):
for
xi
,
xvi
in
zip
(
x
,
xv12
):
assert
n
umpy
.
all
(
xvi
==
get_diagonal_subtensor_view
(
xi
,
0
,
1
))
assert
n
p
.
all
(
xvi
==
get_diagonal_subtensor_view
(
xi
,
0
,
1
))
def
pyconv3d
(
signals
,
filters
,
border_mode
=
'valid'
):
def
pyconv3d
(
signals
,
filters
,
border_mode
=
'valid'
):
...
@@ -68,8 +68,8 @@ def pyconv3d(signals, filters, border_mode='valid'):
...
@@ -68,8 +68,8 @@ def pyconv3d(signals, filters, border_mode='valid'):
if
Tpad
>
0
or
Hpad
>
0
or
Wpad
>
0
:
if
Tpad
>
0
or
Hpad
>
0
or
Wpad
>
0
:
# zero-pad signals
# zero-pad signals
signals_padded
=
n
umpy
.
zeros
((
Ns
,
Ts
+
2
*
Tpad
,
C
,
signals_padded
=
n
p
.
zeros
((
Ns
,
Ts
+
2
*
Tpad
,
C
,
Hs
+
2
*
Hpad
,
Ws
+
2
*
Wpad
),
'float32'
)
Hs
+
2
*
Hpad
,
Ws
+
2
*
Wpad
),
'float32'
)
signals_padded
[:,
Tpad
:(
Ts
+
Tpad
),
:,
Hpad
:(
Hs
+
Hpad
),
signals_padded
[:,
Tpad
:(
Ts
+
Tpad
),
:,
Hpad
:(
Hs
+
Hpad
),
Wpad
:(
Ws
+
Wpad
)]
=
signals
Wpad
:(
Ws
+
Wpad
)]
=
signals
Ns
,
Ts
,
C
,
Hs
,
Ws
=
signals_padded
.
shape
Ns
,
Ts
,
C
,
Hs
,
Ws
=
signals_padded
.
shape
...
@@ -79,7 +79,7 @@ def pyconv3d(signals, filters, border_mode='valid'):
...
@@ -79,7 +79,7 @@ def pyconv3d(signals, filters, border_mode='valid'):
Hf2
=
Hf
//
2
Hf2
=
Hf
//
2
Wf2
=
Wf
//
2
Wf2
=
Wf
//
2
rval
=
n
umpy
.
zeros
((
Ns
,
Ts
-
Tf
+
1
,
Nf
,
Hs
-
Hf
+
1
,
Ws
-
Wf
+
1
))
rval
=
n
p
.
zeros
((
Ns
,
Ts
-
Tf
+
1
,
Nf
,
Hs
-
Hf
+
1
,
Ws
-
Wf
+
1
))
for
ns
in
xrange
(
Ns
):
for
ns
in
xrange
(
Ns
):
for
nf
in
xrange
(
Nf
):
for
nf
in
xrange
(
Nf
):
for
c
in
xrange
(
C
):
for
c
in
xrange
(
C
):
...
@@ -113,8 +113,8 @@ def test_conv3d(border_mode):
...
@@ -113,8 +113,8 @@ def test_conv3d(border_mode):
Ns
,
Ts
,
C
,
Hs
,
Ws
=
3
,
10
,
3
,
32
,
32
Ns
,
Ts
,
C
,
Hs
,
Ws
=
3
,
10
,
3
,
32
,
32
Nf
,
Tf
,
C
,
Hf
,
Wf
=
32
,
5
,
3
,
5
,
5
Nf
,
Tf
,
C
,
Hf
,
Wf
=
32
,
5
,
3
,
5
,
5
signals
=
n
umpy
.
arange
(
Ns
*
Ts
*
C
*
Hs
*
Ws
)
.
reshape
(
Ns
,
Ts
,
C
,
Hs
,
Ws
)
.
astype
(
'float32'
)
signals
=
n
p
.
arange
(
Ns
*
Ts
*
C
*
Hs
*
Ws
)
.
reshape
(
Ns
,
Ts
,
C
,
Hs
,
Ws
)
.
astype
(
'float32'
)
filters
=
n
umpy
.
arange
(
Nf
*
Tf
*
C
*
Hf
*
Wf
)
.
reshape
(
Nf
,
Tf
,
C
,
Hf
,
Wf
)
.
astype
(
'float32'
)
filters
=
n
p
.
arange
(
Nf
*
Tf
*
C
*
Hf
*
Wf
)
.
reshape
(
Nf
,
Tf
,
C
,
Hf
,
Wf
)
.
astype
(
'float32'
)
t0
=
time
.
time
()
t0
=
time
.
time
()
pyres
=
pyconv3d
(
signals
,
filters
,
border_mode
)
pyres
=
pyconv3d
(
signals
,
filters
,
border_mode
)
...
@@ -153,8 +153,8 @@ def test_conv3d(border_mode):
...
@@ -153,8 +153,8 @@ def test_conv3d(border_mode):
Ns
,
Ts
,
C
,
Hs
,
Ws
=
3
,
3
,
3
,
5
,
5
Ns
,
Ts
,
C
,
Hs
,
Ws
=
3
,
3
,
3
,
5
,
5
Nf
,
Tf
,
C
,
Hf
,
Wf
=
4
,
2
,
3
,
2
,
2
Nf
,
Tf
,
C
,
Hf
,
Wf
=
4
,
2
,
3
,
2
,
2
signals
=
n
umpy
.
random
.
rand
(
Ns
,
Ts
,
C
,
Hs
,
Ws
)
.
astype
(
'float32'
)
signals
=
n
p
.
random
.
rand
(
Ns
,
Ts
,
C
,
Hs
,
Ws
)
.
astype
(
'float32'
)
filters
=
n
umpy
.
random
.
rand
(
Nf
,
Tf
,
C
,
Hf
,
Wf
)
.
astype
(
'float32'
)
filters
=
n
p
.
random
.
rand
(
Nf
,
Tf
,
C
,
Hf
,
Wf
)
.
astype
(
'float32'
)
utt
.
verify_grad
(
lambda
s
,
f
:
conv3d
(
s
,
f
,
border_mode
=
border_mode
),
utt
.
verify_grad
(
lambda
s
,
f
:
conv3d
(
s
,
f
,
border_mode
=
border_mode
),
[
signals
,
filters
],
eps
=
1e-1
,
mode
=
mode
)
[
signals
,
filters
],
eps
=
1e-1
,
mode
=
mode
)
...
@@ -162,8 +162,8 @@ def test_conv3d(border_mode):
...
@@ -162,8 +162,8 @@ def test_conv3d(border_mode):
Ns
,
Ts
,
C
,
Hs
,
Ws
=
3
,
10
,
3
,
32
,
32
Ns
,
Ts
,
C
,
Hs
,
Ws
=
3
,
10
,
3
,
32
,
32
Nf
,
Tf
,
C
,
Hf
,
Wf
=
32
,
1
,
3
,
5
,
5
Nf
,
Tf
,
C
,
Hf
,
Wf
=
32
,
1
,
3
,
5
,
5
signals
=
n
umpy
.
arange
(
Ns
*
Ts
*
C
*
Hs
*
Ws
)
.
reshape
(
Ns
,
Ts
,
C
,
Hs
,
Ws
)
.
astype
(
'float32'
)
signals
=
n
p
.
arange
(
Ns
*
Ts
*
C
*
Hs
*
Ws
)
.
reshape
(
Ns
,
Ts
,
C
,
Hs
,
Ws
)
.
astype
(
'float32'
)
filters
=
n
umpy
.
arange
(
Nf
*
Tf
*
C
*
Hf
*
Wf
)
.
reshape
(
Nf
,
Tf
,
C
,
Hf
,
Wf
)
.
astype
(
'float32'
)
filters
=
n
p
.
arange
(
Nf
*
Tf
*
C
*
Hf
*
Wf
)
.
reshape
(
Nf
,
Tf
,
C
,
Hf
,
Wf
)
.
astype
(
'float32'
)
t0
=
time
.
time
()
t0
=
time
.
time
()
pyres
=
pyconv3d
(
signals
,
filters
,
border_mode
)
pyres
=
pyconv3d
(
signals
,
filters
,
border_mode
)
...
@@ -200,7 +200,7 @@ def test_conv3d(border_mode):
...
@@ -200,7 +200,7 @@ def test_conv3d(border_mode):
Ns
,
Ts
,
C
,
Hs
,
Ws
=
3
,
3
,
3
,
5
,
5
Ns
,
Ts
,
C
,
Hs
,
Ws
=
3
,
3
,
3
,
5
,
5
Nf
,
Tf
,
C
,
Hf
,
Wf
=
4
,
1
,
3
,
2
,
2
Nf
,
Tf
,
C
,
Hf
,
Wf
=
4
,
1
,
3
,
2
,
2
signals
=
n
umpy
.
random
.
rand
(
Ns
,
Ts
,
C
,
Hs
,
Ws
)
.
astype
(
'float32'
)
signals
=
n
p
.
random
.
rand
(
Ns
,
Ts
,
C
,
Hs
,
Ws
)
.
astype
(
'float32'
)
filters
=
n
umpy
.
random
.
rand
(
Nf
,
Tf
,
C
,
Hf
,
Wf
)
.
astype
(
'float32'
)
filters
=
n
p
.
random
.
rand
(
Nf
,
Tf
,
C
,
Hf
,
Wf
)
.
astype
(
'float32'
)
utt
.
verify_grad
(
lambda
s
,
f
:
conv3d
(
s
,
f
,
border_mode
=
border_mode
),
utt
.
verify_grad
(
lambda
s
,
f
:
conv3d
(
s
,
f
,
border_mode
=
border_mode
),
[
signals
,
filters
],
eps
=
1e-1
,
mode
=
mode
)
[
signals
,
filters
],
eps
=
1e-1
,
mode
=
mode
)
theano/tensor/nnet/tests/test_corr.py
浏览文件 @
429a8fae
...
@@ -3,7 +3,7 @@ from __future__ import absolute_import, print_function, division
...
@@ -3,7 +3,7 @@ from __future__ import absolute_import, print_function, division
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.attrib
import
attr
from
nose.plugins.attrib
import
attr
from
nose.tools
import
assert_equals
from
nose.tools
import
assert_equals
import
numpy
import
numpy
as
np
from
six
import
integer_types
from
six
import
integer_types
import
theano
import
theano
...
@@ -66,15 +66,15 @@ class TestCorr2D(utt.InferShapeTester):
...
@@ -66,15 +66,15 @@ class TestCorr2D(utt.InferShapeTester):
theano_corr
=
theano
.
function
([
input
,
filters
],
output
,
mode
=
self
.
mode
)
theano_corr
=
theano
.
function
([
input
,
filters
],
output
,
mode
=
self
.
mode
)
# initialize input and compute result
# initialize input and compute result
image_data
=
n
umpy
.
random
.
random
(
N_image_shape
)
.
astype
(
self
.
dtype
)
image_data
=
n
p
.
random
.
random
(
N_image_shape
)
.
astype
(
self
.
dtype
)
filter_data
=
n
umpy
.
random
.
random
(
N_filter_shape
)
.
astype
(
self
.
dtype
)
filter_data
=
n
p
.
random
.
random
(
N_filter_shape
)
.
astype
(
self
.
dtype
)
if
non_contiguous
:
if
non_contiguous
:
image_data
=
n
umpy
.
transpose
(
image_data
,
axes
=
(
0
,
1
,
3
,
2
))
image_data
=
n
p
.
transpose
(
image_data
,
axes
=
(
0
,
1
,
3
,
2
))
image_data
=
image_data
.
copy
()
image_data
=
image_data
.
copy
()
image_data
=
n
umpy
.
transpose
(
image_data
,
axes
=
(
0
,
1
,
3
,
2
))
image_data
=
n
p
.
transpose
(
image_data
,
axes
=
(
0
,
1
,
3
,
2
))
filter_data
=
n
umpy
.
transpose
(
filter_data
,
axes
=
(
0
,
1
,
3
,
2
))
filter_data
=
n
p
.
transpose
(
filter_data
,
axes
=
(
0
,
1
,
3
,
2
))
filter_data
=
filter_data
.
copy
()
filter_data
=
filter_data
.
copy
()
filter_data
=
n
umpy
.
transpose
(
filter_data
,
axes
=
(
0
,
1
,
3
,
2
))
filter_data
=
n
p
.
transpose
(
filter_data
,
axes
=
(
0
,
1
,
3
,
2
))
assert
not
image_data
.
flags
[
'CONTIGUOUS'
]
assert
not
image_data
.
flags
[
'CONTIGUOUS'
]
assert
not
filter_data
.
flags
[
'CONTIGUOUS'
]
assert
not
filter_data
.
flags
[
'CONTIGUOUS'
]
...
@@ -82,38 +82,38 @@ class TestCorr2D(utt.InferShapeTester):
...
@@ -82,38 +82,38 @@ class TestCorr2D(utt.InferShapeTester):
# REFERENCE IMPLEMENTATION
# REFERENCE IMPLEMENTATION
# Testing correlation, not convolution. Reverse filters.
# Testing correlation, not convolution. Reverse filters.
filter_data_corr
=
n
umpy
.
array
(
filter_data
[:,
:,
::
-
1
,
::
-
1
],
filter_data_corr
=
n
p
.
array
(
filter_data
[:,
:,
::
-
1
,
::
-
1
],
copy
=
True
,
copy
=
True
,
order
=
'C'
)
order
=
'C'
)
orig_image_data
=
image_data
orig_image_data
=
image_data
img_shape2d
=
n
umpy
.
array
(
N_image_shape
[
-
2
:])
img_shape2d
=
n
p
.
array
(
N_image_shape
[
-
2
:])
fil_shape2d
=
n
umpy
.
array
(
N_filter_shape
[
-
2
:])
fil_shape2d
=
n
p
.
array
(
N_filter_shape
[
-
2
:])
dil_shape2d
=
n
umpy
.
array
(
filter_dilation
)
dil_shape2d
=
n
p
.
array
(
filter_dilation
)
dil_fil_shape2d
=
(
fil_shape2d
-
1
)
*
dil_shape2d
+
1
dil_fil_shape2d
=
(
fil_shape2d
-
1
)
*
dil_shape2d
+
1
subsample2d
=
n
umpy
.
array
(
subsample
)
subsample2d
=
n
p
.
array
(
subsample
)
if
border_mode
==
'full'
:
if
border_mode
==
'full'
:
padHW
=
(
dil_fil_shape2d
-
1
)
padHW
=
(
dil_fil_shape2d
-
1
)
elif
border_mode
==
'valid'
:
elif
border_mode
==
'valid'
:
padHW
=
n
umpy
.
array
([
0
,
0
])
padHW
=
n
p
.
array
([
0
,
0
])
elif
border_mode
==
'half'
:
elif
border_mode
==
'half'
:
padHW
=
n
umpy
.
floor
(
dil_fil_shape2d
/
2
)
.
astype
(
'int32'
)
padHW
=
n
p
.
floor
(
dil_fil_shape2d
/
2
)
.
astype
(
'int32'
)
elif
isinstance
(
border_mode
,
tuple
):
elif
isinstance
(
border_mode
,
tuple
):
padHW
=
n
umpy
.
array
(
border_mode
)
padHW
=
n
p
.
array
(
border_mode
)
elif
isinstance
(
border_mode
,
integer_types
):
elif
isinstance
(
border_mode
,
integer_types
):
padHW
=
n
umpy
.
array
([
border_mode
,
border_mode
])
padHW
=
n
p
.
array
([
border_mode
,
border_mode
])
else
:
else
:
raise
NotImplementedError
(
'Unsupported border_mode {}'
.
format
(
border_mode
))
raise
NotImplementedError
(
'Unsupported border_mode {}'
.
format
(
border_mode
))
out_shape2d
=
n
umpy
.
floor
((
img_shape2d
+
2
*
(
padHW
)
-
dil_fil_shape2d
)
/
subsample2d
)
+
1
out_shape2d
=
n
p
.
floor
((
img_shape2d
+
2
*
(
padHW
)
-
dil_fil_shape2d
)
/
subsample2d
)
+
1
# avoid numpy deprecation
# avoid numpy deprecation
out_shape2d
=
out_shape2d
.
astype
(
'int32'
)
out_shape2d
=
out_shape2d
.
astype
(
'int32'
)
out_shape
=
(
N_image_shape
[
0
],
N_filter_shape
[
0
])
+
tuple
(
out_shape2d
)
out_shape
=
(
N_image_shape
[
0
],
N_filter_shape
[
0
])
+
tuple
(
out_shape2d
)
ref_output
=
n
umpy
.
zeros
(
out_shape
)
ref_output
=
n
p
.
zeros
(
out_shape
)
# loop over output feature maps
# loop over output feature maps
ref_output
.
fill
(
0
)
ref_output
.
fill
(
0
)
image_data2
=
n
umpy
.
zeros
((
N_image_shape
[
0
],
N_image_shape
[
1
],
image_data2
=
n
p
.
zeros
((
N_image_shape
[
0
],
N_image_shape
[
1
],
N_image_shape
[
2
]
+
2
*
padHW
[
0
],
N_image_shape
[
2
]
+
2
*
padHW
[
0
],
N_image_shape
[
3
]
+
2
*
padHW
[
1
]))
N_image_shape
[
3
]
+
2
*
padHW
[
1
]))
image_data2
[:,
:,
padHW
[
0
]:
padHW
[
0
]
+
N_image_shape
[
2
],
image_data2
[:,
:,
padHW
[
0
]:
padHW
[
0
]
+
N_image_shape
[
2
],
padHW
[
1
]:
padHW
[
1
]
+
N_image_shape
[
3
]]
=
image_data
padHW
[
1
]:
padHW
[
1
]
+
N_image_shape
[
3
]]
=
image_data
image_data
=
image_data2
image_data
=
image_data2
...
@@ -265,7 +265,7 @@ class TestCorr2D(utt.InferShapeTester):
...
@@ -265,7 +265,7 @@ class TestCorr2D(utt.InferShapeTester):
Checks dtype upcast for CorrMM methods.
Checks dtype upcast for CorrMM methods.
"""
"""
def
rand
(
shape
,
dtype
=
'float64'
):
def
rand
(
shape
,
dtype
=
'float64'
):
r
=
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
dtype
)
r
=
n
p
.
asarray
(
np
.
random
.
rand
(
*
shape
),
dtype
=
dtype
)
return
r
*
2
-
1
return
r
*
2
-
1
if
not
theano
.
config
.
cxx
:
if
not
theano
.
config
.
cxx
:
raise
SkipTest
(
"Need cxx to test conv2d"
)
raise
SkipTest
(
"Need cxx to test conv2d"
)
...
@@ -296,7 +296,7 @@ class TestCorr2D(utt.InferShapeTester):
...
@@ -296,7 +296,7 @@ class TestCorr2D(utt.InferShapeTester):
raise
SkipTest
(
"Need cxx for this test"
)
raise
SkipTest
(
"Need cxx for this test"
)
def
rand
(
*
shape
):
def
rand
(
*
shape
):
r
=
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
'float64'
)
r
=
n
p
.
asarray
(
np
.
random
.
rand
(
*
shape
),
dtype
=
'float64'
)
return
r
*
2
-
1
return
r
*
2
-
1
corrMM
=
corr
.
CorrMM
corrMM
=
corr
.
CorrMM
...
@@ -329,7 +329,7 @@ class TestCorr2D(utt.InferShapeTester):
...
@@ -329,7 +329,7 @@ class TestCorr2D(utt.InferShapeTester):
raise
SkipTest
(
"Need cxx for this test"
)
raise
SkipTest
(
"Need cxx for this test"
)
def
rand
(
*
shape
):
def
rand
(
*
shape
):
r
=
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
'float64'
)
r
=
n
p
.
asarray
(
np
.
random
.
rand
(
*
shape
),
dtype
=
'float64'
)
return
r
*
2
-
1
return
r
*
2
-
1
corrMM
=
corr
.
CorrMM
corrMM
=
corr
.
CorrMM
gradW
=
corr
.
CorrMM_gradWeights
gradW
=
corr
.
CorrMM_gradWeights
...
@@ -369,7 +369,7 @@ class TestCorr2D(utt.InferShapeTester):
...
@@ -369,7 +369,7 @@ class TestCorr2D(utt.InferShapeTester):
raise
SkipTest
(
"Need cxx for this test"
)
raise
SkipTest
(
"Need cxx for this test"
)
def
rand
(
*
shape
):
def
rand
(
*
shape
):
r
=
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
'float64'
)
r
=
n
p
.
asarray
(
np
.
random
.
rand
(
*
shape
),
dtype
=
'float64'
)
return
r
*
2
-
1
return
r
*
2
-
1
corrMM
=
corr
.
CorrMM
corrMM
=
corr
.
CorrMM
gradI
=
corr
.
CorrMM_gradInputs
gradI
=
corr
.
CorrMM_gradInputs
...
...
theano/tensor/nnet/tests/test_corr3d.py
浏览文件 @
429a8fae
...
@@ -3,7 +3,7 @@ from __future__ import absolute_import, print_function, division
...
@@ -3,7 +3,7 @@ from __future__ import absolute_import, print_function, division
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.attrib
import
attr
from
nose.plugins.attrib
import
attr
from
nose.tools
import
assert_equals
from
nose.tools
import
assert_equals
import
numpy
import
numpy
as
np
from
six
import
integer_types
from
six
import
integer_types
import
theano
import
theano
...
@@ -67,17 +67,17 @@ class TestCorr3D(utt.InferShapeTester):
...
@@ -67,17 +67,17 @@ class TestCorr3D(utt.InferShapeTester):
theano_corr
=
theano
.
function
([
input
,
filters
],
output
,
mode
=
self
.
mode
)
theano_corr
=
theano
.
function
([
input
,
filters
],
output
,
mode
=
self
.
mode
)
# initialize input and compute result
# initialize input and compute result
image_data
=
n
umpy
.
random
.
random
(
N_image_shape
)
.
astype
(
self
.
dtype
)
image_data
=
n
p
.
random
.
random
(
N_image_shape
)
.
astype
(
self
.
dtype
)
filter_data
=
n
umpy
.
random
.
random
(
N_filter_shape
)
.
astype
(
self
.
dtype
)
filter_data
=
n
p
.
random
.
random
(
N_filter_shape
)
.
astype
(
self
.
dtype
)
image_data
/=
10
image_data
/=
10
filter_data
/=
10
filter_data
/=
10
if
non_contiguous
:
if
non_contiguous
:
image_data
=
n
umpy
.
transpose
(
image_data
,
axes
=
(
0
,
1
,
4
,
3
,
2
))
image_data
=
n
p
.
transpose
(
image_data
,
axes
=
(
0
,
1
,
4
,
3
,
2
))
image_data
=
image_data
.
copy
()
image_data
=
image_data
.
copy
()
image_data
=
n
umpy
.
transpose
(
image_data
,
axes
=
(
0
,
1
,
4
,
3
,
2
))
image_data
=
n
p
.
transpose
(
image_data
,
axes
=
(
0
,
1
,
4
,
3
,
2
))
filter_data
=
n
umpy
.
transpose
(
filter_data
,
axes
=
(
0
,
1
,
4
,
3
,
2
))
filter_data
=
n
p
.
transpose
(
filter_data
,
axes
=
(
0
,
1
,
4
,
3
,
2
))
filter_data
=
filter_data
.
copy
()
filter_data
=
filter_data
.
copy
()
filter_data
=
n
umpy
.
transpose
(
filter_data
,
axes
=
(
0
,
1
,
4
,
3
,
2
))
filter_data
=
n
p
.
transpose
(
filter_data
,
axes
=
(
0
,
1
,
4
,
3
,
2
))
assert
not
image_data
.
flags
[
'CONTIGUOUS'
]
assert
not
image_data
.
flags
[
'CONTIGUOUS'
]
assert
not
filter_data
.
flags
[
'CONTIGUOUS'
]
assert
not
filter_data
.
flags
[
'CONTIGUOUS'
]
...
@@ -85,39 +85,39 @@ class TestCorr3D(utt.InferShapeTester):
...
@@ -85,39 +85,39 @@ class TestCorr3D(utt.InferShapeTester):
# REFERENCE IMPLEMENTATION
# REFERENCE IMPLEMENTATION
# Testing correlation, not convolution. Reverse filters.
# Testing correlation, not convolution. Reverse filters.
filter_data_corr
=
n
umpy
.
array
(
filter_data
[:,
:,
::
-
1
,
::
-
1
,
::
-
1
],
filter_data_corr
=
n
p
.
array
(
filter_data
[:,
:,
::
-
1
,
::
-
1
,
::
-
1
],
copy
=
True
,
copy
=
True
,
order
=
'C'
)
order
=
'C'
)
orig_image_data
=
image_data
orig_image_data
=
image_data
img_shape3d
=
n
umpy
.
array
(
N_image_shape
[
-
3
:])
img_shape3d
=
n
p
.
array
(
N_image_shape
[
-
3
:])
fil_shape3d
=
n
umpy
.
array
(
N_filter_shape
[
-
3
:])
fil_shape3d
=
n
p
.
array
(
N_filter_shape
[
-
3
:])
dil_shape3d
=
n
umpy
.
array
(
filter_dilation
)
dil_shape3d
=
n
p
.
array
(
filter_dilation
)
dil_fil_shape3d
=
(
fil_shape3d
-
1
)
*
dil_shape3d
+
1
dil_fil_shape3d
=
(
fil_shape3d
-
1
)
*
dil_shape3d
+
1
subsample3d
=
n
umpy
.
array
(
subsample
)
subsample3d
=
n
p
.
array
(
subsample
)
if
border_mode
==
'full'
:
if
border_mode
==
'full'
:
padHWD
=
(
dil_fil_shape3d
-
1
)
padHWD
=
(
dil_fil_shape3d
-
1
)
elif
border_mode
==
'valid'
:
elif
border_mode
==
'valid'
:
padHWD
=
n
umpy
.
array
([
0
,
0
,
0
])
padHWD
=
n
p
.
array
([
0
,
0
,
0
])
elif
border_mode
==
'half'
:
elif
border_mode
==
'half'
:
padHWD
=
n
umpy
.
floor
(
dil_fil_shape3d
/
2
)
.
astype
(
'int32'
)
padHWD
=
n
p
.
floor
(
dil_fil_shape3d
/
2
)
.
astype
(
'int32'
)
elif
isinstance
(
border_mode
,
tuple
):
elif
isinstance
(
border_mode
,
tuple
):
padHWD
=
n
umpy
.
array
(
border_mode
)
padHWD
=
n
p
.
array
(
border_mode
)
elif
isinstance
(
border_mode
,
integer_types
):
elif
isinstance
(
border_mode
,
integer_types
):
padHWD
=
n
umpy
.
array
([
border_mode
,
border_mode
,
border_mode
])
padHWD
=
n
p
.
array
([
border_mode
,
border_mode
,
border_mode
])
else
:
else
:
raise
NotImplementedError
(
'Unsupported border_mode {}'
.
format
(
border_mode
))
raise
NotImplementedError
(
'Unsupported border_mode {}'
.
format
(
border_mode
))
out_shape3d
=
n
umpy
.
floor
((
img_shape3d
+
2
*
(
padHWD
)
-
dil_fil_shape3d
)
/
subsample3d
)
+
1
out_shape3d
=
n
p
.
floor
((
img_shape3d
+
2
*
(
padHWD
)
-
dil_fil_shape3d
)
/
subsample3d
)
+
1
# avoid numpy deprecation
# avoid numpy deprecation
out_shape3d
=
out_shape3d
.
astype
(
'int32'
)
out_shape3d
=
out_shape3d
.
astype
(
'int32'
)
out_shape
=
(
N_image_shape
[
0
],
N_filter_shape
[
0
])
+
tuple
(
out_shape3d
)
out_shape
=
(
N_image_shape
[
0
],
N_filter_shape
[
0
])
+
tuple
(
out_shape3d
)
ref_output
=
n
umpy
.
zeros
(
out_shape
)
ref_output
=
n
p
.
zeros
(
out_shape
)
# loop over output feature maps
# loop over output feature maps
ref_output
.
fill
(
0
)
ref_output
.
fill
(
0
)
image_data2
=
n
umpy
.
zeros
((
N_image_shape
[
0
],
N_image_shape
[
1
],
image_data2
=
n
p
.
zeros
((
N_image_shape
[
0
],
N_image_shape
[
1
],
N_image_shape
[
2
]
+
2
*
padHWD
[
0
],
N_image_shape
[
2
]
+
2
*
padHWD
[
0
],
N_image_shape
[
3
]
+
2
*
padHWD
[
1
],
N_image_shape
[
3
]
+
2
*
padHWD
[
1
],
N_image_shape
[
4
]
+
2
*
padHWD
[
2
]))
N_image_shape
[
4
]
+
2
*
padHWD
[
2
]))
image_data2
[:,
:,
image_data2
[:,
:,
padHWD
[
0
]:
padHWD
[
0
]
+
N_image_shape
[
2
],
padHWD
[
0
]:
padHWD
[
0
]
+
N_image_shape
[
2
],
padHWD
[
1
]:
padHWD
[
1
]
+
N_image_shape
[
3
],
padHWD
[
1
]:
padHWD
[
1
]
+
N_image_shape
[
3
],
...
@@ -283,7 +283,7 @@ class TestCorr3D(utt.InferShapeTester):
...
@@ -283,7 +283,7 @@ class TestCorr3D(utt.InferShapeTester):
raise
SkipTest
(
"Need cxx for this test"
)
raise
SkipTest
(
"Need cxx for this test"
)
def
rand
(
shape
,
dtype
=
'float64'
):
def
rand
(
shape
,
dtype
=
'float64'
):
r
=
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
dtype
)
r
=
n
p
.
asarray
(
np
.
random
.
rand
(
*
shape
),
dtype
=
dtype
)
return
r
*
2
-
1
return
r
*
2
-
1
ops
=
[
corr3d
.
Corr3dMM
,
corr3d
.
Corr3dMM_gradWeights
,
corr3d
.
Corr3dMM_gradInputs
]
ops
=
[
corr3d
.
Corr3dMM
,
corr3d
.
Corr3dMM_gradWeights
,
corr3d
.
Corr3dMM_gradInputs
]
...
@@ -312,7 +312,7 @@ class TestCorr3D(utt.InferShapeTester):
...
@@ -312,7 +312,7 @@ class TestCorr3D(utt.InferShapeTester):
raise
SkipTest
(
"Need cxx for this test"
)
raise
SkipTest
(
"Need cxx for this test"
)
def
rand
(
*
shape
):
def
rand
(
*
shape
):
r
=
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
'float64'
)
r
=
n
p
.
asarray
(
np
.
random
.
rand
(
*
shape
),
dtype
=
'float64'
)
return
r
*
2
-
1
return
r
*
2
-
1
corr3dMM
=
corr3d
.
Corr3dMM
corr3dMM
=
corr3d
.
Corr3dMM
...
@@ -345,7 +345,7 @@ class TestCorr3D(utt.InferShapeTester):
...
@@ -345,7 +345,7 @@ class TestCorr3D(utt.InferShapeTester):
raise
SkipTest
(
"Need cxx for this test"
)
raise
SkipTest
(
"Need cxx for this test"
)
def
rand
(
*
shape
):
def
rand
(
*
shape
):
r
=
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
'float64'
)
r
=
n
p
.
asarray
(
np
.
random
.
rand
(
*
shape
),
dtype
=
'float64'
)
return
r
*
2
-
1
return
r
*
2
-
1
corr3dMM
=
corr3d
.
Corr3dMM
corr3dMM
=
corr3d
.
Corr3dMM
gradW
=
corr3d
.
Corr3dMM_gradWeights
gradW
=
corr3d
.
Corr3dMM_gradWeights
...
@@ -386,7 +386,7 @@ class TestCorr3D(utt.InferShapeTester):
...
@@ -386,7 +386,7 @@ class TestCorr3D(utt.InferShapeTester):
raise
SkipTest
(
"Need cxx for this test"
)
raise
SkipTest
(
"Need cxx for this test"
)
def
rand
(
*
shape
):
def
rand
(
*
shape
):
r
=
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
'float64'
)
r
=
n
p
.
asarray
(
np
.
random
.
rand
(
*
shape
),
dtype
=
'float64'
)
return
r
*
2
-
1
return
r
*
2
-
1
corr3dMM
=
corr3d
.
Corr3dMM
corr3dMM
=
corr3d
.
Corr3dMM
gradI
=
corr3d
.
Corr3dMM_gradInputs
gradI
=
corr3d
.
Corr3dMM_gradInputs
...
...
theano/tensor/nnet/tests/test_neighbours.py
浏览文件 @
429a8fae
差异被折叠。
点击展开。
theano/tensor/nnet/tests/test_nnet.py
浏览文件 @
429a8fae
差异被折叠。
点击展开。
theano/tensor/nnet/tests/test_sigm.py
浏览文件 @
429a8fae
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
unittest
import
unittest
import
numpy
import
numpy
as
np
from
theano.compat
import
imap
from
theano.compat
import
imap
import
theano.tensor.inplace
import
theano.tensor.inplace
...
@@ -26,12 +26,12 @@ class T_sigmoid(unittest.TestCase):
...
@@ -26,12 +26,12 @@ class T_sigmoid(unittest.TestCase):
utt
.
seed_rng
()
utt
.
seed_rng
()
def
test_elemwise
(
self
):
def
test_elemwise
(
self
):
utt
.
verify_grad
(
sigmoid
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
sigmoid
,
[
n
p
.
random
.
rand
(
3
,
4
)])
SigmoidTester
=
makeBroadcastTester
(
SigmoidTester
=
makeBroadcastTester
(
op
=
sigmoid
,
op
=
sigmoid
,
expected
=
upcast_int8_nfunc
(
lambda
inputs
:
check_floatX
(
expected
=
upcast_int8_nfunc
(
lambda
inputs
:
check_floatX
(
inputs
,
1
/
(
1
+
n
umpy
.
exp
(
-
inputs
)))),
inputs
,
1
/
(
1
+
n
p
.
exp
(
-
inputs
)))),
good
=
copymod
(
_good_broadcast_unary_normal_no_complex
,
good
=
copymod
(
_good_broadcast_unary_normal_no_complex
,
without
=
[
'uint16'
]),
# The reason that 'uint16' is excluted is that
without
=
[
'uint16'
]),
# The reason that 'uint16' is excluted is that
# theano works well but numpy overflows resulting
# theano works well but numpy overflows resulting
...
@@ -43,7 +43,7 @@ SigmoidTester = makeBroadcastTester(
...
@@ -43,7 +43,7 @@ SigmoidTester = makeBroadcastTester(
UltraFastSigmoidTester
=
makeBroadcastTester
(
UltraFastSigmoidTester
=
makeBroadcastTester
(
op
=
ultra_fast_sigmoid
,
op
=
ultra_fast_sigmoid
,
expected
=
upcast_int8_nfunc
(
lambda
inputs
:
check_floatX
(
expected
=
upcast_int8_nfunc
(
lambda
inputs
:
check_floatX
(
inputs
,
1
/
(
1
+
n
umpy
.
exp
(
-
inputs
)))),
inputs
,
1
/
(
1
+
n
p
.
exp
(
-
inputs
)))),
good
=
copymod
(
_good_broadcast_unary_normal_no_complex
,
good
=
copymod
(
_good_broadcast_unary_normal_no_complex
,
without
=
[
'uint16'
]),
# numpy fucnting overflows with uint16.
without
=
[
'uint16'
]),
# numpy fucnting overflows with uint16.
# grad=_grad_broadcast_unary_normal,
# grad=_grad_broadcast_unary_normal,
...
@@ -54,7 +54,7 @@ UltraFastSigmoidTester = makeBroadcastTester(
...
@@ -54,7 +54,7 @@ UltraFastSigmoidTester = makeBroadcastTester(
HardSigmoidTester
=
makeBroadcastTester
(
HardSigmoidTester
=
makeBroadcastTester
(
op
=
hard_sigmoid
,
op
=
hard_sigmoid
,
expected
=
upcast_int8_nfunc
(
lambda
inputs
:
check_floatX
(
expected
=
upcast_int8_nfunc
(
lambda
inputs
:
check_floatX
(
inputs
,
1
/
(
1
+
n
umpy
.
exp
(
-
inputs
)))),
inputs
,
1
/
(
1
+
n
p
.
exp
(
-
inputs
)))),
good
=
copymod
(
_good_broadcast_unary_normal_no_complex
,
good
=
copymod
(
_good_broadcast_unary_normal_no_complex
,
without
=
[
'uint16'
]),
# numpy fucnting overflows with uint16.
without
=
[
'uint16'
]),
# numpy fucnting overflows with uint16.
# grad=_grad_broadcast_unary_normal,
# grad=_grad_broadcast_unary_normal,
...
@@ -66,11 +66,11 @@ HardSigmoidTester = makeBroadcastTester(
...
@@ -66,11 +66,11 @@ HardSigmoidTester = makeBroadcastTester(
SoftplusTester
=
makeBroadcastTester
(
SoftplusTester
=
makeBroadcastTester
(
op
=
softplus
,
op
=
softplus
,
expected
=
upcast_int8_nfunc
(
lambda
inputs
:
check_floatX
(
expected
=
upcast_int8_nfunc
(
lambda
inputs
:
check_floatX
(
inputs
,
n
umpy
.
log1p
(
numpy
.
exp
(
inputs
)))),
inputs
,
n
p
.
log1p
(
np
.
exp
(
inputs
)))),
good
=
dict
(
copymod
(
_good_broadcast_unary_normal_no_complex
,
good
=
dict
(
copymod
(
_good_broadcast_unary_normal_no_complex
,
without
=
[
'uint8'
,
'uint16'
]),
# numpy fucnting overflows with uint16.
without
=
[
'uint8'
,
'uint16'
]),
# numpy fucnting overflows with uint16.
uint8
=
[
n
umpy
.
arange
(
0
,
89
,
dtype
=
'uint8'
)],
# the range is different in new added uint8.
uint8
=
[
n
p
.
arange
(
0
,
89
,
dtype
=
'uint8'
)],
# the range is different in new added uint8.
int8
=
[
n
umpy
.
arange
(
-
127
,
89
,
dtype
=
'int8'
)]),
int8
=
[
n
p
.
arange
(
-
127
,
89
,
dtype
=
'int8'
)]),
# grad=_grad_broadcast_unary_normal,
# grad=_grad_broadcast_unary_normal,
name
=
'SoftplusTester'
,
name
=
'SoftplusTester'
,
)
)
...
@@ -81,7 +81,7 @@ class T_softplus(unittest.TestCase):
...
@@ -81,7 +81,7 @@ class T_softplus(unittest.TestCase):
utt
.
seed_rng
()
utt
.
seed_rng
()
def
test_elemwise
(
self
):
def
test_elemwise
(
self
):
utt
.
verify_grad
(
softplus
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
softplus
,
[
n
p
.
random
.
rand
(
3
,
4
)])
class
T_sigmoid_opts
(
unittest
.
TestCase
):
class
T_sigmoid_opts
(
unittest
.
TestCase
):
...
@@ -112,7 +112,7 @@ class T_sigmoid_opts(unittest.TestCase):
...
@@ -112,7 +112,7 @@ class T_sigmoid_opts(unittest.TestCase):
m
=
self
.
get_mode
(
excluding
=
[
'local_elemwise_fusion'
])
m
=
self
.
get_mode
(
excluding
=
[
'local_elemwise_fusion'
])
x
=
T
.
vector
()
x
=
T
.
vector
()
data
=
n
umpy
.
random
.
rand
(
54
)
.
astype
(
config
.
floatX
)
data
=
n
p
.
random
.
rand
(
54
)
.
astype
(
config
.
floatX
)
backup
=
config
.
warn
.
identify_1pexp_bug
backup
=
config
.
warn
.
identify_1pexp_bug
config
.
warn
.
identify_1pexp_bug
=
False
config
.
warn
.
identify_1pexp_bug
=
False
...
@@ -321,7 +321,7 @@ class T_sigmoid_opts(unittest.TestCase):
...
@@ -321,7 +321,7 @@ class T_sigmoid_opts(unittest.TestCase):
if
not
isinstance
(
mode
,
theano
.
compile
.
DebugMode
):
if
not
isinstance
(
mode
,
theano
.
compile
.
DebugMode
):
f
=
theano
.
function
([
x
,
lr
],
ux
,
mode
=
mode
)
f
=
theano
.
function
([
x
,
lr
],
ux
,
mode
=
mode
)
ux_v
=
f
([[
50
]],
0.1
)
ux_v
=
f
([[
50
]],
0.1
)
assert
not
n
umpy
.
isnan
(
ux_v
)
assert
not
n
p
.
isnan
(
ux_v
)
def
test_local_ultra_fast_sigmoid
(
self
):
def
test_local_ultra_fast_sigmoid
(
self
):
x
=
tensor
.
matrix
(
'x'
)
x
=
tensor
.
matrix
(
'x'
)
...
@@ -391,7 +391,7 @@ class T_softplus_opts(unittest.TestCase):
...
@@ -391,7 +391,7 @@ class T_softplus_opts(unittest.TestCase):
assert
isinstance
(
topo
[
1
]
.
op
.
scalar_op
,
assert
isinstance
(
topo
[
1
]
.
op
.
scalar_op
,
theano
.
tensor
.
nnet
.
sigm
.
ScalarSoftplus
)
theano
.
tensor
.
nnet
.
sigm
.
ScalarSoftplus
)
assert
isinstance
(
topo
[
2
]
.
op
.
scalar_op
,
theano
.
scalar
.
Neg
)
assert
isinstance
(
topo
[
2
]
.
op
.
scalar_op
,
theano
.
scalar
.
Neg
)
f
(
n
umpy
.
random
.
rand
(
54
)
.
astype
(
config
.
floatX
))
f
(
n
p
.
random
.
rand
(
54
)
.
astype
(
config
.
floatX
))
def
test_log1msigm_to_softplus
(
self
):
def
test_log1msigm_to_softplus
(
self
):
x
=
T
.
matrix
()
x
=
T
.
matrix
()
...
@@ -404,7 +404,7 @@ class T_softplus_opts(unittest.TestCase):
...
@@ -404,7 +404,7 @@ class T_softplus_opts(unittest.TestCase):
theano
.
tensor
.
nnet
.
sigm
.
ScalarSoftplus
)
theano
.
tensor
.
nnet
.
sigm
.
ScalarSoftplus
)
assert
isinstance
(
topo
[
1
]
.
op
.
scalar_op
,
theano
.
scalar
.
Neg
)
assert
isinstance
(
topo
[
1
]
.
op
.
scalar_op
,
theano
.
scalar
.
Neg
)
# assert check_stack_trace(f, ops_to_check='all')
# assert check_stack_trace(f, ops_to_check='all')
f
(
n
umpy
.
random
.
rand
(
54
,
11
)
.
astype
(
config
.
floatX
))
f
(
n
p
.
random
.
rand
(
54
,
11
)
.
astype
(
config
.
floatX
))
# Same test with a flatten
# Same test with a flatten
out
=
T
.
log
(
1
-
T
.
flatten
(
sigmoid
(
x
)))
out
=
T
.
log
(
1
-
T
.
flatten
(
sigmoid
(
x
)))
...
@@ -417,7 +417,7 @@ class T_softplus_opts(unittest.TestCase):
...
@@ -417,7 +417,7 @@ class T_softplus_opts(unittest.TestCase):
assert
isinstance
(
topo
[
1
]
.
op
.
scalar_op
,
assert
isinstance
(
topo
[
1
]
.
op
.
scalar_op
,
theano
.
tensor
.
nnet
.
sigm
.
ScalarSoftplus
)
theano
.
tensor
.
nnet
.
sigm
.
ScalarSoftplus
)
assert
isinstance
(
topo
[
2
]
.
op
.
scalar_op
,
theano
.
scalar
.
Neg
)
assert
isinstance
(
topo
[
2
]
.
op
.
scalar_op
,
theano
.
scalar
.
Neg
)
f
(
n
umpy
.
random
.
rand
(
54
,
11
)
.
astype
(
config
.
floatX
))
f
(
n
p
.
random
.
rand
(
54
,
11
)
.
astype
(
config
.
floatX
))
# Same test with a reshape
# Same test with a reshape
out
=
T
.
log
(
1
-
sigmoid
(
x
)
.
reshape
([
x
.
size
]))
out
=
T
.
log
(
1
-
sigmoid
(
x
)
.
reshape
([
x
.
size
]))
...
@@ -428,7 +428,7 @@ class T_softplus_opts(unittest.TestCase):
...
@@ -428,7 +428,7 @@ class T_softplus_opts(unittest.TestCase):
assert
any
(
isinstance
(
getattr
(
node
.
op
,
'scalar_op'
,
None
),
assert
any
(
isinstance
(
getattr
(
node
.
op
,
'scalar_op'
,
None
),
theano
.
tensor
.
nnet
.
sigm
.
ScalarSoftplus
)
theano
.
tensor
.
nnet
.
sigm
.
ScalarSoftplus
)
for
node
in
topo
)
for
node
in
topo
)
f
(
n
umpy
.
random
.
rand
(
54
,
11
)
.
astype
(
config
.
floatX
))
f
(
n
p
.
random
.
rand
(
54
,
11
)
.
astype
(
config
.
floatX
))
def
test_log1pexp_to_softplus
(
self
):
def
test_log1pexp_to_softplus
(
self
):
m
=
theano
.
config
.
mode
m
=
theano
.
config
.
mode
...
@@ -446,7 +446,7 @@ class T_softplus_opts(unittest.TestCase):
...
@@ -446,7 +446,7 @@ class T_softplus_opts(unittest.TestCase):
assert
len
(
topo
)
==
1
assert
len
(
topo
)
==
1
assert
isinstance
(
topo
[
0
]
.
op
.
scalar_op
,
assert
isinstance
(
topo
[
0
]
.
op
.
scalar_op
,
theano
.
tensor
.
nnet
.
sigm
.
ScalarSoftplus
)
theano
.
tensor
.
nnet
.
sigm
.
ScalarSoftplus
)
f
(
n
umpy
.
random
.
rand
(
54
)
.
astype
(
config
.
floatX
))
f
(
n
p
.
random
.
rand
(
54
)
.
astype
(
config
.
floatX
))
class
T_sigmoid_utils
(
unittest
.
TestCase
):
class
T_sigmoid_utils
(
unittest
.
TestCase
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论