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 个修改的文件
包含
367 行增加
和
368 行删除
+367
-368
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
+33
-33
test_nnet.py
theano/tensor/nnet/tests/test_nnet.py
+129
-129
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
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
unittest
import
unittest
import
theano
import
theano
...
@@ -25,7 +25,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -25,7 +25,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
for
border
in
[
'valid'
,
'ignore_borders'
]:
for
border
in
[
'valid'
,
'ignore_borders'
]:
for
dtype
in
self
.
dtypes
:
for
dtype
in
self
.
dtypes
:
images
=
shared
(
images
=
shared
(
n
umpy
.
arange
(
numpy
.
prod
(
shape
),
dtype
=
dtype
)
.
reshape
(
shape
))
n
p
.
arange
(
np
.
prod
(
shape
),
dtype
=
dtype
)
.
reshape
(
shape
))
neib_shape
=
T
.
as_tensor_variable
(
pshape
)
neib_shape
=
T
.
as_tensor_variable
(
pshape
)
f
=
function
([],
f
=
function
([],
...
@@ -42,13 +42,13 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -42,13 +42,13 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
# print g()
# print g()
assert
n
umpy
.
allclose
(
images
.
get_value
(
borrow
=
True
),
g
())
assert
n
p
.
allclose
(
images
.
get_value
(
borrow
=
True
),
g
())
def
test_neibs_manual
(
self
):
def
test_neibs_manual
(
self
):
shape
=
(
2
,
3
,
4
,
4
)
shape
=
(
2
,
3
,
4
,
4
)
for
dtype
in
self
.
dtypes
:
for
dtype
in
self
.
dtypes
:
images
=
shared
(
images
=
shared
(
n
umpy
.
arange
(
numpy
.
prod
(
shape
),
dtype
=
dtype
)
.
reshape
(
shape
))
n
p
.
arange
(
np
.
prod
(
shape
),
dtype
=
dtype
)
.
reshape
(
shape
))
neib_shape
=
T
.
as_tensor_variable
((
2
,
2
))
neib_shape
=
T
.
as_tensor_variable
((
2
,
2
))
for
border
in
[
'valid'
,
'ignore_borders'
]:
for
border
in
[
'valid'
,
'ignore_borders'
]:
...
@@ -60,7 +60,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -60,7 +60,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
# print images.get_value(borrow=True)
# print images.get_value(borrow=True)
neibs
=
f
()
neibs
=
f
()
# print neibs
# print neibs
assert
n
umpy
.
allclose
(
neibs
,
[
assert
n
p
.
allclose
(
neibs
,
[
[
0
,
1
,
4
,
5
],
[
0
,
1
,
4
,
5
],
[
2
,
3
,
6
,
7
],
[
2
,
3
,
6
,
7
],
[
8
,
9
,
12
,
13
],
[
8
,
9
,
12
,
13
],
...
@@ -88,12 +88,12 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -88,12 +88,12 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
g
=
function
([],
neibs2images
(
neibs
,
neib_shape
,
images
.
shape
),
g
=
function
([],
neibs2images
(
neibs
,
neib_shape
,
images
.
shape
),
mode
=
self
.
mode
)
mode
=
self
.
mode
)
assert
n
umpy
.
allclose
(
images
.
get_value
(
borrow
=
True
),
g
())
assert
n
p
.
allclose
(
images
.
get_value
(
borrow
=
True
),
g
())
def
test_neibs_manual_step
(
self
):
def
test_neibs_manual_step
(
self
):
shape
=
(
2
,
3
,
5
,
5
)
shape
=
(
2
,
3
,
5
,
5
)
for
dtype
in
self
.
dtypes
:
for
dtype
in
self
.
dtypes
:
images
=
shared
(
n
umpy
.
asarray
(
numpy
.
arange
(
numpy
.
prod
(
images
=
shared
(
n
p
.
asarray
(
np
.
arange
(
np
.
prod
(
shape
))
.
reshape
(
shape
),
dtype
=
dtype
))
shape
))
.
reshape
(
shape
),
dtype
=
dtype
))
neib_shape
=
T
.
as_tensor_variable
((
3
,
3
))
neib_shape
=
T
.
as_tensor_variable
((
3
,
3
))
neib_step
=
T
.
as_tensor_variable
((
2
,
2
))
neib_step
=
T
.
as_tensor_variable
((
2
,
2
))
...
@@ -107,7 +107,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -107,7 +107,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
assert
self
.
op
in
[
type
(
node
.
op
)
assert
self
.
op
in
[
type
(
node
.
op
)
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
for
node
in
f
.
maker
.
fgraph
.
toposort
()]
assert
n
umpy
.
allclose
(
neibs
,
[
assert
n
p
.
allclose
(
neibs
,
[
[
0
,
1
,
2
,
5
,
6
,
7
,
10
,
11
,
12
],
[
0
,
1
,
2
,
5
,
6
,
7
,
10
,
11
,
12
],
[
2
,
3
,
4
,
7
,
8
,
9
,
12
,
13
,
14
],
[
2
,
3
,
4
,
7
,
8
,
9
,
12
,
13
,
14
],
[
10
,
11
,
12
,
15
,
16
,
17
,
20
,
21
,
22
],
[
10
,
11
,
12
,
15
,
16
,
17
,
20
,
21
,
22
],
...
@@ -143,8 +143,8 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -143,8 +143,8 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def
test_neibs_bad_shape
(
self
):
def
test_neibs_bad_shape
(
self
):
shape
=
(
2
,
3
,
10
,
10
)
shape
=
(
2
,
3
,
10
,
10
)
for
dtype
in
self
.
dtypes
:
for
dtype
in
self
.
dtypes
:
images
=
shared
(
n
umpy
.
arange
(
images
=
shared
(
n
p
.
arange
(
n
umpy
.
prod
(
shape
),
dtype
=
dtype
)
.
reshape
(
shape
))
n
p
.
prod
(
shape
),
dtype
=
dtype
)
.
reshape
(
shape
))
for
neib_shape
in
[(
3
,
2
),
(
2
,
3
)]:
for
neib_shape
in
[(
3
,
2
),
(
2
,
3
)]:
neib_shape
=
T
.
as_tensor_variable
(
neib_shape
)
neib_shape
=
T
.
as_tensor_variable
(
neib_shape
)
...
@@ -212,11 +212,11 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -212,11 +212,11 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
for
dtype
in
self
.
dtypes
:
for
dtype
in
self
.
dtypes
:
images
=
shared
(
n
umpy
.
asarray
(
numpy
.
arange
(
numpy
.
prod
(
images
=
shared
(
n
p
.
asarray
(
np
.
arange
(
np
.
prod
(
shape
))
.
reshape
(
shape
),
dtype
=
dtype
))
shape
))
.
reshape
(
shape
),
dtype
=
dtype
))
neib_shape
=
T
.
as_tensor_variable
(
neib_shape
)
neib_shape
=
T
.
as_tensor_variable
(
neib_shape
)
neib_step
=
T
.
as_tensor_variable
(
neib_step
)
neib_step
=
T
.
as_tensor_variable
(
neib_step
)
expected
=
n
umpy
.
asarray
(
expected
)
expected
=
n
p
.
asarray
(
expected
)
f
=
function
([],
images2neibs
(
images
,
neib_shape
,
neib_step
,
f
=
function
([],
images2neibs
(
images
,
neib_shape
,
neib_step
,
mode
=
"wrap_centered"
),
mode
=
"wrap_centered"
),
...
@@ -225,7 +225,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -225,7 +225,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
if
expected
.
size
>
1
:
if
expected
.
size
>
1
:
for
i
in
range
(
shape
[
0
]
*
shape
[
1
]):
for
i
in
range
(
shape
[
0
]
*
shape
[
1
]):
assert
n
umpy
.
allclose
(
assert
n
p
.
allclose
(
neibs
[
i
*
expected
.
shape
[
0
]:(
i
+
1
)
*
expected
.
shape
[
0
],
:],
neibs
[
i
*
expected
.
shape
[
0
]:(
i
+
1
)
*
expected
.
shape
[
0
],
:],
expected
+
25
*
i
),
"wrap_centered"
expected
+
25
*
i
),
"wrap_centered"
...
@@ -240,8 +240,8 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -240,8 +240,8 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
shape
=
(
2
,
3
,
10
,
10
)
shape
=
(
2
,
3
,
10
,
10
)
for
dtype
in
self
.
dtypes
:
for
dtype
in
self
.
dtypes
:
images
=
shared
(
n
umpy
.
arange
(
images
=
shared
(
n
p
.
arange
(
n
umpy
.
prod
(
shape
),
dtype
=
dtype
n
p
.
prod
(
shape
),
dtype
=
dtype
)
.
reshape
(
shape
))
)
.
reshape
(
shape
))
for
neib_shape
in
[(
3
,
2
),
(
2
,
3
)]:
for
neib_shape
in
[(
3
,
2
),
(
2
,
3
)]:
...
@@ -253,7 +253,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -253,7 +253,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
self
.
assertRaises
(
TypeError
,
f
)
self
.
assertRaises
(
TypeError
,
f
)
for
shape
in
[(
2
,
3
,
2
,
3
),
(
2
,
3
,
3
,
2
)]:
for
shape
in
[(
2
,
3
,
2
,
3
),
(
2
,
3
,
3
,
2
)]:
images
=
shared
(
n
umpy
.
arange
(
numpy
.
prod
(
shape
))
.
reshape
(
shape
))
images
=
shared
(
n
p
.
arange
(
np
.
prod
(
shape
))
.
reshape
(
shape
))
neib_shape
=
T
.
as_tensor_variable
((
3
,
3
))
neib_shape
=
T
.
as_tensor_variable
((
3
,
3
))
f
=
function
([],
images2neibs
(
images
,
neib_shape
,
f
=
function
([],
images2neibs
(
images
,
neib_shape
,
mode
=
"wrap_centered"
),
mode
=
"wrap_centered"
),
...
@@ -262,7 +262,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -262,7 +262,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
# Test a valid shapes
# Test a valid shapes
shape
=
(
2
,
3
,
3
,
3
)
shape
=
(
2
,
3
,
3
,
3
)
images
=
shared
(
n
umpy
.
arange
(
numpy
.
prod
(
shape
))
.
reshape
(
shape
))
images
=
shared
(
n
p
.
arange
(
np
.
prod
(
shape
))
.
reshape
(
shape
))
neib_shape
=
T
.
as_tensor_variable
((
3
,
3
))
neib_shape
=
T
.
as_tensor_variable
((
3
,
3
))
f
=
function
([],
f
=
function
([],
...
@@ -273,7 +273,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -273,7 +273,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def
test_grad_wrap_centered
(
self
):
def
test_grad_wrap_centered
(
self
):
# It is not implemented for now. So test that we raise an error.
# It is not implemented for now. So test that we raise an error.
shape
=
(
2
,
3
,
6
,
6
)
shape
=
(
2
,
3
,
6
,
6
)
images_val
=
n
umpy
.
random
.
rand
(
*
shape
)
.
astype
(
'float32'
)
images_val
=
n
p
.
random
.
rand
(
*
shape
)
.
astype
(
'float32'
)
def
fn
(
images
):
def
fn
(
images
):
return
images2neibs
(
images
,
(
3
,
3
),
mode
=
'wrap_centered'
)
return
images2neibs
(
images
,
(
3
,
3
),
mode
=
'wrap_centered'
)
...
@@ -283,7 +283,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -283,7 +283,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def
test_grad_valid
(
self
):
def
test_grad_valid
(
self
):
shape
=
(
2
,
3
,
6
,
6
)
shape
=
(
2
,
3
,
6
,
6
)
images_val
=
n
umpy
.
random
.
rand
(
*
shape
)
.
astype
(
'float32'
)
images_val
=
n
p
.
random
.
rand
(
*
shape
)
.
astype
(
'float32'
)
def
fn
(
images
):
def
fn
(
images
):
return
images2neibs
(
images
,
(
2
,
2
))
return
images2neibs
(
images
,
(
2
,
2
))
...
@@ -305,7 +305,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -305,7 +305,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def
test_grad_ignore_border
(
self
):
def
test_grad_ignore_border
(
self
):
shape
=
(
2
,
3
,
5
,
5
)
shape
=
(
2
,
3
,
5
,
5
)
images_val
=
n
umpy
.
random
.
rand
(
*
shape
)
.
astype
(
'float32'
)
images_val
=
n
p
.
random
.
rand
(
*
shape
)
.
astype
(
'float32'
)
def
fn
(
images
):
def
fn
(
images
):
return
images2neibs
(
images
,
(
2
,
2
),
return
images2neibs
(
images
,
(
2
,
2
),
...
@@ -317,7 +317,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -317,7 +317,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def
test_neibs2images_grad
(
self
):
def
test_neibs2images_grad
(
self
):
# say we had images of size (2, 3, 10, 10)
# say we had images of size (2, 3, 10, 10)
# then we extracted 2x2 neighbors on this, we get (2 * 3 * 5 * 5, 4)
# then we extracted 2x2 neighbors on this, we get (2 * 3 * 5 * 5, 4)
neibs_val
=
n
umpy
.
random
.
rand
(
150
,
4
)
neibs_val
=
n
p
.
random
.
rand
(
150
,
4
)
def
fn
(
neibs
):
def
fn
(
neibs
):
return
neibs2images
(
neibs
,
(
2
,
2
),
(
2
,
3
,
10
,
10
))
return
neibs2images
(
neibs
,
(
2
,
2
),
(
2
,
3
,
10
,
10
))
...
@@ -327,8 +327,8 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -327,8 +327,8 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def
test_neibs_valid_with_inconsistent_borders
(
self
):
def
test_neibs_valid_with_inconsistent_borders
(
self
):
shape
=
(
2
,
3
,
5
,
5
)
shape
=
(
2
,
3
,
5
,
5
)
images
=
T
.
dtensor4
()
images
=
T
.
dtensor4
()
images_val
=
n
umpy
.
arange
(
numpy
.
prod
(
shape
),
images_val
=
n
p
.
arange
(
np
.
prod
(
shape
),
dtype
=
'float32'
)
.
reshape
(
shape
)
dtype
=
'float32'
)
.
reshape
(
shape
)
def
fn
(
images
):
def
fn
(
images
):
return
T
.
sum
(
T
.
sqr
(
images2neibs
(
images
,
(
2
,
2
),
mode
=
'valid'
)),
return
T
.
sum
(
T
.
sqr
(
images2neibs
(
images
,
(
2
,
2
),
mode
=
'valid'
)),
...
@@ -356,7 +356,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -356,7 +356,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
original_size
,
mode
=
mode
)
original_size
,
mode
=
mode
)
f
=
theano
.
function
([
patsRecovery
,
original_size
],
out
)
f
=
theano
.
function
([
patsRecovery
,
original_size
],
out
)
im_val
=
n
umpy
.
ones
((
1
,
3
,
320
,
320
),
dtype
=
numpy
.
float32
)
im_val
=
n
p
.
ones
((
1
,
3
,
320
,
320
),
dtype
=
np
.
float32
)
neibs
=
extractPatches
(
im_val
)
neibs
=
extractPatches
(
im_val
)
f
(
neibs
,
im_val
.
shape
)
f
(
neibs
,
im_val
.
shape
)
# Wrong number of dimensions
# Wrong number of dimensions
...
@@ -368,8 +368,8 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -368,8 +368,8 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def
speed_neibs
(
self
):
def
speed_neibs
(
self
):
shape
=
(
100
,
40
,
18
,
18
)
shape
=
(
100
,
40
,
18
,
18
)
images
=
shared
(
n
umpy
.
arange
(
numpy
.
prod
(
shape
),
images
=
shared
(
n
p
.
arange
(
np
.
prod
(
shape
),
dtype
=
'float32'
)
.
reshape
(
shape
))
dtype
=
'float32'
)
.
reshape
(
shape
))
neib_shape
=
T
.
as_tensor_variable
((
3
,
3
))
neib_shape
=
T
.
as_tensor_variable
((
3
,
3
))
f
=
function
([],
images2neibs
(
images
,
neib_shape
),
f
=
function
([],
images2neibs
(
images
,
neib_shape
),
...
@@ -380,8 +380,8 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -380,8 +380,8 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def
speed_neibs_wrap_centered
(
self
):
def
speed_neibs_wrap_centered
(
self
):
shape
=
(
100
,
40
,
18
,
18
)
shape
=
(
100
,
40
,
18
,
18
)
images
=
shared
(
n
umpy
.
arange
(
numpy
.
prod
(
shape
),
images
=
shared
(
n
p
.
arange
(
np
.
prod
(
shape
),
dtype
=
'float32'
)
.
reshape
(
shape
))
dtype
=
'float32'
)
.
reshape
(
shape
))
neib_shape
=
T
.
as_tensor_variable
((
3
,
3
))
neib_shape
=
T
.
as_tensor_variable
((
3
,
3
))
f
=
function
([],
f
=
function
([],
...
@@ -393,7 +393,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -393,7 +393,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
shape
=
(
100
,
40
,
6
,
3
)
shape
=
(
100
,
40
,
6
,
3
)
images
=
n
umpy
.
ones
(
shape
)
.
astype
(
'float32'
)
images
=
n
p
.
ones
(
shape
)
.
astype
(
'float32'
)
x
=
T
.
ftensor4
()
x
=
T
.
ftensor4
()
self
.
_compile_and_check
(
self
.
_compile_and_check
(
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
1
),
mode
=
'valid'
)],
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
1
),
mode
=
'valid'
)],
...
@@ -402,14 +402,14 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -402,14 +402,14 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
3
),
mode
=
'valid'
)],
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
3
),
mode
=
'valid'
)],
[
images
],
Images2Neibs
)
[
images
],
Images2Neibs
)
shape
=
(
100
,
40
,
5
,
4
)
shape
=
(
100
,
40
,
5
,
4
)
images
=
n
umpy
.
ones
(
shape
)
.
astype
(
'float32'
)
images
=
n
p
.
ones
(
shape
)
.
astype
(
'float32'
)
x
=
T
.
ftensor4
()
x
=
T
.
ftensor4
()
self
.
_compile_and_check
(
self
.
_compile_and_check
(
[
x
],
[
images2neibs
(
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
1
),
mode
=
'ignore_borders'
)],
x
,
neib_shape
=
(
2
,
1
),
mode
=
'ignore_borders'
)],
[
images
],
Images2Neibs
)
[
images
],
Images2Neibs
)
shape
=
(
100
,
40
,
5
,
3
)
shape
=
(
100
,
40
,
5
,
3
)
images
=
n
umpy
.
ones
(
shape
)
.
astype
(
'float32'
)
images
=
n
p
.
ones
(
shape
)
.
astype
(
'float32'
)
x
=
T
.
ftensor4
()
x
=
T
.
ftensor4
()
self
.
_compile_and_check
(
self
.
_compile_and_check
(
[
x
],
[
images2neibs
(
[
x
],
[
images2neibs
(
...
@@ -417,14 +417,14 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -417,14 +417,14 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
[
images
],
Images2Neibs
)
[
images
],
Images2Neibs
)
shape
=
(
100
,
40
,
6
,
7
)
shape
=
(
100
,
40
,
6
,
7
)
images
=
n
umpy
.
ones
(
shape
)
.
astype
(
'float32'
)
images
=
n
p
.
ones
(
shape
)
.
astype
(
'float32'
)
x
=
T
.
ftensor4
()
x
=
T
.
ftensor4
()
self
.
_compile_and_check
(
self
.
_compile_and_check
(
[
x
],
[
images2neibs
(
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
2
),
mode
=
'ignore_borders'
)],
x
,
neib_shape
=
(
2
,
2
),
mode
=
'ignore_borders'
)],
[
images
],
Images2Neibs
)
[
images
],
Images2Neibs
)
shape
=
(
100
,
40
,
5
,
10
)
shape
=
(
100
,
40
,
5
,
10
)
images
=
n
umpy
.
ones
(
shape
)
.
astype
(
'float32'
)
images
=
n
p
.
ones
(
shape
)
.
astype
(
'float32'
)
x
=
T
.
ftensor4
()
x
=
T
.
ftensor4
()
self
.
_compile_and_check
(
self
.
_compile_and_check
(
[
x
],
[
images2neibs
(
[
x
],
[
images2neibs
(
...
...
theano/tensor/nnet/tests/test_nnet.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
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
from
six.moves
import
xrange
from
six.moves
import
xrange
...
@@ -47,7 +47,7 @@ class T_sigmoid(unittest.TestCase):
...
@@ -47,7 +47,7 @@ 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
)])
class
T_softplus
(
unittest
.
TestCase
):
class
T_softplus
(
unittest
.
TestCase
):
...
@@ -56,7 +56,7 @@ class T_softplus(unittest.TestCase):
...
@@ -56,7 +56,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_Softmax
(
utt
.
InferShapeTester
):
class
T_Softmax
(
utt
.
InferShapeTester
):
...
@@ -64,26 +64,26 @@ class T_Softmax(utt.InferShapeTester):
...
@@ -64,26 +64,26 @@ class T_Softmax(utt.InferShapeTester):
def
test0
(
self
):
def
test0
(
self
):
def
f
(
a
):
def
f
(
a
):
return
softmax_op
(
a
)[:,
0
]
return
softmax_op
(
a
)[:,
0
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test1
(
self
):
def
test1
(
self
):
def
f
(
a
):
def
f
(
a
):
return
softmax_op
(
a
)[:,
1
]
return
softmax_op
(
a
)[:,
1
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test2
(
self
):
def
test2
(
self
):
def
f
(
a
):
def
f
(
a
):
return
softmax_op
(
a
)[:,
2
]
return
softmax_op
(
a
)[:,
2
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test3
(
self
):
def
test3
(
self
):
def
f
(
a
):
def
f
(
a
):
return
softmax_op
(
a
)[:,
3
]
return
softmax_op
(
a
)[:,
3
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
admat
=
matrix
()
admat
=
matrix
()
admat_val
=
n
umpy
.
random
.
rand
(
3
,
4
)
.
astype
(
config
.
floatX
)
admat_val
=
n
p
.
random
.
rand
(
3
,
4
)
.
astype
(
config
.
floatX
)
self
.
_compile_and_check
([
admat
],
[
Softmax
()(
admat
)],
self
.
_compile_and_check
([
admat
],
[
Softmax
()(
admat
)],
[
admat_val
],
Softmax
)
[
admat_val
],
Softmax
)
...
@@ -91,13 +91,13 @@ class T_Softmax(utt.InferShapeTester):
...
@@ -91,13 +91,13 @@ class T_Softmax(utt.InferShapeTester):
x
=
T
.
vector
()
x
=
T
.
vector
()
f
=
theano
.
function
([
x
],
softmax_op
(
x
))
f
=
theano
.
function
([
x
],
softmax_op
(
x
))
xv
=
n
umpy
.
random
.
randn
(
6
)
.
astype
(
config
.
floatX
)
xv
=
n
p
.
random
.
randn
(
6
)
.
astype
(
config
.
floatX
)
assert
n
umpy
.
allclose
(
f
(
xv
),
numpy
.
exp
(
xv
)
/
numpy
.
exp
(
xv
)
.
sum
())
assert
n
p
.
allclose
(
f
(
xv
),
np
.
exp
(
xv
)
/
np
.
exp
(
xv
)
.
sum
())
def
test_vector_grad
(
self
):
def
test_vector_grad
(
self
):
def
f
(
a
):
def
f
(
a
):
return
softmax_op
(
a
)
return
softmax_op
(
a
)
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
4
)])
class
T_SoftmaxWithBias
(
utt
.
InferShapeTester
):
class
T_SoftmaxWithBias
(
utt
.
InferShapeTester
):
...
@@ -105,35 +105,35 @@ class T_SoftmaxWithBias(utt.InferShapeTester):
...
@@ -105,35 +105,35 @@ class T_SoftmaxWithBias(utt.InferShapeTester):
def
test0
(
self
):
def
test0
(
self
):
def
f
(
a
,
b
):
def
f
(
a
,
b
):
return
softmax_with_bias
(
a
,
b
)[:,
0
]
return
softmax_with_bias
(
a
,
b
)[:,
0
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
),
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
),
n
umpy
.
random
.
rand
(
4
)])
n
p
.
random
.
rand
(
4
)])
def
test1
(
self
):
def
test1
(
self
):
def
f
(
a
,
b
):
def
f
(
a
,
b
):
return
softmax_with_bias
(
a
,
b
)[:,
1
]
return
softmax_with_bias
(
a
,
b
)[:,
1
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
),
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
),
n
umpy
.
random
.
rand
(
4
)])
n
p
.
random
.
rand
(
4
)])
def
test2
(
self
):
def
test2
(
self
):
def
f
(
a
,
b
):
def
f
(
a
,
b
):
return
softmax_with_bias
(
a
,
b
)[:,
2
]
return
softmax_with_bias
(
a
,
b
)[:,
2
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
),
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
),
n
umpy
.
random
.
rand
(
4
)])
n
p
.
random
.
rand
(
4
)])
def
test3
(
self
):
def
test3
(
self
):
def
f
(
a
,
b
):
def
f
(
a
,
b
):
return
softmax_with_bias
(
a
,
b
)[:,
3
]
return
softmax_with_bias
(
a
,
b
)[:,
3
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
),
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
),
n
umpy
.
random
.
rand
(
4
)])
n
p
.
random
.
rand
(
4
)])
def
test_broadcast
(
self
):
def
test_broadcast
(
self
):
# test that we don't raise an error during optimization for no good
# test that we don't raise an error during optimization for no good
# reason as softmax_with_bias don't support correctly some/all
# reason as softmax_with_bias don't support correctly some/all
# broadcasted inputs pattern
# broadcasted inputs pattern
initial_W
=
n
umpy
.
asarray
([[
0.1
,
0.1
,
0.1
],
initial_W
=
n
p
.
asarray
([[
0.1
,
0.1
,
0.1
],
[
0.1
,
0.1
,
0.1
],
[
0.1
,
0.1
,
0.1
],
[
0.1
,
0.1
,
0.1
]],
[
0.1
,
0.1
,
0.1
]],
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
W
=
theano
.
shared
(
value
=
initial_W
,
name
=
'W'
)
W
=
theano
.
shared
(
value
=
initial_W
,
name
=
'W'
)
vbias
=
theano
.
shared
(
value
=
0.1
,
name
=
'vbias'
)
# 0.01
vbias
=
theano
.
shared
(
value
=
0.1
,
name
=
'vbias'
)
# 0.01
hid
=
T
.
vector
(
'hid'
)
hid
=
T
.
vector
(
'hid'
)
...
@@ -148,8 +148,8 @@ class T_SoftmaxWithBias(utt.InferShapeTester):
...
@@ -148,8 +148,8 @@ class T_SoftmaxWithBias(utt.InferShapeTester):
def
test_softmax_with_bias_trace
(
self
):
def
test_softmax_with_bias_trace
(
self
):
a
=
theano
.
shared
(
a
=
theano
.
shared
(
n
umpy
.
random
.
randn
(
3
)
.
astype
(
config
.
floatX
))
n
p
.
random
.
randn
(
3
)
.
astype
(
config
.
floatX
))
b
=
theano
.
shared
(
n
umpy
.
float32
(
numpy
.
random
.
randn
()))
b
=
theano
.
shared
(
n
p
.
float32
(
np
.
random
.
randn
()))
sm
=
T
.
nnet
.
softmax
(
a
+
b
)
sm
=
T
.
nnet
.
softmax
(
a
+
b
)
f
=
theano
.
function
([],
sm
)
f
=
theano
.
function
([],
sm
)
assert
check_stack_trace
(
f
,
ops_to_check
=
'last'
)
assert
check_stack_trace
(
f
,
ops_to_check
=
'last'
)
...
@@ -157,8 +157,8 @@ class T_SoftmaxWithBias(utt.InferShapeTester):
...
@@ -157,8 +157,8 @@ class T_SoftmaxWithBias(utt.InferShapeTester):
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
admat
=
matrix
()
admat
=
matrix
()
advec
=
vector
()
advec
=
vector
()
admat_val
=
n
umpy
.
random
.
rand
(
3
,
4
)
.
astype
(
config
.
floatX
)
admat_val
=
n
p
.
random
.
rand
(
3
,
4
)
.
astype
(
config
.
floatX
)
advec_val
=
n
umpy
.
random
.
rand
(
4
)
.
astype
(
config
.
floatX
)
advec_val
=
n
p
.
random
.
rand
(
4
)
.
astype
(
config
.
floatX
)
self
.
_compile_and_check
([
admat
,
advec
],
self
.
_compile_and_check
([
admat
,
advec
],
[
SoftmaxWithBias
()(
admat
,
advec
)],
[
SoftmaxWithBias
()(
admat
,
advec
)],
[
admat_val
,
advec_val
],
SoftmaxWithBias
)
[
admat_val
,
advec_val
],
SoftmaxWithBias
)
...
@@ -169,40 +169,40 @@ class T_LogSoftmax(utt.InferShapeTester):
...
@@ -169,40 +169,40 @@ class T_LogSoftmax(utt.InferShapeTester):
def
test0
(
self
):
def
test0
(
self
):
def
f
(
a
):
def
f
(
a
):
return
logsoftmax_op
(
a
)[:,
0
]
return
logsoftmax_op
(
a
)[:,
0
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test1
(
self
):
def
test1
(
self
):
def
f
(
a
):
def
f
(
a
):
return
logsoftmax_op
(
a
)[:,
1
]
return
logsoftmax_op
(
a
)[:,
1
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test2
(
self
):
def
test2
(
self
):
def
f
(
a
):
def
f
(
a
):
return
logsoftmax_op
(
a
)[:,
2
]
return
logsoftmax_op
(
a
)[:,
2
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test3
(
self
):
def
test3
(
self
):
def
f
(
a
):
def
f
(
a
):
return
logsoftmax_op
(
a
)[:,
3
]
return
logsoftmax_op
(
a
)[:,
3
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test_matrix
(
self
):
def
test_matrix
(
self
):
def
f
(
a
):
def
f
(
a
):
return
logsoftmax_op
(
a
)
return
logsoftmax_op
(
a
)
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test_vector
(
self
):
def
test_vector
(
self
):
x
=
T
.
vector
()
x
=
T
.
vector
()
f
=
theano
.
function
([
x
],
logsoftmax_op
(
x
))
f
=
theano
.
function
([
x
],
logsoftmax_op
(
x
))
xv
=
n
umpy
.
random
.
randn
(
6
)
.
astype
(
config
.
floatX
)
xv
=
n
p
.
random
.
randn
(
6
)
.
astype
(
config
.
floatX
)
assert
n
umpy
.
allclose
(
f
(
xv
),
assert
n
p
.
allclose
(
f
(
xv
),
numpy
.
log
(
numpy
.
exp
(
xv
)
/
numpy
.
exp
(
xv
)
.
sum
()))
np
.
log
(
np
.
exp
(
xv
)
/
np
.
exp
(
xv
)
.
sum
()))
def
test_vector_grad
(
self
):
def
test_vector_grad
(
self
):
def
f
(
a
):
def
f
(
a
):
return
logsoftmax_op
(
a
)
return
logsoftmax_op
(
a
)
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
4
)])
def
test_allclose
(
self
):
def
test_allclose
(
self
):
m
=
theano
.
config
.
mode
m
=
theano
.
config
.
mode
...
@@ -220,9 +220,9 @@ class T_LogSoftmax(utt.InferShapeTester):
...
@@ -220,9 +220,9 @@ class T_LogSoftmax(utt.InferShapeTester):
grad
=
tensor
.
grad
(
cm2
.
mean
(),
x
)
grad
=
tensor
.
grad
(
cm2
.
mean
(),
x
)
# create some inputs into a softmax that are large and labels
# create some inputs into a softmax that are large and labels
a
=
n
umpy
.
exp
(
10
*
numpy
.
random
.
rand
(
5
,
10
)
.
astype
(
theano
.
config
.
floatX
))
a
=
n
p
.
exp
(
10
*
np
.
random
.
rand
(
5
,
10
)
.
astype
(
theano
.
config
.
floatX
))
# create some one-hot coded labels
# create some one-hot coded labels
b
=
n
umpy
.
eye
(
5
,
10
)
.
astype
(
theano
.
config
.
floatX
)
b
=
n
p
.
eye
(
5
,
10
)
.
astype
(
theano
.
config
.
floatX
)
# show equivalence of softmax and exponentiated numerically stable
# show equivalence of softmax and exponentiated numerically stable
# log-softmax
# log-softmax
...
@@ -241,7 +241,7 @@ class T_LogSoftmax(utt.InferShapeTester):
...
@@ -241,7 +241,7 @@ class T_LogSoftmax(utt.InferShapeTester):
# while in the log-softmax case they don't
# while in the log-softmax case they don't
f3
=
theano
.
function
([
x
,
y
],
[
grad
])
f3
=
theano
.
function
([
x
,
y
],
[
grad
])
grad_
=
f3
(
a
,
b
)
grad_
=
f3
(
a
,
b
)
assert
not
n
umpy
.
any
(
numpy
.
isnan
(
grad_
))
assert
not
n
p
.
any
(
np
.
isnan
(
grad_
))
def
test_isclose
(
self
):
def
test_isclose
(
self
):
def
f
(
a
):
def
f
(
a
):
...
@@ -274,8 +274,8 @@ class T_LogSoftmax(utt.InferShapeTester):
...
@@ -274,8 +274,8 @@ class T_LogSoftmax(utt.InferShapeTester):
m
.
check_isfinite
=
False
m
.
check_isfinite
=
False
# some inputs that are large to make the gradient explode in the non
# some inputs that are large to make the gradient explode in the non
# optimized case
# optimized case
a
=
n
umpy
.
exp
(
a
=
n
p
.
exp
(
10
*
n
umpy
.
random
.
rand
(
5
,
10
)
.
astype
(
theano
.
config
.
floatX
))
10
*
n
p
.
random
.
rand
(
5
,
10
)
.
astype
(
theano
.
config
.
floatX
))
def
myfunc
(
x
):
def
myfunc
(
x
):
sm
=
tensor
.
nnet
.
softmax
(
x
)
sm
=
tensor
.
nnet
.
softmax
(
x
)
...
@@ -317,8 +317,8 @@ class T_SoftmaxGrad(utt.InferShapeTester):
...
@@ -317,8 +317,8 @@ class T_SoftmaxGrad(utt.InferShapeTester):
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
admat
=
matrix
()
admat
=
matrix
()
bdmat
=
matrix
()
bdmat
=
matrix
()
admat_val
=
n
umpy
.
random
.
rand
(
3
,
4
)
.
astype
(
config
.
floatX
)
admat_val
=
n
p
.
random
.
rand
(
3
,
4
)
.
astype
(
config
.
floatX
)
bdmat_val
=
n
umpy
.
random
.
rand
(
3
,
4
)
.
astype
(
config
.
floatX
)
bdmat_val
=
n
p
.
random
.
rand
(
3
,
4
)
.
astype
(
config
.
floatX
)
self
.
_compile_and_check
([
admat
,
bdmat
],
[
SoftmaxGrad
()(
admat
,
bdmat
)],
self
.
_compile_and_check
([
admat
,
bdmat
],
[
SoftmaxGrad
()(
admat
,
bdmat
)],
[
admat_val
,
bdmat_val
],
SoftmaxGrad
)
[
admat_val
,
bdmat_val
],
SoftmaxGrad
)
...
@@ -333,29 +333,29 @@ class T_CrossentropySoftmax1Hot(unittest.TestCase):
...
@@ -333,29 +333,29 @@ class T_CrossentropySoftmax1Hot(unittest.TestCase):
def
f
(
a
,
b
):
def
f
(
a
,
b
):
return
crossentropy_softmax_1hot_with_bias
(
a
,
b
,
y_idx
)[
0
]
return
crossentropy_softmax_1hot_with_bias
(
a
,
b
,
y_idx
)[
0
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
),
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
),
n
umpy
.
random
.
rand
(
4
)])
n
p
.
random
.
rand
(
4
)])
def
test1
(
self
):
def
test1
(
self
):
y_idx
=
[
0
,
1
,
3
]
y_idx
=
[
0
,
1
,
3
]
def
f
(
a
):
def
f
(
a
):
return
crossentropy_softmax_1hot
(
a
,
y_idx
)[
0
]
return
crossentropy_softmax_1hot
(
a
,
y_idx
)[
0
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
3
,
4
)])
def
test_vector
(
self
):
def
test_vector
(
self
):
y_idx
=
[
3
]
y_idx
=
[
3
]
def
f
(
a
):
def
f
(
a
):
return
crossentropy_softmax_1hot
(
T
.
shape_padleft
(
a
),
y_idx
)[
0
]
return
crossentropy_softmax_1hot
(
T
.
shape_padleft
(
a
),
y_idx
)[
0
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
4
)])
def
test_vectors
(
self
):
def
test_vectors
(
self
):
y_idx
=
[
3
]
y_idx
=
[
3
]
def
f
(
a
,
b
):
def
f
(
a
,
b
):
return
crossentropy_softmax_1hot
(
T
.
shape_padleft
(
a
)
+
b
,
y_idx
)[
0
]
return
crossentropy_softmax_1hot
(
T
.
shape_padleft
(
a
)
+
b
,
y_idx
)[
0
]
utt
.
verify_grad
(
f
,
[
n
umpy
.
random
.
rand
(
4
),
numpy
.
random
.
rand
(
4
)])
utt
.
verify_grad
(
f
,
[
n
p
.
random
.
rand
(
4
),
np
.
random
.
rand
(
4
)])
class
T_CrossentropySoftmax1HotWithBiasDx
(
utt
.
InferShapeTester
):
class
T_CrossentropySoftmax1HotWithBiasDx
(
utt
.
InferShapeTester
):
...
@@ -364,20 +364,20 @@ class T_CrossentropySoftmax1HotWithBiasDx(utt.InferShapeTester):
...
@@ -364,20 +364,20 @@ class T_CrossentropySoftmax1HotWithBiasDx(utt.InferShapeTester):
def
ff
(
class_dtype
):
def
ff
(
class_dtype
):
def
f
(
sm
):
def
f
(
sm
):
# Class indices
# Class indices
y
=
n
umpy
.
random
.
randint
(
low
=
0
,
high
=
5
,
size
=
10
)
.
astype
(
class_dtype
)
y
=
n
p
.
random
.
randint
(
low
=
0
,
high
=
5
,
size
=
10
)
.
astype
(
class_dtype
)
return
theano
.
tensor
.
nnet
.
crossentropy_softmax_1hot_with_bias_dx
(
return
theano
.
tensor
.
nnet
.
crossentropy_softmax_1hot_with_bias_dx
(
n
umpy
.
random
.
rand
(
10
),
# Gradient w.r.t. NLL.
n
p
.
random
.
rand
(
10
),
# Gradient w.r.t. NLL.
sm
,
# Softmax output.
sm
,
# Softmax output.
y
)
y
)
return
f
return
f
# Build a random softmax output whose rows sum to 1.
# Build a random softmax output whose rows sum to 1.
softmax_output
=
n
umpy
.
random
.
rand
(
10
,
5
)
softmax_output
=
n
p
.
random
.
rand
(
10
,
5
)
softmax_output
/=
softmax_output
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
softmax_output
/=
softmax_output
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
for
dtype
in
[
'uint8'
,
'int8'
,
'uint64'
,
'int64'
]:
for
dtype
in
[
'uint8'
,
'int8'
,
'uint64'
,
'int64'
]:
utt
.
verify_grad
(
ff
(
dtype
),
[
softmax_output
])
utt
.
verify_grad
(
ff
(
dtype
),
[
softmax_output
])
def
test1
(
self
):
def
test1
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
softmax_output
=
rng
.
rand
(
10
,
5
)
softmax_output
=
rng
.
rand
(
10
,
5
)
softmax_output
/=
softmax_output
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
softmax_output
/=
softmax_output
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
...
@@ -392,7 +392,7 @@ class T_CrossentropySoftmax1HotWithBiasDx(utt.InferShapeTester):
...
@@ -392,7 +392,7 @@ class T_CrossentropySoftmax1HotWithBiasDx(utt.InferShapeTester):
admat
=
matrix
()
admat
=
matrix
()
advec
=
vector
()
advec
=
vector
()
alvec
=
lvector
()
alvec
=
lvector
()
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
admat_val
=
rng
.
rand
(
10
,
5
)
.
astype
(
config
.
floatX
)
admat_val
=
rng
.
rand
(
10
,
5
)
.
astype
(
config
.
floatX
)
admat_val
/=
admat_val
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
admat_val
/=
admat_val
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
advec_val
=
rng
.
rand
(
10
)
.
astype
(
config
.
floatX
)
advec_val
=
rng
.
rand
(
10
)
.
astype
(
config
.
floatX
)
...
@@ -407,7 +407,7 @@ class T_CrossentropySoftmax1HotWithBiasDx(utt.InferShapeTester):
...
@@ -407,7 +407,7 @@ class T_CrossentropySoftmax1HotWithBiasDx(utt.InferShapeTester):
admat
=
matrix
()
admat
=
matrix
()
advec
=
vector
()
advec
=
vector
()
alvec
=
lvector
()
alvec
=
lvector
()
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
admat_val
=
rng
.
rand
(
10
,
5
)
.
astype
(
config
.
floatX
)
admat_val
=
rng
.
rand
(
10
,
5
)
.
astype
(
config
.
floatX
)
admat_val
/=
admat_val
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
admat_val
/=
admat_val
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
advec_val
=
rng
.
rand
(
10
)
.
astype
(
config
.
floatX
)
advec_val
=
rng
.
rand
(
10
)
.
astype
(
config
.
floatX
)
...
@@ -431,28 +431,28 @@ class T_CrossentropySoftmaxArgmax1HotWithBias(utt.InferShapeTester):
...
@@ -431,28 +431,28 @@ class T_CrossentropySoftmaxArgmax1HotWithBias(utt.InferShapeTester):
# First test gradient when getting a gradient on the NLL output.
# First test gradient when getting a gradient on the NLL output.
def
grad_on_nll_dtype
(
dtype
):
def
grad_on_nll_dtype
(
dtype
):
def
grad_on_nll
(
x
,
b
):
def
grad_on_nll
(
x
,
b
):
y_idx
=
n
umpy
.
random
.
randint
(
low
=
0
,
high
=
n_classes
,
size
=
n_samples
)
.
astype
(
dtype
)
y_idx
=
n
p
.
random
.
randint
(
low
=
0
,
high
=
n_classes
,
size
=
n_samples
)
.
astype
(
dtype
)
return
self
.
op
(
x
,
b
,
y_idx
=
y_idx
)[
0
]
return
self
.
op
(
x
,
b
,
y_idx
=
y_idx
)[
0
]
return
grad_on_nll
return
grad_on_nll
for
dtype
in
[
'uint8'
,
'int8'
,
'uint64'
,
'int64'
]:
for
dtype
in
[
'uint8'
,
'int8'
,
'uint64'
,
'int64'
]:
utt
.
verify_grad
(
grad_on_nll_dtype
(
dtype
),
utt
.
verify_grad
(
grad_on_nll_dtype
(
dtype
),
[
n
umpy
.
random
.
rand
(
n_samples
,
n_classes
),
[
n
p
.
random
.
rand
(
n_samples
,
n_classes
),
n
umpy
.
random
.
rand
(
n_classes
)])
n
p
.
random
.
rand
(
n_classes
)])
# Then test gradient when getting a gradient on the softmax output.
# Then test gradient when getting a gradient on the softmax output.
def
grad_on_softmax
(
x
,
b
):
def
grad_on_softmax
(
x
,
b
):
return
self
.
op
(
x
,
b
,
y_idx
=
n
umpy
.
random
.
randint
(
return
self
.
op
(
x
,
b
,
y_idx
=
n
p
.
random
.
randint
(
low
=
0
,
high
=
n_classes
,
size
=
n_samples
))[
1
]
low
=
0
,
high
=
n_classes
,
size
=
n_samples
))[
1
]
utt
.
verify_grad
(
utt
.
verify_grad
(
grad_on_softmax
,
grad_on_softmax
,
[
n
umpy
.
random
.
rand
(
n_samples
,
n_classes
),
[
n
p
.
random
.
rand
(
n_samples
,
n_classes
),
n
umpy
.
random
.
rand
(
n_classes
)])
n
p
.
random
.
rand
(
n_classes
)])
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
admat
=
matrix
()
admat
=
matrix
()
advec
=
vector
()
advec
=
vector
()
alvec
=
lvector
()
alvec
=
lvector
()
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
admat_val
=
rng
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
admat_val
=
rng
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
advec_val
=
rng
.
rand
(
5
)
.
astype
(
config
.
floatX
)
advec_val
=
rng
.
rand
(
5
)
.
astype
(
config
.
floatX
)
alvec_val
=
rng
.
randint
(
low
=
0
,
high
=
5
,
size
=
3
)
alvec_val
=
rng
.
randint
(
low
=
0
,
high
=
5
,
size
=
3
)
...
@@ -466,7 +466,7 @@ class T_CrossentropySoftmaxArgmax1HotWithBias(utt.InferShapeTester):
...
@@ -466,7 +466,7 @@ class T_CrossentropySoftmaxArgmax1HotWithBias(utt.InferShapeTester):
admat
=
matrix
()
admat
=
matrix
()
advec
=
vector
()
advec
=
vector
()
alvec
=
lvector
()
alvec
=
lvector
()
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
admat_val
=
rng
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
admat_val
=
rng
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
advec_val
=
rng
.
rand
(
5
)
.
astype
(
config
.
floatX
)
advec_val
=
rng
.
rand
(
5
)
.
astype
(
config
.
floatX
)
alvec_val
=
rng
.
randint
(
low
=
0
,
high
=
5
,
size
=
3
)
alvec_val
=
rng
.
randint
(
low
=
0
,
high
=
5
,
size
=
3
)
...
@@ -482,27 +482,27 @@ class T_prepend(utt.InferShapeTester):
...
@@ -482,27 +482,27 @@ class T_prepend(utt.InferShapeTester):
x
=
tensor
.
matrix
(
'x'
)
x
=
tensor
.
matrix
(
'x'
)
y
=
Prepend_scalar_constant_to_each_row
(
4.
)(
x
)
y
=
Prepend_scalar_constant_to_each_row
(
4.
)(
x
)
f
=
theano
.
function
([
x
],
y
)
f
=
theano
.
function
([
x
],
y
)
m
=
n
umpy
.
random
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
m
=
n
p
.
random
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
my
=
f
(
m
)
my
=
f
(
m
)
self
.
assertTrue
(
my
.
shape
==
(
3
,
6
),
my
.
shape
)
self
.
assertTrue
(
my
.
shape
==
(
3
,
6
),
my
.
shape
)
self
.
assertTrue
(
n
umpy
.
all
(
my
[:,
0
]
==
4.0
))
self
.
assertTrue
(
n
p
.
all
(
my
[:,
0
]
==
4.0
))
def
test1
(
self
):
def
test1
(
self
):
"basic functionality"
"basic functionality"
x
=
tensor
.
matrix
(
'x'
)
x
=
tensor
.
matrix
(
'x'
)
y
=
Prepend_scalar_to_each_row
()(
5.
,
x
)
y
=
Prepend_scalar_to_each_row
()(
5.
,
x
)
f
=
theano
.
function
([
x
],
y
)
f
=
theano
.
function
([
x
],
y
)
m
=
n
umpy
.
ones
((
3
,
5
),
dtype
=
"float32"
)
m
=
n
p
.
ones
((
3
,
5
),
dtype
=
"float32"
)
my
=
f
(
m
)
my
=
f
(
m
)
self
.
assertTrue
(
my
.
shape
==
(
3
,
6
))
self
.
assertTrue
(
my
.
shape
==
(
3
,
6
))
self
.
assertTrue
(
n
umpy
.
all
(
my
[:,
0
]
==
5.0
))
self
.
assertTrue
(
n
p
.
all
(
my
[:,
0
]
==
5.0
))
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
admat
=
matrix
()
admat
=
matrix
()
adscal
=
scalar
()
adscal
=
scalar
()
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
admat_val
=
rng
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
admat_val
=
rng
.
rand
(
3
,
5
)
.
astype
(
config
.
floatX
)
adscal_val
=
n
umpy
.
asarray
(
rng
.
rand
(),
dtype
=
config
.
floatX
)
.
item
()
adscal_val
=
n
p
.
asarray
(
rng
.
rand
(),
dtype
=
config
.
floatX
)
.
item
()
self
.
_compile_and_check
(
self
.
_compile_and_check
(
[
admat
],
[
admat
],
[
Prepend_scalar_constant_to_each_row
(
adscal_val
)(
admat
)],
[
Prepend_scalar_constant_to_each_row
(
adscal_val
)(
admat
)],
...
@@ -522,7 +522,7 @@ class T_CrossentropyCategorical1HotGrad(utt.InferShapeTester):
...
@@ -522,7 +522,7 @@ class T_CrossentropyCategorical1HotGrad(utt.InferShapeTester):
advec
=
vector
()
advec
=
vector
()
admat
=
matrix
()
admat
=
matrix
()
alvec
=
lvector
()
alvec
=
lvector
()
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
advec_val
=
rng
.
rand
(
3
)
.
astype
(
config
.
floatX
)
advec_val
=
rng
.
rand
(
3
)
.
astype
(
config
.
floatX
)
admat_val
=
rng
.
rand
(
3
,
2
)
.
astype
(
config
.
floatX
)
admat_val
=
rng
.
rand
(
3
,
2
)
.
astype
(
config
.
floatX
)
alvec_val
=
[
0
,
1
,
0
]
alvec_val
=
[
0
,
1
,
0
]
...
@@ -541,21 +541,21 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
...
@@ -541,21 +541,21 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
op
=
crossentropy_categorical_1hot
op
=
crossentropy_categorical_1hot
xe
=
op
(
x
,
one_of_n
)
xe
=
op
(
x
,
one_of_n
)
f
=
theano
.
function
([
x
,
one_of_n
],
xe
)
f
=
theano
.
function
([
x
,
one_of_n
],
xe
)
x_val
=
n
umpy
.
asarray
(
x_val
=
n
p
.
asarray
(
[[
.
4
,
.
6
,
.
0
],
[
.
1
,
.
8
,
.
1
]],
[[
.
4
,
.
6
,
.
0
],
[
.
1
,
.
8
,
.
1
]],
dtype
=
config
.
floatX
)
dtype
=
config
.
floatX
)
xe_val
=
f
(
x_val
,
[
0
,
1
])
xe_val
=
f
(
x_val
,
[
0
,
1
])
assert
n
umpy
.
allclose
(
xe_val
,
-
numpy
.
log
([
.
4
,
.
8
]))
assert
n
p
.
allclose
(
xe_val
,
-
np
.
log
([
.
4
,
.
8
]))
def
oplike
(
x
):
def
oplike
(
x
):
return
op
(
x
,
[
0
,
1
])
return
op
(
x
,
[
0
,
1
])
tensor
.
verify_grad
(
oplike
,
[
x_val
],
rng
=
n
umpy
.
random
)
tensor
.
verify_grad
(
oplike
,
[
x_val
],
rng
=
n
p
.
random
)
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
admat
=
matrix
()
admat
=
matrix
()
alvec
=
lvector
()
alvec
=
lvector
()
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
admat_val
=
rng
.
rand
(
3
,
2
)
.
astype
(
config
.
floatX
)
admat_val
=
rng
.
rand
(
3
,
2
)
.
astype
(
config
.
floatX
)
alvec_val
=
[
0
,
1
,
0
]
alvec_val
=
[
0
,
1
,
0
]
self
.
_compile_and_check
(
self
.
_compile_and_check
(
...
@@ -775,10 +775,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
...
@@ -775,10 +775,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
mode
=
'FAST_RUN'
mode
=
'FAST_RUN'
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x_val
=
rng
.
randn
(
3
,
5
)
.
astype
(
config
.
floatX
)
x_val
=
rng
.
randn
(
3
,
5
)
.
astype
(
config
.
floatX
)
b_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
b_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
y_val
=
n
umpy
.
asarray
([
2
,
4
,
1
])
y_val
=
n
p
.
asarray
([
2
,
4
,
1
])
x
=
T
.
matrix
(
'x'
)
x
=
T
.
matrix
(
'x'
)
b
=
T
.
vector
(
'b'
)
b
=
T
.
vector
(
'b'
)
y
=
T
.
lvector
(
'y'
)
y
=
T
.
lvector
(
'y'
)
...
@@ -954,9 +954,9 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
...
@@ -954,9 +954,9 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
mode
=
'FAST_RUN'
mode
=
'FAST_RUN'
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x_val
=
rng
.
randn
(
3
,
5
)
.
astype
(
config
.
floatX
)
x_val
=
rng
.
randn
(
3
,
5
)
.
astype
(
config
.
floatX
)
y_val
=
n
umpy
.
asarray
([
2
,
4
,
1
],
dtype
=
'int64'
)
y_val
=
n
p
.
asarray
([
2
,
4
,
1
],
dtype
=
'int64'
)
x
=
T
.
matrix
(
'x'
)
x
=
T
.
matrix
(
'x'
)
y
=
T
.
lvector
(
'y'
)
y
=
T
.
lvector
(
'y'
)
yi
=
T
.
cast
(
y
,
'int32'
)
yi
=
T
.
cast
(
y
,
'int32'
)
...
@@ -1002,9 +1002,9 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
...
@@ -1002,9 +1002,9 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
mode
=
'FAST_RUN'
mode
=
'FAST_RUN'
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
x_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
y_val
=
n
umpy
.
asarray
([
2
])
y_val
=
n
p
.
asarray
([
2
])
x
=
T
.
vector
(
'x'
)
x
=
T
.
vector
(
'x'
)
y
=
T
.
lvector
(
'y'
)
y
=
T
.
lvector
(
'y'
)
...
@@ -1047,10 +1047,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
...
@@ -1047,10 +1047,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
mode
=
'FAST_RUN'
mode
=
'FAST_RUN'
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
x_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
b_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
b_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
y_val
=
n
umpy
.
asarray
([
2
])
y_val
=
n
p
.
asarray
([
2
])
x
=
T
.
vector
(
'x'
)
x
=
T
.
vector
(
'x'
)
b
=
T
.
vector
(
'b'
)
b
=
T
.
vector
(
'b'
)
...
@@ -1107,10 +1107,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
...
@@ -1107,10 +1107,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
mode
=
'FAST_RUN'
mode
=
'FAST_RUN'
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
x_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
b_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
b_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
y_val
=
n
umpy
.
asarray
([
2
])
y_val
=
n
p
.
asarray
([
2
])
x
=
T
.
vector
(
'x'
)
x
=
T
.
vector
(
'x'
)
b
=
T
.
vector
(
'b'
)
b
=
T
.
vector
(
'b'
)
...
@@ -1169,10 +1169,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
...
@@ -1169,10 +1169,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
mode
=
'FAST_RUN'
mode
=
'FAST_RUN'
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
x_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
b_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
b_val
=
rng
.
randn
(
5
)
.
astype
(
config
.
floatX
)
y_val
=
n
umpy
.
asarray
([
2
])
y_val
=
n
p
.
asarray
([
2
])
x
=
T
.
vector
(
'x'
)
x
=
T
.
vector
(
'x'
)
b
=
T
.
vector
(
'b'
)
b
=
T
.
vector
(
'b'
)
...
@@ -1228,9 +1228,9 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
...
@@ -1228,9 +1228,9 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
if
mode
==
theano
.
compile
.
mode
.
get_mode
(
'FAST_COMPILE'
):
mode
=
'FAST_RUN'
mode
=
'FAST_RUN'
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x_val
=
rng
.
randn
(
3
,
5
)
.
astype
(
config
.
floatX
)
x_val
=
rng
.
randn
(
3
,
5
)
.
astype
(
config
.
floatX
)
y_val
=
n
umpy
.
asarray
([
2
,
4
,
1
])
y_val
=
n
p
.
asarray
([
2
,
4
,
1
])
x
=
T
.
matrix
(
'x'
)
x
=
T
.
matrix
(
'x'
)
y
=
T
.
lvector
(
'y'
)
y
=
T
.
lvector
(
'y'
)
a
=
T
.
scalar
(
'a'
)
a
=
T
.
scalar
(
'a'
)
...
@@ -1442,21 +1442,21 @@ def test_asymptotic_32():
...
@@ -1442,21 +1442,21 @@ def test_asymptotic_32():
f
=
theano
.
function
([
x
,
y
,
x2
],
[
c
.
sum
(),
f
=
theano
.
function
([
x
,
y
,
x2
],
[
c
.
sum
(),
tensor
.
grad
(
c
.
sum
(),
x
)],
mode
=
'FAST_RUN'
)
tensor
.
grad
(
c
.
sum
(),
x
)],
mode
=
'FAST_RUN'
)
xval
=
n
umpy
.
zeros
((
5
,
5
),
dtype
=
dtype
)
.
astype
(
dtype
)
xval
=
n
p
.
zeros
((
5
,
5
),
dtype
=
dtype
)
.
astype
(
dtype
)
x2val
=
n
umpy
.
zeros
(
5
,
dtype
=
xval
.
dtype
)
.
astype
(
dtype
)
x2val
=
n
p
.
zeros
(
5
,
dtype
=
xval
.
dtype
)
.
astype
(
dtype
)
for
i
in
xrange
(
100
):
for
i
in
xrange
(
100
):
cval
,
gxval
=
f
(
xval
,
n
umpy
.
arange
(
5
),
x2val
)
cval
,
gxval
=
f
(
xval
,
n
p
.
arange
(
5
),
x2val
)
xval
-=
100.3
*
gxval
xval
-=
100.3
*
gxval
# print cval, gxval
# print cval, gxval
assert
cval
==
0
# no problem going to zero error
assert
cval
==
0
# no problem going to zero error
# what about when x gets really big?
# what about when x gets really big?
xval
=
n
umpy
.
zeros
((
5
,
5
),
dtype
=
dtype
)
xval
=
n
p
.
zeros
((
5
,
5
),
dtype
=
dtype
)
x2val
=
n
umpy
.
zeros
(
5
,
dtype
=
xval
.
dtype
)
x2val
=
n
p
.
zeros
(
5
,
dtype
=
xval
.
dtype
)
for
i
in
xrange
(
100
):
for
i
in
xrange
(
100
):
cval
,
gxval
=
f
(
xval
,
n
umpy
.
arange
(
5
),
x2val
)
cval
,
gxval
=
f
(
xval
,
n
p
.
arange
(
5
),
x2val
)
xval
+=
100000.3
*
gxval
xval
+=
100000.3
*
gxval
# print cval, gxval
# print cval, gxval
...
@@ -1477,7 +1477,7 @@ class Test_softmax_opt:
...
@@ -1477,7 +1477,7 @@ class Test_softmax_opt:
def
setUp
(
self
):
def
setUp
(
self
):
utt
.
seed_rng
()
utt
.
seed_rng
()
self
.
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
self
.
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
self
.
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
self
.
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
self
.
mode
=
self
.
mode
.
including
(
'canonicalize'
)
self
.
mode
=
self
.
mode
.
including
(
'canonicalize'
)
...
@@ -1581,7 +1581,7 @@ class Test_softmax_opt:
...
@@ -1581,7 +1581,7 @@ class Test_softmax_opt:
def
test_softmax_graph
():
def
test_softmax_graph
():
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x
=
theano
.
shared
(
rng
.
normal
(
size
=
(
3
,
4
)))
x
=
theano
.
shared
(
rng
.
normal
(
size
=
(
3
,
4
)))
def
f
(
inputs
):
def
f
(
inputs
):
...
@@ -1592,7 +1592,7 @@ def test_softmax_graph():
...
@@ -1592,7 +1592,7 @@ def test_softmax_graph():
def
test_grad_softmax_grad
():
def
test_grad_softmax_grad
():
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
x
=
theano
.
shared
(
rng
.
normal
(
size
=
(
3
,
4
)))
x
=
theano
.
shared
(
rng
.
normal
(
size
=
(
3
,
4
)))
def
f
(
inputs
):
def
f
(
inputs
):
...
@@ -1618,39 +1618,39 @@ def test_stabilize_log_softmax():
...
@@ -1618,39 +1618,39 @@ def test_stabilize_log_softmax():
# call the function so debug mode can verify the optimized
# call the function so debug mode can verify the optimized
# version matches the unoptimized version
# version matches the unoptimized version
rng
=
n
umpy
.
random
.
RandomState
([
2012
,
8
,
22
])
rng
=
n
p
.
random
.
RandomState
([
2012
,
8
,
22
])
f
(
n
umpy
.
cast
[
config
.
floatX
](
rng
.
randn
(
2
,
3
)))
f
(
n
p
.
cast
[
config
.
floatX
](
rng
.
randn
(
2
,
3
)))
def
test_relu
():
def
test_relu
():
x
=
matrix
(
'x'
)
x
=
matrix
(
'x'
)
seed
=
theano
.
tests
.
unittest_tools
.
fetch_seed
()
seed
=
theano
.
tests
.
unittest_tools
.
fetch_seed
()
rng
=
n
umpy
.
random
.
RandomState
(
seed
)
rng
=
n
p
.
random
.
RandomState
(
seed
)
X
=
rng
.
randn
(
20
,
30
)
.
astype
(
config
.
floatX
)
X
=
rng
.
randn
(
20
,
30
)
.
astype
(
config
.
floatX
)
# test the base case, without custom alpha value
# test the base case, without custom alpha value
y
=
relu
(
x
)
.
eval
({
x
:
X
})
y
=
relu
(
x
)
.
eval
({
x
:
X
})
assert
n
umpy
.
allclose
(
y
,
numpy
.
maximum
(
X
,
0
))
assert
n
p
.
allclose
(
y
,
np
.
maximum
(
X
,
0
))
# test for different constant alpha values (also outside of [0, 1])
# test for different constant alpha values (also outside of [0, 1])
for
alpha
in
0
,
0.3
,
1
,
2
,
-
0.3
,
-
1
,
-
2
:
for
alpha
in
0
,
0.3
,
1
,
2
,
-
0.3
,
-
1
,
-
2
:
y
=
relu
(
x
,
alpha
)
.
eval
({
x
:
X
})
y
=
relu
(
x
,
alpha
)
.
eval
({
x
:
X
})
assert
n
umpy
.
allclose
(
y
,
numpy
.
where
(
X
>
0
,
X
,
alpha
*
X
))
assert
n
p
.
allclose
(
y
,
np
.
where
(
X
>
0
,
X
,
alpha
*
X
))
# test for variable alpha (scalar, vector and matrix)
# test for variable alpha (scalar, vector and matrix)
for
alpha
in
scalar
(),
vector
(),
matrix
():
for
alpha
in
scalar
(),
vector
(),
matrix
():
# create value for alpha (correct ndim and broadcastable against X)
# create value for alpha (correct ndim and broadcastable against X)
A
=
n
umpy
.
array
(
rng
.
randn
(
*
X
.
shape
[::
-
1
][:
alpha
.
ndim
][::
-
1
]),
A
=
n
p
.
array
(
rng
.
randn
(
*
X
.
shape
[::
-
1
][:
alpha
.
ndim
][::
-
1
]),
dtype
=
config
.
floatX
)
dtype
=
config
.
floatX
)
y
=
relu
(
x
,
alpha
)
.
eval
({
x
:
X
,
alpha
:
A
})
y
=
relu
(
x
,
alpha
)
.
eval
({
x
:
X
,
alpha
:
A
})
assert
n
umpy
.
allclose
(
y
,
numpy
.
where
(
X
>
0
,
X
,
A
*
X
),
rtol
=
3e-5
)
assert
n
p
.
allclose
(
y
,
np
.
where
(
X
>
0
,
X
,
A
*
X
),
rtol
=
3e-5
)
# test that for alpha of ndarray don't cause upcast.
# test that for alpha of ndarray don't cause upcast.
x
=
matrix
(
'x'
,
dtype
=
'float32'
)
x
=
matrix
(
'x'
,
dtype
=
'float32'
)
rng
=
n
umpy
.
random
.
RandomState
(
seed
)
rng
=
n
p
.
random
.
RandomState
(
seed
)
X
=
rng
.
randn
(
20
,
30
)
.
astype
(
'float32'
)
X
=
rng
.
randn
(
20
,
30
)
.
astype
(
'float32'
)
alpha
=
n
umpy
.
asarray
(
.
123
,
dtype
=
'float32'
)
alpha
=
n
p
.
asarray
(
.
123
,
dtype
=
'float32'
)
y
=
relu
(
x
,
alpha
)
.
eval
({
x
:
X
})
y
=
relu
(
x
,
alpha
)
.
eval
({
x
:
X
})
assert
n
umpy
.
allclose
(
y
,
numpy
.
where
(
X
>
0
,
X
,
alpha
*
X
))
assert
n
p
.
allclose
(
y
,
np
.
where
(
X
>
0
,
X
,
alpha
*
X
))
assert
y
.
dtype
==
'float32'
assert
y
.
dtype
==
'float32'
...
@@ -1678,20 +1678,20 @@ def test_h_softmax():
...
@@ -1678,20 +1678,20 @@ def test_h_softmax():
shared
=
theano
.
shared
shared
=
theano
.
shared
# First level of h_softmax
# First level of h_softmax
W1
=
n
umpy
.
asarray
(
numpy
.
random
.
normal
(
W1
=
n
p
.
asarray
(
np
.
random
.
normal
(
size
=
(
input_size
,
h_softmax_level1_size
)),
dtype
=
floatX
)
size
=
(
input_size
,
h_softmax_level1_size
)),
dtype
=
floatX
)
W1
=
shared
(
W1
)
W1
=
shared
(
W1
)
b1
=
shared
(
n
umpy
.
asarray
(
numpy
.
zeros
((
h_softmax_level1_size
,)),
b1
=
shared
(
n
p
.
asarray
(
np
.
zeros
((
h_softmax_level1_size
,)),
dtype
=
floatX
))
dtype
=
floatX
))
# Second level of h_softmax
# Second level of h_softmax
W2
=
n
umpy
.
asarray
(
numpy
.
random
.
normal
(
W2
=
n
p
.
asarray
(
np
.
random
.
normal
(
size
=
(
h_softmax_level1_size
,
input_size
,
h_softmax_level2_size
)),
size
=
(
h_softmax_level1_size
,
input_size
,
h_softmax_level2_size
)),
dtype
=
floatX
)
dtype
=
floatX
)
W2
=
shared
(
W2
)
W2
=
shared
(
W2
)
b2
=
shared
(
b2
=
shared
(
n
umpy
.
asarray
(
numpy
.
zeros
((
h_softmax_level1_size
,
n
p
.
asarray
(
np
.
zeros
((
h_softmax_level1_size
,
h_softmax_level2_size
)),
dtype
=
floatX
))
h_softmax_level2_size
)),
dtype
=
floatX
))
#############
#############
# Build graph
# Build graph
...
@@ -1716,8 +1716,8 @@ def test_h_softmax():
...
@@ -1716,8 +1716,8 @@ def test_h_softmax():
#############
#############
# Test
# Test
#############
#############
x_mat
=
n
umpy
.
random
.
normal
(
size
=
(
batch_size
,
input_size
))
.
astype
(
floatX
)
x_mat
=
n
p
.
random
.
normal
(
size
=
(
batch_size
,
input_size
))
.
astype
(
floatX
)
y_mat
=
n
umpy
.
random
.
randint
(
0
,
output_size
,
batch_size
)
.
astype
(
'int32'
)
y_mat
=
n
p
.
random
.
randint
(
0
,
output_size
,
batch_size
)
.
astype
(
'int32'
)
tg_output
=
fun_output_tg
(
x_mat
,
y_mat
)
tg_output
=
fun_output_tg
(
x_mat
,
y_mat
)
all_outputs
=
fun_output
(
x_mat
)
all_outputs
=
fun_output
(
x_mat
)
...
@@ -1727,23 +1727,23 @@ def test_h_softmax():
...
@@ -1727,23 +1727,23 @@ def test_h_softmax():
# Verifies that the outputs computed by fun_output_tg are the same as those
# Verifies that the outputs computed by fun_output_tg are the same as those
# computed by fun_output.
# computed by fun_output.
utt
.
assert_allclose
(
utt
.
assert_allclose
(
all_outputs
[
n
umpy
.
arange
(
0
,
batch_size
),
y_mat
],
tg_output
)
all_outputs
[
n
p
.
arange
(
0
,
batch_size
),
y_mat
],
tg_output
)
def
test_elu
():
def
test_elu
():
x
=
matrix
(
'x'
)
x
=
matrix
(
'x'
)
seed
=
theano
.
tests
.
unittest_tools
.
fetch_seed
()
seed
=
theano
.
tests
.
unittest_tools
.
fetch_seed
()
rng
=
n
umpy
.
random
.
RandomState
(
seed
)
rng
=
n
p
.
random
.
RandomState
(
seed
)
X
=
rng
.
randn
(
20
,
30
)
.
astype
(
config
.
floatX
)
X
=
rng
.
randn
(
20
,
30
)
.
astype
(
config
.
floatX
)
# test the base case, without custom alpha value
# test the base case, without custom alpha value
y
=
elu
(
x
)
.
eval
({
x
:
X
})
y
=
elu
(
x
)
.
eval
({
x
:
X
})
utt
.
assert_allclose
(
y
,
n
umpy
.
where
(
X
>
0
,
X
,
numpy
.
exp
(
X
)
-
1
))
utt
.
assert_allclose
(
y
,
n
p
.
where
(
X
>
0
,
X
,
np
.
exp
(
X
)
-
1
))
# test for different constant alpha values
# test for different constant alpha values
for
alpha
in
1.5
,
2
,
-
1
,
-
1.5
,
-
2
:
for
alpha
in
1.5
,
2
,
-
1
,
-
1.5
,
-
2
:
y
=
elu
(
x
,
alpha
)
.
eval
({
x
:
X
})
y
=
elu
(
x
,
alpha
)
.
eval
({
x
:
X
})
utt
.
assert_allclose
(
y
,
n
umpy
.
where
(
X
>
0
,
X
,
alpha
*
(
numpy
.
exp
(
X
)
-
1
)))
utt
.
assert_allclose
(
y
,
n
p
.
where
(
X
>
0
,
X
,
alpha
*
(
np
.
exp
(
X
)
-
1
)))
def
test_binary_crossentropy_reshape
():
def
test_binary_crossentropy_reshape
():
...
@@ -1756,13 +1756,13 @@ def test_binary_crossentropy_reshape():
...
@@ -1756,13 +1756,13 @@ def test_binary_crossentropy_reshape():
# This only works when "specialize" options are included
# This only works when "specialize" options are included
mode
=
theano
.
compile
.
get_default_mode
()
.
including
(
'fast_run'
)
mode
=
theano
.
compile
.
get_default_mode
()
.
including
(
'fast_run'
)
fga
=
theano
.
function
([
a
],
ga
,
mode
=
mode
)
fga
=
theano
.
function
([
a
],
ga
,
mode
=
mode
)
utt
.
assert_allclose
(
fga
(
n
umpy
.
array
([[[[
30.
]]]],
dtype
=
config
.
floatX
)),
utt
.
assert_allclose
(
fga
(
n
p
.
array
([[[[
30.
]]]],
dtype
=
config
.
floatX
)),
n
umpy
.
zeros
((
1
,
1
,
1
,
1
),
dtype
=
config
.
floatX
))
n
p
.
zeros
((
1
,
1
,
1
,
1
),
dtype
=
config
.
floatX
))
SoftsignTester
=
makeBroadcastTester
(
SoftsignTester
=
makeBroadcastTester
(
op
=
softsign
,
op
=
softsign
,
expected
=
upcast_int8_nfunc
(
lambda
inputs
:
check_floatX
(
expected
=
upcast_int8_nfunc
(
lambda
inputs
:
check_floatX
(
inputs
,
inputs
/
(
1.0
+
n
umpy
.
fabs
(
inputs
)))),
inputs
,
inputs
/
(
1.0
+
n
p
.
fabs
(
inputs
)))),
good
=
_good_broadcast_unary_normal_float_no_complex
,
good
=
_good_broadcast_unary_normal_float_no_complex
,
name
=
'SoftsignTester'
,
name
=
'SoftsignTester'
,
)
)
...
@@ -1771,13 +1771,13 @@ SoftsignTester = makeBroadcastTester(
...
@@ -1771,13 +1771,13 @@ SoftsignTester = makeBroadcastTester(
def
test_confusion_matrix
():
def
test_confusion_matrix
():
# Defining numpy implementation of confusion matrix
# Defining numpy implementation of confusion matrix
def
numpy_conf_mat
(
actual
,
pred
):
def
numpy_conf_mat
(
actual
,
pred
):
order
=
n
umpy
.
union1d
(
actual
,
pred
)
order
=
n
p
.
union1d
(
actual
,
pred
)
colA
=
n
umpy
.
matrix
(
actual
)
.
T
colA
=
n
p
.
matrix
(
actual
)
.
T
colP
=
n
umpy
.
matrix
(
pred
)
.
T
colP
=
n
p
.
matrix
(
pred
)
.
T
oneHotA
=
colA
.
__eq__
(
order
)
.
astype
(
'int64'
)
oneHotA
=
colA
.
__eq__
(
order
)
.
astype
(
'int64'
)
oneHotP
=
colP
.
__eq__
(
order
)
.
astype
(
'int64'
)
oneHotP
=
colP
.
__eq__
(
order
)
.
astype
(
'int64'
)
conf_mat
=
n
umpy
.
dot
(
oneHotA
.
T
,
oneHotP
)
conf_mat
=
n
p
.
dot
(
oneHotA
.
T
,
oneHotP
)
conf_mat
=
n
umpy
.
asarray
(
conf_mat
)
conf_mat
=
n
p
.
asarray
(
conf_mat
)
return
[
conf_mat
,
order
]
return
[
conf_mat
,
order
]
x
=
tensor
.
vector
()
x
=
tensor
.
vector
()
...
@@ -1787,8 +1787,8 @@ def test_confusion_matrix():
...
@@ -1787,8 +1787,8 @@ def test_confusion_matrix():
[[
2
,
0
,
2
,
2
,
0
,
1
],
[
0
,
0
,
2
,
2
,
0
,
2
]]]
[[
2
,
0
,
2
,
2
,
0
,
1
],
[
0
,
0
,
2
,
2
,
0
,
2
]]]
for
case
in
list_inputs
:
for
case
in
list_inputs
:
a
=
n
umpy
.
asarray
(
case
[
0
])
a
=
n
p
.
asarray
(
case
[
0
])
b
=
n
umpy
.
asarray
(
case
[
1
])
b
=
n
p
.
asarray
(
case
[
1
])
out_exp
=
numpy_conf_mat
(
a
,
b
)
out_exp
=
numpy_conf_mat
(
a
,
b
)
outs
=
f
(
case
[
0
],
case
[
1
])
outs
=
f
(
case
[
0
],
case
[
1
])
for
exp
,
out
in
zip
(
out_exp
,
outs
):
for
exp
,
out
in
zip
(
out_exp
,
outs
):
...
...
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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论