Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
2413550e
提交
2413550e
authored
9月 15, 2009
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
差异文件
added longer lenet_32 test
上级
44903030
132c9d49
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
183 行增加
和
78 行删除
+183
-78
basic_ops.py
basic_ops.py
+0
-0
blas.py
blas.py
+77
-27
nnet.py
nnet.py
+0
-3
test_blas.py
tests/test_blas.py
+64
-17
test_nnet.py
tests/test_nnet.py
+34
-27
var.py
var.py
+8
-4
没有找到文件。
basic_ops.py
浏览文件 @
2413550e
差异被折叠。
点击展开。
blas.py
浏览文件 @
2413550e
...
@@ -174,9 +174,11 @@ class GpuConv(Op):
...
@@ -174,9 +174,11 @@ class GpuConv(Op):
def
make_node
(
self
,
img
,
kern
):
def
make_node
(
self
,
img
,
kern
):
if
img
.
type
.
ndim
!=
4
:
if
img
.
type
.
ndim
!=
4
:
raise
TypeError
(
'img must be 4D tensor'
)
raise
TypeError
(
'img must be 4D tensor'
)
if
img
.
type
!=
kern
.
type
:
if
kern
.
type
.
ndim
!=
4
:
raise
TypeError
(
'img and kern must have same type'
)
raise
TypeError
(
'kern must be 4D tensor'
)
return
Apply
(
self
,
[
img
,
kern
],
[
img
.
type
()])
broadcastable
=
[
img
.
type
.
broadcastable
[
0
],
kern
.
type
.
broadcastable
[
0
],
False
,
False
]
return
Apply
(
self
,
[
img
,
kern
],
[
CudaNdarrayType
(
broadcastable
)()])
def
perform
(
self
,
node
,
(
img
,
kern
),
(
out
,)):
def
perform
(
self
,
node
,
(
img
,
kern
),
(
out
,)):
out
[
0
]
=
cuda_ndarray
.
conv
(
img
,
kern
,
out
[
0
]
=
cuda_ndarray
.
conv
(
img
,
kern
,
...
@@ -187,13 +189,28 @@ class GpuConv(Op):
...
@@ -187,13 +189,28 @@ class GpuConv(Op):
kern_align
=
self
.
logical_kern_align_top
,
kern_align
=
self
.
logical_kern_align_top
,
verbose
=
0
)
verbose
=
0
)
from
theano.sandbox.downsample
import
DownsampleFactorMax
class
GpuDownsampleFactorMax
(
Op
):
class
GpuDownsampleFactorMax
(
DownsampleFactorMax
):
def
__init__
(
self
,
ds
,
ignore_border
=
False
):
# inherit __eq__, __hash__, __str__
self
.
ds
=
tuple
(
ds
)
self
.
ignore_border
=
ignore_border
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
and
self
.
ds
==
other
.
ds
and
self
.
ignore_border
==
other
.
ignore_border
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
ds
)
^
hash
(
self
.
ignore_border
)
def
__str__
(
self
):
return
'
%
s{
%
s,
%
s}'
%
(
self
.
__class__
.
__name__
,
self
.
ds
,
self
.
ignore_border
)
def
make_node
(
self
,
x
):
def
make_node
(
self
,
x
):
if
not
isinstance
(
x
.
type
,
CudaNdarrayType
):
raise
TypeError
()
if
not
x
.
type
.
ndim
==
4
:
raise
TypeError
()
return
Apply
(
self
,
[
x
],
[
x
.
type
()])
return
Apply
(
self
,
[
x
],
[
x
.
type
()])
def
perform
(
self
,
node
,
input_storage
,
output_storage
):
#
def perform(self, node, input_storage, output_storage):
raise
NotImplementedError
(
'only C is implemented'
)
#
raise NotImplementedError('only C is implemented')
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
()
return
()
def
c_code
(
self
,
node
,
nodename
,
(
x
,),
(
z
,),
sub
):
def
c_code
(
self
,
node
,
nodename
,
(
x
,),
(
z
,),
sub
):
...
@@ -240,8 +257,8 @@ class GpuDownsampleFactorMax(DownsampleFactorMax):
...
@@ -240,8 +257,8 @@ class GpuDownsampleFactorMax(DownsampleFactorMax):
//dim3 block(std::min(dims[3], 512)); //TODO: implement this by supporting more
//dim3 block(std::min(dims[3], 512)); //TODO: implement this by supporting more
//outputs than threads
//outputs than threads
dim3 block(dims[3]);
dim3 block(dims[3]);
i
nt shared= xdim3*sizeof(float);
i
f ((grid.x*grid.y) && dims[3])
kMaxPool_
%(nodename)
s<
%(ds0)
s,
%(ds1)
s> <<<grid, block,
shared
>>>(
kMaxPool_
%(nodename)
s<
%(ds0)
s,
%(ds1)
s> <<<grid, block,
xdim3*sizeof(float)
>>>(
dims[0], dims[1], dims[2], dims[3], xdim2, xdim3,
dims[0], dims[1], dims[2], dims[3], xdim2, xdim3,
CudaNdarray_DEV_DATA(cnda_
%(x)
s),
CudaNdarray_DEV_DATA(cnda_
%(x)
s),
CudaNdarray_HOST_STRIDES(cnda_
%(x)
s)[0],
CudaNdarray_HOST_STRIDES(cnda_
%(x)
s)[0],
...
@@ -253,8 +270,14 @@ class GpuDownsampleFactorMax(DownsampleFactorMax):
...
@@ -253,8 +270,14 @@ class GpuDownsampleFactorMax(DownsampleFactorMax):
cudaError_t err = cudaGetLastError();
cudaError_t err = cudaGetLastError();
if( cudaSuccess != err)
if( cudaSuccess != err)
{
{
PyErr_Format(PyExc_RuntimeError, "Cuda error:
%%
s:
%%
s.threads.x=
%%
d threads.y=
%%
d threads.z=
%%
d grid.x=
%%
d grid.y=
%%
d shared=
%%
d
\\
n", "kMaxPool_
%(nodename)
s",
PyErr_Format(PyExc_RuntimeError, "Cuda error:
%%
s:
%%
s. (grid:
%%
i x
%%
i; block:
%%
i x
%%
i x
%%
i)
\\
n",
cudaGetErrorString(err), block.x, block.y, block.z, grid.x, grid.y, shared);
"kMaxPool_
%(nodename)
s",
cudaGetErrorString(err),
grid.x,
grid.y,
block.x,
block.y,
block.z);
%(fail)
s;
%(fail)
s;
}
}
}
}
...
@@ -270,8 +293,8 @@ class GpuDownsampleFactorMax(DownsampleFactorMax):
...
@@ -270,8 +293,8 @@ class GpuDownsampleFactorMax(DownsampleFactorMax):
float *z)
float *z)
{
{
float cur_max, cur_x;
float cur_max, cur_x;
int i0 = blockIdx.x
/
D0;
int i0 = blockIdx.x
%%
D0;
int i1 = blockIdx.x
%%
D0;
int i1 = blockIdx.x
/
D0;
int i2 = blockIdx.y;
int i2 = blockIdx.y;
extern __shared__ float xbuf[]; //size [xD3]
extern __shared__ float xbuf[]; //size [xD3]
...
@@ -280,9 +303,9 @@ class GpuDownsampleFactorMax(DownsampleFactorMax):
...
@@ -280,9 +303,9 @@ class GpuDownsampleFactorMax(DownsampleFactorMax):
{
{
__syncthreads();
__syncthreads();
// load the current row of the image into shared memory
// load the current row of the image into shared memory
for (int
i3 = threadIdx.x; i3 < xD3; i3
+= blockDim.x)
for (int
j = threadIdx.x; j < xD3; j
+= blockDim.x)
{
{
xbuf[
i3] = x[i0*xS0 + i1*xS1 + (i2*pf2+r2)*xS2 + i3
*xS3];
xbuf[
j] = x[i0*xS0 + i1*xS1 + (i2*pf2+r2)*xS2 + j
*xS3];
}
}
__syncthreads();
__syncthreads();
...
@@ -290,10 +313,24 @@ class GpuDownsampleFactorMax(DownsampleFactorMax):
...
@@ -290,10 +313,24 @@ class GpuDownsampleFactorMax(DownsampleFactorMax):
cur_max = (r2 == 0) ? xbuf[threadIdx.x*pf3] : cur_max;
cur_max = (r2 == 0) ? xbuf[threadIdx.x*pf3] : cur_max;
// do a mini-reduction over the pf3 relevant elements in the current row
// do a mini-reduction over the pf3 relevant elements in the current row
for (int k = 0; k < pf3; ++k)
if (
%(ignore_border)
s)
{
for (int k = 0; k < pf3; ++k)
{
cur_x = xbuf[threadIdx.x*pf3+k];
cur_max = (cur_x > cur_max) ? cur_x : cur_max;
}
}
else
{
{
cur_x = xbuf[threadIdx.x*pf3+k];
for (int k = 0; k < pf3; ++k)
cur_max = (cur_x < cur_max) ? cur_x : cur_max;
{
if (threadIdx.x*pf3 + k < xD3)
{
cur_x = xbuf[threadIdx.x*pf3+k];
cur_max = (cur_x > cur_max) ? cur_x : cur_max;
}
}
}
}
}
}
...
@@ -302,13 +339,24 @@ class GpuDownsampleFactorMax(DownsampleFactorMax):
...
@@ -302,13 +339,24 @@ class GpuDownsampleFactorMax(DownsampleFactorMax):
}
}
"""
%
locals
()
"""
%
locals
()
from
theano.sandbox.downsample
import
DownsampleFactorMaxGrad
class
GpuDownsampleFactorMaxGrad
(
Op
):
class
GpuDownsampleFactorMaxGrad
(
DownsampleFactorMaxGrad
):
def
__init__
(
self
,
ds
,
ignore_border
):
# inherit __eq__, __hash__, __str__
self
.
ds
=
tuple
(
ds
)
self
.
ignore_border
=
ignore_border
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
and
self
.
ds
==
other
.
ds
and
self
.
ignore_border
==
other
.
ignore_border
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
ds
)
^
hash
(
self
.
ignore_border
)
def
__str__
(
self
):
return
'
%
s{
%
s,
%
s}'
%
(
self
.
__class__
.
__name__
,
self
.
ds
,
self
.
ignore_border
)
def
make_node
(
self
,
x
,
z
,
gz
):
def
make_node
(
self
,
x
,
z
,
gz
):
return
Apply
(
self
,
[
x
,
z
,
gz
],
[
x
.
type
()])
return
Apply
(
self
,
[
x
,
z
,
gz
],
[
x
.
type
()])
def
perform
(
self
,
node
,
input_storage
,
output_storage
):
#
def perform(self, node, input_storage, output_storage):
raise
NotImplementedError
(
'only C is implemented'
)
#
raise NotImplementedError('only C is implemented')
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
()
return
()
def
c_code
(
self
,
node
,
nodename
,
(
x
,
z
,
gz
),
(
gx
,),
sub
):
def
c_code
(
self
,
node
,
nodename
,
(
x
,
z
,
gz
),
(
gx
,),
sub
):
...
@@ -340,9 +388,9 @@ class GpuDownsampleFactorMaxGrad(DownsampleFactorMaxGrad):
...
@@ -340,9 +388,9 @@ class GpuDownsampleFactorMaxGrad(DownsampleFactorMaxGrad):
}
}
}
}
{
{
dim3 grid(CudaNdarray_HOST_DIMS(cnda_
%(x)
s)[0], CudaNdarray_HOST_DIMS(cnda_
%(x)
s)[2]);
//TODO: implement this by supporting more
//TODO: implement this by supporting more
//outputs than threads
//outputs than threads
dim3 grid(CudaNdarray_HOST_DIMS(cnda_
%(x)
s)[0], CudaNdarray_HOST_DIMS(cnda_
%(x)
s)[2]);
dim3 block(CudaNdarray_HOST_DIMS(cnda_
%(x)
s)[3]);
dim3 block(CudaNdarray_HOST_DIMS(cnda_
%(x)
s)[3]);
kDownsampleMaxGrad_
%(nodename)
s<
%(ds0)
s,
%(ds1)
s> <<<grid, block>>>(
kDownsampleMaxGrad_
%(nodename)
s<
%(ds0)
s,
%(ds1)
s> <<<grid, block>>>(
CudaNdarray_HOST_DIMS(cnda_
%(z)
s)[0],
CudaNdarray_HOST_DIMS(cnda_
%(z)
s)[0],
...
@@ -401,9 +449,11 @@ class GpuDownsampleFactorMaxGrad(DownsampleFactorMaxGrad):
...
@@ -401,9 +449,11 @@ class GpuDownsampleFactorMaxGrad(DownsampleFactorMaxGrad):
int i2 = blockIdx.y; // row wrt z and/or gz
int i2 = blockIdx.y; // row wrt z and/or gz
int x_col = threadIdx.x;
int x_col = threadIdx.x;
// The algorithm here is that every thread writes one output pixel per line
//TODO: raise occupancy. Use threadIdx.y to run several iterations of this i1 loop
//in parallel
for (i1 = 0; i1 < D1; ++i1)
for (i1 = 0; i1 < D1; ++i1)
{
{
// The algorithm here is that every thread writes one output pixel per line
if (
%(ignore_border)
s && (x_col >= ds1 * D3))
if (
%(ignore_border)
s && (x_col >= ds1 * D3))
{
{
my_gz = 0;
my_gz = 0;
...
@@ -417,7 +467,7 @@ class GpuDownsampleFactorMaxGrad(DownsampleFactorMaxGrad):
...
@@ -417,7 +467,7 @@ class GpuDownsampleFactorMaxGrad(DownsampleFactorMaxGrad):
for (int x_row = i2*ds0; (x_row < i2*ds0+ds0) && (
%(ignore_border)
s || (x_row < xD2)); ++x_row)
for (int x_row = i2*ds0; (x_row < i2*ds0+ds0) && (
%(ignore_border)
s || (x_row < xD2)); ++x_row)
{
{
gx[i0 * D1*xD2*xD3 + i1*xD2*xD3 + x_row*xD3 + x_col]
gx[i0 * D1*xD2*xD3 + i1*xD2*xD3 + x_row*xD3 + x_col]
= (my_z == x[i0*xS0 + i1*xS1 + x_row*xS2 + x_col]) ? my_gz : 0;
= (my_z == x[i0*xS0 + i1*xS1 + x_row*xS2 + x_col
*xS3
]) ? my_gz : 0;
}
}
}
}
}
}
...
...
nnet.py
浏览文件 @
2413550e
...
@@ -186,9 +186,6 @@ class GpuCrossentropySoftmax1HotWithBiasDx (Op):
...
@@ -186,9 +186,6 @@ class GpuCrossentropySoftmax1HotWithBiasDx (Op):
return
self
.
__class__
.
__name__
return
self
.
__class__
.
__name__
def
make_node
(
self
,
dy
,
sm
,
y_idx
):
def
make_node
(
self
,
dy
,
sm
,
y_idx
):
return
Apply
(
self
,
[
dy
,
sm
,
y_idx
],[
sm
.
type
()])
return
Apply
(
self
,
[
dy
,
sm
,
y_idx
],[
sm
.
type
()])
def
perform
(
self
,
node
,
input_storage
,
output_storage
):
raise
NotImplementedError
(
'only C is implemented'
)
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
()
return
()
def
c_code
(
self
,
node
,
nodename
,
(
dnll
,
sm
,
y_idx
),
(
dx
,),
sub
):
def
c_code
(
self
,
node
,
nodename
,
(
dnll
,
sm
,
y_idx
),
(
dx
,),
sub
):
...
...
tests/test_blas.py
浏览文件 @
2413550e
...
@@ -7,6 +7,7 @@ import numpy
...
@@ -7,6 +7,7 @@ import numpy
import
theano_cuda_ndarray
as
tcn
import
theano_cuda_ndarray
as
tcn
from
theano.sandbox.downsample
import
DownsampleFactorMax
def
test_dot
():
def
test_dot
():
...
@@ -46,20 +47,66 @@ def test_gemm():
...
@@ -46,20 +47,66 @@ def test_gemm():
assert
numpy
.
allclose
(
numpy
.
dot
(
a0
,
bval
)
+
numpy
.
exp
(
cval
),
a
.
value
)
assert
numpy
.
allclose
(
numpy
.
dot
(
a0
,
bval
)
+
numpy
.
exp
(
cval
),
a
.
value
)
def
test_maxpool
():
if
0
:
"""TODO: test the gpu version!!! """
# This is commented out because it doesn't make sense...
for
d0
,
d1
,
r_true
,
r_false
in
[(
4
,
4
,[[[[
5
,
7
],[
13
,
15
]]]],[[[[
5
,
7
],[
13
,
15
]]]]),
# tcn.blas has no op called DownsampleFactorMax
(
5
,
5
,[[[[
6
,
8
],[
16
,
18
],
[
21
,
23
]]]],
# tcn.blas has an op called GpuDownsampleFactorMax, but that op requires arguments that are
[[[[
6
,
8
,
9
],[
16
,
18
,
19
],
[
21
,
23
,
24
]]]])]:
# CudaNdarrayType variables... so rethink this test?
for
border
,
ret
in
[(
True
,
r_true
),(
False
,
r_false
)]:
def
test_maxpool
():
ret
=
numpy
.
array
(
ret
)
"""TODO: test the gpu version!!! """
a
=
tcn
.
blas
.
DownsampleFactorMax
((
2
,
2
),
border
)
for
d0
,
d1
,
r_true
,
r_false
in
[(
4
,
4
,[[[[
5
,
7
],[
13
,
15
]]]],[[[[
5
,
7
],[
13
,
15
]]]]),
dmatrix4
=
tensor
.
TensorType
(
"float32"
,
(
False
,
False
,
False
,
False
))
(
5
,
5
,[[[[
6
,
8
],[
16
,
18
],
[
21
,
23
]]]],
b
=
dmatrix4
()
[[[[
6
,
8
,
9
],[
16
,
18
,
19
],
[
21
,
23
,
24
]]]])]:
f
=
pfunc
([
b
],
[
a
(
b
)])
for
border
,
ret
in
[(
True
,
r_true
),(
False
,
r_false
)]:
ret
=
numpy
.
array
(
ret
)
bval
=
numpy
.
arange
(
0
,
d0
*
d1
)
.
reshape
(
1
,
1
,
d0
,
d1
)
a
=
tcn
.
blas
.
DownsampleFactorMax
((
2
,
2
),
border
)
r
=
f
(
bval
)[
0
]
dmatrix4
=
tensor
.
TensorType
(
"float32"
,
(
False
,
False
,
False
,
False
))
# print bval, bval.shape, border
b
=
dmatrix4
()
print
r
,
r
.
shape
f
=
pfunc
([
b
],
[
a
(
b
)])
assert
(
ret
==
r
)
.
all
()
bval
=
numpy
.
arange
(
0
,
d0
*
d1
)
.
reshape
(
1
,
1
,
d0
,
d1
)
r
=
f
(
bval
)[
0
]
# print bval, bval.shape, border
print
r
,
r
.
shape
assert
(
ret
==
r
)
.
all
()
def
test_downsample
():
for
shp
in
[
(
1
,
1
,
1
,
12
),
(
1
,
1
,
2
,
2
),
#(1, 1, 1, 1), #### Commented out because it makes FP-exception that I don't understand
(
1
,
1
,
4
,
4
),
(
1
,
1
,
10
,
11
),
(
1
,
2
,
2
,
2
),
(
3
,
5
,
4
,
4
),
(
1
,
1
,
12
,
12
),
(
1
,
1
,
2
,
14
),
(
1
,
1
,
12
,
14
),
(
1
,
1
,
14
,
14
),
(
1
,
1
,
16
,
16
),
(
1
,
1
,
18
,
18
),
(
1
,
1
,
24
,
24
),
(
1
,
6
,
24
,
24
),
(
10
,
1
,
24
,
24
),
(
10
,
6
,
24
,
24
),
(
30
,
6
,
12
,
12
),
(
30
,
2
,
24
,
24
),
(
30
,
6
,
24
,
24
),
(
10
,
10
,
10
,
11
)]:
for
ds
in
(
1
,
1
),
(
2
,
2
):
if
ds
[
0
]
>
shp
[
2
]:
continue
if
ds
[
1
]
>
shp
[
3
]:
continue
for
ignore_border
in
(
True
,
False
):
print
'test_downsample'
,
shp
,
ds
,
ignore_border
ds_op
=
DownsampleFactorMax
(
ds
,
ignore_border
=
ignore_border
)
a
=
tcn
.
shared_constructor
(
numpy
.
random
.
rand
(
*
shp
),
'a'
)
f
=
pfunc
([],
ds_op
(
tensor
.
as_tensor_variable
(
a
)))
worked
=
False
for
i
,
node
in
enumerate
(
f
.
maker
.
env
.
toposort
()):
print
i
,
node
if
isinstance
(
node
.
op
,
tcn
.
blas
.
GpuDownsampleFactorMax
):
f
()
# let debugmode do the testing
worked
=
True
assert
worked
tests/test_nnet.py
浏览文件 @
2413550e
...
@@ -14,7 +14,7 @@ import numpy
...
@@ -14,7 +14,7 @@ import numpy
import
theano_cuda_ndarray
as
tcn
import
theano_cuda_ndarray
as
tcn
import
logging
import
logging
logging
.
getLogger
(
't
heano.gradien
t'
)
.
setLevel
(
logging
.
INFO
)
logging
.
getLogger
(
't
est_cuda_ndarray.tests.test_nne
t'
)
.
setLevel
(
logging
.
INFO
)
def
get_mode
():
def
get_mode
():
...
@@ -97,18 +97,18 @@ def run_conv_nnet1(shared_fn):
...
@@ -97,18 +97,18 @@ def run_conv_nnet1(shared_fn):
n_out
=
10
n_out
=
10
w
=
shared_fn
(
numpy
.
asarray
(
0.01
*
(
numpy
.
random
.
rand
(
*
shape_kern
)
-
0.5
),
dtype
=
'float32'
),
'w'
)
w
=
shared_fn
(
numpy
.
asarray
(
0.01
*
(
numpy
.
random
.
rand
(
*
shape_kern
)
-
0.5
),
dtype
=
'float32'
),
'w'
)
b
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
((
n_kern
,
1
,
1
)),
dtype
=
'float32'
),
'b'
)
b
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
((
n_kern
,)),
dtype
=
'float32'
),
'b'
)
v
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
((
n_hid
,
n_out
)),
dtype
=
'float32'
),
'c'
)
v
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
((
n_hid
,
n_out
)),
dtype
=
'float32'
),
'c'
)
c
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
(
n_out
),
dtype
=
'float32'
),
'c'
)
c
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
(
n_out
),
dtype
=
'float32'
),
'c'
)
x
=
tensor
.
Tensor
(
dtype
=
'float32'
,
broadcastable
=
(
0
,
0
,
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
=
theano
.
sandbox
.
conv
.
ConvOp
(
shape_img
[
2
:],
shape_kern
[
2
:],
n_kern
,
n_batch
,
1
,
1
)
conv_op
=
theano
.
sandbox
.
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
)
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
)
...
@@ -174,13 +174,13 @@ def run_conv_nnet2(shared_fn): # pretend we are training LeNet for MNIST
...
@@ -174,13 +174,13 @@ def run_conv_nnet2(shared_fn): # pretend we are training LeNet for MNIST
n_out
=
10
n_out
=
10
w0
=
shared_fn
(
numpy
.
asarray
(
0.01
*
(
numpy
.
random
.
rand
(
*
shape_kern
)
-
0.5
),
dtype
=
'float32'
),
'w0'
)
w0
=
shared_fn
(
numpy
.
asarray
(
0.01
*
(
numpy
.
random
.
rand
(
*
shape_kern
)
-
0.5
),
dtype
=
'float32'
),
'w0'
)
b0
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
((
n_kern
,
1
,
1
)),
dtype
=
'float32'
),
'b0'
)
b0
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
((
n_kern
,)),
dtype
=
'float32'
),
'b0'
)
w1
=
shared_fn
(
numpy
.
asarray
(
0.01
*
(
numpy
.
random
.
rand
(
*
shape_kern1
)
-
0.5
),
dtype
=
'float32'
),
'w1'
)
w1
=
shared_fn
(
numpy
.
asarray
(
0.01
*
(
numpy
.
random
.
rand
(
*
shape_kern1
)
-
0.5
),
dtype
=
'float32'
),
'w1'
)
b1
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
((
n_kern1
,
1
,
1
)),
dtype
=
'float32'
),
'b1'
)
b1
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
((
n_kern1
,)),
dtype
=
'float32'
),
'b1'
)
v
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
((
n_hid
,
n_out
)),
dtype
=
'float32'
),
'c'
)
v
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
((
n_hid
,
n_out
)),
dtype
=
'float32'
),
'c'
)
c
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
(
n_out
),
dtype
=
'float32'
),
'c'
)
c
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
(
n_out
),
dtype
=
'float32'
),
'c'
)
x
=
tensor
.
Tensor
(
dtype
=
'float32'
,
broadcastable
=
(
0
,
0
,
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'
)
...
@@ -188,10 +188,9 @@ def run_conv_nnet2(shared_fn): # pretend we are training LeNet for MNIST
...
@@ -188,10 +188,9 @@ def run_conv_nnet2(shared_fn): # pretend we are training LeNet for MNIST
conv_op1
=
theano
.
sandbox
.
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
=
theano
.
sandbox
.
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
)
hid
=
tensor
.
tanh
(
conv_op
(
x
,
w0
)
+
b0
.
dimshuffle
((
0
,
'x'
,
'x'
))
)
hid1
=
tensor
.
tanh
(
conv_op1
(
hid
[:,:,::
2
,::
2
],
w1
)
+
b1
)
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
)
...
@@ -226,7 +225,7 @@ def test_conv_nnet2():
...
@@ -226,7 +225,7 @@ def test_conv_nnet2():
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
run_conv_nnet2_classif
(
shared_fn
,
isize
,
ksize
,
n_batch
=
60
,
n_iter
=
25
):
def
run_conv_nnet2_classif
(
shared_fn
,
isize
,
ksize
,
n_batch
,
n_iter
):
shape_img
=
(
n_batch
,
1
,
isize
,
isize
)
shape_img
=
(
n_batch
,
1
,
isize
,
isize
)
...
@@ -243,13 +242,13 @@ def run_conv_nnet2_classif(shared_fn, isize, ksize, n_batch=60, n_iter=25):
...
@@ -243,13 +242,13 @@ def run_conv_nnet2_classif(shared_fn, isize, ksize, n_batch=60, n_iter=25):
n_out
=
10
n_out
=
10
w0
=
shared_fn
(
numpy
.
asarray
(
0.01
*
(
numpy
.
random
.
rand
(
*
shape_kern
)
-
0.5
),
dtype
=
'float32'
),
'w0'
)
w0
=
shared_fn
(
numpy
.
asarray
(
0.01
*
(
numpy
.
random
.
rand
(
*
shape_kern
)
-
0.5
),
dtype
=
'float32'
),
'w0'
)
b0
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
((
n_kern
,
1
,
1
)),
dtype
=
'float32'
),
'b0'
)
b0
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
((
n_kern
,)),
dtype
=
'float32'
),
'b0'
)
w1
=
shared_fn
(
numpy
.
asarray
(
0.01
*
(
numpy
.
random
.
rand
(
*
shape_kern1
)
-
0.5
),
dtype
=
'float32'
),
'w1'
)
w1
=
shared_fn
(
numpy
.
asarray
(
0.01
*
(
numpy
.
random
.
rand
(
*
shape_kern1
)
-
0.5
),
dtype
=
'float32'
),
'w1'
)
b1
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
((
n_kern1
,
1
,
1
)),
dtype
=
'float32'
),
'b1'
)
b1
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
((
n_kern1
,)),
dtype
=
'float32'
),
'b1'
)
v
=
shared_fn
(
numpy
.
asarray
(
0.01
*
numpy
.
random
.
randn
(
n_hid
,
n_out
),
dtype
=
'float32'
),
'c'
)
v
=
shared_fn
(
numpy
.
asarray
(
0.01
*
numpy
.
random
.
randn
(
n_hid
,
n_out
),
dtype
=
'float32'
),
'c'
)
c
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
(
n_out
),
dtype
=
'float32'
),
'c'
)
c
=
shared_fn
(
numpy
.
asarray
(
numpy
.
zeros
(
n_out
),
dtype
=
'float32'
),
'c'
)
x
=
tensor
.
Tensor
(
dtype
=
'float32'
,
broadcastable
=
(
0
,
0
,
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'
)
...
@@ -260,15 +259,15 @@ def run_conv_nnet2_classif(shared_fn, isize, ksize, n_batch=60, n_iter=25):
...
@@ -260,15 +259,15 @@ def run_conv_nnet2_classif(shared_fn, isize, ksize, n_batch=60, n_iter=25):
ds_op
=
theano
.
sandbox
.
downsample
.
DownsampleFactorMax
((
2
,
2
),
ignore_border
=
False
)
ds_op
=
theano
.
sandbox
.
downsample
.
DownsampleFactorMax
((
2
,
2
),
ignore_border
=
False
)
hid
=
tensor
.
tanh
(
ds_op
(
conv_op
(
x
,
w0
)
+
b0
))
hid
=
tensor
.
tanh
(
ds_op
(
conv_op
(
x
,
w0
)
+
b0
.
dimshuffle
((
0
,
'x'
,
'x'
))
))
hid1
=
tensor
.
tanh
(
conv_op1
(
hid
,
w1
)
+
b1
)
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
]
gparams
=
tensor
.
grad
(
loss
,
params
)
gparams
=
tensor
.
grad
(
loss
,
params
,
warn_type
=
True
)
mode
=
get_mode
()
mode
=
get_mode
()
...
@@ -291,16 +290,19 @@ def run_conv_nnet2_classif(shared_fn, isize, ksize, n_batch=60, n_iter=25):
...
@@ -291,16 +290,19 @@ def run_conv_nnet2_classif(shared_fn, isize, ksize, n_batch=60, n_iter=25):
print_mode
(
mode
)
print_mode
(
mode
)
return
rvals
,
t1
-
t0
return
rvals
,
t1
-
t0
def
run_test_conv_nnet2_classif
(
seed
,
isize
,
ksize
,
bsize
,
ignore_error
=
False
,
gpu_only
=
False
):
def
cmp_run_conv_nnet2_classif
(
seed
,
isize
,
ksize
,
bsize
,
ignore_error
=
False
,
n_iter
=
10
,
gpu_only
=
False
):
if
gpu_only
:
if
gpu_only
:
numpy
.
random
.
seed
(
seed
)
numpy
.
random
.
seed
(
seed
)
rval_gpu
,
t
=
run_conv_nnet2_classif
(
tcn
.
shared_constructor
,
isize
,
ksize
,
bsize
)
rval_gpu
,
t
=
run_conv_nnet2_classif
(
tcn
.
shared_constructor
,
isize
,
ksize
,
bsize
)
return
return
numpy
.
random
.
seed
(
seed
)
numpy
.
random
.
seed
(
seed
)
rval_
cpu
,
tc
=
run_conv_nnet2_classif
(
shared
,
isize
,
ksize
,
bsize
)
rval_
gpu
,
tg
=
run_conv_nnet2_classif
(
tcn
.
shared_constructor
,
isize
,
ksize
,
bsize
,
n_iter
)
numpy
.
random
.
seed
(
seed
)
numpy
.
random
.
seed
(
seed
)
rval_
gpu
,
tg
=
run_conv_nnet2_classif
(
tcn
.
shared_constructor
,
isize
,
ksize
,
bsize
)
rval_
cpu
,
tc
=
run_conv_nnet2_classif
(
shared
,
isize
,
ksize
,
bsize
,
n_iter
)
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
)
...
@@ -309,16 +311,21 @@ def run_test_conv_nnet2_classif(seed, isize, ksize, bsize, ignore_error=False, g
...
@@ -309,16 +311,21 @@ def run_test_conv_nnet2_classif(seed, isize, ksize, bsize, ignore_error=False, g
assert
numpy
.
allclose
(
rval_cpu
[:
2
],
rval_gpu
[:
2
],
rtol
=
1e-4
,
atol
=
1e-6
)
assert
numpy
.
allclose
(
rval_cpu
[:
2
],
rval_gpu
[:
2
],
rtol
=
1e-4
,
atol
=
1e-6
)
def
test_lenet_28
():
#MNIST
def
test_lenet_28
():
#MNIST
run_test_conv_nnet2_classif
(
23485
,
28
,
5
,
60
)
cmp_run_conv_nnet2_classif
(
23485
,
28
,
5
,
60
,
n_iter
=
3
)
def
test_lenet_32
():
#CIFAR10 / Shapeset
def
test_lenet_32
():
#CIFAR10 / Shapeset
run_test_conv_nnet2_classif
(
23485
,
32
,
5
,
60
,
ignore_error
=
False
)
cmp_run_conv_nnet2_classif
(
23485
,
32
,
5
,
60
,
ignore_error
=
False
,
n_iter
=
3
)
def
test_lenet_32_long
():
#CIFAR10 / Shapeset
# this tests the gradient of downsample on the GPU,
# which does not recieve specific testing
cmp_run_conv_nnet2_classif
(
23485
,
32
,
5
,
30
,
ignore_error
=
False
,
n_iter
=
50
)
def
test_lenet_64
():
# ???
def
test_lenet_64
():
# ???
run_test_conv_nnet2_classif
(
23485
,
64
,
7
,
10
,
ignore_error
=
True
)
cmp_run_conv_nnet2_classif
(
23485
,
64
,
7
,
10
,
ignore_error
=
False
,
n_iter
=
3
)
def
test_lenet_108
():
# NORB
#
def test_lenet_108(): # NORB
run_test
_conv_nnet2_classif
(
23485
,
108
,
7
,
10
)
#cmp_run
_conv_nnet2_classif(23485, 108, 7, 10)
def
test_lenet_256
():
# ImageNet
#
def test_lenet_256(): # ImageNet
run_test
_conv_nnet2_classif
(
23485
,
256
,
9
,
2
)
#cmp_run
_conv_nnet2_classif(23485, 256, 9, 2)
var.py
浏览文件 @
2413550e
...
@@ -54,13 +54,16 @@ class CudaNdarraySharedVariable(SharedVariable, _operators):
...
@@ -54,13 +54,16 @@ class CudaNdarraySharedVariable(SharedVariable, _operators):
if
(
other
.
type
.
dtype
!=
self
.
dtype
):
if
(
other
.
type
.
dtype
!=
self
.
dtype
):
raise
TypeError
(
'Incompatible dtype'
,
(
self
.
dtype
,
other
.
type
.
dtype
))
raise
TypeError
(
'Incompatible dtype'
,
(
self
.
dtype
,
other
.
type
.
dtype
))
if
(
other
.
type
.
broadcastable
!=
self
.
broadcastable
):
if
(
other
.
type
.
broadcastable
!=
self
.
broadcastable
):
raise
TypeError
(
'Incompatible broadcastable'
,
(
self
.
broadcastable
,
other
.
type
.
broadcastable
))
raise
TypeError
(
'Incompatible broadcastable'
,
(
self
,
(
self
.
broadcastable
,
other
.
type
.
broadcastable
)))
return
GpuFromHost
()(
other
)
return
GpuFromHost
()(
other
)
CudaNdarrayType
.
SharedVariable
=
CudaNdarraySharedVariable
CudaNdarrayType
.
SharedVariable
=
CudaNdarraySharedVariable
def
shared_constructor
(
value
,
name
,
strict
=
False
):
def
shared_constructor
(
value
,
name
,
strict
=
False
,
broadcastable
=
None
):
"""SharedVariable Constructor for TensorType"""
"""SharedVariable Constructor for TensorType"""
#TODO: what should strict mean in this context, since we always have to make a copy?
if
strict
:
if
strict
:
_value
=
value
_value
=
value
else
:
else
:
...
@@ -71,8 +74,9 @@ def shared_constructor(value, name, strict=False):
...
@@ -71,8 +74,9 @@ def shared_constructor(value, name, strict=False):
if
_value
.
dtype
.
num
!=
CudaNdarrayType
.
typenum
:
if
_value
.
dtype
.
num
!=
CudaNdarrayType
.
typenum
:
raise
TypeError
(
'float32 ndarray required'
)
raise
TypeError
(
'float32 ndarray required'
)
bcast
=
[
0
for
b
in
value
.
shape
]
if
broadcastable
is
None
:
type
=
CudaNdarrayType
(
broadcastable
=
bcast
)
broadcastable
=
[
b
==
1
for
b
in
value
.
shape
]
type
=
CudaNdarrayType
(
broadcastable
=
broadcastable
)
return
CudaNdarraySharedVariable
(
type
=
type
,
value
=
_value
,
name
=
name
,
strict
=
strict
)
return
CudaNdarraySharedVariable
(
type
=
type
,
value
=
_value
,
name
=
name
,
strict
=
strict
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论