Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
49d42955
提交
49d42955
authored
2月 18, 2016
作者:
Samira Shabanian
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Some functions deleted
上级
09eadfb9
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
0 行增加
和
225 行删除
+0
-225
sp.py
theano/sparse/sandbox/sp.py
+0
-69
test_sp.py
theano/sparse/sandbox/test_sp.py
+0
-156
没有找到文件。
theano/sparse/sandbox/sp.py
浏览文件 @
49d42955
...
...
@@ -264,75 +264,6 @@ class ConvolutionIndices(Op):
convolution_indices
=
ConvolutionIndices
()
def
applySparseFilter
(
kerns
,
kshp
,
nkern
,
images
,
imgshp
,
step
=
(
1
,
1
),
bias
=
None
,
mode
=
'valid'
):
"""
"images" is assumed to be a matrix of shape batch_size x img_size,
where the second dimension represents each image in raster order
Output feature map will have shape:
.. code-block:: python
batch_size x number of kernels * output_size
.. note::
IMPORTANT: note that this means that each feature map is
contiguous in memory.
The memory layout will therefore be:
[ <feature_map_0> <feature_map_1> ... <feature_map_n>],
where <feature_map> represents a "feature map" in raster order
Note that the concept of feature map doesn't really apply to
sparse filters without weight sharing. Basically, nkern=1 will
generate one output img/feature map, nkern=2 a second feature map,
etc.
kerns is a 1D tensor, and assume to be of shape:
.. code-block:: python
nkern * N.prod(outshp) x N.prod(kshp)
Each filter is applied seperately to consecutive output pixels.
:param kerns: nkern*outsize*ksize vector containing kernels
:param kshp: tuple containing actual dimensions of kernel (not symbolic)
:param nkern: number of kernels to apply at each pixel in the
input image. nkern=1 will apply a single unique
filter for each input pixel.
:param images: bsize x imgsize matrix containing images on which
to apply filters
:param imgshp: tuple containing actual image dimensions (not symbolic)
:param step: determines number of pixels between adjacent receptive fields
(tuple containing dx,dy values)
:param mode: 'full', 'valid' see CSM.evaluate function for details
:return: out1, symbolic result
:return: out2, logical shape of the output img (nkern,height,width)
(after dot product, not of the sparse matrix!)
"""
# inshp contains either 2 entries (height,width) or 3 (nfeatures,h,w)
# in the first case, default nfeatures to 1
if
numpy
.
size
(
imgshp
)
==
2
:
imgshp
=
(
1
,)
+
imgshp
# construct indices and index pointers for sparse matrix
indices
,
indptr
,
spmat_shape
,
sptype
,
outshp
,
kmap
=
\
convolution_indices
.
sparse_eval
(
imgshp
,
kshp
,
nkern
,
step
,
mode
)
# build a sparse weight matrix
sparsew
=
theano
.
sparse
.
CSM
(
sptype
,
kmap
)(
kerns
,
indices
,
indptr
,
spmat_shape
)
output
=
sparse
.
structured_dot
(
sparsew
,
images
.
T
)
.
T
if
bias
is
not
None
:
output
+=
bias
return
output
,
numpy
.
hstack
((
nkern
,
outshp
))
def
convolve
(
kerns
,
kshp
,
nkern
,
images
,
imgshp
,
step
=
(
1
,
1
),
bias
=
None
,
mode
=
'valid'
,
flatten
=
True
):
"""Convolution implementation by sparse matrix multiplication.
...
...
theano/sparse/sandbox/test_sp.py
浏览文件 @
49d42955
...
...
@@ -130,133 +130,6 @@ class TestSP(unittest.TestCase):
# profmode.print_summary()
@attr
(
'slow'
)
def
test_sparse
(
self
):
# print '\n\n*************************************************'
# print ' TEST SPARSE'
# print '*************************************************'
# fixed parameters
bsize
=
10
# batch size
imshp
=
(
8
,
8
)
kshp
=
(
5
,
5
)
nkern
=
1
# per output pixel
ssizes
=
((
1
,
1
),
(
2
,
2
))
convmodes
=
(
'full'
,
'valid'
,)
# symbolic stuff
bias
=
tensor
.
dvector
()
kerns
=
tensor
.
dvector
()
input
=
tensor
.
dmatrix
()
rng
=
numpy
.
random
.
RandomState
(
3423489
)
import
theano.gof
as
gof
for
mode
in
(
None
,):
ntot
,
ttot
=
0
,
0
for
conv_mode
in
convmodes
:
for
ss
in
ssizes
:
output
,
outshp
=
sp
.
applySparseFilter
(
kerns
,
kshp
,
\
nkern
,
input
,
imshp
,
ss
,
bias
=
bias
,
mode
=
conv_mode
)
f
=
function
([
kerns
,
bias
,
input
],
output
,
mode
=
mode
)
# build actual input images
img2d
=
numpy
.
arange
(
bsize
*
numpy
.
prod
(
imshp
))
.
reshape
((
bsize
,)
+
imshp
)
img1d
=
img2d
.
reshape
(
bsize
,
-
1
)
zeropad_img
=
numpy
.
zeros
((
bsize
,
\
img2d
.
shape
[
1
]
+
2
*
(
kshp
[
0
]
-
1
),
\
img2d
.
shape
[
2
]
+
2
*
(
kshp
[
1
]
-
1
)))
zeropad_img
[:,
kshp
[
0
]
-
1
:
kshp
[
0
]
-
1
+
img2d
.
shape
[
1
],
kshp
[
1
]
-
1
:
kshp
[
1
]
-
1
+
img2d
.
shape
[
2
]]
=
img2d
# build kernel matrix -- flatten it for theano stuff
filters
=
numpy
.
arange
(
numpy
.
prod
(
outshp
)
*
numpy
.
prod
(
kshp
))
.
\
reshape
(
nkern
,
numpy
.
prod
(
outshp
[
1
:]),
numpy
.
prod
(
kshp
))
spfilt
=
filters
.
flatten
()
biasvals
=
numpy
.
arange
(
numpy
.
prod
(
outshp
))
# compute output by hand
ntime1
=
time
.
time
()
refout
=
numpy
.
zeros
((
bsize
,
nkern
,
outshp
[
1
],
outshp
[
2
]))
patch
=
numpy
.
zeros
((
kshp
[
0
],
kshp
[
1
]))
for
b
in
xrange
(
bsize
):
for
k
in
xrange
(
nkern
):
pixi
=
0
# pixel index in raster order
for
j
in
xrange
(
outshp
[
1
]):
for
i
in
xrange
(
outshp
[
2
]):
n
=
j
*
ss
[
0
]
m
=
i
*
ss
[
1
]
patch
=
zeropad_img
[
b
,
n
:
n
+
kshp
[
0
],
m
:
m
+
kshp
[
1
]]
refout
[
b
,
k
,
j
,
i
]
=
numpy
.
dot
(
filters
[
k
,
pixi
,
:],
\
patch
.
flatten
())
pixi
+=
1
refout
=
refout
.
reshape
(
bsize
,
-
1
)
+
biasvals
ntot
+=
time
.
time
()
-
ntime1
# need to flatten images
ttime1
=
time
.
time
()
out1
=
f
(
spfilt
,
biasvals
,
img1d
)
ttot
+=
time
.
time
()
-
ttime1
temp
=
refout
-
out1
assert
(
temp
<
1e-10
)
.
all
()
# test downward propagation
vis
=
tensor
.
grad
(
0.5
*
tensor
.
sqr
(
output
)
.
sum
(),
input
)
downprop
=
function
([
kerns
,
output
],
vis
)
temp1
=
time
.
time
()
for
zz
in
range
(
100
):
visval
=
downprop
(
spfilt
,
out1
)
indices
,
indptr
,
spmat_shape
,
sptype
,
outshp
,
kmap
=
\
sp
.
convolution_indices
.
sparse_eval
(
imshp
,
kshp
,
nkern
,
ss
,
conv_mode
)
spmat
=
sparse
.
csc_matrix
((
spfilt
[
kmap
],
indices
,
indptr
),
spmat_shape
)
visref
=
numpy
.
dot
(
out1
,
spmat
.
todense
())
assert
numpy
.
all
(
visref
==
visval
),
(
visref
,
visval
)
# print '**** Sparse Profiling Results (',mode,') ****'
# print 'Numpy processing time: ', ntot
# print 'Theano processing time: ', ttot
# profmode.print_summary()
@attr
(
'slow'
)
def
test_multilayer_sparse
(
self
):
# fixed parameters
bsize
=
10
# batch size
imshp
=
(
5
,
5
)
kshp
=
((
3
,
3
),
(
2
,
2
))
nkerns
=
(
10
,
20
)
# per output pixel
ssizes
=
((
1
,
1
),
(
2
,
2
))
convmodes
=
(
'full'
,
'valid'
,)
# symbolic stuff
kerns
=
[
tensor
.
dvector
(),
tensor
.
dvector
()]
input
=
tensor
.
dmatrix
()
rng
=
numpy
.
random
.
RandomState
(
3423489
)
# build actual input images
img2d
=
numpy
.
arange
(
bsize
*
numpy
.
prod
(
imshp
))
.
reshape
((
bsize
,)
+
imshp
)
img1d
=
img2d
.
reshape
(
bsize
,
-
1
)
for
mode
in
(
'FAST_COMPILE'
,
'FAST_RUN'
):
for
conv_mode
in
convmodes
:
for
ss
in
ssizes
:
l1hid
,
l1outshp
=
sp
.
applySparseFilter
(
kerns
[
0
],
kshp
[
0
],
\
nkerns
[
0
],
input
,
imshp
,
ss
,
mode
=
conv_mode
)
l2hid
,
l2outshp
=
sp
.
applySparseFilter
(
kerns
[
1
],
kshp
[
1
],
\
nkerns
[
1
],
l1hid
,
l1outshp
,
ss
,
mode
=
conv_mode
)
l1propup
=
function
([
kerns
[
0
],
input
],
l1hid
,
mode
=
mode
)
l2propup
=
function
([
kerns
[
1
],
l1hid
],
l2hid
,
mode
=
mode
)
# actual values
l1kernvals
=
numpy
.
arange
(
numpy
.
prod
(
l1outshp
)
*
numpy
.
prod
(
kshp
[
0
]))
l2kernvals
=
numpy
.
arange
(
numpy
.
prod
(
l2outshp
)
*
numpy
.
prod
(
kshp
[
1
])
*
nkerns
[
0
])
l1hidval
=
l1propup
(
l1kernvals
,
img1d
)
l2hidval
=
l2propup
(
l2kernvals
,
l1hidval
)
# this doesn't compare the output of anything... but I manually verified that the patches
# are properly generated
def
test_multilayer_conv
(
self
):
...
...
@@ -335,35 +208,6 @@ class TestSP(unittest.TestCase):
return
output
utt
.
verify_grad
(
mp
,
[
imval
.
reshape
(
imval
.
shape
[
0
],
-
1
)])
def
test_CSMGrad
(
self
):
imshp
=
(
3
,
3
)
nkern
=
1
# per output pixel
kshp
=
(
2
,
2
)
#ssizes = ((1,1),(2,2))
ssizes
=
((
1
,
1
),)
#convmodes = ('full','valid',)
convmodes
=
(
'full'
,)
kerns
=
tensor
.
dvector
()
indices
=
tensor
.
ivector
()
indptr
=
tensor
.
ivector
()
spmat_shape
=
tensor
.
ivector
()
for
mode
in
[
'FAST_COMPILE'
,
'FAST_RUN'
]:
for
conv_mode
in
convmodes
:
for
ss
in
ssizes
:
indvals
,
indptrvals
,
spshapevals
,
sptype
,
outshp
,
kmap
=
\
sp
.
convolution_indices
.
sparse_eval
(
imshp
,
kshp
,
nkern
,
ss
,
conv_mode
)
kvals
=
numpy
.
random
.
random
(
nkern
*
numpy
.
prod
(
kshp
)
*
numpy
.
prod
(
outshp
))
.
flatten
()
def
d
(
kerns
):
return
theano
.
sparse
.
dense_from_sparse
(
theano
.
sparse
.
CSM
(
sptype
,
kmap
)(
kerns
,
indvals
,
indptrvals
,
spshapevals
))
# symbolic stuff
utt
.
verify_grad
(
d
,
[
kvals
])
if
__name__
==
'__main__'
:
if
0
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论