Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
b483da5e
提交
b483da5e
authored
9月 11, 2012
作者:
Ian Goodfellow
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pep8 test_mlp.py
上级
3f734be4
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
75 行增加
和
64 行删除
+75
-64
test_mlp.py
theano/sandbox/cuda/tests/test_mlp.py
+75
-64
没有找到文件。
theano/sandbox/cuda/tests/test_mlp.py
浏览文件 @
b483da5e
...
@@ -172,26 +172,27 @@ def run_conv_nnet1(use_gpu):
...
@@ -172,26 +172,27 @@ def run_conv_nnet1(use_gpu):
if
config
.
mode
==
'DEBUG_MODE'
:
if
config
.
mode
==
'DEBUG_MODE'
:
n_train
=
1
n_train
=
1
logical_hid_shape
=
tcn
.
blas
.
GpuConv
.
logical_output_shape_2d
(
shape_img
[
2
:],
shape_kern
[
2
:],
'valid'
)
logical_hid_shape
=
tcn
.
blas
.
GpuConv
.
logical_output_shape_2d
(
shape_img
[
2
:],
shape_kern
[
2
:],
'valid'
)
n_hid
=
n_kern
*
logical_hid_shape
[
0
]
*
logical_hid_shape
[
1
]
n_hid
=
n_kern
*
logical_hid_shape
[
0
]
*
logical_hid_shape
[
1
]
n_out
=
10
n_out
=
10
w
=
shared_fn
(
0.01
*
(
my_rand
(
*
shape_kern
)
-
0.5
),
'w'
)
w
=
shared_fn
(
0.01
*
(
my_rand
(
*
shape_kern
)
-
0.5
),
'w'
)
b
=
shared_fn
(
my_zeros
((
n_kern
,)),
'b'
)
b
=
shared_fn
(
my_zeros
((
n_kern
,)),
'b'
)
v
=
shared_fn
(
my_zeros
((
n_hid
,
n_out
)),
'c'
)
v
=
shared_fn
(
my_zeros
((
n_hid
,
n_out
)),
'c'
)
c
=
shared_fn
(
my_zeros
(
n_out
),
'c'
)
c
=
shared_fn
(
my_zeros
(
n_out
),
'c'
)
x
=
tensor
.
Tensor
(
dtype
=
'float32'
,
broadcastable
=
(
0
,
1
,
0
,
0
))(
'x'
)
x
=
tensor
.
Tensor
(
dtype
=
'float32'
,
broadcastable
=
(
0
,
1
,
0
,
0
))(
'x'
)
y
=
tensor
.
fmatrix
(
'y'
)
y
=
tensor
.
fmatrix
(
'y'
)
lr
=
tensor
.
fscalar
(
'lr'
)
lr
=
tensor
.
fscalar
(
'lr'
)
conv_op
=
conv
.
ConvOp
(
shape_img
[
2
:],
shape_kern
[
2
:],
n_kern
,
n_batch
,
1
,
1
)
conv_op
=
conv
.
ConvOp
(
shape_img
[
2
:],
shape_kern
[
2
:],
n_kern
,
n_batch
,
1
,
1
)
conv_op
.
set_flops
()
conv_op
.
set_flops
()
hid
=
tensor
.
tanh
(
conv_op
(
x
,
w
)
+
b
.
dimshuffle
((
0
,
'x'
,
'x'
)))
hid
=
tensor
.
tanh
(
conv_op
(
x
,
w
)
+
b
.
dimshuffle
((
0
,
'x'
,
'x'
)))
hid_flat
=
hid
.
reshape
((
n_batch
,
n_hid
))
hid_flat
=
hid
.
reshape
((
n_batch
,
n_hid
))
out
=
tensor
.
tanh
(
tensor
.
dot
(
hid_flat
,
v
)
+
c
)
out
=
tensor
.
tanh
(
tensor
.
dot
(
hid_flat
,
v
)
+
c
)
loss
=
tensor
.
sum
(
0.5
*
(
out
-
y
)
**
2
*
lr
)
loss
=
tensor
.
sum
(
0.5
*
(
out
-
y
)
**
2
*
lr
)
#print 'loss type', loss.type
#print 'loss type', loss.type
params
=
[
w
,
b
,
v
,
c
]
params
=
[
w
,
b
,
v
,
c
]
...
@@ -200,7 +201,8 @@ def run_conv_nnet1(use_gpu):
...
@@ -200,7 +201,8 @@ def run_conv_nnet1(use_gpu):
mode
=
get_mode
(
use_gpu
)
mode
=
get_mode
(
use_gpu
)
#print 'building pfunc ...'
#print 'building pfunc ...'
train
=
pfunc
([
x
,
y
,
lr
],
[
loss
],
mode
=
mode
,
updates
=
[(
p
,
p
-
g
)
for
p
,
g
in
zip
(
params
,
gparams
)])
train
=
pfunc
([
x
,
y
,
lr
],
[
loss
],
mode
=
mode
,
updates
=
[(
p
,
p
-
g
)
for
p
,
g
in
zip
(
params
,
gparams
)])
# for i, n in enumerate(train.maker.fgraph.toposort()):
# for i, n in enumerate(train.maker.fgraph.toposort()):
# print i, n
# print i, n
...
@@ -221,7 +223,7 @@ def test_conv_nnet1():
...
@@ -221,7 +223,7 @@ def test_conv_nnet1():
rval_cpu
=
run_conv_nnet1
(
False
)
rval_cpu
=
run_conv_nnet1
(
False
)
utt
.
seed_rng
()
utt
.
seed_rng
()
rval_gpu
=
run_conv_nnet1
(
True
)
rval_gpu
=
run_conv_nnet1
(
True
)
assert
numpy
.
allclose
(
rval_cpu
,
rval_gpu
,
rtol
=
1e-4
,
atol
=
1e-6
)
assert
numpy
.
allclose
(
rval_cpu
,
rval_gpu
,
rtol
=
1e-4
,
atol
=
1e-6
)
def
run_conv_nnet2
(
use_gpu
):
# pretend we are training LeNet for MNIST
def
run_conv_nnet2
(
use_gpu
):
# pretend we are training LeNet for MNIST
...
@@ -239,10 +241,8 @@ def run_conv_nnet2(use_gpu): # pretend we are training LeNet for MNIST
...
@@ -239,10 +241,8 @@ def run_conv_nnet2(use_gpu): # pretend we are training LeNet for MNIST
#n_train=10, n_batch=60, n_kern=10, n_kern1=10, error see of -5.26905e-05
#n_train=10, n_batch=60, n_kern=10, n_kern1=10, error see of -5.26905e-05
#n_train=30, n_batch=60, n_kern=10, n_kern1=10, error see of -3.8147e-06
#n_train=30, n_batch=60, n_kern=10, n_kern1=10, error see of -3.8147e-06
#n_train=30, n_batch=60, n_kern=20, n_kern1=10, error see of 6.82771e-05
#n_train=30, n_batch=60, n_kern=20, n_kern1=10, error see of 6.82771e-05
#n_train=30, n_batch=60, n_kern=20, n_kern1=30, error see of 0.000231534
#n_train=30, n_batch=60, n_kern=20, n_kern1=30, error see of 0.000231534
n_batch
=
60
n_batch
=
60
shape_img
=
(
n_batch
,
1
,
32
,
32
)
shape_img
=
(
n_batch
,
1
,
32
,
32
)
...
@@ -252,35 +252,40 @@ def run_conv_nnet2(use_gpu): # pretend we are training LeNet for MNIST
...
@@ -252,35 +252,40 @@ def run_conv_nnet2(use_gpu): # pretend we are training LeNet for MNIST
n_kern1
=
10
n_kern1
=
10
shape_kern1
=
(
n_kern1
,
n_kern
,
5
,
5
)
shape_kern1
=
(
n_kern1
,
n_kern
,
5
,
5
)
n_train
=
30
n_train
=
30
if
config
.
mode
==
'DEBUG_MODE'
:
n_train
=
1
if
config
.
mode
==
'DEBUG_MODE'
:
n_train
=
1
logical_hid_shape
=
tcn
.
blas
.
GpuConv
.
logical_output_shape_2d
(
tuple
(
shape_img
[
2
:]),
tuple
(
shape_kern
[
2
:]),
'valid'
)
logical_hid_shape
=
tcn
.
blas
.
GpuConv
.
logical_output_shape_2d
(
tuple
(
logical_hid_shape1
=
tcn
.
blas
.
GpuConv
.
logical_output_shape_2d
((
logical_hid_shape
[
0
]
/
2
,
logical_hid_shape
[
1
]
/
2
),
tuple
(
shape_kern1
[
2
:]),
'valid'
)
shape_img
[
2
:]),
tuple
(
shape_kern
[
2
:]),
'valid'
)
logical_hid_shape1
=
tcn
.
blas
.
GpuConv
.
logical_output_shape_2d
((
logical_hid_shape
[
0
]
/
2
,
logical_hid_shape
[
1
]
/
2
),
tuple
(
shape_kern1
[
2
:]),
'valid'
)
n_hid
=
n_kern1
*
logical_hid_shape1
[
0
]
*
logical_hid_shape1
[
1
]
n_hid
=
n_kern1
*
logical_hid_shape1
[
0
]
*
logical_hid_shape1
[
1
]
n_out
=
10
n_out
=
10
w0
=
shared_fn
(
0.01
*
(
my_rand
(
*
shape_kern
)
-
0.5
),
'w0'
)
w0
=
shared_fn
(
0.01
*
(
my_rand
(
*
shape_kern
)
-
0.5
),
'w0'
)
b0
=
shared_fn
(
my_zeros
((
n_kern
,)),
'b0'
)
b0
=
shared_fn
(
my_zeros
((
n_kern
,)),
'b0'
)
w1
=
shared_fn
(
0.01
*
(
my_rand
(
*
shape_kern1
)
-
0.5
),
'w1'
)
w1
=
shared_fn
(
0.01
*
(
my_rand
(
*
shape_kern1
)
-
0.5
),
'w1'
)
b1
=
shared_fn
(
my_zeros
((
n_kern1
,)),
'b1'
)
b1
=
shared_fn
(
my_zeros
((
n_kern1
,)),
'b1'
)
v
=
shared_fn
(
my_zeros
((
n_hid
,
n_out
)),
'c'
)
v
=
shared_fn
(
my_zeros
((
n_hid
,
n_out
)),
'c'
)
c
=
shared_fn
(
my_zeros
(
n_out
),
'c'
)
c
=
shared_fn
(
my_zeros
(
n_out
),
'c'
)
x
=
tensor
.
Tensor
(
dtype
=
'float32'
,
broadcastable
=
(
0
,
1
,
0
,
0
))(
'x'
)
x
=
tensor
.
Tensor
(
dtype
=
'float32'
,
broadcastable
=
(
0
,
1
,
0
,
0
))(
'x'
)
y
=
tensor
.
fmatrix
(
'y'
)
y
=
tensor
.
fmatrix
(
'y'
)
lr
=
tensor
.
fscalar
(
'lr'
)
lr
=
tensor
.
fscalar
(
'lr'
)
conv_op
=
conv
.
ConvOp
(
shape_img
[
2
:],
shape_kern
[
2
:],
n_kern
,
n_batch
,
1
,
1
)
conv_op
=
conv
.
ConvOp
(
shape_img
[
2
:],
shape_kern
[
2
:],
n_kern
,
n_batch
,
1
,
1
)
conv_op1
=
conv
.
ConvOp
((
n_kern
,
logical_hid_shape
[
0
]
/
2
,
logical_hid_shape
[
1
]
/
2
),
shape_kern1
[
2
:],
n_kern1
,
n_batch
,
1
,
1
)
conv_op1
=
conv
.
ConvOp
((
n_kern
,
logical_hid_shape
[
0
]
/
2
,
logical_hid_shape
[
1
]
/
2
),
shape_kern1
[
2
:],
n_kern1
,
n_batch
,
1
,
1
)
conv_op
.
set_flops
()
conv_op
.
set_flops
()
conv_op1
.
set_flops
()
conv_op1
.
set_flops
()
hid
=
tensor
.
tanh
(
conv_op
(
x
,
w0
)
+
b0
.
dimshuffle
((
0
,
'x'
,
'x'
)))
hid
=
tensor
.
tanh
(
conv_op
(
x
,
w0
)
+
b0
.
dimshuffle
((
0
,
'x'
,
'x'
)))
hid1
=
tensor
.
tanh
(
conv_op1
(
hid
[:,:,::
2
,::
2
],
w1
)
+
b1
.
dimshuffle
((
0
,
'x'
,
'x'
)))
hid1
=
tensor
.
tanh
(
conv_op1
(
hid
[:,
:,
::
2
,
::
2
],
w1
)
+
b1
.
dimshuffle
((
0
,
'x'
,
'x'
)))
hid_flat
=
hid1
.
reshape
((
n_batch
,
n_hid
))
hid_flat
=
hid1
.
reshape
((
n_batch
,
n_hid
))
out
=
tensor
.
tanh
(
tensor
.
dot
(
hid_flat
,
v
)
+
c
)
out
=
tensor
.
tanh
(
tensor
.
dot
(
hid_flat
,
v
)
+
c
)
loss
=
tensor
.
sum
(
0.5
*
(
out
-
y
)
**
2
*
lr
)
loss
=
tensor
.
sum
(
0.5
*
(
out
-
y
)
**
2
*
lr
)
#print 'loss type', loss.type
#print 'loss type', loss.type
params
=
[
w0
,
b0
,
w1
,
b1
,
v
,
c
]
params
=
[
w0
,
b0
,
w1
,
b1
,
v
,
c
]
...
@@ -289,13 +294,14 @@ def run_conv_nnet2(use_gpu): # pretend we are training LeNet for MNIST
...
@@ -289,13 +294,14 @@ def run_conv_nnet2(use_gpu): # pretend we are training LeNet for MNIST
mode
=
get_mode
(
use_gpu
)
mode
=
get_mode
(
use_gpu
)
#print 'building pfunc ...'
#print 'building pfunc ...'
train
=
pfunc
([
x
,
y
,
lr
],
[
loss
],
mode
=
mode
,
updates
=
[(
p
,
p
-
g
)
for
p
,
g
in
zip
(
params
,
gparams
)])
train
=
pfunc
([
x
,
y
,
lr
],
[
loss
],
mode
=
mode
,
updates
=
[(
p
,
p
-
g
)
for
p
,
g
in
zip
(
params
,
gparams
)])
# for i, n in enumerate(train.maker.fgraph.toposort()):
# for i, n in enumerate(train.maker.fgraph.toposort()):
# print i, n
# print i, n
xval
=
my_rand
(
*
shape_img
)
xval
=
my_rand
(
*
shape_img
)
yval
=
my_rand
(
n_batch
,
n_out
)
#
int32 make all 0...
yval
=
my_rand
(
n_batch
,
n_out
)
#
int32 make all 0...
lr
=
theano
.
_asarray
(
0.01
,
dtype
=
'float32'
)
lr
=
theano
.
_asarray
(
0.01
,
dtype
=
'float32'
)
for
i
in
xrange
(
n_train
):
for
i
in
xrange
(
n_train
):
rval
=
train
(
xval
,
yval
,
lr
)
rval
=
train
(
xval
,
yval
,
lr
)
...
@@ -311,7 +317,7 @@ def test_conv_nnet2():
...
@@ -311,7 +317,7 @@ def test_conv_nnet2():
utt
.
seed_rng
()
utt
.
seed_rng
()
rval_cpu
=
run_conv_nnet2
(
False
)
rval_cpu
=
run_conv_nnet2
(
False
)
#print rval_cpu[0], rval_gpu[0],rval_cpu[0]-rval_gpu[0]
#print rval_cpu[0], rval_gpu[0],rval_cpu[0]-rval_gpu[0]
assert
numpy
.
allclose
(
rval_cpu
,
rval_gpu
,
rtol
=
1e-4
,
atol
=
1e-4
)
assert
numpy
.
allclose
(
rval_cpu
,
rval_gpu
,
rtol
=
1e-4
,
atol
=
1e-4
)
def
build_conv_nnet2_classif
(
use_gpu
,
isize
,
ksize
,
n_batch
,
def
build_conv_nnet2_classif
(
use_gpu
,
isize
,
ksize
,
n_batch
,
...
@@ -322,11 +328,11 @@ def build_conv_nnet2_classif(use_gpu, isize, ksize, n_batch,
...
@@ -322,11 +328,11 @@ def build_conv_nnet2_classif(use_gpu, isize, ksize, n_batch,
else
:
else
:
shared_fn
=
shared
shared_fn
=
shared
isize1
=
isize
isize1
=
isize
isize2
=
isize
isize2
=
isize
if
isinstance
(
isize
,
(
tuple
,
)):
if
isinstance
(
isize
,
(
tuple
,
)):
isize1
=
isize
[
0
]
isize1
=
isize
[
0
]
isize2
=
isize
[
1
]
isize2
=
isize
[
1
]
shape_img
=
(
n_batch
,
1
,
isize1
,
isize2
)
shape_img
=
(
n_batch
,
1
,
isize1
,
isize2
)
...
@@ -336,45 +342,47 @@ def build_conv_nnet2_classif(use_gpu, isize, ksize, n_batch,
...
@@ -336,45 +342,47 @@ def build_conv_nnet2_classif(use_gpu, isize, ksize, n_batch,
n_kern1
=
30
# 16 were used in LeNet5
n_kern1
=
30
# 16 were used in LeNet5
shape_kern1
=
(
n_kern1
,
n_kern
,
ksize
,
ksize
)
shape_kern1
=
(
n_kern1
,
n_kern
,
ksize
,
ksize
)
logical_hid_shape
=
tcn
.
blas
.
GpuConv
.
logical_output_shape_2d
((
isize1
,
isize2
),
(
ksize
,
ksize
),
'valid'
)
logical_hid_shape
=
tcn
.
blas
.
GpuConv
.
logical_output_shape_2d
((
isize1
,
isize2
),
(
ksize
,
ksize
),
'valid'
)
logical_hid_shape1
=
tcn
.
blas
.
GpuConv
.
logical_output_shape_2d
((
logical_hid_shape
[
0
]
/
2
,
logical_hid_shape1
=
tcn
.
blas
.
GpuConv
.
logical_output_shape_2d
((
logical_hid_shape
[
0
]
/
2
,
logical_hid_shape
[
1
]
/
2
),
(
ksize
,
ksize
),
'valid'
)
logical_hid_shape
[
1
]
/
2
),
(
ksize
,
ksize
),
'valid'
)
n_hid
=
n_kern1
*
logical_hid_shape1
[
0
]
*
logical_hid_shape1
[
1
]
n_hid
=
n_kern1
*
logical_hid_shape1
[
0
]
*
logical_hid_shape1
[
1
]
n_out
=
10
n_out
=
10
w0
=
shared_fn
(
0.01
*
(
my_rand
(
*
shape_kern
)
-
0.5
),
'w0'
)
w0
=
shared_fn
(
0.01
*
(
my_rand
(
*
shape_kern
)
-
0.5
),
'w0'
)
b0
=
shared_fn
(
my_zeros
((
n_kern
,)),
'b0'
)
b0
=
shared_fn
(
my_zeros
((
n_kern
,)),
'b0'
)
w1
=
shared_fn
(
0.01
*
(
my_rand
(
*
shape_kern1
)
-
0.5
),
'w1'
)
w1
=
shared_fn
(
0.01
*
(
my_rand
(
*
shape_kern1
)
-
0.5
),
'w1'
)
b1
=
shared_fn
(
my_zeros
((
n_kern1
,)),
'b1'
)
b1
=
shared_fn
(
my_zeros
((
n_kern1
,)),
'b1'
)
v
=
shared_fn
(
0.01
*
my_randn
(
n_hid
,
n_out
),
'v'
)
v
=
shared_fn
(
0.01
*
my_randn
(
n_hid
,
n_out
),
'v'
)
c
=
shared_fn
(
my_zeros
(
n_out
),
'c'
)
c
=
shared_fn
(
my_zeros
(
n_out
),
'c'
)
#print 'ALLOCATING ARCH: w0 shape', w0.get_value(borrow=True).shape
#print 'ALLOCATING ARCH: w0 shape', w0.get_value(borrow=True).shape
#print 'ALLOCATING ARCH: w1 shape', w1.get_value(borrow=True).shape
#print 'ALLOCATING ARCH: w1 shape', w1.get_value(borrow=True).shape
#print 'ALLOCATING ARCH: v shape', v.get_value(borrow=True).shape
#print 'ALLOCATING ARCH: v shape', v.get_value(borrow=True).shape
x
=
tensor
.
Tensor
(
dtype
=
'float32'
,
broadcastable
=
(
0
,
1
,
0
,
0
))(
'x'
)
x
=
tensor
.
Tensor
(
dtype
=
'float32'
,
broadcastable
=
(
0
,
1
,
0
,
0
))(
'x'
)
y
=
tensor
.
fmatrix
(
'y'
)
y
=
tensor
.
fmatrix
(
'y'
)
lr
=
tensor
.
fscalar
(
'lr'
)
lr
=
tensor
.
fscalar
(
'lr'
)
conv_op
=
conv
.
ConvOp
(
shape_img
[
2
:],
shape_kern
[
2
:],
n_kern
,
conv_op
=
conv
.
ConvOp
(
shape_img
[
2
:],
shape_kern
[
2
:],
n_kern
,
n_batch
,
1
,
1
,
verbose
=
verbose
,
version
=
version
)
n_batch
,
1
,
1
,
verbose
=
verbose
,
version
=
version
)
conv_op1
=
conv
.
ConvOp
(
conv_op1
=
conv
.
ConvOp
(
(
n_kern
,
logical_hid_shape
[
0
]
/
2
,
logical_hid_shape
[
1
]
/
2
),
(
n_kern
,
logical_hid_shape
[
0
]
/
2
,
logical_hid_shape
[
1
]
/
2
),
shape_kern1
[
2
:],
n_kern1
,
n_batch
,
1
,
1
,
verbose
=
verbose
,
version
=
version
)
shape_kern1
[
2
:],
n_kern1
,
n_batch
,
1
,
1
,
verbose
=
verbose
,
version
=
version
)
conv_op
.
set_flops
()
conv_op
.
set_flops
()
conv_op1
.
set_flops
()
conv_op1
.
set_flops
()
ds_op
=
downsample
.
DownsampleFactorMax
((
2
,
2
),
ignore_border
=
False
)
ds_op
=
downsample
.
DownsampleFactorMax
((
2
,
2
),
ignore_border
=
False
)
if
downsample_ops
:
if
downsample_ops
:
hid
=
tensor
.
tanh
(
ds_op
(
conv_op
(
x
,
w0
)
+
b0
.
dimshuffle
((
0
,
'x'
,
'x'
))))
hid
=
tensor
.
tanh
(
ds_op
(
conv_op
(
x
,
w0
)
+
b0
.
dimshuffle
((
0
,
'x'
,
'x'
))))
else
:
else
:
hid
=
tensor
.
tanh
((
conv_op
(
x
,
w0
)
+
b0
.
dimshuffle
((
0
,
'x'
,
'x'
)))[:,:,::
2
,::
2
])
hid
=
tensor
.
tanh
((
conv_op
(
x
,
w0
)
+
b0
.
dimshuffle
((
0
,
'x'
,
'x'
)
hid1
=
tensor
.
tanh
(
conv_op1
(
hid
,
w1
)
+
b1
.
dimshuffle
((
0
,
'x'
,
'x'
)))
))[:,
:,
::
2
,
::
2
])
hid1
=
tensor
.
tanh
(
conv_op1
(
hid
,
w1
)
+
b1
.
dimshuffle
((
0
,
'x'
,
'x'
)))
hid_flat
=
hid1
.
reshape
((
n_batch
,
n_hid
))
hid_flat
=
hid1
.
reshape
((
n_batch
,
n_hid
))
out
=
tensor
.
nnet
.
softmax
(
tensor
.
dot
(
hid_flat
,
v
)
+
c
)
out
=
tensor
.
nnet
.
softmax
(
tensor
.
dot
(
hid_flat
,
v
)
+
c
)
loss
=
tensor
.
sum
(
tensor
.
nnet
.
crossentropy_categorical_1hot
(
out
,
tensor
.
argmax
(
y
,
axis
=
1
))
*
lr
)
loss
=
tensor
.
sum
(
tensor
.
nnet
.
crossentropy_categorical_1hot
(
out
,
tensor
.
argmax
(
y
,
axis
=
1
))
*
lr
)
#print 'loss type', loss.type
#print 'loss type', loss.type
params
=
[
w0
,
b0
,
w1
,
b1
,
v
,
c
]
params
=
[
w0
,
b0
,
w1
,
b1
,
v
,
c
]
...
@@ -383,7 +391,8 @@ def build_conv_nnet2_classif(use_gpu, isize, ksize, n_batch,
...
@@ -383,7 +391,8 @@ def build_conv_nnet2_classif(use_gpu, isize, ksize, n_batch,
mode
=
get_mode
(
use_gpu
,
check_isfinite
)
mode
=
get_mode
(
use_gpu
,
check_isfinite
)
#print 'building pfunc ...'
#print 'building pfunc ...'
train
=
pfunc
([
x
,
y
,
lr
],
[
loss
],
mode
=
mode
,
updates
=
[(
p
,
p
-
g
)
for
p
,
g
in
zip
(
params
,
gparams
)])
train
=
pfunc
([
x
,
y
,
lr
],
[
loss
],
mode
=
mode
,
updates
=
[(
p
,
p
-
g
)
for
p
,
g
in
zip
(
params
,
gparams
)])
if
verbose
:
if
verbose
:
theano
.
printing
.
debugprint
(
train
)
theano
.
printing
.
debugprint
(
train
)
...
@@ -392,7 +401,7 @@ def build_conv_nnet2_classif(use_gpu, isize, ksize, n_batch,
...
@@ -392,7 +401,7 @@ def build_conv_nnet2_classif(use_gpu, isize, ksize, n_batch,
topo
=
train
.
maker
.
fgraph
.
toposort
()
topo
=
train
.
maker
.
fgraph
.
toposort
()
assert
len
([
n
for
n
in
topo
if
isinstance
(
n
.
op
,
tcn
.
blas
.
GpuConv
)])
>
0
assert
len
([
n
for
n
in
topo
if
isinstance
(
n
.
op
,
tcn
.
blas
.
GpuConv
)])
>
0
shape_target
=
(
n_batch
,
n_out
)
shape_target
=
(
n_batch
,
n_out
)
return
train
,
params
,
shape_img
,
shape_target
,
mode
return
train
,
params
,
shape_img
,
shape_target
,
mode
...
@@ -525,28 +534,30 @@ def cmp_run_conv_nnet2_classif(seed, isize, ksize, bsize,
...
@@ -525,28 +534,30 @@ def cmp_run_conv_nnet2_classif(seed, isize, ksize, bsize,
t0
=
time
.
time
()
t0
=
time
.
time
()
rval_cpu
=
train_cpu
(
xval
,
yval
,
lr
)[
0
]
rval_cpu
=
train_cpu
(
xval
,
yval
,
lr
)[
0
]
t1
=
time
.
time
()
t1
=
time
.
time
()
time_cpu
+=
(
t1
-
t0
)
time_cpu
+=
(
t1
-
t0
)
# Train one batch on GPU
# Train one batch on GPU
t0
=
time
.
time
()
t0
=
time
.
time
()
rval_gpu
=
train_gpu
(
xval
,
yval
,
lr
)[
0
]
rval_gpu
=
train_gpu
(
xval
,
yval
,
lr
)[
0
]
t1
=
time
.
time
()
t1
=
time
.
time
()
time_gpu
+=
(
t1
-
t0
)
time_gpu
+=
(
t1
-
t0
)
# Compare results
# Compare results
if
(
verbose
or
not
if
(
verbose
or
not
numpy
.
allclose
(
rval_cpu
,
rval_gpu
,
rtol
=
1e-5
,
atol
=
float_atol
)):
numpy
.
allclose
(
rval_cpu
,
rval_gpu
,
rtol
=
1e-5
,
atol
=
float_atol
)):
print
"At batch:"
,
i
+
1
print
"At batch:"
,
i
+
1
print
"CPU:"
,
rval_cpu
print
"CPU:"
,
rval_cpu
print
"GPU:"
,
rval_gpu
print
"GPU:"
,
rval_gpu
print
"abs diff:"
,
numpy
.
absolute
(
rval_gpu
-
rval_cpu
)
print
"abs diff:"
,
numpy
.
absolute
(
rval_gpu
-
rval_cpu
)
print
"rel diff:"
,
numpy
.
absolute
((
rval_gpu
-
rval_cpu
)
/
rval_gpu
)
print
"rel diff:"
,
numpy
.
absolute
((
rval_gpu
-
rval_cpu
)
/
rval_gpu
)
if
not
ignore_error
:
if
not
ignore_error
:
assert
numpy
.
allclose
(
rval_cpu
,
rval_gpu
,
rtol
=
1e-5
,
atol
=
float_atol
)
assert
numpy
.
allclose
(
rval_cpu
,
rval_gpu
,
rtol
=
1e-5
,
atol
=
float_atol
)
# Synchronize parameters to start from the same point next time
# Synchronize parameters to start from the same point next time
if
i
<
n_train
-
1
:
if
i
<
n_train
-
1
:
for
cpu_p
,
gpu_p
in
zip
(
params_cpu
,
params_gpu
):
for
cpu_p
,
gpu_p
in
zip
(
params_cpu
,
params_gpu
):
cpu_p
.
set_value
(
gpu_p
.
get_value
(
borrow
=
False
),
borrow
=
True
)
cpu_p
.
set_value
(
gpu_p
.
get_value
(
borrow
=
False
),
borrow
=
True
)
...
@@ -574,27 +585,27 @@ def cmp_run_conv_nnet2_classif(seed, isize, ksize, bsize,
...
@@ -574,27 +585,27 @@ def cmp_run_conv_nnet2_classif(seed, isize, ksize, bsize,
# Default parameters for all subsequent tests
# Default parameters for all subsequent tests
gpu_only
=
False
gpu_only
=
False
cpu_only
=
False
cpu_only
=
False
ignore_error
=
False
ignore_error
=
False
verbose
=
0
verbose
=
0
version
=
-
1
version
=
-
1
seed
=
utt
.
fetch_seed
()
seed
=
utt
.
fetch_seed
()
def
test_lenet_28
():
#
MNIST
def
test_lenet_28
():
#
MNIST
cmp_run_conv_nnet2_classif
(
seed
,
28
,
5
,
60
,
n_train
=
10
,
cmp_run_conv_nnet2_classif
(
seed
,
28
,
5
,
60
,
n_train
=
10
,
ignore_error
=
ignore_error
,
gpu_only
=
gpu_only
,
ignore_error
=
ignore_error
,
gpu_only
=
gpu_only
,
cpu_only
=
cpu_only
,
verbose
=
verbose
,
version
=
version
)
cpu_only
=
cpu_only
,
verbose
=
verbose
,
version
=
version
)
def
test_lenet_32
():
#
CIFAR10 / Shapeset
def
test_lenet_32
():
#
CIFAR10 / Shapeset
cmp_run_conv_nnet2_classif
(
seed
,
32
,
5
,
60
,
n_train
=
8
,
cmp_run_conv_nnet2_classif
(
seed
,
32
,
5
,
60
,
n_train
=
8
,
ignore_error
=
ignore_error
,
gpu_only
=
gpu_only
,
ignore_error
=
ignore_error
,
gpu_only
=
gpu_only
,
verbose
=
verbose
,
version
=
version
)
verbose
=
verbose
,
version
=
version
)
def
test_lenet_32_long
():
#
CIFAR10 / Shapeset
def
test_lenet_32_long
():
#
CIFAR10 / Shapeset
# this tests the gradient of downsample on the GPU,
# this tests the gradient of downsample on the GPU,
# which does not recieve specific testing
# which does not recieve specific testing
cmp_run_conv_nnet2_classif
(
seed
,
32
,
5
,
30
,
n_train
=
50
,
cmp_run_conv_nnet2_classif
(
seed
,
32
,
5
,
30
,
n_train
=
50
,
...
@@ -626,16 +637,16 @@ def test_lenet_256(): # ImageNet
...
@@ -626,16 +637,16 @@ def test_lenet_256(): # ImageNet
#I did a wanted error in the name as we don't want it to execute automatically for now as it don't work
#I did a wanted error in the name as we don't want it to execute automatically for now as it don't work
def
tes_lenet_hd
():
#
HD 720p: 1280(wid)x720(len)
def
tes_lenet_hd
():
#
HD 720p: 1280(wid)x720(len)
cmp_run_conv_nnet2_classif
(
seed
,
(
720
,
1280
),
9
,
2
,
n_train
=
3
,
cmp_run_conv_nnet2_classif
(
seed
,
(
720
,
1280
),
9
,
2
,
n_train
=
3
,
ignore_error
=
ignore_error
,
gpu_only
=
gpu_only
,
ignore_error
=
ignore_error
,
gpu_only
=
gpu_only
,
cpu_only
=
cpu_only
,
verbose
=
verbose
,
cpu_only
=
cpu_only
,
verbose
=
verbose
,
check_isfinite
=
True
,
version
=
version
)
check_isfinite
=
True
,
version
=
version
)
#I did a wanted error in the name as we don't want it to execute automatically for now as it don't work
#I did a wanted error in the name as we don't want it to execute automatically for now as it don't work
def
tes_lenet_full_hd
():
#
HD 1080p: 1920(wid)x1080(len)
def
tes_lenet_full_hd
():
#
HD 1080p: 1920(wid)x1080(len)
cmp_run_conv_nnet2_classif
(
seed
,
(
1080
,
1920
),
9
,
2
,
n_train
=
3
,
cmp_run_conv_nnet2_classif
(
seed
,
(
1080
,
1920
),
9
,
2
,
n_train
=
3
,
ignore_error
=
ignore_error
,
gpu_only
=
gpu_only
,
ignore_error
=
ignore_error
,
gpu_only
=
gpu_only
,
cpu_only
=
cpu_only
,
verbose
=
verbose
,
cpu_only
=
cpu_only
,
verbose
=
verbose
,
check_isfinite
=
True
,
version
=
version
)
check_isfinite
=
True
,
version
=
version
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论