Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
2b7ee2ec
提交
2b7ee2ec
authored
11月 08, 2016
作者:
Frédéric Bastien
提交者:
GitHub
11月 08, 2016
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5193 from kvmanohar22/numpy_imports
Numpy imports
上级
a3424449
ac4b7a5d
全部展开
显示空白字符变更
内嵌
并排
正在显示
11 个修改的文件
包含
128 行增加
和
131 行删除
+128
-131
basic.py
theano/sparse/basic.py
+26
-26
opt.py
theano/sparse/opt.py
+2
-2
sp.py
theano/sparse/sandbox/sp.py
+34
-37
sp2.py
theano/sparse/sandbox/sp2.py
+5
-5
test_sp.py
theano/sparse/sandbox/test_sp.py
+30
-30
truedot.py
theano/sparse/sandbox/truedot.py
+1
-1
test_basic.py
theano/sparse/tests/test_basic.py
+0
-0
test_opt.py
theano/sparse/tests/test_opt.py
+4
-4
test_sp2.py
theano/sparse/tests/test_sp2.py
+9
-9
test_utils.py
theano/sparse/tests/test_utils.py
+8
-8
type.py
theano/sparse/type.py
+9
-9
没有找到文件。
theano/sparse/basic.py
浏览文件 @
2b7ee2ec
...
...
@@ -12,7 +12,7 @@ from __future__ import absolute_import, print_function, division
import
sys
import
numpy
import
numpy
as
np
from
numpy.lib.stride_tricks
import
as_strided
from
six
import
integer_types
from
six.moves
import
xrange
...
...
@@ -86,11 +86,11 @@ def _is_dense(x):
L{numpy.ndarray}).
"""
if
not
isinstance
(
x
,
(
scipy
.
sparse
.
spmatrix
,
n
umpy
.
ndarray
)):
if
not
isinstance
(
x
,
(
scipy
.
sparse
.
spmatrix
,
n
p
.
ndarray
)):
raise
NotImplementedError
(
"this function should only be called on "
"sparse.scipy.sparse.spmatrix or "
"numpy.ndarray, not,"
,
x
)
return
isinstance
(
x
,
n
umpy
.
ndarray
)
return
isinstance
(
x
,
n
p
.
ndarray
)
# Wrapper type
...
...
@@ -205,8 +205,8 @@ def sp_zeros_like(x):
# TODO: don't restrict to CSM formats
_
,
_
,
indptr
,
shape
=
csm_properties
(
x
)
return
CSM
(
format
=
x
.
format
)(
data
=
n
umpy
.
array
([],
dtype
=
x
.
type
.
dtype
),
indices
=
n
umpy
.
array
([],
dtype
=
'int32'
),
return
CSM
(
format
=
x
.
format
)(
data
=
n
p
.
array
([],
dtype
=
x
.
type
.
dtype
),
indices
=
n
p
.
array
([],
dtype
=
'int32'
),
indptr
=
tensor
.
zeros_like
(
indptr
),
shape
=
shape
)
...
...
@@ -293,9 +293,9 @@ class _sparse_py_operators:
args
=
args
,
if
len
(
args
)
==
2
:
scalar_arg_1
=
(
n
umpy
.
isscalar
(
args
[
0
])
or
scalar_arg_1
=
(
n
p
.
isscalar
(
args
[
0
])
or
getattr
(
args
[
0
],
'type'
,
None
)
==
tensor
.
iscalar
)
scalar_arg_2
=
(
n
umpy
.
isscalar
(
args
[
1
])
or
scalar_arg_2
=
(
n
p
.
isscalar
(
args
[
1
])
or
getattr
(
args
[
1
],
'type'
,
None
)
==
tensor
.
iscalar
)
if
scalar_arg_1
and
scalar_arg_2
:
ret
=
get_item_scalar
(
self
,
args
)
...
...
@@ -554,17 +554,17 @@ class CSM(gof.Op):
data
=
tensor
.
as_tensor_variable
(
data
)
if
not
isinstance
(
indices
,
gof
.
Variable
):
indices_
=
n
umpy
.
asarray
(
indices
)
indices_
=
n
p
.
asarray
(
indices
)
indices_32
=
theano
.
_asarray
(
indices
,
dtype
=
'int32'
)
assert
(
indices_
==
indices_32
)
.
all
()
indices
=
indices_32
if
not
isinstance
(
indptr
,
gof
.
Variable
):
indptr_
=
n
umpy
.
asarray
(
indptr
)
indptr_
=
n
p
.
asarray
(
indptr
)
indptr_32
=
theano
.
_asarray
(
indptr
,
dtype
=
'int32'
)
assert
(
indptr_
==
indptr_32
)
.
all
()
indptr
=
indptr_32
if
not
isinstance
(
shape
,
gof
.
Variable
):
shape_
=
n
umpy
.
asarray
(
shape
)
shape_
=
n
p
.
asarray
(
shape
)
shape_32
=
theano
.
_asarray
(
shape
,
dtype
=
'int32'
)
assert
(
shape_
==
shape_32
)
.
all
()
shape
=
shape_32
...
...
@@ -606,7 +606,7 @@ class CSM(gof.Op):
if
self
.
format
==
'csc'
:
out
[
0
]
=
scipy
.
sparse
.
csc_matrix
((
data
,
indices
.
copy
(),
indptr
.
copy
()),
n
umpy
.
asarray
(
shape
),
copy
=
False
)
n
p
.
asarray
(
shape
),
copy
=
False
)
else
:
assert
self
.
format
==
'csr'
out
[
0
]
=
scipy
.
sparse
.
csr_matrix
((
data
,
indices
.
copy
(),
...
...
@@ -729,8 +729,8 @@ class CSMGrad(gof.op.Op):
else
:
sp_dim
=
x_shape
[
0
]
g_row
=
n
umpy
.
zeros
(
sp_dim
,
dtype
=
g_data
.
dtype
)
gout_data
=
n
umpy
.
zeros
(
x_data
.
shape
,
dtype
=
node
.
outputs
[
0
]
.
dtype
)
g_row
=
n
p
.
zeros
(
sp_dim
,
dtype
=
g_data
.
dtype
)
gout_data
=
n
p
.
zeros
(
x_data
.
shape
,
dtype
=
node
.
outputs
[
0
]
.
dtype
)
for
i
in
range
(
len
(
x_indptr
)
-
1
):
for
j_ptr
in
range
(
g_indptr
[
i
],
g_indptr
[
i
+
1
]):
...
...
@@ -1100,7 +1100,7 @@ class GetItem2Lists(gof.op.Op):
x
=
inp
[
0
]
ind1
=
inp
[
1
]
ind2
=
inp
[
2
]
out
[
0
]
=
n
umpy
.
asarray
(
x
[
ind1
,
ind2
])
.
flatten
()
out
[
0
]
=
n
p
.
asarray
(
x
[
ind1
,
ind2
])
.
flatten
()
"""
Here scipy returns the corresponding elements in a matrix which isn't
what we are aiming for. Using asarray and flatten, out[0] becomes an
...
...
@@ -1244,7 +1244,7 @@ class GetItem2d(gof.op.Op):
elif
((
isinstance
(
ind
,
gof
.
Variable
)
and
getattr
(
ind
,
'ndim'
,
-
1
)
==
0
)
or
n
umpy
.
isscalar
(
ind
)):
n
p
.
isscalar
(
ind
)):
raise
NotImplementedError
(
'Theano has no sparse vector'
+
'Use X[a:b, c:d], X[a:b, c:c+1] or X[a:b] instead.'
)
...
...
@@ -1653,9 +1653,9 @@ class SpSum(gof.op.Op):
(
x
,)
=
inputs
(
z
,)
=
outputs
if
self
.
axis
is
None
:
z
[
0
]
=
n
umpy
.
asarray
(
x
.
sum
())
z
[
0
]
=
n
p
.
asarray
(
x
.
sum
())
else
:
z
[
0
]
=
n
umpy
.
asarray
(
x
.
sum
(
self
.
axis
))
.
ravel
()
z
[
0
]
=
n
p
.
asarray
(
x
.
sum
(
self
.
axis
))
.
ravel
()
def
grad
(
self
,
inputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -2540,7 +2540,7 @@ class __ComparisonOpSD(gof.op.Op):
assert
x
.
shape
==
y
.
shape
assert
_is_dense
(
y
)
o
=
self
.
comparison
(
x
,
y
)
.
astype
(
'uint8'
)
o
=
n
umpy
.
asarray
(
o
)
o
=
n
p
.
asarray
(
o
)
out
[
0
]
=
o
def
infer_shape
(
self
,
node
,
ins_shapes
):
...
...
@@ -3382,7 +3382,7 @@ class TrueDot(gof.op.Op):
# 'ushort', 'intc', 'uintc', 'longlong', 'ulonglong', 'single',
# 'double', 'longdouble', 'csingle', 'cdouble', 'clongdouble']
# But ulonglong is uint64 on x86-64, but with a different typenum!
if
rval
.
dtype
.
num
!=
n
umpy
.
dtype
(
str
(
rval
.
dtype
))
.
num
:
if
rval
.
dtype
.
num
!=
n
p
.
dtype
(
str
(
rval
.
dtype
))
.
num
:
assert
str
(
rval
.
dtype
)
==
node
.
outputs
[
0
]
.
dtype
# Create a view with the expected typenum.
format
=
node
.
outputs
[
0
]
.
type
.
format
...
...
@@ -3509,7 +3509,7 @@ class StructuredDot(gof.Op):
# dot of an NxM sparse matrix, with a Mx1 dense matrix, returns vector
# not matrix
if
variable
.
ndim
==
1
:
variable
=
n
umpy
.
expand_dims
(
variable
,
1
)
variable
=
n
p
.
expand_dims
(
variable
,
1
)
elif
variable
.
ndim
!=
2
:
raise
Exception
(
'Output of structured dot should be a matrix '
'(ndim=2)'
)
...
...
@@ -3622,7 +3622,7 @@ class StructuredDotGradCSC(gof.Op):
def
perform
(
self
,
node
,
inputs
,
outputs
):
(
a_indices
,
a_indptr
,
b
,
g_ab
)
=
inputs
(
out
,)
=
outputs
g_a_data
=
n
umpy
.
zeros
(
a_indices
.
shape
,
dtype
=
g_ab
.
dtype
)
g_a_data
=
n
p
.
zeros
(
a_indices
.
shape
,
dtype
=
g_ab
.
dtype
)
for
j
in
xrange
(
len
(
a_indptr
)
-
1
):
ind0
=
a_indptr
[
j
]
ind1
=
a_indptr
[
j
+
1
]
...
...
@@ -3631,7 +3631,7 @@ class StructuredDotGradCSC(gof.Op):
# Depending on the type of g_ab and b (sparse or dense),
# the following dot product can result in a scalar or
# a (1, 1) sparse matrix.
dot_val
=
n
umpy
.
dot
(
g_ab
[
i
],
b
[
j
]
.
T
)
dot_val
=
n
p
.
dot
(
g_ab
[
i
],
b
[
j
]
.
T
)
if
isinstance
(
dot_val
,
scipy
.
sparse
.
spmatrix
):
dot_val
=
dot_val
[
0
,
0
]
g_a_data
[
i_idx
]
=
dot_val
...
...
@@ -3752,7 +3752,7 @@ class StructuredDotGradCSR(gof.Op):
def
perform
(
self
,
node
,
inputs
,
outputs
):
(
a_indices
,
a_indptr
,
b
,
g_ab
)
=
inputs
(
out
,)
=
outputs
g_a_data
=
n
umpy
.
zeros
(
a_indices
.
shape
,
dtype
=
g_ab
.
dtype
)
g_a_data
=
n
p
.
zeros
(
a_indices
.
shape
,
dtype
=
g_ab
.
dtype
)
for
i
in
xrange
(
len
(
a_indptr
)
-
1
):
# loop over rows
ind0
=
a_indptr
[
i
]
ind1
=
a_indptr
[
i
+
1
]
...
...
@@ -3763,7 +3763,7 @@ class StructuredDotGradCSR(gof.Op):
# Depending on the type of g_ab and b (sparse or dense),
# the following dot product can result in a scalar or
# a (1, 1) sparse matrix.
dot_val
=
n
umpy
.
dot
(
g_ab
[
i
],
b
[
j
]
.
T
)
dot_val
=
n
p
.
dot
(
g_ab
[
i
],
b
[
j
]
.
T
)
if
isinstance
(
dot_val
,
scipy
.
sparse
.
spmatrix
):
dot_val
=
dot_val
[
0
,
0
]
g_a_data
[
j_idx
]
=
dot_val
...
...
@@ -3910,7 +3910,7 @@ class SamplingDot(gof.op.Op):
if
not
_is_sparse
(
p
):
raise
TypeError
(
p
)
out
[
0
]
=
p
.
__class__
(
p
.
multiply
(
n
umpy
.
dot
(
x
,
y
.
T
)))
out
[
0
]
=
p
.
__class__
(
p
.
multiply
(
n
p
.
dot
(
x
,
y
.
T
)))
def
grad
(
self
,
inputs
,
gout
):
(
x
,
y
,
p
)
=
inputs
...
...
@@ -4243,7 +4243,7 @@ class ConstructSparseFromList(gof.Op):
out
,
=
out_
rows
,
cols
=
values
.
shape
assert
rows
==
len
(
ilist
)
indptr
=
n
umpy
.
arange
(
cols
+
1
)
*
rows
indptr
=
n
p
.
arange
(
cols
+
1
)
*
rows
indices
=
as_strided
(
ilist
,
strides
=
(
0
,
ilist
.
strides
[
0
]),
shape
=
(
cols
,
ilist
.
shape
[
0
]))
.
flatten
()
...
...
theano/sparse/opt.py
浏览文件 @
2b7ee2ec
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
scipy
import
theano
...
...
@@ -879,7 +879,7 @@ local_usmm = gof.opt.PatternSub(
(
theano
.
tensor
.
sub
,
'z'
,
(
theano
.
tensor
.
mul
,
{
'pattern'
:
'alpha'
,
'constraint'
:
lambda
expr
:
(
n
umpy
.
all
(
expr
.
type
.
broadcastable
)
and
'constraint'
:
lambda
expr
:
(
n
p
.
all
(
expr
.
type
.
broadcastable
)
and
theano
.
config
.
blas
.
ldflags
)},
(
sparse
.
_dot
,
'x'
,
'y'
))),
(
usmm
,
(
theano
.
tensor
.
neg
,
'alpha'
),
'x'
,
'y'
,
'z'
))
...
...
theano/sparse/sandbox/sp.py
浏览文件 @
2b7ee2ec
...
...
@@ -8,7 +8,7 @@ U{http://www-users.cs.umn.edu/~saad/software/SPARSKIT/paper.ps}.
"""
# COPIED FROM hpu/icml09/sp.py
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
scipy
from
scipy
import
sparse
as
scipy_sparse
from
six.moves
import
xrange
...
...
@@ -81,18 +81,17 @@ class ConvolutionIndices(Op):
raise
Exception
(
"ws is obsolete and it must be always True"
)
(
dx
,
dy
)
=
strides
N
=
numpy
# inshp contains either 2 entries (height,width) or 3 (nfeatures,h,w)
# in the first case, default nfeatures to 1
if
N
.
size
(
inshp
)
==
2
:
if
np
.
size
(
inshp
)
==
2
:
inshp
=
(
1
,)
+
inshp
inshp
=
N
.
array
(
inshp
)
kshp
=
N
.
array
(
kshp
)
ksize
=
N
.
prod
(
kshp
)
inshp
=
np
.
array
(
inshp
)
kshp
=
np
.
array
(
kshp
)
ksize
=
np
.
prod
(
kshp
)
kern
=
ksize
-
1
-
N
.
arange
(
ksize
)
kern
=
ksize
-
1
-
np
.
arange
(
ksize
)
# size of output image if doing proper convolution
# (mode='full',dx=dy=0) outshp is the actual output shape
...
...
@@ -102,32 +101,32 @@ class ConvolutionIndices(Op):
s
=
-
1
else
:
s
=
1
outshp
=
N
.
int64
(
N
.
ceil
((
inshp
[
1
:]
+
s
*
kshp
-
s
*
1
)
\
/
N
.
array
([
dy
,
dx
],
dtype
=
'float'
)))
outshp
=
np
.
int64
(
np
.
ceil
((
inshp
[
1
:]
+
s
*
kshp
-
s
*
1
)
\
/
np
.
array
([
dy
,
dx
],
dtype
=
'float'
)))
if
any
(
outshp
<=
0
):
err
=
'Invalid kernel'
,
kshp
,
'and/or step size'
,
(
dx
,
dy
),
\
'for given input shape'
,
inshp
raise
ValueError
(
err
)
outsize
=
N
.
prod
(
outshp
)
insize
=
N
.
prod
(
inshp
)
outsize
=
np
.
prod
(
outshp
)
insize
=
np
.
prod
(
inshp
)
# range of output units over which to iterate
if
mode
==
'valid'
:
lbound
=
N
.
array
([
kshp
[
0
]
-
1
,
kshp
[
1
]
-
1
])
lbound
=
np
.
array
([
kshp
[
0
]
-
1
,
kshp
[
1
]
-
1
])
ubound
=
lbound
+
(
inshp
[
1
:]
-
kshp
+
1
)
else
:
lbound
=
N
.
zeros
(
2
)
lbound
=
np
.
zeros
(
2
)
ubound
=
fulloutshp
# coordinates of image in "fulloutshp" coordinates
topleft
=
N
.
array
([
kshp
[
0
]
-
1
,
kshp
[
1
]
-
1
])
topleft
=
np
.
array
([
kshp
[
0
]
-
1
,
kshp
[
1
]
-
1
])
# bound when counting the receptive field
botright
=
topleft
+
inshp
[
1
:]
# sparse matrix specifics...
if
ws
:
spmatshp
=
(
outsize
*
N
.
prod
(
kshp
)
*
inshp
[
0
],
insize
)
spmatshp
=
(
outsize
*
np
.
prod
(
kshp
)
*
inshp
[
0
],
insize
)
else
:
spmatshp
=
(
nkern
*
outsize
,
insize
)
spmat
=
scipy_sparse
.
lil_matrix
(
spmatshp
)
...
...
@@ -152,17 +151,17 @@ class ConvolutionIndices(Op):
# FOR EACH OUTPUT PIXEL...
# loop over output image height
for
oy
in
N
.
arange
(
lbound
[
0
],
ubound
[
0
],
dy
):
for
oy
in
np
.
arange
(
lbound
[
0
],
ubound
[
0
],
dy
):
# loop over output image width
for
ox
in
N
.
arange
(
lbound
[
1
],
ubound
[
1
],
dx
):
for
ox
in
np
.
arange
(
lbound
[
1
],
ubound
[
1
],
dx
):
# kern[l] is filter value to apply at (oj,oi)
# for (iy,ix)
l
=
0
# ... ITERATE OVER INPUT UNITS IN RECEPTIVE FIELD
for
ky
in
oy
+
N
.
arange
(
kshp
[
0
]):
for
kx
in
ox
+
N
.
arange
(
kshp
[
1
]):
for
ky
in
oy
+
np
.
arange
(
kshp
[
0
]):
for
kx
in
ox
+
np
.
arange
(
kshp
[
1
]):
# verify if we are still within image
# boundaries. Equivalent to
...
...
@@ -173,13 +172,13 @@ class ConvolutionIndices(Op):
# convert to "valid" input space
# coords used to determine column
# index to write to in sparse mat
iy
,
ix
=
N
.
array
((
ky
,
kx
))
-
topleft
iy
,
ix
=
np
.
array
((
ky
,
kx
))
-
topleft
# determine raster-index of input pixel...
# taking into account multiple
# input features
col
=
iy
*
inshp
[
2
]
+
ix
+
\
fmapi
*
N
.
prod
(
inshp
[
1
:])
fmapi
*
np
.
prod
(
inshp
[
1
:])
# convert oy,ox values to output
# space coordinates
...
...
@@ -188,7 +187,7 @@ class ConvolutionIndices(Op):
else
:
(
y
,
x
)
=
(
oy
,
ox
)
-
topleft
# taking into account step size
(
y
,
x
)
=
N
.
array
([
y
,
x
])
/
(
dy
,
dx
)
(
y
,
x
)
=
np
.
array
([
y
,
x
])
/
(
dy
,
dx
)
# convert to row index of sparse matrix
if
ws
:
...
...
@@ -228,7 +227,7 @@ class ConvolutionIndices(Op):
if
ws
:
kmap
=
None
else
:
kmap
=
N
.
zeros
(
ntaps
,
dtype
=
'int'
)
kmap
=
np
.
zeros
(
ntaps
,
dtype
=
'int'
)
k
=
0
# print 'TEMPORARY BUGFIX: REMOVE !!!'
for
j
in
xrange
(
spmat
.
shape
[
1
]):
...
...
@@ -259,7 +258,7 @@ class ConvolutionIndices(Op):
indices
,
indptr
,
spmatshp
,
outshp
=
self
.
evaluate
(
inshp
,
kshp
)
out_indices
[
0
]
=
indices
out_indptr
[
0
]
=
indptr
spmat_shape
[
0
]
=
n
umpy
.
asarray
(
spmatshp
)
spmat_shape
[
0
]
=
n
p
.
asarray
(
spmatshp
)
convolution_indices
=
ConvolutionIndices
()
...
...
@@ -318,13 +317,12 @@ def convolve(kerns, kshp, nkern, images, imgshp, step=(1, 1), bias=None,
:TODO: test for 1D and think of how to do n-d convolutions
"""
N
=
numpy
# start by computing output dimensions, size, etc
kern_size
=
N
.
int64
(
N
.
prod
(
kshp
))
kern_size
=
np
.
int64
(
np
.
prod
(
kshp
))
# inshp contains either 2 entries (height,width) or 3 (nfeatures,h,w)
# in the first case, default nfeatures to 1
if
N
.
size
(
imgshp
)
==
2
:
if
np
.
size
(
imgshp
)
==
2
:
imgshp
=
(
1
,)
+
imgshp
# construct indices and index pointers for sparse matrix, which,
...
...
@@ -334,12 +332,12 @@ def convolve(kerns, kshp, nkern, images, imgshp, step=(1, 1), bias=None,
convolution_indices
.
conv_eval
(
imgshp
,
kshp
,
step
,
mode
)
# build sparse matrix, then generate stack of image patches
csc
=
theano
.
sparse
.
CSM
(
sptype
)(
N
.
ones
(
indices
.
size
),
indices
,
csc
=
theano
.
sparse
.
CSM
(
sptype
)(
np
.
ones
(
indices
.
size
),
indices
,
indptr
,
spmat_shape
)
patches
=
(
sparse
.
structured_dot
(
csc
,
images
.
T
))
.
T
# compute output of linear classifier
pshape
=
tensor
.
stack
([
images
.
shape
[
0
]
*
tensor
.
as_tensor
(
N
.
prod
(
outshp
)),
\
pshape
=
tensor
.
stack
([
images
.
shape
[
0
]
*
tensor
.
as_tensor
(
np
.
prod
(
outshp
)),
\
tensor
.
as_tensor
(
imgshp
[
0
]
*
kern_size
)])
patch_stack
=
tensor
.
reshape
(
patches
,
pshape
,
ndim
=
2
)
...
...
@@ -354,14 +352,14 @@ def convolve(kerns, kshp, nkern, images, imgshp, step=(1, 1), bias=None,
# now to have feature maps in raster order ...
# go from bsize*outshp x nkern to bsize x nkern*outshp
newshp
=
tensor
.
stack
([
images
.
shape
[
0
],
\
tensor
.
as_tensor
(
N
.
prod
(
outshp
)),
\
tensor
.
as_tensor
(
np
.
prod
(
outshp
)),
\
tensor
.
as_tensor
(
nkern
)])
tensout
=
tensor
.
reshape
(
output
,
newshp
,
ndim
=
3
)
output
=
tensor
.
DimShuffle
((
False
,)
*
tensout
.
ndim
,
(
0
,
2
,
1
))(
tensout
)
if
flatten
:
output
=
tensor
.
flatten
(
output
,
2
)
return
output
,
N
.
hstack
((
nkern
,
outshp
))
return
output
,
np
.
hstack
((
nkern
,
outshp
))
def
max_pool
(
images
,
imgshp
,
maxpoolshp
):
...
...
@@ -380,12 +378,11 @@ def max_pool(images, imgshp, maxpoolshp):
:return: out1, symbolic result (2D tensor)
:return: out2, logical shape of the output
"""
N
=
numpy
poolsize
=
N
.
int64
(
N
.
prod
(
maxpoolshp
))
poolsize
=
np
.
int64
(
np
.
prod
(
maxpoolshp
))
# imgshp contains either 2 entries (height,width) or 3 (nfeatures,h,w)
# in the first case, default nfeatures to 1
if
N
.
size
(
imgshp
)
==
2
:
if
np
.
size
(
imgshp
)
==
2
:
imgshp
=
(
1
,)
+
imgshp
# construct indices and index pointers for sparse matrix, which,
...
...
@@ -401,12 +398,12 @@ def max_pool(images, imgshp, maxpoolshp):
# print 'outshp = ', outshp
# build sparse matrix, then generate stack of image patches
csc
=
theano
.
sparse
.
CSM
(
sptype
)(
N
.
ones
(
indices
.
size
),
indices
,
csc
=
theano
.
sparse
.
CSM
(
sptype
)(
np
.
ones
(
indices
.
size
),
indices
,
indptr
,
spmat_shape
)
patches
=
sparse
.
structured_dot
(
csc
,
images
.
T
)
.
T
pshape
=
tensor
.
stack
([
images
.
shape
[
0
]
*
\
tensor
.
as_tensor
(
N
.
prod
(
outshp
)),
tensor
.
as_tensor
(
np
.
prod
(
outshp
)),
tensor
.
as_tensor
(
imgshp
[
0
]),
tensor
.
as_tensor
(
poolsize
)])
patch_stack
=
tensor
.
reshape
(
patches
,
pshape
,
ndim
=
3
)
...
...
@@ -414,7 +411,7 @@ def max_pool(images, imgshp, maxpoolshp):
out1
=
tensor
.
max
(
patch_stack
,
axis
=
2
)
pshape
=
tensor
.
stack
([
images
.
shape
[
0
],
tensor
.
as_tensor
(
N
.
prod
(
outshp
)),
tensor
.
as_tensor
(
np
.
prod
(
outshp
)),
tensor
.
as_tensor
(
imgshp
[
0
])])
out2
=
tensor
.
reshape
(
out1
,
pshape
,
ndim
=
3
)
...
...
theano/sparse/sandbox/sp2.py
浏览文件 @
2b7ee2ec
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
from
six.moves
import
xrange
import
theano
import
scipy.sparse
...
...
@@ -74,7 +74,7 @@ class Poisson(gof.op.Op):
assert
_is_sparse
(
x
)
assert
x
.
format
in
[
"csr"
,
"csc"
]
out
[
0
]
=
x
.
copy
()
out
[
0
]
.
data
=
n
umpy
.
asarray
(
numpy
.
random
.
poisson
(
out
[
0
]
.
data
),
out
[
0
]
.
data
=
n
p
.
asarray
(
np
.
random
.
poisson
(
out
[
0
]
.
data
),
dtype
=
x
.
dtype
)
out
[
0
]
.
eliminate_zeros
()
...
...
@@ -123,7 +123,7 @@ class Binomial(gof.op.Op):
def
perform
(
self
,
node
,
inputs
,
outputs
):
(
n
,
p
,
shape
)
=
inputs
(
out
,)
=
outputs
binomial
=
n
umpy
.
random
.
binomial
(
n
,
p
,
size
=
shape
)
binomial
=
n
p
.
random
.
binomial
(
n
,
p
,
size
=
shape
)
csx_matrix
=
getattr
(
scipy
.
sparse
,
self
.
format
+
'_matrix'
)
out
[
0
]
=
csx_matrix
(
binomial
,
dtype
=
self
.
dtype
)
...
...
@@ -195,14 +195,14 @@ class Multinomial(gof.op.Op):
if
n
.
ndim
==
0
:
for
i
in
xrange
(
p
.
shape
[
0
]):
k
,
l
=
p
.
indptr
[
i
],
p
.
indptr
[
i
+
1
]
out
[
0
]
.
data
[
k
:
l
]
=
n
umpy
.
random
.
multinomial
(
n
,
p
.
data
[
k
:
l
])
out
[
0
]
.
data
[
k
:
l
]
=
n
p
.
random
.
multinomial
(
n
,
p
.
data
[
k
:
l
])
elif
n
.
ndim
==
1
:
if
n
.
shape
[
0
]
!=
p
.
shape
[
0
]:
raise
ValueError
(
'The number of element of n must be '
'the same as the number of row of p.'
)
for
i
in
xrange
(
p
.
shape
[
0
]):
k
,
l
=
p
.
indptr
[
i
],
p
.
indptr
[
i
+
1
]
out
[
0
]
.
data
[
k
:
l
]
=
n
umpy
.
random
.
multinomial
(
n
[
i
],
p
.
data
[
k
:
l
])
out
[
0
]
.
data
[
k
:
l
]
=
n
p
.
random
.
multinomial
(
n
[
i
],
p
.
data
[
k
:
l
])
def
grad
(
self
,
inputs
,
outputs_gradients
):
comment_n
=
"No gradient exists for the number of samples in class
\
...
...
theano/sparse/sandbox/test_sp.py
浏览文件 @
2b7ee2ec
...
...
@@ -11,7 +11,7 @@ if not theano.sparse.enable_sparse:
import
scipy.sparse
from
scipy.signal
import
convolve2d
import
scipy.sparse
as
sparse
import
numpy
import
numpy
as
np
from
six.moves
import
xrange
from
theano
import
function
,
tensor
...
...
@@ -43,8 +43,8 @@ class TestSP(unittest.TestCase):
bias
=
tensor
.
dvector
()
kerns
=
tensor
.
dmatrix
()
input
=
tensor
.
dmatrix
()
rng
=
n
umpy
.
random
.
RandomState
(
3423489
)
filters
=
rng
.
randn
(
nkern
,
n
umpy
.
prod
(
kshp
))
rng
=
n
p
.
random
.
RandomState
(
3423489
)
filters
=
rng
.
randn
(
nkern
,
n
p
.
prod
(
kshp
))
biasvals
=
rng
.
randn
(
nkern
)
for
mode
in
(
'FAST_COMPILE'
,
'FAST_RUN'
):
...
...
@@ -57,12 +57,12 @@ class TestSP(unittest.TestCase):
f
=
function
([
kerns
,
bias
,
input
],
output
,
mode
=
mode
)
# now test with real values
img2d
=
n
umpy
.
arange
(
bsize
*
numpy
.
prod
(
imshp
))
.
reshape
((
\
img2d
=
n
p
.
arange
(
bsize
*
np
.
prod
(
imshp
))
.
reshape
((
\
bsize
,)
+
imshp
)
img1d
=
img2d
.
reshape
(
bsize
,
-
1
)
# create filters (need to be flipped to use convolve2d)
filtersflipped
=
n
umpy
.
zeros
((
nkern
,)
+
kshp
)
filtersflipped
=
n
p
.
zeros
((
nkern
,)
+
kshp
)
for
k
in
range
(
nkern
):
it
=
reversed
(
filters
[
k
,
:])
for
i
in
range
(
kshp
[
0
]):
...
...
@@ -71,11 +71,11 @@ class TestSP(unittest.TestCase):
# compute output with convolve2d
if
conv_mode
==
'valid'
:
fulloutshp
=
n
umpy
.
array
(
imshp
)
-
numpy
.
array
(
kshp
)
+
1
fulloutshp
=
n
p
.
array
(
imshp
)
-
np
.
array
(
kshp
)
+
1
else
:
fulloutshp
=
n
umpy
.
array
(
imshp
)
+
numpy
.
array
(
kshp
)
-
1
fulloutshp
=
n
p
.
array
(
imshp
)
+
np
.
array
(
kshp
)
-
1
ntime1
=
time
.
time
()
refout
=
n
umpy
.
zeros
((
bsize
,)
+
tuple
(
fulloutshp
)
+
(
nkern
,))
refout
=
n
p
.
zeros
((
bsize
,)
+
tuple
(
fulloutshp
)
+
(
nkern
,))
for
b
in
range
(
bsize
):
for
n
in
range
(
nkern
):
refout
[
b
,
...
,
n
]
=
convolve2d
(
img2d
[
b
,
:,
:],
...
...
@@ -88,7 +88,7 @@ class TestSP(unittest.TestCase):
bench1
+=
biasvals
.
reshape
(
1
,
1
,
nkern
)
# swap the last two dimensions (output needs to be nkern x outshp)
bench1
=
n
umpy
.
swapaxes
(
bench1
,
1
,
2
)
bench1
=
n
p
.
swapaxes
(
bench1
,
1
,
2
)
ttime1
=
time
.
time
()
out1
=
f
(
filters
,
biasvals
,
img1d
)
ttot
+=
time
.
time
()
-
ttime1
...
...
@@ -101,13 +101,13 @@ class TestSP(unittest.TestCase):
#downprop = function([kerns,input], vis, mode=mode)
#visval = downprop(filters,img1d)
# test downward propagation -- reference implementation
#pshape = (img1d.shape[0],n
umpy.prod(outshp[1:]),numpy
.prod(kshp))
#patchstack = n
umpy
.zeros(pshape)
# for bi in n
umpy
.arange(pshape[0]): # batch index
#pshape = (img1d.shape[0],n
p.prod(outshp[1:]),np
.prod(kshp))
#patchstack = n
p
.zeros(pshape)
# for bi in n
p
.arange(pshape[0]): # batch index
#abspos = 0
# for outy in n
umpy
.arange(outshp[1]):
# for outx in n
umpy
.arange(outshp[2]):
# for ni in n
umpy
.arange(nkern):
# for outy in n
p
.arange(outshp[1]):
# for outx in n
p
.arange(outshp[2]):
# for ni in n
p
.arange(nkern):
# print 'filters[n,:].shape = ', filters[n,:].shape
# print 'out1[bi,abspos].shape =',out1[bi,abspos].shape
#patchstack[bi,abspos,:] = filters[n,:]*out1[bi,abspos]
...
...
@@ -115,13 +115,13 @@ class TestSP(unittest.TestCase):
#patchstack = patchstack.reshape(1,-1)
# indices, indptr, spmat_shape, sptype, outshp = \
# sp.convolution_indices.conv_eval(imshp,kshp,ss,conv_mode)
#spmat = sparse.csc_matrix((n
umpy
.ones_like(indices),indices,indptr),spmat_shape)
#visref = n
umpy
.dot(patchstack, spmat.todense())
#spmat = sparse.csc_matrix((n
p
.ones_like(indices),indices,indptr),spmat_shape)
#visref = n
p
.dot(patchstack, spmat.todense())
# print 'visval = ', visval
# print 'visref = ', visref
#assert n
umpy
.all(visref==visval)
#assert n
p
.all(visref==visval)
# print '**** Convolution Profiling Results (',mode,') ****'
...
...
@@ -143,10 +143,10 @@ class TestSP(unittest.TestCase):
# symbolic stuff
kerns
=
[
tensor
.
dmatrix
(),
tensor
.
dmatrix
()]
input
=
tensor
.
dmatrix
()
rng
=
n
umpy
.
random
.
RandomState
(
3423489
)
rng
=
n
p
.
random
.
RandomState
(
3423489
)
# build actual input images
img2d
=
n
umpy
.
arange
(
bsize
*
numpy
.
prod
(
imshp
))
.
reshape
((
bsize
,)
+
imshp
)
img2d
=
n
p
.
arange
(
bsize
*
np
.
prod
(
imshp
))
.
reshape
((
bsize
,)
+
imshp
)
img1d
=
img2d
.
reshape
(
bsize
,
-
1
)
for
mode
in
(
'FAST_COMPILE'
,
'FAST_RUN'
):
...
...
@@ -157,8 +157,8 @@ class TestSP(unittest.TestCase):
nkerns
[
0
],
input
,
imshp
,
ss
[
0
],
mode
=
conv_mode
)
l1propup
=
function
([
kerns
[
0
],
input
],
l1hid
,
mode
=
mode
)
#l1kernvals = n
umpy.random.rand(nkerns[0],numpy
.prod(kshp[0]))
l1kernvals
=
n
umpy
.
arange
(
nkerns
[
0
]
*
numpy
.
prod
(
kshp
[
0
]))
.
reshape
(
nkerns
[
0
],
numpy
.
prod
(
kshp
[
0
]))
#l1kernvals = n
p.random.rand(nkerns[0],np
.prod(kshp[0]))
l1kernvals
=
n
p
.
arange
(
nkerns
[
0
]
*
np
.
prod
(
kshp
[
0
]))
.
reshape
(
nkerns
[
0
],
np
.
prod
(
kshp
[
0
]))
l1hidval
=
l1propup
(
l1kernvals
,
img1d
)
# actual values
...
...
@@ -166,17 +166,17 @@ class TestSP(unittest.TestCase):
nkerns
[
1
],
l1hid
,
l1shp
,
ss
[
1
],
mode
=
conv_mode
)
l2propup
=
function
([
kerns
[
1
],
l1hid
],
l2hid
,
mode
=
mode
)
#l2kernvals = n
umpy.random.rand(nkerns[1],numpy
.prod(kshp[1])*nkerns[0])
l2kernvals
=
n
umpy
.
arange
(
nkerns
[
1
]
*
numpy
.
prod
(
kshp
[
1
])
*
nkerns
[
0
])
.
reshape
(
nkerns
[
1
],
numpy
.
prod
(
kshp
[
1
])
*
nkerns
[
0
])
#l2kernvals = n
p.random.rand(nkerns[1],np
.prod(kshp[1])*nkerns[0])
l2kernvals
=
n
p
.
arange
(
nkerns
[
1
]
*
np
.
prod
(
kshp
[
1
])
*
nkerns
[
0
])
.
reshape
(
nkerns
[
1
],
np
.
prod
(
kshp
[
1
])
*
nkerns
[
0
])
# for debugging, we bring things back to integers
l1hidval
=
n
umpy
.
arange
(
numpy
.
size
(
l1hidval
))
.
reshape
(
l1hidval
.
shape
)
l1hidval
=
n
p
.
arange
(
np
.
size
(
l1hidval
))
.
reshape
(
l1hidval
.
shape
)
l2hidval
=
l2propup
(
l2kernvals
,
l1hidval
)
def
test_maxpool
(
self
):
# generate flatted images
maxpoolshps
=
((
2
,
2
),
(
3
,
3
),
(
4
,
4
),
(
5
,
5
),
(
6
,
6
))
imval
=
n
umpy
.
random
.
rand
(
4
,
5
,
10
,
10
)
imval
=
n
p
.
random
.
rand
(
4
,
5
,
10
,
10
)
images
=
tensor
.
dmatrix
()
for
maxpoolshp
in
maxpoolshps
:
...
...
@@ -187,10 +187,10 @@ class TestSP(unittest.TestCase):
output_val
=
f
(
imval
.
reshape
(
imval
.
shape
[
0
],
-
1
))
# numeric verification
my_output_val
=
n
umpy
.
zeros
((
imval
.
shape
[
0
],
imval
.
shape
[
1
],
my_output_val
=
n
p
.
zeros
((
imval
.
shape
[
0
],
imval
.
shape
[
1
],
imval
.
shape
[
2
]
//
maxpoolshp
[
0
],
imval
.
shape
[
3
]
//
maxpoolshp
[
1
]))
assert
n
umpy
.
prod
(
my_output_val
.
shape
[
1
:])
==
numpy
.
prod
(
numpy
.
r_
[
imval
.
shape
[
1
],
outshp
])
assert
n
p
.
prod
(
my_output_val
.
shape
[
1
:])
==
np
.
prod
(
np
.
r_
[
imval
.
shape
[
1
],
outshp
])
for
n
in
range
(
imval
.
shape
[
0
]):
for
k
in
range
(
imval
.
shape
[
1
]):
...
...
@@ -198,9 +198,9 @@ class TestSP(unittest.TestCase):
for
j
in
range
(
imval
.
shape
[
3
]
//
maxpoolshp
[
1
]):
ii
,
jj
=
i
*
maxpoolshp
[
0
],
j
*
maxpoolshp
[
1
]
patch
=
imval
[
n
,
k
,
ii
:
ii
+
maxpoolshp
[
0
],
jj
:
jj
+
maxpoolshp
[
1
]]
my_output_val
[
n
,
k
,
i
,
j
]
=
n
umpy
.
max
(
patch
)
my_output_val
[
n
,
k
,
i
,
j
]
=
n
p
.
max
(
patch
)
my_output_val
=
my_output_val
.
reshape
(
imval
.
shape
[
0
],
-
1
)
assert
n
umpy
.
all
(
output_val
==
my_output_val
)
assert
n
p
.
all
(
output_val
==
my_output_val
)
def
mp
(
input
):
output
,
outshp
=
sp
.
max_pool
(
input
,
imval
.
shape
[
1
:],
maxpoolshp
)
...
...
theano/sparse/sandbox/truedot.py
浏览文件 @
2b7ee2ec
...
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
import
unittest
import
theano
import
numpy
import
numpy
as
np
import
scipy.sparse
as
sp
from
theano
import
sparse
...
...
theano/sparse/tests/test_basic.py
浏览文件 @
2b7ee2ec
差异被折叠。
点击展开。
theano/sparse/tests/test_opt.py
浏览文件 @
2b7ee2ec
from
__future__
import
absolute_import
,
print_function
,
division
from
nose.plugins.skip
import
SkipTest
import
numpy
import
numpy
as
np
try
:
import
scipy.sparse
as
sp
import
scipy.sparse
...
...
@@ -157,14 +157,14 @@ def test_local_dense_from_sparse_sparse_from_dense():
def
test_sd_csc
():
A
=
sp
.
rand
(
4
,
5
,
density
=
0.60
,
format
=
'csc'
,
dtype
=
n
umpy
.
float32
)
b
=
n
umpy
.
random
.
rand
(
5
,
2
)
.
astype
(
numpy
.
float32
)
A
=
sp
.
rand
(
4
,
5
,
density
=
0.60
,
format
=
'csc'
,
dtype
=
n
p
.
float32
)
b
=
n
p
.
random
.
rand
(
5
,
2
)
.
astype
(
np
.
float32
)
target
=
A
*
b
a_val
=
theano
.
tensor
.
as_tensor_variable
(
A
.
data
)
a_ind
=
theano
.
tensor
.
as_tensor_variable
(
A
.
indices
)
a_ptr
=
theano
.
tensor
.
as_tensor_variable
(
A
.
indptr
)
nrows
=
theano
.
tensor
.
as_tensor_variable
(
n
umpy
.
int32
(
A
.
shape
[
0
]))
nrows
=
theano
.
tensor
.
as_tensor_variable
(
n
p
.
int32
(
A
.
shape
[
0
]))
b
=
theano
.
tensor
.
as_tensor_variable
(
b
)
res
=
theano
.
sparse
.
opt
.
sd_csc
(
a_val
,
a_ind
,
a_ptr
,
nrows
,
b
)
.
eval
()
...
...
theano/sparse/tests/test_sp2.py
浏览文件 @
2b7ee2ec
...
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
import
unittest
from
nose.plugins.skip
import
SkipTest
import
numpy
import
numpy
as
np
try
:
import
scipy.sparse
as
sp
except
ImportError
:
...
...
@@ -30,7 +30,7 @@ class PoissonTester(utt.InferShapeTester):
for
format
in
sparse
.
sparse_formats
:
variable
=
getattr
(
theano
.
sparse
,
format
+
'_matrix'
)
rand
=
n
umpy
.
array
(
numpy
.
random
.
randint
(
1
,
4
,
size
=
(
3
,
4
))
-
1
,
rand
=
n
p
.
array
(
np
.
random
.
randint
(
1
,
4
,
size
=
(
3
,
4
))
-
1
,
dtype
=
theano
.
config
.
floatX
)
x
[
format
]
=
variable
()
...
...
@@ -50,7 +50,7 @@ class PoissonTester(utt.InferShapeTester):
assert
tested
.
format
==
format
assert
tested
.
dtype
==
self
.
a
[
format
]
.
dtype
assert
n
umpy
.
allclose
(
numpy
.
floor
(
tested
.
data
),
tested
.
data
)
assert
n
p
.
allclose
(
np
.
floor
(
tested
.
data
),
tested
.
data
)
assert
tested
.
shape
==
self
.
a
[
format
]
.
shape
def
test_infer_shape
(
self
):
...
...
@@ -67,7 +67,7 @@ class BinomialTester(utt.InferShapeTester):
shape
=
tensor
.
lvector
()
_n
=
5
_p
=
.
25
_shape
=
n
umpy
.
asarray
([
3
,
5
],
dtype
=
'int64'
)
_shape
=
n
p
.
asarray
([
3
,
5
],
dtype
=
'int64'
)
inputs
=
[
n
,
p
,
shape
]
_inputs
=
[
_n
,
_p
,
_shape
]
...
...
@@ -88,7 +88,7 @@ class BinomialTester(utt.InferShapeTester):
assert
tested
.
shape
==
tuple
(
self
.
_shape
)
assert
tested
.
format
==
sp_format
assert
tested
.
dtype
==
o_type
assert
n
umpy
.
allclose
(
numpy
.
floor
(
tested
.
todense
()),
assert
n
p
.
allclose
(
np
.
floor
(
tested
.
todense
()),
tested
.
todense
())
def
test_infer_shape
(
self
):
...
...
@@ -103,7 +103,7 @@ class BinomialTester(utt.InferShapeTester):
class
MultinomialTester
(
utt
.
InferShapeTester
):
p
=
sparse
.
csr_matrix
()
_p
=
sp
.
csr_matrix
(
n
umpy
.
asarray
([[
0.0
,
0.5
,
0.0
,
0.5
],
_p
=
sp
.
csr_matrix
(
n
p
.
asarray
([[
0.0
,
0.5
,
0.0
,
0.5
],
[
0.1
,
0.2
,
0.3
,
0.4
],
[
0.0
,
1.0
,
0.0
,
0.0
],
[
0.3
,
0.3
,
0.0
,
0.4
]],
...
...
@@ -120,16 +120,16 @@ class MultinomialTester(utt.InferShapeTester):
_n
=
5
tested
=
f
(
self
.
_p
,
_n
)
assert
tested
.
shape
==
self
.
_p
.
shape
assert
n
umpy
.
allclose
(
numpy
.
floor
(
tested
.
todense
()),
tested
.
todense
())
assert
n
p
.
allclose
(
np
.
floor
(
tested
.
todense
()),
tested
.
todense
())
assert
tested
[
2
,
1
]
==
_n
n
=
tensor
.
lvector
()
f
=
theano
.
function
([
self
.
p
,
n
],
multinomial
(
n
,
self
.
p
))
_n
=
n
umpy
.
asarray
([
1
,
2
,
3
,
4
],
dtype
=
'int64'
)
_n
=
n
p
.
asarray
([
1
,
2
,
3
,
4
],
dtype
=
'int64'
)
tested
=
f
(
self
.
_p
,
_n
)
assert
tested
.
shape
==
self
.
_p
.
shape
assert
n
umpy
.
allclose
(
numpy
.
floor
(
tested
.
todense
()),
tested
.
todense
())
assert
n
p
.
allclose
(
np
.
floor
(
tested
.
todense
()),
tested
.
todense
())
assert
tested
[
2
,
1
]
==
_n
[
2
]
def
test_infer_shape
(
self
):
...
...
theano/sparse/tests/test_utils.py
浏览文件 @
2b7ee2ec
from
__future__
import
absolute_import
,
print_function
,
division
from
nose.plugins.skip
import
SkipTest
import
numpy
import
numpy
as
np
import
theano.sparse
if
not
theano
.
sparse
.
enable_sparse
:
raise
SkipTest
(
'Optional package sparse disabled'
)
...
...
@@ -11,21 +11,21 @@ from theano.sparse.tests.test_basic import as_sparse_format
def
test_hash_from_sparse
():
hashs
=
[]
rng
=
n
umpy
.
random
.
rand
(
5
,
5
)
rng
=
n
p
.
random
.
rand
(
5
,
5
)
for
format
in
[
'csc'
,
'csr'
]:
rng
=
as_sparse_format
(
rng
,
format
)
for
data
in
[[[
-
2
]],
[[
-
1
]],
[[
0
]],
[[
1
]],
[[
2
]],
n
umpy
.
zeros
((
1
,
5
)),
numpy
.
zeros
((
1
,
6
)),
n
p
.
zeros
((
1
,
5
)),
np
.
zeros
((
1
,
6
)),
# Data buffer empty but different shapes
# n
umpy.zeros((1, 0)), numpy
.zeros((2, 0)),
# n
p.zeros((1, 0)), np
.zeros((2, 0)),
# Same data buffer and shapes but different strides
n
umpy
.
arange
(
25
)
.
reshape
(
5
,
5
),
n
umpy
.
arange
(
25
)
.
reshape
(
5
,
5
)
.
T
,
n
p
.
arange
(
25
)
.
reshape
(
5
,
5
),
n
p
.
arange
(
25
)
.
reshape
(
5
,
5
)
.
T
,
# Same data buffer, shapes and strides
# but different dtypes
n
umpy
.
zeros
((
5
,
5
),
dtype
=
"uint32"
),
n
umpy
.
zeros
((
5
,
5
),
dtype
=
"int32"
),
n
p
.
zeros
((
5
,
5
),
dtype
=
"uint32"
),
n
p
.
zeros
((
5
,
5
),
dtype
=
"int32"
),
# Test slice
rng
,
rng
[
1
:],
rng
[:
4
],
rng
[
1
:
3
],
# Don't test step as they are not supported by sparse
...
...
theano/sparse/type.py
浏览文件 @
2b7ee2ec
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
try
:
import
scipy.sparse
imported_scipy
=
True
...
...
@@ -20,7 +20,7 @@ def _is_sparse(x):
True iff x is a L{scipy.sparse.spmatrix} (and not a L{numpy.ndarray}).
"""
if
not
isinstance
(
x
,
(
scipy
.
sparse
.
spmatrix
,
n
umpy
.
ndarray
,
tuple
,
list
)):
if
not
isinstance
(
x
,
(
scipy
.
sparse
.
spmatrix
,
n
p
.
ndarray
,
tuple
,
list
)):
raise
NotImplementedError
(
"this function should only be called on "
"sparse.scipy.sparse.spmatrix or "
"numpy.ndarray, not,"
,
x
)
...
...
@@ -107,12 +107,12 @@ class SparseType(gof.Type):
return
(
SparseType
.
may_share_memory
(
a
,
b
.
data
)
or
SparseType
.
may_share_memory
(
a
,
b
.
indices
)
or
SparseType
.
may_share_memory
(
a
,
b
.
indptr
))
if
_is_sparse
(
b
)
and
isinstance
(
a
,
n
umpy
.
ndarray
):
if
_is_sparse
(
b
)
and
isinstance
(
a
,
n
p
.
ndarray
):
a
,
b
=
b
,
a
if
_is_sparse
(
a
)
and
isinstance
(
b
,
n
umpy
.
ndarray
):
if
(
n
umpy
.
may_share_memory
(
a
.
data
,
b
)
or
n
umpy
.
may_share_memory
(
a
.
indices
,
b
)
or
n
umpy
.
may_share_memory
(
a
.
indptr
,
b
)):
if
_is_sparse
(
a
)
and
isinstance
(
b
,
n
p
.
ndarray
):
if
(
n
p
.
may_share_memory
(
a
.
data
,
b
)
or
n
p
.
may_share_memory
(
a
.
indices
,
b
)
or
n
p
.
may_share_memory
(
a
.
indptr
,
b
)):
# currently we can't share memory with a.shape as it is a tuple
return
True
return
False
...
...
@@ -168,8 +168,8 @@ class SparseType(gof.Type):
obj
.
indices
.
size
,
obj
.
indptr
.
size
,
obj
.
nnz
)
def
get_size
(
self
,
shape_info
):
return
(
shape_info
[
1
]
*
n
umpy
.
dtype
(
self
.
dtype
)
.
itemsize
+
(
shape_info
[
2
]
+
shape_info
[
3
])
*
n
umpy
.
dtype
(
'int32'
)
.
itemsize
)
return
(
shape_info
[
1
]
*
n
p
.
dtype
(
self
.
dtype
)
.
itemsize
+
(
shape_info
[
2
]
+
shape_info
[
3
])
*
n
p
.
dtype
(
'int32'
)
.
itemsize
)
# Register SparseType's C code for ViewOp.
theano
.
compile
.
register_view_op_c_code
(
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论