Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a5b241e2
提交
a5b241e2
authored
7月 10, 2012
作者:
Eric Larsen
提交者:
Frederic
7月 26, 2012
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
correction to file layout
上级
d3d54bb9
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
248 行增加
和
219 行删除
+248
-219
test_conv3d.py
theano/tensor/nnet/tests/test_conv3d.py
+248
-219
没有找到文件。
theano/tensor/nnet/tests/test_conv3d.py
浏览文件 @
a5b241e2
...
...
@@ -20,7 +20,9 @@ floatX = theano.config.floatX
# a subset of the tests they will do different things than if you
# run all of them
class
DummyConv3D
:
"""A dummy version of Conv3D passed to verify_grad
Stores a fixed stride, since stride is not differentiable
Exposes only one scalar argument, which is used as the position
...
...
@@ -30,69 +32,84 @@ class DummyConv3D:
verify_grad will not need to test hundreds of variables. Disadvantage
is we can't be certain that all of them are correct, advantange is that
this random projection lets us test lots of variables very quickly """
def
__init__
(
self
,
rng
,
VWbVals
,
d
):
"""
param: rng Random number generator used to pick direction of the line
param: rng Random number generator used to pick direction of the
line
param: VWbVals tuple containing values to test V,W,b around
param: d shared variable for d, the stride
"""
self
.
V
,
self
.
W
,
self
.
b
=
VWbVals
self
.
dV
=
shared
(
rng
.
uniform
(
-
1
,
1
,
self
.
V
.
get_value
(
borrow
=
True
)
.
shape
))
self
.
dW
=
shared
(
rng
.
uniform
(
-
1
,
1
,
self
.
W
.
get_value
(
borrow
=
True
)
.
shape
))
self
.
db
=
shared
(
rng
.
uniform
(
-
1
,
1
,
self
.
b
.
get_value
(
borrow
=
True
)
.
shape
))
self
.
dV
=
shared
(
rng
.
uniform
(
-
1
,
1
,
self
.
V
.
get_value
(
borrow
=
True
)
.
shape
))
self
.
dW
=
shared
(
rng
.
uniform
(
-
1
,
1
,
self
.
W
.
get_value
(
borrow
=
True
)
.
shape
))
self
.
db
=
shared
(
rng
.
uniform
(
-
1
,
1
,
self
.
b
.
get_value
(
borrow
=
True
)
.
shape
))
self
.
d
=
d
def
__call__
(
self
,
t
):
output
=
conv3D
(
self
.
V
+
t
*
self
.
dV
,
self
.
W
+
t
*
self
.
dW
,
self
.
b
+
t
*
self
.
db
,
self
.
d
)
output
=
conv3D
(
self
.
V
+
t
*
self
.
dV
,
self
.
W
+
t
*
self
.
dW
,
self
.
b
+
t
*
self
.
db
,
self
.
d
)
return
output
class
DummyConvGrad3D
:
def
__init__
(
self
,
rng
,
VdHvals
,
d
,
WShape
):
"""
param: rng Random number generator used to pick direction of the line
param: rng Random number generator used to pick direction of the
line
param: VWbVals tuple containing values to test V,W,b around
param: d shared variable for d, the stride
"""
self
.
V
,
self
.
dCdH
=
VdHvals
self
.
dV
=
shared
(
rng
.
uniform
(
-
1
,
1
,
self
.
V
.
get_value
(
borrow
=
True
)
.
shape
))
self
.
ddCdH
=
shared
(
rng
.
uniform
(
-
1
,
1
,
self
.
dCdH
.
get_value
(
borrow
=
True
)
.
shape
))
self
.
dV
=
shared
(
rng
.
uniform
(
-
1
,
1
,
self
.
V
.
get_value
(
borrow
=
True
)
.
shape
))
self
.
ddCdH
=
shared
(
rng
.
uniform
(
-
1
,
1
,
self
.
dCdH
.
get_value
(
borrow
=
True
)
.
shape
))
self
.
d
=
d
self
.
WShape
=
WShape
def
__call__
(
self
,
t
):
output
=
convGrad3D
(
self
.
V
+
t
*
self
.
dV
,
self
.
d
,
self
.
WShape
,
self
.
dCdH
+
t
*
self
.
ddCdH
)
output
=
convGrad3D
(
self
.
V
+
t
*
self
.
dV
,
self
.
d
,
self
.
WShape
,
self
.
dCdH
+
t
*
self
.
ddCdH
)
return
output
class
DummyConvTransp3D
:
def
__init__
(
self
,
rng
,
WbHvals
,
d
,
RShape
):
"""
param: rng Random number generator used to pick direction of the line
param: rng Random number generator used to pick direction of the
line
param: VWbVals tuple containing values to test V,W,b around
param: d shared variable for d, the stride
"""
self
.
W
,
self
.
b
,
self
.
H
=
WbHvals
self
.
dW
=
rng
.
uniform
(
-
1
,
1
,
self
.
W
.
get_value
(
borrow
=
True
)
.
shape
)
self
.
db
=
rng
.
uniform
(
-
1
,
1
,
self
.
b
.
get_value
(
borrow
=
True
)
.
shape
)
self
.
dH
=
rng
.
uniform
(
-
1
,
1
,
self
.
H
.
get_value
(
borrow
=
True
)
.
shape
)
self
.
dW
,
self
.
db
,
self
.
dH
=
shared
(
self
.
dW
),
shared
(
self
.
db
),
shared
(
self
.
dH
)
self
.
dW
=
rng
.
uniform
(
-
1
,
1
,
self
.
W
.
get_value
(
borrow
=
True
)
.
shape
)
self
.
db
=
rng
.
uniform
(
-
1
,
1
,
self
.
b
.
get_value
(
borrow
=
True
)
.
shape
)
self
.
dH
=
rng
.
uniform
(
-
1
,
1
,
self
.
H
.
get_value
(
borrow
=
True
)
.
shape
)
self
.
dW
,
self
.
db
,
self
.
dH
=
shared
(
self
.
dW
),
shared
(
self
.
db
),
shared
(
self
.
dH
)
self
.
d
=
d
self
.
RShape
=
RShape
def
__call__
(
self
,
t
):
output
=
convTransp3D
(
self
.
W
+
t
*
self
.
dW
,
self
.
b
+
t
*
self
.
db
,
self
.
d
,
self
.
H
+
t
*
self
.
dH
,
self
.
RShape
)
output
=
convTransp3D
(
self
.
W
+
t
*
self
.
dW
,
self
.
b
+
t
*
self
.
db
,
self
.
d
,
self
.
H
+
t
*
self
.
dH
,
self
.
RShape
)
return
output
class
TestConv3D
(
utt
.
InferShapeTester
):
def
setUp
(
self
):
...
...
@@ -103,75 +120,86 @@ class TestConv3D(utt.InferShapeTester):
mode
=
copy
.
copy
(
theano
.
compile
.
mode
.
get_default_mode
())
mode
.
check_py_code
=
False
self
.
W
=
shared
(
N
.
ndarray
(
shape
=
(
1
,
1
,
1
,
1
,
1
),
dtype
=
floatX
))
self
.
b
=
shared
(
N
.
zeros
(
1
,
dtype
=
floatX
))
self
.
rb
=
shared
(
N
.
zeros
(
1
,
dtype
=
floatX
))
self
.
V
=
shared
(
N
.
ndarray
(
shape
=
(
1
,
1
,
1
,
1
,
1
),
dtype
=
floatX
))
self
.
d
=
shared
(
N
.
ndarray
(
shape
=
(
3
,),
dtype
=
int
))
self
.
W
=
shared
(
N
.
ndarray
(
shape
=
(
1
,
1
,
1
,
1
,
1
),
dtype
=
floatX
))
self
.
b
=
shared
(
N
.
zeros
(
1
,
dtype
=
floatX
))
self
.
rb
=
shared
(
N
.
zeros
(
1
,
dtype
=
floatX
))
self
.
V
=
shared
(
N
.
ndarray
(
shape
=
(
1
,
1
,
1
,
1
,
1
),
dtype
=
floatX
))
self
.
d
=
shared
(
N
.
ndarray
(
shape
=
(
3
,
),
dtype
=
int
))
self
.
H
=
conv3D
(
self
.
V
,
self
.
W
,
self
.
b
,
self
.
d
)
self
.
H_func
=
function
([],
self
.
H
,
mode
=
mode
)
self
.
H_shape_func
=
function
(
[],
self
.
H
.
shape
,
mode
=
mode
)
self
.
H_func
=
function
([],
self
.
H
,
mode
=
mode
)
self
.
H_shape_func
=
function
([],
self
.
H
.
shape
,
mode
=
mode
)
self
.
RShape
=
T
.
vector
(
dtype
=
'int64'
)
self
.
otherH
=
T
.
TensorType
(
floatX
,(
False
,
False
,
False
,
False
,
False
))(
name
=
'otherH'
)
self
.
transp
=
convTransp3D
(
self
.
W
,
self
.
rb
,
self
.
d
,
self
.
otherH
,
self
.
RShape
)
self
.
transp_func
=
function
([
self
.
otherH
,
self
.
RShape
],
self
.
transp
,
mode
=
mode
)
self
.
otherH
=
T
.
TensorType
(
floatX
,
(
False
,
False
,
False
,
False
,
False
))(
name
=
'otherH'
)
self
.
transp
=
convTransp3D
(
self
.
W
,
self
.
rb
,
self
.
d
,
self
.
otherH
,
self
.
RShape
)
self
.
transp_func
=
function
([
self
.
otherH
,
self
.
RShape
],
self
.
transp
,
mode
=
mode
)
self
.
R
=
convTransp3D
(
self
.
W
,
self
.
rb
,
self
.
d
,
self
.
H
,
self
.
RShape
)
self
.
R_func
=
function
([
self
.
RShape
],
self
.
R
,
mode
=
mode
)
self
.
R_func
=
function
([
self
.
RShape
],
self
.
R
,
mode
=
mode
)
self
.
R_shape_func
=
function
([
self
.
RShape
],
self
.
R
.
shape
)
self
.
reconsObj
=
T
.
sum
(
T
.
sqr
(
self
.
V
-
self
.
R
))
self
.
reconsObj
=
T
.
sum
(
T
.
sqr
(
self
.
V
-
self
.
R
))
self
.
reconsObjFunc
=
function
([
self
.
RShape
],
self
.
reconsObj
,
mode
=
mode
)
self
.
gradientsFunc
=
function
([
self
.
RShape
],
[
T
.
grad
(
self
.
reconsObj
,
self
.
W
),
T
.
grad
(
self
.
reconsObj
,
self
.
H
),
T
.
grad
(
self
.
reconsObj
,
self
.
V
),
T
.
grad
(
self
.
reconsObj
,
self
.
b
)
]
,
mode
=
mode
)
self
.
check_c_against_python
=
function
([
self
.
RShape
],
[
T
.
grad
(
self
.
reconsObj
,
self
.
W
),
T
.
grad
(
self
.
reconsObj
,
self
.
H
),
T
.
grad
(
self
.
reconsObj
,
self
.
V
),
T
.
grad
(
self
.
reconsObj
,
self
.
b
)
]
,
mode
=
'DEBUG_MODE'
)
self
.
gradientsFunc
=
function
([
self
.
RShape
],
[
T
.
grad
(
self
.
reconsObj
,
self
.
W
),
T
.
grad
(
self
.
reconsObj
,
self
.
H
),
T
.
grad
(
self
.
reconsObj
,
self
.
V
),
T
.
grad
(
self
.
reconsObj
,
self
.
b
)],
mode
=
mode
)
self
.
dCdW_shape_func
=
function
([
self
.
RShape
],
T
.
grad
(
self
.
reconsObj
,
self
.
W
)
.
shape
,
mode
=
mode
)
self
.
check_c_against_python
=
function
([
self
.
RShape
],
[
T
.
grad
(
self
.
reconsObj
,
self
.
W
),
T
.
grad
(
self
.
reconsObj
,
self
.
H
),
T
.
grad
(
self
.
reconsObj
,
self
.
V
),
T
.
grad
(
self
.
reconsObj
,
self
.
b
)],
mode
=
'DEBUG_MODE'
)
self
.
dCdW_shape_func
=
function
([
self
.
RShape
],
T
.
grad
(
self
.
reconsObj
,
self
.
W
)
.
shape
,
mode
=
mode
)
def
random_tensor
(
self
,
*
dims
):
return
N
.
asarray
(
self
.
rng
.
uniform
(
-.
05
,
.
05
,
dims
),
dtype
=
floatX
)
def
random_tensor
(
self
,
*
dims
):
return
N
.
asarray
(
self
.
rng
.
uniform
(
-.
05
,
.
05
,
dims
),
dtype
=
floatX
)
def
randomize
(
self
):
batchSize
=
self
.
rng
.
randint
(
1
,
4
)
videoDur
=
self
.
rng
.
randint
(
8
,
30
)
filterWidth
=
self
.
rng
.
randint
(
1
,
8
)
filterHeight
=
self
.
rng
.
randint
(
1
,
8
)
filterDur
=
self
.
rng
.
randint
(
1
,
8
)
tsteps
=
self
.
rng
.
randint
(
1
,
4
)
rsteps
=
self
.
rng
.
randint
(
1
,
4
)
csteps
=
self
.
rng
.
randint
(
1
,
4
)
videoDur
=
tsteps
*
filterDur
+
self
.
rng
.
randint
(
0
,
3
)
videoWidth
=
csteps
*
filterWidth
+
self
.
rng
.
randint
(
0
,
3
)
videoHeight
=
rsteps
*
filterHeight
+
self
.
rng
.
randint
(
0
,
3
)
numFilters
=
self
.
rng
.
randint
(
1
,
3
)
inputChannels
=
self
.
rng
.
randint
(
1
,
3
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
0
]
=
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
1
]
=
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
2
]
=
self
.
rng
.
randint
(
1
,
15
)
outputHeight
=
int
(
(
videoHeight
-
filterHeight
)
/
self
.
d
.
get_value
(
borrow
=
True
)[
0
]
)
+
1
outputWidth
=
int
(
(
videoWidth
-
filterWidth
)
/
self
.
d
.
get_value
(
borrow
=
True
)[
1
]
)
+
1
outputDur
=
int
(
(
videoDur
-
filterDur
)
/
self
.
d
.
get_value
(
borrow
=
True
)[
2
]
)
+
1
self
.
W
.
set_value
(
self
.
random_tensor
(
numFilters
,
filterHeight
,
filterWidth
,
filterDur
,
inputChannels
),
borrow
=
True
)
batchSize
=
self
.
rng
.
randint
(
1
,
4
)
videoDur
=
self
.
rng
.
randint
(
8
,
30
)
filterWidth
=
self
.
rng
.
randint
(
1
,
8
)
filterHeight
=
self
.
rng
.
randint
(
1
,
8
)
filterDur
=
self
.
rng
.
randint
(
1
,
8
)
tsteps
=
self
.
rng
.
randint
(
1
,
4
)
rsteps
=
self
.
rng
.
randint
(
1
,
4
)
csteps
=
self
.
rng
.
randint
(
1
,
4
)
videoDur
=
tsteps
*
filterDur
+
self
.
rng
.
randint
(
0
,
3
)
videoWidth
=
csteps
*
filterWidth
+
self
.
rng
.
randint
(
0
,
3
)
videoHeight
=
rsteps
*
filterHeight
+
self
.
rng
.
randint
(
0
,
3
)
numFilters
=
self
.
rng
.
randint
(
1
,
3
)
inputChannels
=
self
.
rng
.
randint
(
1
,
3
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
0
]
=
\
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
1
]
=
\
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
2
]
=
\
self
.
rng
.
randint
(
1
,
15
)
outputHeight
=
int
((
videoHeight
-
filterHeight
)
/
self
.
d
.
get_value
(
borrow
=
True
)[
0
])
+
1
outputWidth
=
int
((
videoWidth
-
filterWidth
)
/
self
.
d
.
get_value
(
borrow
=
True
)[
1
])
+
1
outputDur
=
int
((
videoDur
-
filterDur
)
/
self
.
d
.
get_value
(
borrow
=
True
)[
2
])
+
1
self
.
W
.
set_value
(
self
.
random_tensor
(
numFilters
,
filterHeight
,
filterWidth
,
filterDur
,
inputChannels
),
borrow
=
True
)
self
.
b
.
set_value
(
self
.
random_tensor
(
numFilters
),
borrow
=
True
)
self
.
rb
.
set_value
(
self
.
random_tensor
(
inputChannels
),
borrow
=
True
)
self
.
V
.
set_value
(
self
.
random_tensor
(
batchSize
,
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
),
borrow
=
True
)
self
.
V
.
set_value
(
self
.
random_tensor
(
batchSize
,
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
),
borrow
=
True
)
self
.
rb
.
set_value
(
self
.
random_tensor
(
inputChannels
),
borrow
=
True
)
def
test_c_against_python
(
self
):
...
...
@@ -179,37 +207,38 @@ class TestConv3D(utt.InferShapeTester):
self
.
check_c_against_python
(
self
.
V
.
get_value
(
borrow
=
True
)
.
shape
[
1
:
4
])
def
test_c_against_mat_mul
(
self
):
#Use a filter of the same size as the image, so the convolution is just a dense matrix multiply
#Check that dense matrix multiplication gives the same result as convolution
batchSize
=
self
.
rng
.
randint
(
1
,
10
)
videoDur
=
self
.
rng
.
randint
(
3
,
10
)
videoWidth
=
self
.
rng
.
randint
(
1
,
5
)
videoHeight
=
self
.
rng
.
randint
(
1
,
5
)
# Use a filter of the same size as the image, so the convolution is
# just a dense matrix multiply.
# Check that dense matrix multiplication gives the same result as
# convolution.
filterWidth
=
videoWidth
filterHeight
=
videoHeight
filterDur
=
videoDur
numFilters
=
self
.
rng
.
randint
(
1
,
3
)
inputChannels
=
self
.
rng
.
randint
(
1
,
4
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
0
]
=
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
1
]
=
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
2
]
=
self
.
rng
.
randint
(
1
,
15
)
batchSize
=
self
.
rng
.
randint
(
1
,
10
)
videoDur
=
self
.
rng
.
randint
(
3
,
10
)
videoWidth
=
self
.
rng
.
randint
(
1
,
5
)
videoHeight
=
self
.
rng
.
randint
(
1
,
5
)
filterWidth
=
videoWidth
filterHeight
=
videoHeight
filterDur
=
videoDur
numFilters
=
self
.
rng
.
randint
(
1
,
3
)
inputChannels
=
self
.
rng
.
randint
(
1
,
4
)
self
.
W
.
set_value
(
self
.
random_tensor
(
numFilters
,
filterHeight
,
filterWidth
,
filterDur
,
inputChannels
),
borrow
=
True
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
0
]
=
\
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
1
]
=
\
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
2
]
=
\
self
.
rng
.
randint
(
1
,
15
)
self
.
W
.
set_value
(
self
.
W
.
get_value
(
borrow
=
True
)
*
(
self
.
W
.
get_value
(
borrow
=
True
)
<
1e-5
),
borrow
=
True
)
self
.
W
.
set_value
(
self
.
random_tensor
(
numFilters
,
filterHeight
,
filterWidth
,
filterDur
,
inputChannels
),
borrow
=
True
)
self
.
W
.
set_value
(
self
.
W
.
get_value
(
borrow
=
True
)
*
(
self
.
W
.
get_value
(
borrow
=
True
)
<
1e-5
),
borrow
=
True
)
self
.
b
.
set_value
(
self
.
random_tensor
(
numFilters
),
borrow
=
True
)
self
.
V
.
set_value
(
self
.
random_tensor
(
batchSize
,
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
)
,
borrow
=
True
)
self
.
V
.
set_value
(
self
.
random_tensor
(
batchSize
,
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
),
borrow
=
True
)
Hv
=
self
.
H_func
()
...
...
@@ -219,156 +248,163 @@ class TestConv3D(utt.InferShapeTester):
n
=
inputChannels
*
videoHeight
*
videoWidth
*
videoDur
W_mat
=
N
.
zeros
((
n
,
numFilters
))
V_mat
=
N
.
zeros
((
batchSize
,
n
))
V_mat
=
N
.
zeros
((
batchSize
,
n
))
Hv_mat
=
N
.
zeros
((
batchSize
,
numFilters
))
for
qi
in
xrange
(
0
,
numFilters
):
W_mat
[:,
qi
]
=
self
.
W
.
get_value
(
borrow
=
True
)[
qi
,:,:,:,:]
.
reshape
((
n
))
Hv_mat
[:,
qi
]
=
Hv
[:,
0
,
0
,
0
,
qi
]
for
qi
in
xrange
(
0
,
batchSize
):
V_mat
[
qi
,:]
=
self
.
V
.
get_value
(
borrow
=
True
)[
qi
,:,:,:,:]
.
reshape
((
n
))
for
qi
in
xrange
(
0
,
numFilters
):
W_mat
[:,
qi
]
=
\
self
.
W
.
get_value
(
borrow
=
True
)[
qi
,
:,
:,
:,
:]
.
reshape
((
n
))
Hv_mat
[:,
qi
]
=
Hv
[:,
0
,
0
,
0
,
qi
]
for
qi
in
xrange
(
0
,
batchSize
):
V_mat
[
qi
,
:]
=
\
self
.
V
.
get_value
(
borrow
=
True
)[
qi
,
:,
:,
:,
:]
.
reshape
((
n
))
H_mat
=
N
.
dot
(
V_mat
,
W_mat
)
+
self
.
b
.
get_value
(
borrow
=
True
)
H_mat
=
N
.
dot
(
V_mat
,
W_mat
)
+
self
.
b
.
get_value
(
borrow
=
True
)
tol
=
1e-5
if
floatX
==
'float32'
:
tol
=
1e-4
if
N
.
abs
(
H_mat
-
Hv_mat
)
.
max
()
>
tol
and
not
N
.
allclose
(
H_mat
,
Hv_mat
):
if
N
.
abs
(
H_mat
-
Hv_mat
)
.
max
()
>
tol
and
not
N
.
allclose
(
H_mat
,
Hv_mat
):
print
H_mat
print
Hv_mat
print
'max error: '
+
str
(
N
.
abs
(
H_mat
-
Hv_mat
)
.
max
())
print
'max error: '
+
str
(
N
.
abs
(
H_mat
-
Hv_mat
)
.
max
())
W
.
get_value
(
borrow
=
True
)[
W
.
get_value
(
borrow
=
True
)
!=
0
]
+=
1.0
print
'min non-zero kernel mag: '
+
str
(
N
.
abs
(
W
.
get_value
(
borrow
=
True
))
.
min
())
print
'min non-zero kernel mag: '
+
\
str
(
N
.
abs
(
W
.
get_value
(
borrow
=
True
))
.
min
())
assert
False
def
test_c_against_mat_transp_mul
(
self
):
#Use a filter of the same size as the image, so the convolution is just a dense matrix multiply
#Check that dense matrix multiplication by the transpose of the matrix gives the same result as ConvTransp
batchSize
=
self
.
rng
.
randint
(
1
,
10
)
videoDur
=
self
.
rng
.
randint
(
3
,
15
)
videoWidth
=
self
.
rng
.
randint
(
3
,
15
)
videoHeight
=
self
.
rng
.
randint
(
3
,
15
)
# Use a filter of the same size as the image, so the convolution is just a
# dense matrix multiply.
# Check that dense matrix multiplication by the transpose of the matrix
# gives the same result as ConvTransp.
batchSize
=
self
.
rng
.
randint
(
1
,
10
)
videoDur
=
self
.
rng
.
randint
(
3
,
15
)
videoWidth
=
self
.
rng
.
randint
(
3
,
15
)
videoHeight
=
self
.
rng
.
randint
(
3
,
15
)
filterWidth
=
videoWidth
filterHeight
=
videoHeight
filterDur
=
videoDur
numFilters
=
self
.
rng
.
randint
(
1
,
15
)
inputChannels
=
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
0
]
=
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
1
]
=
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
2
]
=
self
.
rng
.
randint
(
1
,
15
)
self
.
W
.
set_value
(
self
.
random_tensor
(
numFilters
,
filterHeight
,
filterWidth
,
filterDur
,
inputChannels
)
,
borrow
=
True
)
numFilters
=
self
.
rng
.
randint
(
1
,
15
)
inputChannels
=
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
0
]
=
\
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
1
]
=
\
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
2
]
=
\
self
.
rng
.
randint
(
1
,
15
)
self
.
W
.
set_value
(
self
.
random_tensor
(
numFilters
,
filterHeight
,
filterWidth
,
filterDur
,
inputChannels
),
borrow
=
True
)
self
.
b
.
set_value
(
self
.
random_tensor
(
numFilters
),
borrow
=
True
)
self
.
V
.
set_value
(
self
.
random_tensor
(
batchSize
,
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
),
borrow
=
True
)
self
.
V
.
set_value
(
self
.
random_tensor
(
batchSize
,
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
),
borrow
=
True
)
self
.
rb
.
set_value
(
self
.
random_tensor
(
inputChannels
),
borrow
=
True
)
H_shape
=
self
.
H_shape_func
()
assert
H_shape
[
1
]
==
1
assert
H_shape
[
1
]
==
1
assert
H_shape
[
2
]
==
1
assert
H_shape
[
3
]
==
1
Hv
=
self
.
random_tensor
(
*
H_shape
)
Hv
=
self
.
random_tensor
(
*
H_shape
)
Vv
=
self
.
transp_func
(
Hv
,
[
videoHeight
,
videoWidth
,
videoDur
])
Vv
=
self
.
transp_func
(
Hv
,
[
videoHeight
,
videoWidth
,
videoDur
])
n
=
inputChannels
*
videoHeight
*
videoWidth
*
videoDur
rbim
=
N
.
zeros
((
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
))
for
qi
in
xrange
(
0
,
inputChannels
):
rbim
[:,
:,:,
qi
]
=
self
.
rb
.
get_value
(
borrow
=
True
)[
qi
]
rbim
=
N
.
zeros
((
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
))
for
qi
in
xrange
(
0
,
inputChannels
):
rbim
[:,
:,
:,
qi
]
=
self
.
rb
.
get_value
(
borrow
=
True
)[
qi
]
rbv
=
rbim
.
reshape
((
n
))
W_mat
=
N
.
zeros
((
numFilters
,
n
))
Vv_mat
=
N
.
zeros
((
n
,
batchSize
))
Hv_mat
=
N
.
zeros
((
numFilters
,
batchSize
))
for
qi
in
xrange
(
0
,
numFilters
):
W_mat
[
qi
,:]
=
self
.
W
.
get_value
(
borrow
=
True
)[
qi
,:,:,:,:]
.
reshape
((
n
))
Hv_mat
[
qi
,:]
=
Hv
[:,
0
,
0
,
0
,
qi
]
for
qi
in
xrange
(
0
,
batchSize
):
Vv_mat
[:,
qi
]
=
Vv
[
qi
,:,:,:,:]
.
reshape
((
n
))
V_mat
=
(
N
.
dot
(
W_mat
.
transpose
(),
Hv_mat
)
.
transpose
()
+
rbv
)
.
transpose
()
if
N
.
abs
(
V_mat
-
Vv_mat
)
.
max
()
>
1e-5
:
Hv_mat
=
N
.
zeros
((
numFilters
,
batchSize
))
for
qi
in
xrange
(
0
,
numFilters
):
W_mat
[
qi
,
:]
=
\
self
.
W
.
get_value
(
borrow
=
True
)[
qi
,
:,
:,
:,
:]
.
reshape
((
n
))
Hv_mat
[
qi
,
:]
=
Hv
[:,
0
,
0
,
0
,
qi
]
for
qi
in
xrange
(
0
,
batchSize
):
Vv_mat
[:,
qi
]
=
Vv
[
qi
,
:,
:,
:,
:]
.
reshape
((
n
))
V_mat
=
(
N
.
dot
(
W_mat
.
transpose
(),
Hv_mat
)
.
transpose
()
+
\
rbv
)
.
transpose
()
if
N
.
abs
(
V_mat
-
Vv_mat
)
.
max
()
>
1e-5
:
print
V_mat
print
Vv_mat
for
qq
in
xrange
(
V_mat
.
shape
[
0
]):
for
qqq
in
xrange
(
Vv_mat
.
shape
[
1
]):
if
abs
(
V_mat
[
qq
,
qqq
]
-
Vv_mat
[
qq
,
qqq
])
>
1e-5
:
print
'wrong at '
+
str
((
qq
,
qqq
))
+
': '
+
str
((
V_mat
[
qq
,
qqq
],
Vv_mat
[
qq
,
qqq
]))
if
abs
(
V_mat
[
qq
,
qqq
]
-
Vv_mat
[
qq
,
qqq
])
>
1e-5
:
print
(
'wrong at '
+
str
((
qq
,
qqq
))
+
': '
+
str
(
V_mat
[
qq
,
qqq
],
Vv_mat
[
qq
,
qqq
]))
assert
False
def
test_c_against_sparse_mat_transp_mul
(
self
):
#like test_c_against_mat_transp_mul but using a sparse matrix and a kernel that is smaller than the image
# like test_c_against_mat_transp_mul but using a sparse matrix and a kernel
# that is smaller than the image
if
not
theano
.
sparse
.
enable_sparse
:
raise
SkipTest
(
'Optional package sparse disabled'
)
batchSize
=
self
.
rng
.
randint
(
1
,
3
)
filterWidth
=
self
.
rng
.
randint
(
1
,
8
)
filterHeight
=
self
.
rng
.
randint
(
1
,
8
)
filterDur
=
self
.
rng
.
randint
(
1
,
8
)
batchSize
=
self
.
rng
.
randint
(
1
,
3
)
filterWidth
=
self
.
rng
.
randint
(
1
,
8
)
filterHeight
=
self
.
rng
.
randint
(
1
,
8
)
filterDur
=
self
.
rng
.
randint
(
1
,
8
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
0
]
=
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
1
]
=
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
2
]
=
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
0
]
=
\
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
1
]
=
\
self
.
rng
.
randint
(
1
,
15
)
self
.
d
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
)[
2
]
=
\
self
.
rng
.
randint
(
1
,
15
)
dr
=
self
.
d
.
get_value
(
borrow
=
True
)[
0
]
dc
=
self
.
d
.
get_value
(
borrow
=
True
)[
1
]
dt
=
self
.
d
.
get_value
(
borrow
=
True
)[
2
]
numFilters
=
self
.
rng
.
randint
(
1
,
3
)
row_steps
=
self
.
rng
.
randint
(
1
,
4
)
col_steps
=
self
.
rng
.
randint
(
1
,
4
)
time_steps
=
self
.
rng
.
randint
(
1
,
4
)
numFilters
=
self
.
rng
.
randint
(
1
,
3
)
row_steps
=
self
.
rng
.
randint
(
1
,
4
)
col_steps
=
self
.
rng
.
randint
(
1
,
4
)
time_steps
=
self
.
rng
.
randint
(
1
,
4
)
#print (row_steps,col_steps,time_steps)
videoDur
=
(
time_steps
-
1
)
*
dt
+
filterDur
+
self
.
rng
.
randint
(
0
,
3
)
videoWidth
=
(
col_steps
-
1
)
*
dc
+
filterWidth
+
self
.
rng
.
randint
(
0
,
3
)
videoHeight
=
(
row_steps
-
1
)
*
dr
+
filterHeight
+
self
.
rng
.
randint
(
0
,
3
)
videoDur
=
(
time_steps
-
1
)
*
dt
+
filterDur
+
\
self
.
rng
.
randint
(
0
,
3
)
videoWidth
=
(
col_steps
-
1
)
*
dc
+
filterWidth
+
\
self
.
rng
.
randint
(
0
,
3
)
videoHeight
=
(
row_steps
-
1
)
*
dr
+
filterHeight
+
\
self
.
rng
.
randint
(
0
,
3
)
inputChannels
=
self
.
rng
.
randint
(
1
,
15
)
inputChannels
=
self
.
rng
.
randint
(
1
,
15
)
self
.
W
.
set_value
(
self
.
random_tensor
(
numFilters
,
filterHeight
,
filterWidth
,
filterDur
,
inputChannels
),
borrow
=
True
)
self
.
W
.
set_value
(
self
.
random_tensor
(
numFilters
,
filterHeight
,
filterWidth
,
filterDur
,
inputChannels
),
borrow
=
True
)
self
.
b
.
set_value
(
self
.
random_tensor
(
numFilters
),
borrow
=
True
)
#just needed so H_shape works
self
.
V
.
set_value
(
self
.
random_tensor
(
batchSize
,
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
),
borrow
=
True
)
self
.
V
.
set_value
(
self
.
random_tensor
(
batchSize
,
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
),
borrow
=
True
)
self
.
rb
.
set_value
(
self
.
random_tensor
(
inputChannels
),
borrow
=
True
)
H_shape
=
self
.
H_shape_func
()
#make index maps
h
=
N
.
zeros
(
H_shape
[
1
:])
r
=
N
.
zeros
(
H_shape
[
1
:])
c
=
N
.
zeros
(
H_shape
[
1
:])
t
=
N
.
zeros
(
H_shape
[
1
:])
for
qi
in
xrange
(
0
,
H_shape
[
4
]):
h
[:,
:,:,
qi
]
=
qi
for
qi
in
xrange
(
0
,
H_shape
[
1
]):
r
[
qi
,
:,:,
:]
=
qi
for
qi
in
xrange
(
0
,
H_shape
[
2
]):
c
[:,
qi
,:,
:]
=
qi
for
qi
in
xrange
(
0
,
H_shape
[
3
]):
t
[:,
:,
qi
,
:]
=
qi
h
=
N
.
zeros
(
H_shape
[
1
:])
r
=
N
.
zeros
(
H_shape
[
1
:])
c
=
N
.
zeros
(
H_shape
[
1
:])
t
=
N
.
zeros
(
H_shape
[
1
:])
for
qi
in
xrange
(
0
,
H_shape
[
4
]):
h
[:,
:,
:,
qi
]
=
qi
for
qi
in
xrange
(
0
,
H_shape
[
1
]):
r
[
qi
,
:,
:,
:]
=
qi
for
qi
in
xrange
(
0
,
H_shape
[
2
]):
c
[:,
qi
,
:,
:]
=
qi
for
qi
in
xrange
(
0
,
H_shape
[
3
]):
t
[:,
:,
qi
,
:]
=
qi
hn
=
H_shape
[
1
]
*
H_shape
[
2
]
*
H_shape
[
3
]
*
H_shape
[
4
]
...
...
@@ -377,21 +413,20 @@ class TestConv3D(utt.InferShapeTester):
c
=
c
.
reshape
((
hn
))
t
=
t
.
reshape
((
hn
))
Hv
=
self
.
random_tensor
(
*
H_shape
)
Hv
=
self
.
random_tensor
(
*
H_shape
)
Vv
=
self
.
transp_func
(
Hv
,[
videoHeight
,
videoWidth
,
videoDur
])
Vv
=
self
.
transp_func
(
Hv
,
[
videoHeight
,
videoWidth
,
videoDur
])
n
=
inputChannels
*
videoHeight
*
videoWidth
*
videoDur
rbim
=
N
.
zeros
((
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
))
for
qi
in
xrange
(
0
,
inputChannels
):
rbim
[:,
:,:,
qi
]
=
self
.
rb
.
get_value
(
borrow
=
True
)[
qi
]
rbim
=
N
.
zeros
((
videoHeight
,
videoWidth
,
videoDur
,
inputChannels
))
for
qi
in
xrange
(
0
,
inputChannels
):
rbim
[:,
:,
:,
qi
]
=
self
.
rb
.
get_value
(
borrow
=
True
)[
qi
]
rbv
=
rbim
.
reshape
((
n
))
W_mat
=
N
.
zeros
((
hn
,
n
))
W_mat
=
N
.
zeros
((
hn
,
n
))
Vv_mat
=
N
.
zeros
((
n
,
batchSize
))
Hv_mat
=
N
.
zeros
((
hn
,
batchSize
))
for
qi
in
xrange
(
0
,
hn
):
Hv_mat
=
N
.
zeros
((
hn
,
batchSize
))
for
qi
in
xrange
(
0
,
hn
):
hi
=
h
[
qi
]
ri
=
r
[
qi
]
ci
=
c
[
qi
]
...
...
@@ -400,36 +435,35 @@ class TestConv3D(utt.InferShapeTester):
placed_filter
=
N
.
zeros
(
self
.
V
.
get_value
(
borrow
=
True
)
.
shape
[
1
:])
placed_filter
[
ri
*
dr
:
ri
*
dr
+
self
.
W
.
get_value
(
borrow
=
True
)
.
shape
[
1
],
ci
*
dc
:
ci
*
dc
+
self
.
W
.
get_value
(
borrow
=
True
)
.
shape
[
2
],
ti
*
dt
:
ti
*
dt
+
self
.
W
.
get_value
(
borrow
=
True
)
.
shape
[
3
],
:]
=
self
.
W
.
get_value
(
borrow
=
True
)[
hi
,:,:,:,:]
W_mat
[
qi
,:]
=
placed_filter
.
reshape
((
n
))
Hv_mat
[
qi
,:]
=
Hv
[:,
ri
,
ci
,
ti
,
hi
]
for
qi
in
xrange
(
0
,
batchSize
):
Vv_mat
[:,
qi
]
=
Vv
[
qi
,:,:,:,:]
.
reshape
((
n
))
ri
*
dr
:
ri
*
dr
+
self
.
W
.
get_value
(
borrow
=
True
)
.
shape
[
1
],
ci
*
dc
:
ci
*
dc
+
self
.
W
.
get_value
(
borrow
=
True
)
.
shape
[
2
],
ti
*
dt
:
ti
*
dt
+
self
.
W
.
get_value
(
borrow
=
True
)
.
shape
[
3
],
:]
=
self
.
W
.
get_value
(
borrow
=
True
)[
hi
,
:,
:,
:,
:]
W_mat
[
qi
,
:]
=
placed_filter
.
reshape
((
n
))
Hv_mat
[
qi
,
:]
=
Hv
[:,
ri
,
ci
,
ti
,
hi
]
for
qi
in
xrange
(
0
,
batchSize
):
Vv_mat
[:,
qi
]
=
Vv
[
qi
,
:,
:,
:,
:]
.
reshape
((
n
))
W_mat_T
=
sparse
.
csr_matrix
(
W_mat
.
transpose
())
temp
=
W_mat_T
*
Hv_mat
V_mat
=
(
temp
.
transpose
()
+
rbv
)
.
transpose
()
if
N
.
abs
(
V_mat
-
Vv_mat
)
.
max
()
>
1e-5
:
if
N
.
abs
(
V_mat
-
Vv_mat
)
.
max
()
>
1e-5
:
print
'mul'
print
V_mat
print
'conv'
print
Vv_mat
for
i
in
xrange
(
0
,
n
):
for
j
in
xrange
(
0
,
batchSize
):
if
abs
(
V_mat
[
i
,
j
]
-
Vv_mat
[
i
,
j
])
>
1e-5
:
print
'wrong at
%
d,
%
d:
%
f mul versus
%
f conv'
%
(
i
,
j
,
V_mat
[
i
,
j
],
Vv_mat
[
i
,
j
])
for
i
in
xrange
(
0
,
n
):
for
j
in
xrange
(
0
,
batchSize
):
if
abs
(
V_mat
[
i
,
j
]
-
Vv_mat
[
i
,
j
])
>
1e-5
:
print
(
'wrong at
%
d,
%
d:
%
f mul versus
%
f conv'
%
(
i
,
j
,
V_mat
[
i
,
j
],
Vv_mat
[
i
,
j
]))
assert
False
def
test_infer_shape
(
self
):
self
.
randomize
()
# Conv3D
self
.
_compile_and_check
([],
[
self
.
H
],
[],
Conv3D
)
...
...
@@ -446,24 +480,19 @@ class TestConv3D(utt.InferShapeTester):
def
test_gradient
(
self
):
self
.
randomize
()
rng
,
V
,
W
,
b
,
d
,
rb
=
self
.
rng
,
self
.
V
,
self
.
W
,
self
.
b
,
self
.
d
,
self
.
rb
dCdH
=
shared
(
self
.
random_tensor
(
*
self
.
H_shape_func
()
))
rng
,
V
,
W
,
b
,
d
,
rb
=
self
.
rng
,
self
.
V
,
self
.
W
,
self
.
b
,
self
.
d
,
self
.
rb
dCdH
=
shared
(
self
.
random_tensor
(
*
self
.
H_shape_func
()
))
testsPerDir
=
2
theano
.
tests
.
unittest_tools
.
verify_grad
(
DummyConv3D
(
rng
,
(
V
,
W
,
b
),
d
),
[
0.0
],
n_tests
=
testsPerDir
)
theano
.
tests
.
unittest_tools
.
verify_grad
(
DummyConvTransp3D
(
rng
,
(
W
,
rb
,
dCdH
),
d
,
V
.
get_value
(
borrow
=
True
)
.
shape
[
1
:
4
]),
[
0.0
],
n_tests
=
testsPerDir
)
theano
.
tests
.
unittest_tools
.
verify_grad
(
DummyConvGrad3D
(
rng
,
(
V
,
dCdH
),
d
,
W
.
get_value
(
borrow
=
True
)
.
shape
),
[
0.0
],
n_tests
=
testsPerDir
)
theano
.
tests
.
unittest_tools
.
verify_grad
(
DummyConv3D
(
rng
,
(
V
,
W
,
b
),
d
),
[
0.0
],
n_tests
=
testsPerDir
)
theano
.
tests
.
unittest_tools
.
verify_grad
(
DummyConvTransp3D
(
rng
,
(
W
,
rb
,
dCdH
),
d
,
V
.
get_value
(
borrow
=
True
)
.
shape
[
1
:
4
]),
[
0.0
],
n_tests
=
testsPerDir
)
theano
.
tests
.
unittest_tools
.
verify_grad
(
DummyConvGrad3D
(
rng
,
(
V
,
dCdH
),
d
,
W
.
get_value
(
borrow
=
True
)
.
shape
),
[
0.0
],
n_tests
=
testsPerDir
)
if
__name__
==
'__main__'
:
t
=
TestConv3D
(
'setUp'
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论