Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
84cd72e6
提交
84cd72e6
authored
1月 26, 2017
作者:
Benjamin Scellier
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
file theano/gpuarray/tests/test_dnn.py
上级
f07161ed
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
117 行增加
和
117 行删除
+117
-117
test_dnn.py
theano/gpuarray/tests/test_dnn.py
+117
-117
没有找到文件。
theano/gpuarray/tests/test_dnn.py
浏览文件 @
84cd72e6
...
@@ -3,7 +3,7 @@ import logging
...
@@ -3,7 +3,7 @@ import logging
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
from
itertools
import
product
,
chain
from
itertools
import
product
,
chain
import
theano
import
theano
...
@@ -38,7 +38,7 @@ def test_dnn_conv_desc_merge():
...
@@ -38,7 +38,7 @@ def test_dnn_conv_desc_merge():
if
not
dnn
.
dnn_available
(
test_ctx_name
):
if
not
dnn
.
dnn_available
(
test_ctx_name
):
raise
SkipTest
(
dnn
.
dnn_available
.
msg
)
raise
SkipTest
(
dnn
.
dnn_available
.
msg
)
kern_shp
=
T
.
as_tensor_variable
(
kern_shp
=
T
.
as_tensor_variable
(
n
umpy
.
asarray
([
3
,
1
,
2
,
2
])
.
astype
(
'int64'
))
n
p
.
asarray
([
3
,
1
,
2
,
2
])
.
astype
(
'int64'
))
desc1
=
dnn
.
GpuDnnConvDesc
(
border_mode
=
'valid'
,
subsample
=
(
2
,
2
),
desc1
=
dnn
.
GpuDnnConvDesc
(
border_mode
=
'valid'
,
subsample
=
(
2
,
2
),
conv_mode
=
'conv'
)(
kern_shp
)
conv_mode
=
'conv'
)(
kern_shp
)
desc2
=
dnn
.
GpuDnnConvDesc
(
border_mode
=
'full'
,
subsample
=
(
1
,
1
),
desc2
=
dnn
.
GpuDnnConvDesc
(
border_mode
=
'full'
,
subsample
=
(
1
,
1
),
...
@@ -69,8 +69,8 @@ def test_dnn_conv_merge():
...
@@ -69,8 +69,8 @@ def test_dnn_conv_merge():
o1
=
dnn
.
dnn_conv
(
img
,
kern
)
o1
=
dnn
.
dnn_conv
(
img
,
kern
)
o2
=
dnn
.
dnn_conv
(
img
,
kern
)
o2
=
dnn
.
dnn_conv
(
img
,
kern
)
f
=
theano
.
function
([
img
,
kern
],
[
o1
,
o2
],
mode
=
mode_with_gpu
)
f
=
theano
.
function
([
img
,
kern
],
[
o1
,
o2
],
mode
=
mode_with_gpu
)
d1
,
d2
=
f
(
n
umpy
.
random
.
rand
(
*
img_shp
)
.
astype
(
theano
.
config
.
floatX
),
d1
,
d2
=
f
(
n
p
.
random
.
rand
(
*
img_shp
)
.
astype
(
theano
.
config
.
floatX
),
n
umpy
.
random
.
rand
(
*
kern_shp
)
.
astype
(
theano
.
config
.
floatX
))
n
p
.
random
.
rand
(
*
kern_shp
)
.
astype
(
theano
.
config
.
floatX
))
topo
=
f
.
maker
.
fgraph
.
toposort
()
topo
=
f
.
maker
.
fgraph
.
toposort
()
assert
len
([
n
for
n
in
topo
if
isinstance
(
n
.
op
,
dnn
.
GpuDnnConv
)])
==
1
assert
len
([
n
for
n
in
topo
if
isinstance
(
n
.
op
,
dnn
.
GpuDnnConv
)])
==
1
...
@@ -110,8 +110,8 @@ def test_dnn_conv_inplace():
...
@@ -110,8 +110,8 @@ def test_dnn_conv_inplace():
o1
=
dnn
.
dnn_conv
(
img
,
kern
,
conv_mode
=
'conv'
)
o1
=
dnn
.
dnn_conv
(
img
,
kern
,
conv_mode
=
'conv'
)
o2
=
dnn
.
dnn_conv
(
img
,
kern
,
conv_mode
=
'cross'
)
o2
=
dnn
.
dnn_conv
(
img
,
kern
,
conv_mode
=
'cross'
)
f
=
theano
.
function
([
img
,
kern
],
[
o1
,
o2
],
mode
=
mode_with_gpu
)
f
=
theano
.
function
([
img
,
kern
],
[
o1
,
o2
],
mode
=
mode_with_gpu
)
d1
,
d2
=
f
(
n
umpy
.
random
.
rand
(
*
img_shp
)
.
astype
(
theano
.
config
.
floatX
),
d1
,
d2
=
f
(
n
p
.
random
.
rand
(
*
img_shp
)
.
astype
(
theano
.
config
.
floatX
),
n
umpy
.
random
.
rand
(
*
kern_shp
)
.
astype
(
theano
.
config
.
floatX
))
n
p
.
random
.
rand
(
*
kern_shp
)
.
astype
(
theano
.
config
.
floatX
))
topo
=
f
.
maker
.
fgraph
.
toposort
()
topo
=
f
.
maker
.
fgraph
.
toposort
()
convs
=
[
n
for
n
in
topo
if
isinstance
(
n
.
op
,
dnn
.
GpuDnnConv
)]
convs
=
[
n
for
n
in
topo
if
isinstance
(
n
.
op
,
dnn
.
GpuDnnConv
)]
assert
len
(
convs
)
==
2
assert
len
(
convs
)
==
2
...
@@ -189,7 +189,7 @@ def test_pooling():
...
@@ -189,7 +189,7 @@ def test_pooling():
(
1
,
3
,
99
,
99
),
(
1
,
3
,
99
,
99
),
(
32
,
1
,
147
,
197
),
(
32
,
1
,
147
,
197
),
]:
]:
data
=
n
umpy
.
random
.
normal
(
0
,
1
,
shp
)
.
astype
(
theano
.
config
.
floatX
)
data
=
n
p
.
random
.
normal
(
0
,
1
,
shp
)
.
astype
(
theano
.
config
.
floatX
)
a
=
f_cpu
(
data
)
.
__array__
()
a
=
f_cpu
(
data
)
.
__array__
()
b
=
f_gpu
(
data
)
.
__array__
()
b
=
f_gpu
(
data
)
.
__array__
()
utt
.
assert_allclose
(
a
,
b
)
utt
.
assert_allclose
(
a
,
b
)
...
@@ -197,7 +197,7 @@ def test_pooling():
...
@@ -197,7 +197,7 @@ def test_pooling():
# Test the grad
# Test the grad
for
shp
in
[(
1
,
1
,
2
,
2
),
for
shp
in
[(
1
,
1
,
2
,
2
),
(
1
,
1
,
3
,
3
)]:
(
1
,
1
,
3
,
3
)]:
data
=
n
umpy
.
random
.
normal
(
0
,
1
,
shp
)
.
astype
(
theano
.
config
.
floatX
)
*
10
data
=
n
p
.
random
.
normal
(
0
,
1
,
shp
)
.
astype
(
theano
.
config
.
floatX
)
*
10
ws
=
2
ws
=
2
stride
=
2
stride
=
2
...
@@ -236,9 +236,9 @@ def test_pooling_with_tensor_vars():
...
@@ -236,9 +236,9 @@ def test_pooling_with_tensor_vars():
if
not
dnn
.
dnn_available
(
test_ctx_name
):
if
not
dnn
.
dnn_available
(
test_ctx_name
):
raise
SkipTest
(
dnn
.
dnn_available
.
msg
)
raise
SkipTest
(
dnn
.
dnn_available
.
msg
)
x
=
T
.
tensor4
()
x
=
T
.
tensor4
()
ws
=
theano
.
shared
(
n
umpy
.
array
([
2
,
2
],
dtype
=
'int32'
))
ws
=
theano
.
shared
(
n
p
.
array
([
2
,
2
],
dtype
=
'int32'
))
stride
=
theano
.
shared
(
n
umpy
.
array
([
1
,
1
],
dtype
=
'int32'
))
stride
=
theano
.
shared
(
n
p
.
array
([
1
,
1
],
dtype
=
'int32'
))
pad
=
theano
.
shared
(
n
umpy
.
array
([
0
,
0
],
dtype
=
'int32'
))
pad
=
theano
.
shared
(
n
p
.
array
([
0
,
0
],
dtype
=
'int32'
))
mode
=
'max'
mode
=
'max'
def
fn
(
x
):
def
fn
(
x
):
...
@@ -251,7 +251,7 @@ def test_pooling_with_tensor_vars():
...
@@ -251,7 +251,7 @@ def test_pooling_with_tensor_vars():
for
shp
in
[(
1
,
1
,
2
,
2
),
for
shp
in
[(
1
,
1
,
2
,
2
),
(
1
,
1
,
3
,
3
)]:
(
1
,
1
,
3
,
3
)]:
data
=
n
umpy
.
random
.
normal
(
0
,
1
,
shp
)
.
astype
(
theano
.
config
.
floatX
)
*
10
data
=
n
p
.
random
.
normal
(
0
,
1
,
shp
)
.
astype
(
theano
.
config
.
floatX
)
*
10
theano
.
tests
.
unittest_tools
.
verify_grad
(
theano
.
tests
.
unittest_tools
.
verify_grad
(
fn
,
[
data
],
mode
=
mode_with_gpu
)
fn
,
[
data
],
mode
=
mode_with_gpu
)
...
@@ -275,10 +275,10 @@ def test_pooling_with_tensor_vars():
...
@@ -275,10 +275,10 @@ def test_pooling_with_tensor_vars():
for
shp
in
[(
1
,
10
,
100
,
100
),
for
shp
in
[(
1
,
10
,
100
,
100
),
(
1
,
3
,
99
,
99
),
(
1
,
3
,
99
,
99
),
(
32
,
1
,
147
,
197
)]:
(
32
,
1
,
147
,
197
)]:
data
=
n
umpy
.
random
.
normal
(
0
,
1
,
shp
)
.
astype
(
theano
.
config
.
floatX
)
data
=
n
p
.
random
.
normal
(
0
,
1
,
shp
)
.
astype
(
theano
.
config
.
floatX
)
# Change the window size dynamically
# Change the window size dynamically
ws
.
set_value
(
n
umpy
.
array
([
i
,
i
])
.
astype
(
'int32'
))
ws
.
set_value
(
n
p
.
array
([
i
,
i
])
.
astype
(
'int32'
))
a
=
f_gpu
(
data
)
.
__array__
()
a
=
f_gpu
(
data
)
.
__array__
()
b
=
f_cpu
(
data
)
.
__array__
()
b
=
f_cpu
(
data
)
.
__array__
()
utt
.
assert_allclose
(
a
,
b
)
utt
.
assert_allclose
(
a
,
b
)
...
@@ -336,11 +336,11 @@ def test_pooling3d():
...
@@ -336,11 +336,11 @@ def test_pooling3d():
(
1
,
3
,
99
,
99
,
29
),
(
1
,
3
,
99
,
99
,
29
),
(
2
,
1
,
147
,
97
,
37
),
(
2
,
1
,
147
,
97
,
37
),
]:
]:
data
=
n
umpy
.
random
.
normal
(
0
,
1
,
shp
)
.
astype
(
theano
.
config
.
floatX
)
data
=
n
p
.
random
.
normal
(
0
,
1
,
shp
)
.
astype
(
theano
.
config
.
floatX
)
a
=
f_cpu
(
data
)
.
__array__
()
a
=
f_cpu
(
data
)
.
__array__
()
b
=
f_gpu
(
data
)
.
__array__
()
b
=
f_gpu
(
data
)
.
__array__
()
utt
.
assert_allclose
(
a
,
b
,
utt
.
assert_allclose
(
a
,
b
,
atol
=
n
umpy
.
finfo
(
theano
.
config
.
floatX
)
.
eps
)
atol
=
n
p
.
finfo
(
theano
.
config
.
floatX
)
.
eps
)
# Test the grad
# Test the grad
for
shp
in
[(
1
,
1
,
2
,
2
,
2
),
for
shp
in
[(
1
,
1
,
2
,
2
,
2
),
...
@@ -350,7 +350,7 @@ def test_pooling3d():
...
@@ -350,7 +350,7 @@ def test_pooling3d():
(
1
,
1
,
4
,
3
,
3
),
(
1
,
1
,
4
,
3
,
3
),
(
1
,
1
,
4
,
4
,
4
),
(
1
,
1
,
4
,
4
,
4
),
(
1
,
1
,
5
,
5
,
5
)]:
(
1
,
1
,
5
,
5
,
5
)]:
data
=
n
umpy
.
random
.
normal
(
0
,
1
,
shp
)
.
astype
(
theano
.
config
.
floatX
)
*
10
data
=
n
p
.
random
.
normal
(
0
,
1
,
shp
)
.
astype
(
theano
.
config
.
floatX
)
*
10
ws
=
2
ws
=
2
stride
=
2
stride
=
2
...
@@ -390,7 +390,7 @@ def test_pooling_opt():
...
@@ -390,7 +390,7 @@ def test_pooling_opt():
assert
any
([
isinstance
(
n
.
op
,
dnn
.
GpuDnnPool
)
assert
any
([
isinstance
(
n
.
op
,
dnn
.
GpuDnnPool
)
for
n
in
f
.
maker
.
fgraph
.
toposort
()])
for
n
in
f
.
maker
.
fgraph
.
toposort
()])
f
(
n
umpy
.
zeros
((
10
,
10
),
dtype
=
theano
.
config
.
floatX
))
f
(
n
p
.
zeros
((
10
,
10
),
dtype
=
theano
.
config
.
floatX
))
# gradient of 2D pooling
# gradient of 2D pooling
f
=
theano
.
function
(
f
=
theano
.
function
(
...
@@ -403,7 +403,7 @@ def test_pooling_opt():
...
@@ -403,7 +403,7 @@ def test_pooling_opt():
assert
any
([
isinstance
(
n
.
op
,
dnn
.
GpuDnnPoolGrad
)
assert
any
([
isinstance
(
n
.
op
,
dnn
.
GpuDnnPoolGrad
)
for
n
in
f
.
maker
.
fgraph
.
toposort
()])
for
n
in
f
.
maker
.
fgraph
.
toposort
()])
f
(
n
umpy
.
zeros
((
10
,
10
),
dtype
=
theano
.
config
.
floatX
))
f
(
n
p
.
zeros
((
10
,
10
),
dtype
=
theano
.
config
.
floatX
))
# Test sum pooling
# Test sum pooling
f
=
theano
.
function
(
f
=
theano
.
function
(
...
@@ -414,7 +414,7 @@ def test_pooling_opt():
...
@@ -414,7 +414,7 @@ def test_pooling_opt():
assert
any
([
isinstance
(
n
.
op
,
dnn
.
GpuDnnPool
)
assert
any
([
isinstance
(
n
.
op
,
dnn
.
GpuDnnPool
)
for
n
in
f
.
maker
.
fgraph
.
toposort
()])
for
n
in
f
.
maker
.
fgraph
.
toposort
()])
data
=
n
umpy
.
random
.
rand
(
10
,
10
)
.
astype
(
theano
.
config
.
floatX
)
data
=
n
p
.
random
.
rand
(
10
,
10
)
.
astype
(
theano
.
config
.
floatX
)
f
(
data
)
f
(
data
)
# 3D pooling
# 3D pooling
...
@@ -429,7 +429,7 @@ def test_pooling_opt():
...
@@ -429,7 +429,7 @@ def test_pooling_opt():
assert
any
([
isinstance
(
n
.
op
,
dnn
.
GpuDnnPool
)
assert
any
([
isinstance
(
n
.
op
,
dnn
.
GpuDnnPool
)
for
n
in
f
.
maker
.
fgraph
.
toposort
()])
for
n
in
f
.
maker
.
fgraph
.
toposort
()])
f
(
n
umpy
.
zeros
((
10
,
10
,
10
),
dtype
=
theano
.
config
.
floatX
))
f
(
n
p
.
zeros
((
10
,
10
,
10
),
dtype
=
theano
.
config
.
floatX
))
# gradient of 3D pooling
# gradient of 3D pooling
f
=
theano
.
function
(
f
=
theano
.
function
(
...
@@ -442,7 +442,7 @@ def test_pooling_opt():
...
@@ -442,7 +442,7 @@ def test_pooling_opt():
assert
any
([
isinstance
(
n
.
op
,
dnn
.
GpuDnnPoolGrad
)
assert
any
([
isinstance
(
n
.
op
,
dnn
.
GpuDnnPoolGrad
)
for
n
in
f
.
maker
.
fgraph
.
toposort
()])
for
n
in
f
.
maker
.
fgraph
.
toposort
()])
f
(
n
umpy
.
zeros
((
10
,
10
,
10
),
dtype
=
theano
.
config
.
floatX
))
f
(
n
p
.
zeros
((
10
,
10
,
10
),
dtype
=
theano
.
config
.
floatX
))
def
test_pooling_opt_arbitrary_dimensions
():
def
test_pooling_opt_arbitrary_dimensions
():
...
@@ -463,7 +463,7 @@ def test_pooling_opt_arbitrary_dimensions():
...
@@ -463,7 +463,7 @@ def test_pooling_opt_arbitrary_dimensions():
# create input shape: non-pooling dimensions
# create input shape: non-pooling dimensions
# followed by 2 or 3 pooling dimensions
# followed by 2 or 3 pooling dimensions
shp
=
tuple
(
range
(
2
,
2
+
n_non_pool_dims
))
+
tuple
(
range
(
5
,
5
+
len
(
ws
)))
shp
=
tuple
(
range
(
2
,
2
+
n_non_pool_dims
))
+
tuple
(
range
(
5
,
5
+
len
(
ws
)))
data
=
n
umpy
.
random
.
normal
(
0
,
1
,
shp
)
.
astype
(
theano
.
config
.
floatX
)
data
=
n
p
.
random
.
normal
(
0
,
1
,
shp
)
.
astype
(
theano
.
config
.
floatX
)
input
=
gpuarray_shared_constructor
(
data
)
input
=
gpuarray_shared_constructor
(
data
)
for
mode
in
modes
:
for
mode
in
modes
:
...
@@ -543,8 +543,8 @@ class TestDnnInferShapes(utt.InferShapeTester):
...
@@ -543,8 +543,8 @@ class TestDnnInferShapes(utt.InferShapeTester):
if
not
dnn
.
dnn_available
(
test_ctx_name
):
if
not
dnn
.
dnn_available
(
test_ctx_name
):
raise
SkipTest
(
dnn
.
dnn_available
.
msg
)
raise
SkipTest
(
dnn
.
dnn_available
.
msg
)
t
=
T
.
tensor4
(
't'
)
t
=
T
.
tensor4
(
't'
)
rand_tensor
=
n
umpy
.
asarray
(
rand_tensor
=
n
p
.
asarray
(
n
umpy
.
random
.
rand
(
5
,
4
,
3
,
2
),
n
p
.
random
.
rand
(
5
,
4
,
3
,
2
),
dtype
=
theano
.
config
.
floatX
dtype
=
theano
.
config
.
floatX
)
)
self
.
_compile_and_check
(
self
.
_compile_and_check
(
...
@@ -573,11 +573,11 @@ class TestDnnInferShapes(utt.InferShapeTester):
...
@@ -573,11 +573,11 @@ class TestDnnInferShapes(utt.InferShapeTester):
if
not
dnn
.
dnn_available
(
test_ctx_name
):
if
not
dnn
.
dnn_available
(
test_ctx_name
):
raise
SkipTest
(
dnn
.
dnn_available
.
msg
)
raise
SkipTest
(
dnn
.
dnn_available
.
msg
)
img_val
=
n
umpy
.
asarray
(
img_val
,
dtype
=
theano
.
config
.
floatX
)
img_val
=
n
p
.
asarray
(
img_val
,
dtype
=
theano
.
config
.
floatX
)
kern_vals
=
n
umpy
.
asarray
(
kern_vals
,
dtype
=
theano
.
config
.
floatX
)
kern_vals
=
n
p
.
asarray
(
kern_vals
,
dtype
=
theano
.
config
.
floatX
)
for
subsample
in
subsamples
:
for
subsample
in
subsamples
:
out_vals
=
n
umpy
.
zeros
(
out_vals
=
n
p
.
zeros
(
dnn
.
GpuDnnConv
.
get_out_shape
(
img_val
.
shape
,
kern_vals
.
shape
,
dnn
.
GpuDnnConv
.
get_out_shape
(
img_val
.
shape
,
kern_vals
.
shape
,
border_mode
=
border_mode
,
border_mode
=
border_mode
,
subsample
=
subsample
),
subsample
=
subsample
),
...
@@ -607,8 +607,8 @@ class TestDnnInferShapes(utt.InferShapeTester):
...
@@ -607,8 +607,8 @@ class TestDnnInferShapes(utt.InferShapeTester):
self
.
_test_conv
(
T
.
tensor4
(
'img'
),
self
.
_test_conv
(
T
.
tensor4
(
'img'
),
T
.
tensor4
(
'kerns'
),
T
.
tensor4
(
'kerns'
),
T
.
tensor4
(
'out'
),
T
.
tensor4
(
'out'
),
n
umpy
.
random
.
rand
(
7
,
2
,
8
,
4
),
n
p
.
random
.
rand
(
7
,
2
,
8
,
4
),
n
umpy
.
random
.
rand
(
8
,
2
,
4
,
3
),
n
p
.
random
.
rand
(
8
,
2
,
4
,
3
),
border_mode
,
border_mode
,
conv_mode
,
conv_mode
,
[(
1
,
1
),
(
2
,
2
)],
[(
1
,
1
),
(
2
,
2
)],
...
@@ -619,8 +619,8 @@ class TestDnnInferShapes(utt.InferShapeTester):
...
@@ -619,8 +619,8 @@ class TestDnnInferShapes(utt.InferShapeTester):
self
.
_test_conv
(
T
.
tensor5
(
'img'
),
self
.
_test_conv
(
T
.
tensor5
(
'img'
),
T
.
tensor5
(
'kerns'
),
T
.
tensor5
(
'kerns'
),
T
.
tensor5
(
'out'
),
T
.
tensor5
(
'out'
),
n
umpy
.
random
.
rand
(
10
,
2
,
6
,
4
,
11
),
n
p
.
random
.
rand
(
10
,
2
,
6
,
4
,
11
),
n
umpy
.
random
.
rand
(
8
,
2
,
4
,
3
,
1
),
n
p
.
random
.
rand
(
8
,
2
,
4
,
3
,
1
),
border_mode
,
border_mode
,
conv_mode
,
conv_mode
,
[(
1
,
1
,
1
),
(
2
,
2
,
2
)],
[(
1
,
1
,
1
),
(
2
,
2
,
2
)],
...
@@ -633,17 +633,17 @@ class TestDnnInferShapes(utt.InferShapeTester):
...
@@ -633,17 +633,17 @@ class TestDnnInferShapes(utt.InferShapeTester):
topgrad_shape
=
get_conv_output_shape
(
img_shape
,
kerns_shape
,
topgrad_shape
=
get_conv_output_shape
(
img_shape
,
kerns_shape
,
border_mode
,
subsample
)
border_mode
,
subsample
)
img_val
=
n
umpy
.
asarray
(
img_val
=
n
p
.
asarray
(
n
umpy
.
random
.
rand
(
*
img_shape
),
n
p
.
random
.
rand
(
*
img_shape
),
dtype
=
theano
.
config
.
floatX
dtype
=
theano
.
config
.
floatX
)
)
topgrad_vals
=
n
umpy
.
asarray
(
topgrad_vals
=
n
p
.
asarray
(
n
umpy
.
random
.
rand
(
*
topgrad_shape
),
n
p
.
random
.
rand
(
*
topgrad_shape
),
dtype
=
theano
.
config
.
floatX
dtype
=
theano
.
config
.
floatX
)
)
kerns_vals
=
n
umpy
.
zeros
(
kerns_shape
,
dtype
=
theano
.
config
.
floatX
)
kerns_vals
=
n
p
.
zeros
(
kerns_shape
,
dtype
=
theano
.
config
.
floatX
)
kerns_shape
=
theano
.
shared
(
n
umpy
.
asarray
(
kerns_shape
))
kerns_shape
=
theano
.
shared
(
n
p
.
asarray
(
kerns_shape
))
desc
=
dnn
.
GpuDnnConvDesc
(
desc
=
dnn
.
GpuDnnConvDesc
(
border_mode
=
border_mode
,
border_mode
=
border_mode
,
subsample
=
subsample
,
subsample
=
subsample
,
...
@@ -680,12 +680,12 @@ class TestDnnInferShapes(utt.InferShapeTester):
...
@@ -680,12 +680,12 @@ class TestDnnInferShapes(utt.InferShapeTester):
img
=
T
.
tensor4
(
'img'
)
img
=
T
.
tensor4
(
'img'
)
kerns
=
T
.
tensor4
(
'kerns'
)
kerns
=
T
.
tensor4
(
'kerns'
)
out
=
T
.
tensor4
(
'out'
)
out
=
T
.
tensor4
(
'out'
)
kern_vals
=
n
umpy
.
asarray
(
kern_vals
=
n
p
.
asarray
(
n
umpy
.
random
.
rand
(
13
,
14
,
15
,
16
),
n
p
.
random
.
rand
(
13
,
14
,
15
,
16
),
dtype
=
theano
.
config
.
floatX
dtype
=
theano
.
config
.
floatX
)
)
out_vals
=
n
umpy
.
asarray
(
out_vals
=
n
p
.
asarray
(
n
umpy
.
random
.
rand
(
3
,
13
,
5
,
6
),
n
p
.
random
.
rand
(
3
,
13
,
5
,
6
),
dtype
=
theano
.
config
.
floatX
dtype
=
theano
.
config
.
floatX
)
)
...
@@ -699,7 +699,7 @@ class TestDnnInferShapes(utt.InferShapeTester):
...
@@ -699,7 +699,7 @@ class TestDnnInferShapes(utt.InferShapeTester):
out_vals
.
shape
[
2
]
+
kern_vals
.
shape
[
2
]
-
1
,
out_vals
.
shape
[
2
]
+
kern_vals
.
shape
[
2
]
-
1
,
out_vals
.
shape
[
3
]
+
kern_vals
.
shape
[
3
]
-
1
out_vals
.
shape
[
3
]
+
kern_vals
.
shape
[
3
]
-
1
)
)
img_vals
=
n
umpy
.
zeros
(
shape
,
dtype
=
theano
.
config
.
floatX
)
img_vals
=
n
p
.
zeros
(
shape
,
dtype
=
theano
.
config
.
floatX
)
desc
=
dnn
.
GpuDnnConvDesc
(
desc
=
dnn
.
GpuDnnConvDesc
(
border_mode
=
params
[
0
],
border_mode
=
params
[
0
],
subsample
=
params
[
1
],
subsample
=
params
[
1
],
...
@@ -723,8 +723,8 @@ class TestDnnInferShapes(utt.InferShapeTester):
...
@@ -723,8 +723,8 @@ class TestDnnInferShapes(utt.InferShapeTester):
if
not
dnn
.
dnn_available
(
test_ctx_name
):
if
not
dnn
.
dnn_available
(
test_ctx_name
):
raise
SkipTest
(
dnn
.
dnn_available
.
msg
)
raise
SkipTest
(
dnn
.
dnn_available
.
msg
)
img
=
T
.
tensor4
(
'img'
)
img
=
T
.
tensor4
(
'img'
)
img_val
=
n
umpy
.
asarray
(
img_val
=
n
p
.
asarray
(
n
umpy
.
random
.
rand
(
2
,
3
,
4
,
5
),
n
p
.
random
.
rand
(
2
,
3
,
4
,
5
),
dtype
=
theano
.
config
.
floatX
dtype
=
theano
.
config
.
floatX
)
)
...
@@ -750,8 +750,8 @@ class TestDnnInferShapes(utt.InferShapeTester):
...
@@ -750,8 +750,8 @@ class TestDnnInferShapes(utt.InferShapeTester):
if
not
dnn
.
dnn_available
(
test_ctx_name
):
if
not
dnn
.
dnn_available
(
test_ctx_name
):
raise
SkipTest
(
dnn
.
dnn_available
.
msg
)
raise
SkipTest
(
dnn
.
dnn_available
.
msg
)
img
=
T
.
tensor5
(
'img'
)
img
=
T
.
tensor5
(
'img'
)
img_val
=
n
umpy
.
asarray
(
img_val
=
n
p
.
asarray
(
n
umpy
.
random
.
rand
(
2
,
3
,
4
,
5
,
6
),
n
p
.
random
.
rand
(
2
,
3
,
4
,
5
,
6
),
dtype
=
theano
.
config
.
floatX
dtype
=
theano
.
config
.
floatX
)
)
...
@@ -779,16 +779,16 @@ class TestDnnInferShapes(utt.InferShapeTester):
...
@@ -779,16 +779,16 @@ class TestDnnInferShapes(utt.InferShapeTester):
img
=
T
.
tensor4
(
'img'
)
img
=
T
.
tensor4
(
'img'
)
img_grad
=
T
.
tensor4
(
'img_grad'
)
img_grad
=
T
.
tensor4
(
'img_grad'
)
out
=
T
.
tensor4
(
'out'
)
out
=
T
.
tensor4
(
'out'
)
img_val
=
n
umpy
.
asarray
(
img_val
=
n
p
.
asarray
(
n
umpy
.
random
.
rand
(
2
,
3
,
4
,
5
),
n
p
.
random
.
rand
(
2
,
3
,
4
,
5
),
dtype
=
theano
.
config
.
floatX
dtype
=
theano
.
config
.
floatX
)
)
img_grad_val
=
n
umpy
.
asarray
(
img_grad_val
=
n
p
.
asarray
(
n
umpy
.
random
.
rand
(
2
,
3
,
4
,
5
),
n
p
.
random
.
rand
(
2
,
3
,
4
,
5
),
dtype
=
theano
.
config
.
floatX
dtype
=
theano
.
config
.
floatX
)
)
out_val
=
n
umpy
.
asarray
(
out_val
=
n
p
.
asarray
(
n
umpy
.
random
.
rand
(
2
,
3
,
4
,
5
),
n
p
.
random
.
rand
(
2
,
3
,
4
,
5
),
dtype
=
theano
.
config
.
floatX
dtype
=
theano
.
config
.
floatX
)
)
...
@@ -818,16 +818,16 @@ class TestDnnInferShapes(utt.InferShapeTester):
...
@@ -818,16 +818,16 @@ class TestDnnInferShapes(utt.InferShapeTester):
img
=
T
.
tensor5
(
'img'
)
img
=
T
.
tensor5
(
'img'
)
img_grad
=
T
.
tensor5
(
'img_grad'
)
img_grad
=
T
.
tensor5
(
'img_grad'
)
out
=
T
.
tensor5
(
'out'
)
out
=
T
.
tensor5
(
'out'
)
img_val
=
n
umpy
.
asarray
(
img_val
=
n
p
.
asarray
(
n
umpy
.
random
.
rand
(
2
,
3
,
4
,
5
,
6
),
n
p
.
random
.
rand
(
2
,
3
,
4
,
5
,
6
),
dtype
=
theano
.
config
.
floatX
dtype
=
theano
.
config
.
floatX
)
)
img_grad_val
=
n
umpy
.
asarray
(
img_grad_val
=
n
p
.
asarray
(
n
umpy
.
random
.
rand
(
2
,
3
,
4
,
5
,
6
),
n
p
.
random
.
rand
(
2
,
3
,
4
,
5
,
6
),
dtype
=
theano
.
config
.
floatX
dtype
=
theano
.
config
.
floatX
)
)
out_val
=
n
umpy
.
asarray
(
out_val
=
n
p
.
asarray
(
n
umpy
.
random
.
rand
(
2
,
3
,
4
,
5
,
6
),
n
p
.
random
.
rand
(
2
,
3
,
4
,
5
,
6
),
dtype
=
theano
.
config
.
floatX
dtype
=
theano
.
config
.
floatX
)
)
...
@@ -880,16 +880,16 @@ def test_dnn_conv_alpha_output_merge():
...
@@ -880,16 +880,16 @@ def test_dnn_conv_alpha_output_merge():
iw
=
8
iw
=
8
kh
=
2
kh
=
2
kw
=
6
kw
=
6
img_val
=
n
umpy
.
random
.
random
((
b
,
c
,
ih
,
iw
))
.
astype
(
theano
.
config
.
floatX
)
img_val
=
n
p
.
random
.
random
((
b
,
c
,
ih
,
iw
))
.
astype
(
theano
.
config
.
floatX
)
kern_val
=
n
umpy
.
random
.
random
((
f
,
c
,
kh
,
kw
))
.
astype
(
theano
.
config
.
floatX
)
kern_val
=
n
p
.
random
.
random
((
f
,
c
,
kh
,
kw
))
.
astype
(
theano
.
config
.
floatX
)
out_val
=
n
umpy
.
random
.
random
((
b
,
f
,
ih
-
kh
+
1
,
out_val
=
n
p
.
random
.
random
((
b
,
f
,
ih
-
kh
+
1
,
iw
-
kw
+
1
))
.
astype
(
theano
.
config
.
floatX
)
iw
-
kw
+
1
))
.
astype
(
theano
.
config
.
floatX
)
conv
=
dnn
.
dnn_conv
(
img
,
kern
)
conv
=
dnn
.
dnn_conv
(
img
,
kern
)
gw
=
theano
.
grad
(
conv
.
sum
(),
kern
)
gw
=
theano
.
grad
(
conv
.
sum
(),
kern
)
gi
=
theano
.
grad
(
conv
.
sum
(),
img
)
gi
=
theano
.
grad
(
conv
.
sum
(),
img
)
lr
=
n
umpy
.
asarray
(
0.05
,
dtype
=
theano
.
config
.
floatX
)
lr
=
n
p
.
asarray
(
0.05
,
dtype
=
theano
.
config
.
floatX
)
fr
=
lr
*
(
conv
+
out
)
fr
=
lr
*
(
conv
+
out
)
wr
=
kern
+
lr
*
gw
wr
=
kern
+
lr
*
gw
...
@@ -939,10 +939,10 @@ def test_dnn_conv_grad():
...
@@ -939,10 +939,10 @@ def test_dnn_conv_grad():
iw
=
8
iw
=
8
kh
=
2
kh
=
2
kw
=
2
kw
=
2
img_val
=
n
umpy
.
random
.
random
((
b
,
c
,
ih
,
iw
))
.
astype
(
theano
.
config
.
floatX
)
img_val
=
n
p
.
random
.
random
((
b
,
c
,
ih
,
iw
))
.
astype
(
theano
.
config
.
floatX
)
kern_val
=
n
umpy
.
random
.
random
((
f
,
c
,
kh
,
kw
))
.
astype
(
theano
.
config
.
floatX
)
kern_val
=
n
p
.
random
.
random
((
f
,
c
,
kh
,
kw
))
.
astype
(
theano
.
config
.
floatX
)
out_val
=
n
umpy
.
random
.
random
((
b
,
f
,
ih
-
kw
+
1
,
out_val
=
n
p
.
random
.
random
((
b
,
f
,
ih
-
kw
+
1
,
iw
-
kw
+
1
))
.
astype
(
theano
.
config
.
floatX
)
iw
-
kw
+
1
))
.
astype
(
theano
.
config
.
floatX
)
def
dconv
(
img
,
kern
,
out
):
def
dconv
(
img
,
kern
,
out
):
desc
=
dnn
.
GpuDnnConvDesc
(
border_mode
=
'valid'
,
subsample
=
(
1
,
1
),
desc
=
dnn
.
GpuDnnConvDesc
(
border_mode
=
'valid'
,
subsample
=
(
1
,
1
),
...
@@ -1008,8 +1008,8 @@ def test_conv3d_fwd():
...
@@ -1008,8 +1008,8 @@ def test_conv3d_fwd():
def
run_conv3d_fwd
(
inputs_shape
,
filters_shape
,
subsample
,
def
run_conv3d_fwd
(
inputs_shape
,
filters_shape
,
subsample
,
border_mode
,
conv_mode
):
border_mode
,
conv_mode
):
inputs_val
=
n
umpy
.
random
.
random
(
inputs_shape
)
.
astype
(
theano
.
config
.
floatX
)
inputs_val
=
n
p
.
random
.
random
(
inputs_shape
)
.
astype
(
theano
.
config
.
floatX
)
filters_val
=
n
umpy
.
random
.
random
(
filters_shape
)
.
astype
(
theano
.
config
.
floatX
)
filters_val
=
n
p
.
random
.
random
(
filters_shape
)
.
astype
(
theano
.
config
.
floatX
)
# Scale down the input values to prevent very large absolute errors
# Scale down the input values to prevent very large absolute errors
# due to float rounding
# due to float rounding
...
@@ -1062,8 +1062,8 @@ def test_conv3d_bwd():
...
@@ -1062,8 +1062,8 @@ def test_conv3d_bwd():
def
run_conv3d_bwd
(
inputs_shape
,
filters_shape
,
subsample
,
def
run_conv3d_bwd
(
inputs_shape
,
filters_shape
,
subsample
,
border_mode
,
conv_mode
):
border_mode
,
conv_mode
):
inputs_val
=
n
umpy
.
random
.
random
(
inputs_shape
)
.
astype
(
theano
.
config
.
floatX
)
inputs_val
=
n
p
.
random
.
random
(
inputs_shape
)
.
astype
(
theano
.
config
.
floatX
)
filters_val
=
n
umpy
.
random
.
random
(
filters_shape
)
.
astype
(
theano
.
config
.
floatX
)
filters_val
=
n
p
.
random
.
random
(
filters_shape
)
.
astype
(
theano
.
config
.
floatX
)
inputs
=
theano
.
shared
(
inputs_val
)
inputs
=
theano
.
shared
(
inputs_val
)
filters
=
theano
.
shared
(
filters_val
)
filters
=
theano
.
shared
(
filters_val
)
...
@@ -1148,8 +1148,8 @@ class test_SoftMax(test_nnet.test_SoftMax):
...
@@ -1148,8 +1148,8 @@ class test_SoftMax(test_nnet.test_SoftMax):
def
test_softmax_shape_0
(
self
):
def
test_softmax_shape_0
(
self
):
dims
=
(
2
,
0
,
4
,
5
)
dims
=
(
2
,
0
,
4
,
5
)
data
=
n
umpy
.
arange
(
data
=
n
p
.
arange
(
n
umpy
.
product
(
dims
),
n
p
.
product
(
dims
),
dtype
=
theano
.
config
.
floatX
dtype
=
theano
.
config
.
floatX
)
.
reshape
(
dims
)
)
.
reshape
(
dims
)
...
@@ -1176,22 +1176,22 @@ class test_SoftMax(test_nnet.test_SoftMax):
...
@@ -1176,22 +1176,22 @@ class test_SoftMax(test_nnet.test_SoftMax):
)
)
def
cmp
(
n
,
m
,
f
,
f_gpu
):
def
cmp
(
n
,
m
,
f
,
f_gpu
):
data
=
n
umpy
.
random
.
random
((
n
,
m
))
.
astype
(
'float16'
)
data
=
n
p
.
random
.
random
((
n
,
m
))
.
astype
(
'float16'
)
gdata
=
n
umpy
.
asarray
(
data
)[:,
:,
None
,
None
]
gdata
=
n
p
.
asarray
(
data
)[:,
:,
None
,
None
]
out
=
f
(
data
)
out
=
f
(
data
)
gout
=
n
umpy
.
asarray
(
f_gpu
(
gdata
))[:,
:,
0
,
0
]
gout
=
n
p
.
asarray
(
f_gpu
(
gdata
))[:,
:,
0
,
0
]
utt
.
assert_allclose
(
out
,
gout
)
utt
.
assert_allclose
(
out
,
gout
)
self
.
_test_softmax
(
x
,
x_gpu
,
f_z
,
f_gpu
,
cmp
)
self
.
_test_softmax
(
x
,
x_gpu
,
f_z
,
f_gpu
,
cmp
)
def
test_softmax_grad
(
self
):
def
test_softmax_grad
(
self
):
def
cmp
(
n
,
m
,
f
,
f_gpu
):
def
cmp
(
n
,
m
,
f
,
f_gpu
):
data
=
n
umpy
.
arange
(
n
*
m
,
dtype
=
theano
.
config
.
floatX
)
.
reshape
(
n
,
m
)
data
=
n
p
.
arange
(
n
*
m
,
dtype
=
theano
.
config
.
floatX
)
.
reshape
(
n
,
m
)
gdata
=
n
umpy
.
asarray
(
data
)[:,
:,
None
,
None
]
gdata
=
n
p
.
asarray
(
data
)[:,
:,
None
,
None
]
out
=
f
(
data
)
out
=
f
(
data
)
gout
=
n
umpy
.
asarray
(
f_gpu
(
gdata
))[:,
:,
0
,
0
]
gout
=
n
p
.
asarray
(
f_gpu
(
gdata
))[:,
:,
0
,
0
]
utt
.
assert_allclose
(
out
,
gout
)
utt
.
assert_allclose
(
out
,
gout
)
x
=
T
.
matrix
(
'x'
)
x
=
T
.
matrix
(
'x'
)
...
@@ -1204,11 +1204,11 @@ class test_SoftMax(test_nnet.test_SoftMax):
...
@@ -1204,11 +1204,11 @@ class test_SoftMax(test_nnet.test_SoftMax):
# Verify the grad operation
# Verify the grad operation
dims
=
(
2
,
3
,
4
,
5
)
dims
=
(
2
,
3
,
4
,
5
)
gdata
=
n
umpy
.
arange
(
gdata
=
n
p
.
arange
(
n
umpy
.
product
(
dims
),
n
p
.
product
(
dims
),
dtype
=
theano
.
config
.
floatX
dtype
=
theano
.
config
.
floatX
)
.
reshape
(
dims
)
)
.
reshape
(
dims
)
T
.
verify_grad
(
f_gpu
,
[
gdata
],
rng
=
n
umpy
.
random
,
T
.
verify_grad
(
f_gpu
,
[
gdata
],
rng
=
n
p
.
random
,
mode
=
mode_with_gpu
)
mode
=
mode_with_gpu
)
# Verify that the CPU and GPU implementations return the same results
# Verify that the CPU and GPU implementations return the same results
...
@@ -1235,7 +1235,7 @@ class test_SoftMax(test_nnet.test_SoftMax):
...
@@ -1235,7 +1235,7 @@ class test_SoftMax(test_nnet.test_SoftMax):
mode
=
mode_with_gpu
mode
=
mode_with_gpu
)
)
sorted_f
=
f
.
maker
.
fgraph
.
toposort
()
sorted_f
=
f
.
maker
.
fgraph
.
toposort
()
val
=
n
umpy
.
random
.
rand
(
5
)
.
astype
(
theano
.
config
.
floatX
)
val
=
n
p
.
random
.
rand
(
5
)
.
astype
(
theano
.
config
.
floatX
)
out_dnn
=
f
(
val
)
out_dnn
=
f
(
val
)
assert
(
len
([
i
assert
(
len
([
i
for
i
in
sorted_f
for
i
in
sorted_f
...
@@ -1325,11 +1325,11 @@ class test_SoftMax(test_nnet.test_SoftMax):
...
@@ -1325,11 +1325,11 @@ class test_SoftMax(test_nnet.test_SoftMax):
(
2
,
3
,
4
,
66000
)]
(
2
,
3
,
4
,
66000
)]
for
inp_shape
in
input_shapes
:
for
inp_shape
in
input_shapes
:
input_val
=
n
umpy
.
random
.
normal
(
0
,
1
,
inp_shape
)
.
astype
(
theano
.
config
.
floatX
)
input_val
=
n
p
.
random
.
normal
(
0
,
1
,
inp_shape
)
.
astype
(
theano
.
config
.
floatX
)
out
=
f
(
input_val
)
out
=
f
(
input_val
)
expected_out
=
n
umpy
.
log
(
numpy
.
exp
(
input_val
)
/
expected_out
=
n
p
.
log
(
np
.
exp
(
input_val
)
/
numpy
.
exp
(
input_val
)
.
sum
(
1
)[:,
None
,
:,
:])
np
.
exp
(
input_val
)
.
sum
(
1
)[:,
None
,
:,
:])
utt
.
assert_allclose
(
out
,
expected_out
)
utt
.
assert_allclose
(
out
,
expected_out
)
...
@@ -1357,7 +1357,7 @@ class test_SoftMax(test_nnet.test_SoftMax):
...
@@ -1357,7 +1357,7 @@ class test_SoftMax(test_nnet.test_SoftMax):
assert
dnn_softmax_nodes
[
0
]
.
op
.
algo
==
"log"
assert
dnn_softmax_nodes
[
0
]
.
op
.
algo
==
"log"
# Compare the output of the function with the reference function
# Compare the output of the function with the reference function
inp
=
n
umpy
.
random
.
normal
(
0
,
1
,
(
5
,
6
))
.
astype
(
theano
.
config
.
floatX
)
inp
=
n
p
.
random
.
normal
(
0
,
1
,
(
5
,
6
))
.
astype
(
theano
.
config
.
floatX
)
utt
.
assert_allclose
(
f
(
inp
),
f_ref
(
inp
))
utt
.
assert_allclose
(
f
(
inp
),
f_ref
(
inp
))
# Build the first graph and ensure that the optimization is applied
# Build the first graph and ensure that the optimization is applied
...
@@ -1370,7 +1370,7 @@ class test_SoftMax(test_nnet.test_SoftMax):
...
@@ -1370,7 +1370,7 @@ class test_SoftMax(test_nnet.test_SoftMax):
assert
dnn_softmax_nodes
[
0
]
.
op
.
algo
==
"log"
assert
dnn_softmax_nodes
[
0
]
.
op
.
algo
==
"log"
# Compare the output of the function with the reference function
# Compare the output of the function with the reference function
inp
=
n
umpy
.
random
.
normal
(
0
,
1
,
(
5
,
6
))
.
astype
(
theano
.
config
.
floatX
)
inp
=
n
p
.
random
.
normal
(
0
,
1
,
(
5
,
6
))
.
astype
(
theano
.
config
.
floatX
)
utt
.
assert_allclose
(
f
(
inp
),
f_ref
(
inp
))
utt
.
assert_allclose
(
f
(
inp
),
f_ref
(
inp
))
...
@@ -1412,10 +1412,10 @@ def test_dnn_batchnorm_train():
...
@@ -1412,10 +1412,10 @@ def test_dnn_batchnorm_train():
data_shape
=
data_shape
[:
ndim
]
data_shape
=
data_shape
[:
ndim
]
param_shape
=
tuple
(
1
if
d
in
axes
else
s
param_shape
=
tuple
(
1
if
d
in
axes
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
)
outputs
=
f
(
X
,
Scale
,
Bias
,
Dy
)
outputs
=
f
(
X
,
Scale
,
Bias
,
Dy
)
# compare outputs
# compare outputs
utt
.
assert_allclose
(
outputs
[
0
],
outputs
[
0
+
3
])
# out
utt
.
assert_allclose
(
outputs
[
0
],
outputs
[
0
+
3
])
# out
...
@@ -1463,12 +1463,12 @@ def test_batchnorm_inference():
...
@@ -1463,12 +1463,12 @@ def test_batchnorm_inference():
data_shape
=
data_shape
[:
ndim
]
data_shape
=
data_shape
[:
ndim
]
param_shape
=
tuple
(
1
if
d
in
axes
else
s
param_shape
=
tuple
(
1
if
d
in
axes
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
...
@@ -1496,7 +1496,7 @@ def test_dnn_rnn_gru():
...
@@ -1496,7 +1496,7 @@ def test_dnn_rnn_gru():
rnnb
=
dnn
.
RNNBlock
(
theano
.
config
.
floatX
,
hidden_dim
,
depth
,
'gru'
)
rnnb
=
dnn
.
RNNBlock
(
theano
.
config
.
floatX
,
hidden_dim
,
depth
,
'gru'
)
psize
=
rnnb
.
get_param_size
([
batch_size
,
input_dim
])
psize
=
rnnb
.
get_param_size
([
batch_size
,
input_dim
])
params_cudnn
=
gpuarray_shared_constructor
(
params_cudnn
=
gpuarray_shared_constructor
(
n
umpy
.
zeros
((
psize
,),
dtype
=
theano
.
config
.
floatX
))
n
p
.
zeros
((
psize
,),
dtype
=
theano
.
config
.
floatX
))
model
=
Model
()
model
=
Model
()
last_layer
=
WrapperLayer
(
X
)
last_layer
=
WrapperLayer
(
X
)
...
@@ -1551,9 +1551,9 @@ def test_dnn_rnn_gru():
...
@@ -1551,9 +1551,9 @@ def test_dnn_rnn_gru():
ref_grad_fns
=
[
ref_grad_fn
,
ref2_grad_fn
,
ref3_grad_fn
]
ref_grad_fns
=
[
ref_grad_fn
,
ref2_grad_fn
,
ref3_grad_fn
]
cudnn_grad_fns
=
[
cudnn_grad_fn
,
cudnn2_grad_fn
,
cudnn3_grad_fn
]
cudnn_grad_fns
=
[
cudnn_grad_fn
,
cudnn2_grad_fn
,
cudnn3_grad_fn
]
x_val
=
n
umpy
.
random
.
random
((
timesteps
,
batch_size
,
input_dim
))
.
astype
(
theano
.
config
.
floatX
)
x_val
=
n
p
.
random
.
random
((
timesteps
,
batch_size
,
input_dim
))
.
astype
(
theano
.
config
.
floatX
)
y_val
=
n
umpy
.
random
.
random
((
timesteps
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
y_val
=
n
p
.
random
.
random
((
timesteps
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
h0_val
=
n
umpy
.
random
.
random
((
depth
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
h0_val
=
n
p
.
random
.
random
((
depth
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
ref_out
=
ref_fn
(
x_val
,
h0_val
)
ref_out
=
ref_fn
(
x_val
,
h0_val
)
cudnn_out
=
cudnn_fn
(
x_val
,
h0_val
)
cudnn_out
=
cudnn_fn
(
x_val
,
h0_val
)
...
@@ -1595,7 +1595,7 @@ def test_dnn_rnn_gru_bidi():
...
@@ -1595,7 +1595,7 @@ def test_dnn_rnn_gru_bidi():
rnnb
=
dnn
.
RNNBlock
(
theano
.
config
.
floatX
,
hidden_dim
,
depth
,
'gru'
,
direction_mode
=
'bidirectional'
)
rnnb
=
dnn
.
RNNBlock
(
theano
.
config
.
floatX
,
hidden_dim
,
depth
,
'gru'
,
direction_mode
=
'bidirectional'
)
psize
=
rnnb
.
get_param_size
([
batch_size
,
input_dim
])
psize
=
rnnb
.
get_param_size
([
batch_size
,
input_dim
])
params_cudnn
=
gpuarray_shared_constructor
(
params_cudnn
=
gpuarray_shared_constructor
(
n
umpy
.
random
.
random
((
psize
,))
.
astype
(
theano
.
config
.
floatX
))
n
p
.
random
.
random
((
psize
,))
.
astype
(
theano
.
config
.
floatX
))
def
funcs
(
out
,
params
,
hy
=
None
):
def
funcs
(
out
,
params
,
hy
=
None
):
cost
=
0
cost
=
0
...
@@ -1618,9 +1618,9 @@ def test_dnn_rnn_gru_bidi():
...
@@ -1618,9 +1618,9 @@ def test_dnn_rnn_gru_bidi():
cudnn_grad_fns
=
[
cudnn_grad_fn
,
cudnn2_grad_fn
,
cudnn3_grad_fn
]
cudnn_grad_fns
=
[
cudnn_grad_fn
,
cudnn2_grad_fn
,
cudnn3_grad_fn
]
x_val
=
n
umpy
.
random
.
random
((
timesteps
,
batch_size
,
input_dim
))
.
astype
(
theano
.
config
.
floatX
)
x_val
=
n
p
.
random
.
random
((
timesteps
,
batch_size
,
input_dim
))
.
astype
(
theano
.
config
.
floatX
)
y_val
=
n
umpy
.
random
.
random
((
timesteps
,
batch_size
,
2
*
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
y_val
=
n
p
.
random
.
random
((
timesteps
,
batch_size
,
2
*
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
h0_val
=
n
umpy
.
random
.
random
((
2
*
depth
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
h0_val
=
n
p
.
random
.
random
((
2
*
depth
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
cudnn_fn
(
x_val
,
h0_val
)
cudnn_fn
(
x_val
,
h0_val
)
...
@@ -1645,7 +1645,7 @@ def test_dnn_rnn_lstm():
...
@@ -1645,7 +1645,7 @@ def test_dnn_rnn_lstm():
rnnb
=
dnn
.
RNNBlock
(
theano
.
config
.
floatX
,
hidden_dim
,
depth
,
'lstm'
)
rnnb
=
dnn
.
RNNBlock
(
theano
.
config
.
floatX
,
hidden_dim
,
depth
,
'lstm'
)
psize
=
rnnb
.
get_param_size
([
batch_size
,
input_dim
])
psize
=
rnnb
.
get_param_size
([
batch_size
,
input_dim
])
params_cudnn
=
gpuarray_shared_constructor
(
params_cudnn
=
gpuarray_shared_constructor
(
n
umpy
.
zeros
((
psize
,),
dtype
=
theano
.
config
.
floatX
))
n
p
.
zeros
((
psize
,),
dtype
=
theano
.
config
.
floatX
))
model
=
Model
()
model
=
Model
()
last_layer
=
WrapperLayer
(
X
)
last_layer
=
WrapperLayer
(
X
)
...
@@ -1674,10 +1674,10 @@ def test_dnn_rnn_lstm():
...
@@ -1674,10 +1674,10 @@ def test_dnn_rnn_lstm():
cudnn_fn
,
cudnn_grad_fn
=
funcs
(
rnnb
.
apply
(
params_cudnn
,
X
,
h0
,
c0
)[
0
],
cudnn_fn
,
cudnn_grad_fn
=
funcs
(
rnnb
.
apply
(
params_cudnn
,
X
,
h0
,
c0
)[
0
],
[
params_cudnn
])
[
params_cudnn
])
x_val
=
n
umpy
.
random
.
random
((
timesteps
,
batch_size
,
input_dim
))
.
astype
(
theano
.
config
.
floatX
)
x_val
=
n
p
.
random
.
random
((
timesteps
,
batch_size
,
input_dim
))
.
astype
(
theano
.
config
.
floatX
)
y_val
=
n
umpy
.
random
.
random
((
timesteps
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
y_val
=
n
p
.
random
.
random
((
timesteps
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
h0_val
=
n
umpy
.
random
.
random
((
depth
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
h0_val
=
n
p
.
random
.
random
((
depth
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
c0_val
=
n
umpy
.
random
.
random
((
depth
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
c0_val
=
n
p
.
random
.
random
((
depth
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
ref_out
=
ref_fn
(
x_val
,
h0_val
,
c0_val
)
ref_out
=
ref_fn
(
x_val
,
h0_val
,
c0_val
)
cudnn_out
=
cudnn_fn
(
x_val
,
h0_val
,
c0_val
)
cudnn_out
=
cudnn_fn
(
x_val
,
h0_val
,
c0_val
)
...
@@ -1720,7 +1720,7 @@ def test_dnn_rnn_lstm_grad_c():
...
@@ -1720,7 +1720,7 @@ def test_dnn_rnn_lstm_grad_c():
rnnb
=
dnn
.
RNNBlock
(
theano
.
config
.
floatX
,
hidden_dim
,
depth
,
'lstm'
)
rnnb
=
dnn
.
RNNBlock
(
theano
.
config
.
floatX
,
hidden_dim
,
depth
,
'lstm'
)
psize
=
rnnb
.
get_param_size
([
batch_size
,
input_dim
])
psize
=
rnnb
.
get_param_size
([
batch_size
,
input_dim
])
params_cudnn
=
gpuarray_shared_constructor
(
params_cudnn
=
gpuarray_shared_constructor
(
n
umpy
.
zeros
((
psize
,),
dtype
=
theano
.
config
.
floatX
))
n
p
.
zeros
((
psize
,),
dtype
=
theano
.
config
.
floatX
))
model
=
Model
()
model
=
Model
()
last_layer
=
WrapperLayer
(
X
)
last_layer
=
WrapperLayer
(
X
)
...
@@ -1751,10 +1751,10 @@ def test_dnn_rnn_lstm_grad_c():
...
@@ -1751,10 +1751,10 @@ def test_dnn_rnn_lstm_grad_c():
ref_grad_fn
=
funcs
(
ref_cy
,
model
.
get_params
())
ref_grad_fn
=
funcs
(
ref_cy
,
model
.
get_params
())
cudnn_grad_fn
=
funcs
(
cy
,
[
params_cudnn
])
cudnn_grad_fn
=
funcs
(
cy
,
[
params_cudnn
])
x_val
=
n
umpy
.
random
.
random
((
timesteps
,
batch_size
,
input_dim
))
.
astype
(
theano
.
config
.
floatX
)
x_val
=
n
p
.
random
.
random
((
timesteps
,
batch_size
,
input_dim
))
.
astype
(
theano
.
config
.
floatX
)
cy_val
=
n
umpy
.
random
.
random
((
depth
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
cy_val
=
n
p
.
random
.
random
((
depth
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
h0_val
=
n
umpy
.
random
.
random
((
depth
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
h0_val
=
n
p
.
random
.
random
((
depth
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
c0_val
=
n
umpy
.
random
.
random
((
depth
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
c0_val
=
n
p
.
random
.
random
((
depth
,
batch_size
,
hidden_dim
))
.
astype
(
theano
.
config
.
floatX
)
ref_grads
=
ref_grad_fn
(
x_val
,
cy_val
,
h0_val
,
c0_val
)
ref_grads
=
ref_grad_fn
(
x_val
,
cy_val
,
h0_val
,
c0_val
)
cudnn_grads
=
cudnn_grad_fn
(
x_val
,
cy_val
,
h0_val
,
c0_val
)
cudnn_grads
=
cudnn_grad_fn
(
x_val
,
cy_val
,
h0_val
,
c0_val
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论