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 个修改的文件
包含
316 行增加
和
319 行删除
+316
-319
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
+188
-188
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
...
@@ -12,7 +12,7 @@ from __future__ import absolute_import, print_function, division
import
sys
import
sys
import
numpy
import
numpy
as
np
from
numpy.lib.stride_tricks
import
as_strided
from
numpy.lib.stride_tricks
import
as_strided
from
six
import
integer_types
from
six
import
integer_types
from
six.moves
import
xrange
from
six.moves
import
xrange
...
@@ -86,11 +86,11 @@ def _is_dense(x):
...
@@ -86,11 +86,11 @@ def _is_dense(x):
L{numpy.ndarray}).
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 "
raise
NotImplementedError
(
"this function should only be called on "
"sparse.scipy.sparse.spmatrix or "
"sparse.scipy.sparse.spmatrix or "
"numpy.ndarray, not,"
,
x
)
"numpy.ndarray, not,"
,
x
)
return
isinstance
(
x
,
n
umpy
.
ndarray
)
return
isinstance
(
x
,
n
p
.
ndarray
)
# Wrapper type
# Wrapper type
...
@@ -205,8 +205,8 @@ def sp_zeros_like(x):
...
@@ -205,8 +205,8 @@ def sp_zeros_like(x):
# TODO: don't restrict to CSM formats
# TODO: don't restrict to CSM formats
_
,
_
,
indptr
,
shape
=
csm_properties
(
x
)
_
,
_
,
indptr
,
shape
=
csm_properties
(
x
)
return
CSM
(
format
=
x
.
format
)(
data
=
n
umpy
.
array
([],
dtype
=
x
.
type
.
dtype
),
return
CSM
(
format
=
x
.
format
)(
data
=
n
p
.
array
([],
dtype
=
x
.
type
.
dtype
),
indices
=
n
umpy
.
array
([],
dtype
=
'int32'
),
indices
=
n
p
.
array
([],
dtype
=
'int32'
),
indptr
=
tensor
.
zeros_like
(
indptr
),
indptr
=
tensor
.
zeros_like
(
indptr
),
shape
=
shape
)
shape
=
shape
)
...
@@ -293,9 +293,9 @@ class _sparse_py_operators:
...
@@ -293,9 +293,9 @@ class _sparse_py_operators:
args
=
args
,
args
=
args
,
if
len
(
args
)
==
2
:
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
)
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
)
getattr
(
args
[
1
],
'type'
,
None
)
==
tensor
.
iscalar
)
if
scalar_arg_1
and
scalar_arg_2
:
if
scalar_arg_1
and
scalar_arg_2
:
ret
=
get_item_scalar
(
self
,
args
)
ret
=
get_item_scalar
(
self
,
args
)
...
@@ -554,17 +554,17 @@ class CSM(gof.Op):
...
@@ -554,17 +554,17 @@ class CSM(gof.Op):
data
=
tensor
.
as_tensor_variable
(
data
)
data
=
tensor
.
as_tensor_variable
(
data
)
if
not
isinstance
(
indices
,
gof
.
Variable
):
if
not
isinstance
(
indices
,
gof
.
Variable
):
indices_
=
n
umpy
.
asarray
(
indices
)
indices_
=
n
p
.
asarray
(
indices
)
indices_32
=
theano
.
_asarray
(
indices
,
dtype
=
'int32'
)
indices_32
=
theano
.
_asarray
(
indices
,
dtype
=
'int32'
)
assert
(
indices_
==
indices_32
)
.
all
()
assert
(
indices_
==
indices_32
)
.
all
()
indices
=
indices_32
indices
=
indices_32
if
not
isinstance
(
indptr
,
gof
.
Variable
):
if
not
isinstance
(
indptr
,
gof
.
Variable
):
indptr_
=
n
umpy
.
asarray
(
indptr
)
indptr_
=
n
p
.
asarray
(
indptr
)
indptr_32
=
theano
.
_asarray
(
indptr
,
dtype
=
'int32'
)
indptr_32
=
theano
.
_asarray
(
indptr
,
dtype
=
'int32'
)
assert
(
indptr_
==
indptr_32
)
.
all
()
assert
(
indptr_
==
indptr_32
)
.
all
()
indptr
=
indptr_32
indptr
=
indptr_32
if
not
isinstance
(
shape
,
gof
.
Variable
):
if
not
isinstance
(
shape
,
gof
.
Variable
):
shape_
=
n
umpy
.
asarray
(
shape
)
shape_
=
n
p
.
asarray
(
shape
)
shape_32
=
theano
.
_asarray
(
shape
,
dtype
=
'int32'
)
shape_32
=
theano
.
_asarray
(
shape
,
dtype
=
'int32'
)
assert
(
shape_
==
shape_32
)
.
all
()
assert
(
shape_
==
shape_32
)
.
all
()
shape
=
shape_32
shape
=
shape_32
...
@@ -606,7 +606,7 @@ class CSM(gof.Op):
...
@@ -606,7 +606,7 @@ class CSM(gof.Op):
if
self
.
format
==
'csc'
:
if
self
.
format
==
'csc'
:
out
[
0
]
=
scipy
.
sparse
.
csc_matrix
((
data
,
indices
.
copy
(),
out
[
0
]
=
scipy
.
sparse
.
csc_matrix
((
data
,
indices
.
copy
(),
indptr
.
copy
()),
indptr
.
copy
()),
n
umpy
.
asarray
(
shape
),
copy
=
False
)
n
p
.
asarray
(
shape
),
copy
=
False
)
else
:
else
:
assert
self
.
format
==
'csr'
assert
self
.
format
==
'csr'
out
[
0
]
=
scipy
.
sparse
.
csr_matrix
((
data
,
indices
.
copy
(),
out
[
0
]
=
scipy
.
sparse
.
csr_matrix
((
data
,
indices
.
copy
(),
...
@@ -729,8 +729,8 @@ class CSMGrad(gof.op.Op):
...
@@ -729,8 +729,8 @@ class CSMGrad(gof.op.Op):
else
:
else
:
sp_dim
=
x_shape
[
0
]
sp_dim
=
x_shape
[
0
]
g_row
=
n
umpy
.
zeros
(
sp_dim
,
dtype
=
g_data
.
dtype
)
g_row
=
n
p
.
zeros
(
sp_dim
,
dtype
=
g_data
.
dtype
)
gout_data
=
n
umpy
.
zeros
(
x_data
.
shape
,
dtype
=
node
.
outputs
[
0
]
.
dtype
)
gout_data
=
n
p
.
zeros
(
x_data
.
shape
,
dtype
=
node
.
outputs
[
0
]
.
dtype
)
for
i
in
range
(
len
(
x_indptr
)
-
1
):
for
i
in
range
(
len
(
x_indptr
)
-
1
):
for
j_ptr
in
range
(
g_indptr
[
i
],
g_indptr
[
i
+
1
]):
for
j_ptr
in
range
(
g_indptr
[
i
],
g_indptr
[
i
+
1
]):
...
@@ -1100,7 +1100,7 @@ class GetItem2Lists(gof.op.Op):
...
@@ -1100,7 +1100,7 @@ class GetItem2Lists(gof.op.Op):
x
=
inp
[
0
]
x
=
inp
[
0
]
ind1
=
inp
[
1
]
ind1
=
inp
[
1
]
ind2
=
inp
[
2
]
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
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
what we are aiming for. Using asarray and flatten, out[0] becomes an
...
@@ -1244,7 +1244,7 @@ class GetItem2d(gof.op.Op):
...
@@ -1244,7 +1244,7 @@ class GetItem2d(gof.op.Op):
elif
((
isinstance
(
ind
,
gof
.
Variable
)
and
elif
((
isinstance
(
ind
,
gof
.
Variable
)
and
getattr
(
ind
,
'ndim'
,
-
1
)
==
0
)
or
getattr
(
ind
,
'ndim'
,
-
1
)
==
0
)
or
n
umpy
.
isscalar
(
ind
)):
n
p
.
isscalar
(
ind
)):
raise
NotImplementedError
(
raise
NotImplementedError
(
'Theano has no sparse vector'
+
'Theano has no sparse vector'
+
'Use X[a:b, c:d], X[a:b, c:c+1] or X[a:b] instead.'
)
'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):
...
@@ -1653,9 +1653,9 @@ class SpSum(gof.op.Op):
(
x
,)
=
inputs
(
x
,)
=
inputs
(
z
,)
=
outputs
(
z
,)
=
outputs
if
self
.
axis
is
None
:
if
self
.
axis
is
None
:
z
[
0
]
=
n
umpy
.
asarray
(
x
.
sum
())
z
[
0
]
=
n
p
.
asarray
(
x
.
sum
())
else
:
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
):
def
grad
(
self
,
inputs
,
gout
):
(
x
,)
=
inputs
(
x
,)
=
inputs
...
@@ -2540,7 +2540,7 @@ class __ComparisonOpSD(gof.op.Op):
...
@@ -2540,7 +2540,7 @@ class __ComparisonOpSD(gof.op.Op):
assert
x
.
shape
==
y
.
shape
assert
x
.
shape
==
y
.
shape
assert
_is_dense
(
y
)
assert
_is_dense
(
y
)
o
=
self
.
comparison
(
x
,
y
)
.
astype
(
'uint8'
)
o
=
self
.
comparison
(
x
,
y
)
.
astype
(
'uint8'
)
o
=
n
umpy
.
asarray
(
o
)
o
=
n
p
.
asarray
(
o
)
out
[
0
]
=
o
out
[
0
]
=
o
def
infer_shape
(
self
,
node
,
ins_shapes
):
def
infer_shape
(
self
,
node
,
ins_shapes
):
...
@@ -3382,7 +3382,7 @@ class TrueDot(gof.op.Op):
...
@@ -3382,7 +3382,7 @@ class TrueDot(gof.op.Op):
# 'ushort', 'intc', 'uintc', 'longlong', 'ulonglong', 'single',
# 'ushort', 'intc', 'uintc', 'longlong', 'ulonglong', 'single',
# 'double', 'longdouble', 'csingle', 'cdouble', 'clongdouble']
# 'double', 'longdouble', 'csingle', 'cdouble', 'clongdouble']
# But ulonglong is uint64 on x86-64, but with a different typenum!
# 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
assert
str
(
rval
.
dtype
)
==
node
.
outputs
[
0
]
.
dtype
# Create a view with the expected typenum.
# Create a view with the expected typenum.
format
=
node
.
outputs
[
0
]
.
type
.
format
format
=
node
.
outputs
[
0
]
.
type
.
format
...
@@ -3509,7 +3509,7 @@ class StructuredDot(gof.Op):
...
@@ -3509,7 +3509,7 @@ class StructuredDot(gof.Op):
# dot of an NxM sparse matrix, with a Mx1 dense matrix, returns vector
# dot of an NxM sparse matrix, with a Mx1 dense matrix, returns vector
# not matrix
# not matrix
if
variable
.
ndim
==
1
:
if
variable
.
ndim
==
1
:
variable
=
n
umpy
.
expand_dims
(
variable
,
1
)
variable
=
n
p
.
expand_dims
(
variable
,
1
)
elif
variable
.
ndim
!=
2
:
elif
variable
.
ndim
!=
2
:
raise
Exception
(
'Output of structured dot should be a matrix '
raise
Exception
(
'Output of structured dot should be a matrix '
'(ndim=2)'
)
'(ndim=2)'
)
...
@@ -3622,7 +3622,7 @@ class StructuredDotGradCSC(gof.Op):
...
@@ -3622,7 +3622,7 @@ class StructuredDotGradCSC(gof.Op):
def
perform
(
self
,
node
,
inputs
,
outputs
):
def
perform
(
self
,
node
,
inputs
,
outputs
):
(
a_indices
,
a_indptr
,
b
,
g_ab
)
=
inputs
(
a_indices
,
a_indptr
,
b
,
g_ab
)
=
inputs
(
out
,)
=
outputs
(
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
):
for
j
in
xrange
(
len
(
a_indptr
)
-
1
):
ind0
=
a_indptr
[
j
]
ind0
=
a_indptr
[
j
]
ind1
=
a_indptr
[
j
+
1
]
ind1
=
a_indptr
[
j
+
1
]
...
@@ -3631,7 +3631,7 @@ class StructuredDotGradCSC(gof.Op):
...
@@ -3631,7 +3631,7 @@ class StructuredDotGradCSC(gof.Op):
# Depending on the type of g_ab and b (sparse or dense),
# Depending on the type of g_ab and b (sparse or dense),
# the following dot product can result in a scalar or
# the following dot product can result in a scalar or
# a (1, 1) sparse matrix.
# 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
):
if
isinstance
(
dot_val
,
scipy
.
sparse
.
spmatrix
):
dot_val
=
dot_val
[
0
,
0
]
dot_val
=
dot_val
[
0
,
0
]
g_a_data
[
i_idx
]
=
dot_val
g_a_data
[
i_idx
]
=
dot_val
...
@@ -3752,7 +3752,7 @@ class StructuredDotGradCSR(gof.Op):
...
@@ -3752,7 +3752,7 @@ class StructuredDotGradCSR(gof.Op):
def
perform
(
self
,
node
,
inputs
,
outputs
):
def
perform
(
self
,
node
,
inputs
,
outputs
):
(
a_indices
,
a_indptr
,
b
,
g_ab
)
=
inputs
(
a_indices
,
a_indptr
,
b
,
g_ab
)
=
inputs
(
out
,)
=
outputs
(
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
for
i
in
xrange
(
len
(
a_indptr
)
-
1
):
# loop over rows
ind0
=
a_indptr
[
i
]
ind0
=
a_indptr
[
i
]
ind1
=
a_indptr
[
i
+
1
]
ind1
=
a_indptr
[
i
+
1
]
...
@@ -3763,7 +3763,7 @@ class StructuredDotGradCSR(gof.Op):
...
@@ -3763,7 +3763,7 @@ class StructuredDotGradCSR(gof.Op):
# Depending on the type of g_ab and b (sparse or dense),
# Depending on the type of g_ab and b (sparse or dense),
# the following dot product can result in a scalar or
# the following dot product can result in a scalar or
# a (1, 1) sparse matrix.
# 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
):
if
isinstance
(
dot_val
,
scipy
.
sparse
.
spmatrix
):
dot_val
=
dot_val
[
0
,
0
]
dot_val
=
dot_val
[
0
,
0
]
g_a_data
[
j_idx
]
=
dot_val
g_a_data
[
j_idx
]
=
dot_val
...
@@ -3910,7 +3910,7 @@ class SamplingDot(gof.op.Op):
...
@@ -3910,7 +3910,7 @@ class SamplingDot(gof.op.Op):
if
not
_is_sparse
(
p
):
if
not
_is_sparse
(
p
):
raise
TypeError
(
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
):
def
grad
(
self
,
inputs
,
gout
):
(
x
,
y
,
p
)
=
inputs
(
x
,
y
,
p
)
=
inputs
...
@@ -4243,7 +4243,7 @@ class ConstructSparseFromList(gof.Op):
...
@@ -4243,7 +4243,7 @@ class ConstructSparseFromList(gof.Op):
out
,
=
out_
out
,
=
out_
rows
,
cols
=
values
.
shape
rows
,
cols
=
values
.
shape
assert
rows
==
len
(
ilist
)
assert
rows
==
len
(
ilist
)
indptr
=
n
umpy
.
arange
(
cols
+
1
)
*
rows
indptr
=
n
p
.
arange
(
cols
+
1
)
*
rows
indices
=
as_strided
(
ilist
,
indices
=
as_strided
(
ilist
,
strides
=
(
0
,
ilist
.
strides
[
0
]),
strides
=
(
0
,
ilist
.
strides
[
0
]),
shape
=
(
cols
,
ilist
.
shape
[
0
]))
.
flatten
()
shape
=
(
cols
,
ilist
.
shape
[
0
]))
.
flatten
()
...
...
theano/sparse/opt.py
浏览文件 @
2b7ee2ec
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
scipy
import
scipy
import
theano
import
theano
...
@@ -879,7 +879,7 @@ local_usmm = gof.opt.PatternSub(
...
@@ -879,7 +879,7 @@ local_usmm = gof.opt.PatternSub(
(
theano
.
tensor
.
sub
,
'z'
,
(
theano
.
tensor
.
sub
,
'z'
,
(
theano
.
tensor
.
mul
,
(
theano
.
tensor
.
mul
,
{
'pattern'
:
'alpha'
,
{
'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
)},
theano
.
config
.
blas
.
ldflags
)},
(
sparse
.
_dot
,
'x'
,
'y'
))),
(
sparse
.
_dot
,
'x'
,
'y'
))),
(
usmm
,
(
theano
.
tensor
.
neg
,
'alpha'
),
'x'
,
'y'
,
'z'
))
(
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}.
...
@@ -8,7 +8,7 @@ U{http://www-users.cs.umn.edu/~saad/software/SPARSKIT/paper.ps}.
"""
"""
# COPIED FROM hpu/icml09/sp.py
# COPIED FROM hpu/icml09/sp.py
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
scipy
import
scipy
from
scipy
import
sparse
as
scipy_sparse
from
scipy
import
sparse
as
scipy_sparse
from
six.moves
import
xrange
from
six.moves
import
xrange
...
@@ -81,18 +81,17 @@ class ConvolutionIndices(Op):
...
@@ -81,18 +81,17 @@ class ConvolutionIndices(Op):
raise
Exception
(
"ws is obsolete and it must be always True"
)
raise
Exception
(
"ws is obsolete and it must be always True"
)
(
dx
,
dy
)
=
strides
(
dx
,
dy
)
=
strides
N
=
numpy
# inshp contains either 2 entries (height,width) or 3 (nfeatures,h,w)
# inshp contains either 2 entries (height,width) or 3 (nfeatures,h,w)
# in the first case, default nfeatures to 1
# in the first case, default nfeatures to 1
if
N
.
size
(
inshp
)
==
2
:
if
np
.
size
(
inshp
)
==
2
:
inshp
=
(
1
,)
+
inshp
inshp
=
(
1
,)
+
inshp
inshp
=
N
.
array
(
inshp
)
inshp
=
np
.
array
(
inshp
)
kshp
=
N
.
array
(
kshp
)
kshp
=
np
.
array
(
kshp
)
ksize
=
N
.
prod
(
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
# size of output image if doing proper convolution
# (mode='full',dx=dy=0) outshp is the actual output shape
# (mode='full',dx=dy=0) outshp is the actual output shape
...
@@ -102,32 +101,32 @@ class ConvolutionIndices(Op):
...
@@ -102,32 +101,32 @@ class ConvolutionIndices(Op):
s
=
-
1
s
=
-
1
else
:
else
:
s
=
1
s
=
1
outshp
=
N
.
int64
(
N
.
ceil
((
inshp
[
1
:]
+
s
*
kshp
-
s
*
1
)
\
outshp
=
np
.
int64
(
np
.
ceil
((
inshp
[
1
:]
+
s
*
kshp
-
s
*
1
)
\
/
N
.
array
([
dy
,
dx
],
dtype
=
'float'
)))
/
np
.
array
([
dy
,
dx
],
dtype
=
'float'
)))
if
any
(
outshp
<=
0
):
if
any
(
outshp
<=
0
):
err
=
'Invalid kernel'
,
kshp
,
'and/or step size'
,
(
dx
,
dy
),
\
err
=
'Invalid kernel'
,
kshp
,
'and/or step size'
,
(
dx
,
dy
),
\
'for given input shape'
,
inshp
'for given input shape'
,
inshp
raise
ValueError
(
err
)
raise
ValueError
(
err
)
outsize
=
N
.
prod
(
outshp
)
outsize
=
np
.
prod
(
outshp
)
insize
=
N
.
prod
(
inshp
)
insize
=
np
.
prod
(
inshp
)
# range of output units over which to iterate
# range of output units over which to iterate
if
mode
==
'valid'
:
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
)
ubound
=
lbound
+
(
inshp
[
1
:]
-
kshp
+
1
)
else
:
else
:
lbound
=
N
.
zeros
(
2
)
lbound
=
np
.
zeros
(
2
)
ubound
=
fulloutshp
ubound
=
fulloutshp
# coordinates of image in "fulloutshp" coordinates
# 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
# bound when counting the receptive field
botright
=
topleft
+
inshp
[
1
:]
botright
=
topleft
+
inshp
[
1
:]
# sparse matrix specifics...
# sparse matrix specifics...
if
ws
:
if
ws
:
spmatshp
=
(
outsize
*
N
.
prod
(
kshp
)
*
inshp
[
0
],
insize
)
spmatshp
=
(
outsize
*
np
.
prod
(
kshp
)
*
inshp
[
0
],
insize
)
else
:
else
:
spmatshp
=
(
nkern
*
outsize
,
insize
)
spmatshp
=
(
nkern
*
outsize
,
insize
)
spmat
=
scipy_sparse
.
lil_matrix
(
spmatshp
)
spmat
=
scipy_sparse
.
lil_matrix
(
spmatshp
)
...
@@ -152,17 +151,17 @@ class ConvolutionIndices(Op):
...
@@ -152,17 +151,17 @@ class ConvolutionIndices(Op):
# FOR EACH OUTPUT PIXEL...
# FOR EACH OUTPUT PIXEL...
# loop over output image height
# 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
# 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)
# kern[l] is filter value to apply at (oj,oi)
# for (iy,ix)
# for (iy,ix)
l
=
0
l
=
0
# ... ITERATE OVER INPUT UNITS IN RECEPTIVE FIELD
# ... ITERATE OVER INPUT UNITS IN RECEPTIVE FIELD
for
ky
in
oy
+
N
.
arange
(
kshp
[
0
]):
for
ky
in
oy
+
np
.
arange
(
kshp
[
0
]):
for
kx
in
ox
+
N
.
arange
(
kshp
[
1
]):
for
kx
in
ox
+
np
.
arange
(
kshp
[
1
]):
# verify if we are still within image
# verify if we are still within image
# boundaries. Equivalent to
# boundaries. Equivalent to
...
@@ -173,13 +172,13 @@ class ConvolutionIndices(Op):
...
@@ -173,13 +172,13 @@ class ConvolutionIndices(Op):
# convert to "valid" input space
# convert to "valid" input space
# coords used to determine column
# coords used to determine column
# index to write to in sparse mat
# 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...
# determine raster-index of input pixel...
# taking into account multiple
# taking into account multiple
# input features
# input features
col
=
iy
*
inshp
[
2
]
+
ix
+
\
col
=
iy
*
inshp
[
2
]
+
ix
+
\
fmapi
*
N
.
prod
(
inshp
[
1
:])
fmapi
*
np
.
prod
(
inshp
[
1
:])
# convert oy,ox values to output
# convert oy,ox values to output
# space coordinates
# space coordinates
...
@@ -188,7 +187,7 @@ class ConvolutionIndices(Op):
...
@@ -188,7 +187,7 @@ class ConvolutionIndices(Op):
else
:
else
:
(
y
,
x
)
=
(
oy
,
ox
)
-
topleft
(
y
,
x
)
=
(
oy
,
ox
)
-
topleft
# taking into account step size
# 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
# convert to row index of sparse matrix
if
ws
:
if
ws
:
...
@@ -228,7 +227,7 @@ class ConvolutionIndices(Op):
...
@@ -228,7 +227,7 @@ class ConvolutionIndices(Op):
if
ws
:
if
ws
:
kmap
=
None
kmap
=
None
else
:
else
:
kmap
=
N
.
zeros
(
ntaps
,
dtype
=
'int'
)
kmap
=
np
.
zeros
(
ntaps
,
dtype
=
'int'
)
k
=
0
k
=
0
# print 'TEMPORARY BUGFIX: REMOVE !!!'
# print 'TEMPORARY BUGFIX: REMOVE !!!'
for
j
in
xrange
(
spmat
.
shape
[
1
]):
for
j
in
xrange
(
spmat
.
shape
[
1
]):
...
@@ -259,7 +258,7 @@ class ConvolutionIndices(Op):
...
@@ -259,7 +258,7 @@ class ConvolutionIndices(Op):
indices
,
indptr
,
spmatshp
,
outshp
=
self
.
evaluate
(
inshp
,
kshp
)
indices
,
indptr
,
spmatshp
,
outshp
=
self
.
evaluate
(
inshp
,
kshp
)
out_indices
[
0
]
=
indices
out_indices
[
0
]
=
indices
out_indptr
[
0
]
=
indptr
out_indptr
[
0
]
=
indptr
spmat_shape
[
0
]
=
n
umpy
.
asarray
(
spmatshp
)
spmat_shape
[
0
]
=
n
p
.
asarray
(
spmatshp
)
convolution_indices
=
ConvolutionIndices
()
convolution_indices
=
ConvolutionIndices
()
...
@@ -318,13 +317,12 @@ def convolve(kerns, kshp, nkern, images, imgshp, step=(1, 1), bias=None,
...
@@ -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
:TODO: test for 1D and think of how to do n-d convolutions
"""
"""
N
=
numpy
# start by computing output dimensions, size, etc
# 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)
# inshp contains either 2 entries (height,width) or 3 (nfeatures,h,w)
# in the first case, default nfeatures to 1
# in the first case, default nfeatures to 1
if
N
.
size
(
imgshp
)
==
2
:
if
np
.
size
(
imgshp
)
==
2
:
imgshp
=
(
1
,)
+
imgshp
imgshp
=
(
1
,)
+
imgshp
# construct indices and index pointers for sparse matrix, which,
# 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,
...
@@ -334,12 +332,12 @@ def convolve(kerns, kshp, nkern, images, imgshp, step=(1, 1), bias=None,
convolution_indices
.
conv_eval
(
imgshp
,
kshp
,
step
,
mode
)
convolution_indices
.
conv_eval
(
imgshp
,
kshp
,
step
,
mode
)
# build sparse matrix, then generate stack of image patches
# 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
)
indptr
,
spmat_shape
)
patches
=
(
sparse
.
structured_dot
(
csc
,
images
.
T
))
.
T
patches
=
(
sparse
.
structured_dot
(
csc
,
images
.
T
))
.
T
# compute output of linear classifier
# 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
)])
tensor
.
as_tensor
(
imgshp
[
0
]
*
kern_size
)])
patch_stack
=
tensor
.
reshape
(
patches
,
pshape
,
ndim
=
2
)
patch_stack
=
tensor
.
reshape
(
patches
,
pshape
,
ndim
=
2
)
...
@@ -354,14 +352,14 @@ def convolve(kerns, kshp, nkern, images, imgshp, step=(1, 1), bias=None,
...
@@ -354,14 +352,14 @@ def convolve(kerns, kshp, nkern, images, imgshp, step=(1, 1), bias=None,
# now to have feature maps in raster order ...
# now to have feature maps in raster order ...
# go from bsize*outshp x nkern to bsize x nkern*outshp
# go from bsize*outshp x nkern to bsize x nkern*outshp
newshp
=
tensor
.
stack
([
images
.
shape
[
0
],
\
newshp
=
tensor
.
stack
([
images
.
shape
[
0
],
\
tensor
.
as_tensor
(
N
.
prod
(
outshp
)),
\
tensor
.
as_tensor
(
np
.
prod
(
outshp
)),
\
tensor
.
as_tensor
(
nkern
)])
tensor
.
as_tensor
(
nkern
)])
tensout
=
tensor
.
reshape
(
output
,
newshp
,
ndim
=
3
)
tensout
=
tensor
.
reshape
(
output
,
newshp
,
ndim
=
3
)
output
=
tensor
.
DimShuffle
((
False
,)
*
tensout
.
ndim
,
(
0
,
2
,
1
))(
tensout
)
output
=
tensor
.
DimShuffle
((
False
,)
*
tensout
.
ndim
,
(
0
,
2
,
1
))(
tensout
)
if
flatten
:
if
flatten
:
output
=
tensor
.
flatten
(
output
,
2
)
output
=
tensor
.
flatten
(
output
,
2
)
return
output
,
N
.
hstack
((
nkern
,
outshp
))
return
output
,
np
.
hstack
((
nkern
,
outshp
))
def
max_pool
(
images
,
imgshp
,
maxpoolshp
):
def
max_pool
(
images
,
imgshp
,
maxpoolshp
):
...
@@ -380,12 +378,11 @@ def max_pool(images, imgshp, maxpoolshp):
...
@@ -380,12 +378,11 @@ def max_pool(images, imgshp, maxpoolshp):
:return: out1, symbolic result (2D tensor)
:return: out1, symbolic result (2D tensor)
:return: out2, logical shape of the output
:return: out2, logical shape of the output
"""
"""
N
=
numpy
poolsize
=
np
.
int64
(
np
.
prod
(
maxpoolshp
))
poolsize
=
N
.
int64
(
N
.
prod
(
maxpoolshp
))
# imgshp contains either 2 entries (height,width) or 3 (nfeatures,h,w)
# imgshp contains either 2 entries (height,width) or 3 (nfeatures,h,w)
# in the first case, default nfeatures to 1
# in the first case, default nfeatures to 1
if
N
.
size
(
imgshp
)
==
2
:
if
np
.
size
(
imgshp
)
==
2
:
imgshp
=
(
1
,)
+
imgshp
imgshp
=
(
1
,)
+
imgshp
# construct indices and index pointers for sparse matrix, which,
# construct indices and index pointers for sparse matrix, which,
...
@@ -401,12 +398,12 @@ def max_pool(images, imgshp, maxpoolshp):
...
@@ -401,12 +398,12 @@ def max_pool(images, imgshp, maxpoolshp):
# print 'outshp = ', outshp
# print 'outshp = ', outshp
# build sparse matrix, then generate stack of image patches
# 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
)
indptr
,
spmat_shape
)
patches
=
sparse
.
structured_dot
(
csc
,
images
.
T
)
.
T
patches
=
sparse
.
structured_dot
(
csc
,
images
.
T
)
.
T
pshape
=
tensor
.
stack
([
images
.
shape
[
0
]
*
\
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
(
imgshp
[
0
]),
tensor
.
as_tensor
(
poolsize
)])
tensor
.
as_tensor
(
poolsize
)])
patch_stack
=
tensor
.
reshape
(
patches
,
pshape
,
ndim
=
3
)
patch_stack
=
tensor
.
reshape
(
patches
,
pshape
,
ndim
=
3
)
...
@@ -414,7 +411,7 @@ def max_pool(images, imgshp, maxpoolshp):
...
@@ -414,7 +411,7 @@ def max_pool(images, imgshp, maxpoolshp):
out1
=
tensor
.
max
(
patch_stack
,
axis
=
2
)
out1
=
tensor
.
max
(
patch_stack
,
axis
=
2
)
pshape
=
tensor
.
stack
([
images
.
shape
[
0
],
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
(
imgshp
[
0
])])
out2
=
tensor
.
reshape
(
out1
,
pshape
,
ndim
=
3
)
out2
=
tensor
.
reshape
(
out1
,
pshape
,
ndim
=
3
)
...
...
theano/sparse/sandbox/sp2.py
浏览文件 @
2b7ee2ec
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
from
six.moves
import
xrange
from
six.moves
import
xrange
import
theano
import
theano
import
scipy.sparse
import
scipy.sparse
...
@@ -74,7 +74,7 @@ class Poisson(gof.op.Op):
...
@@ -74,7 +74,7 @@ class Poisson(gof.op.Op):
assert
_is_sparse
(
x
)
assert
_is_sparse
(
x
)
assert
x
.
format
in
[
"csr"
,
"csc"
]
assert
x
.
format
in
[
"csr"
,
"csc"
]
out
[
0
]
=
x
.
copy
()
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
)
dtype
=
x
.
dtype
)
out
[
0
]
.
eliminate_zeros
()
out
[
0
]
.
eliminate_zeros
()
...
@@ -123,7 +123,7 @@ class Binomial(gof.op.Op):
...
@@ -123,7 +123,7 @@ class Binomial(gof.op.Op):
def
perform
(
self
,
node
,
inputs
,
outputs
):
def
perform
(
self
,
node
,
inputs
,
outputs
):
(
n
,
p
,
shape
)
=
inputs
(
n
,
p
,
shape
)
=
inputs
(
out
,)
=
outputs
(
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'
)
csx_matrix
=
getattr
(
scipy
.
sparse
,
self
.
format
+
'_matrix'
)
out
[
0
]
=
csx_matrix
(
binomial
,
dtype
=
self
.
dtype
)
out
[
0
]
=
csx_matrix
(
binomial
,
dtype
=
self
.
dtype
)
...
@@ -195,14 +195,14 @@ class Multinomial(gof.op.Op):
...
@@ -195,14 +195,14 @@ class Multinomial(gof.op.Op):
if
n
.
ndim
==
0
:
if
n
.
ndim
==
0
:
for
i
in
xrange
(
p
.
shape
[
0
]):
for
i
in
xrange
(
p
.
shape
[
0
]):
k
,
l
=
p
.
indptr
[
i
],
p
.
indptr
[
i
+
1
]
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
:
elif
n
.
ndim
==
1
:
if
n
.
shape
[
0
]
!=
p
.
shape
[
0
]:
if
n
.
shape
[
0
]
!=
p
.
shape
[
0
]:
raise
ValueError
(
'The number of element of n must be '
raise
ValueError
(
'The number of element of n must be '
'the same as the number of row of p.'
)
'the same as the number of row of p.'
)
for
i
in
xrange
(
p
.
shape
[
0
]):
for
i
in
xrange
(
p
.
shape
[
0
]):
k
,
l
=
p
.
indptr
[
i
],
p
.
indptr
[
i
+
1
]
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
):
def
grad
(
self
,
inputs
,
outputs_gradients
):
comment_n
=
"No gradient exists for the number of samples in class
\
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:
...
@@ -11,7 +11,7 @@ if not theano.sparse.enable_sparse:
import
scipy.sparse
import
scipy.sparse
from
scipy.signal
import
convolve2d
from
scipy.signal
import
convolve2d
import
scipy.sparse
as
sparse
import
scipy.sparse
as
sparse
import
numpy
import
numpy
as
np
from
six.moves
import
xrange
from
six.moves
import
xrange
from
theano
import
function
,
tensor
from
theano
import
function
,
tensor
...
@@ -43,8 +43,8 @@ class TestSP(unittest.TestCase):
...
@@ -43,8 +43,8 @@ class TestSP(unittest.TestCase):
bias
=
tensor
.
dvector
()
bias
=
tensor
.
dvector
()
kerns
=
tensor
.
dmatrix
()
kerns
=
tensor
.
dmatrix
()
input
=
tensor
.
dmatrix
()
input
=
tensor
.
dmatrix
()
rng
=
n
umpy
.
random
.
RandomState
(
3423489
)
rng
=
n
p
.
random
.
RandomState
(
3423489
)
filters
=
rng
.
randn
(
nkern
,
n
umpy
.
prod
(
kshp
))
filters
=
rng
.
randn
(
nkern
,
n
p
.
prod
(
kshp
))
biasvals
=
rng
.
randn
(
nkern
)
biasvals
=
rng
.
randn
(
nkern
)
for
mode
in
(
'FAST_COMPILE'
,
'FAST_RUN'
):
for
mode
in
(
'FAST_COMPILE'
,
'FAST_RUN'
):
...
@@ -57,12 +57,12 @@ class TestSP(unittest.TestCase):
...
@@ -57,12 +57,12 @@ class TestSP(unittest.TestCase):
f
=
function
([
kerns
,
bias
,
input
],
output
,
mode
=
mode
)
f
=
function
([
kerns
,
bias
,
input
],
output
,
mode
=
mode
)
# now test with real values
# 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
)
bsize
,)
+
imshp
)
img1d
=
img2d
.
reshape
(
bsize
,
-
1
)
img1d
=
img2d
.
reshape
(
bsize
,
-
1
)
# create filters (need to be flipped to use convolve2d)
# 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
):
for
k
in
range
(
nkern
):
it
=
reversed
(
filters
[
k
,
:])
it
=
reversed
(
filters
[
k
,
:])
for
i
in
range
(
kshp
[
0
]):
for
i
in
range
(
kshp
[
0
]):
...
@@ -71,11 +71,11 @@ class TestSP(unittest.TestCase):
...
@@ -71,11 +71,11 @@ class TestSP(unittest.TestCase):
# compute output with convolve2d
# compute output with convolve2d
if
conv_mode
==
'valid'
:
if
conv_mode
==
'valid'
:
fulloutshp
=
n
umpy
.
array
(
imshp
)
-
numpy
.
array
(
kshp
)
+
1
fulloutshp
=
n
p
.
array
(
imshp
)
-
np
.
array
(
kshp
)
+
1
else
:
else
:
fulloutshp
=
n
umpy
.
array
(
imshp
)
+
numpy
.
array
(
kshp
)
-
1
fulloutshp
=
n
p
.
array
(
imshp
)
+
np
.
array
(
kshp
)
-
1
ntime1
=
time
.
time
()
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
b
in
range
(
bsize
):
for
n
in
range
(
nkern
):
for
n
in
range
(
nkern
):
refout
[
b
,
...
,
n
]
=
convolve2d
(
img2d
[
b
,
:,
:],
refout
[
b
,
...
,
n
]
=
convolve2d
(
img2d
[
b
,
:,
:],
...
@@ -88,7 +88,7 @@ class TestSP(unittest.TestCase):
...
@@ -88,7 +88,7 @@ class TestSP(unittest.TestCase):
bench1
+=
biasvals
.
reshape
(
1
,
1
,
nkern
)
bench1
+=
biasvals
.
reshape
(
1
,
1
,
nkern
)
# swap the last two dimensions (output needs to be nkern x outshp)
# 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
()
ttime1
=
time
.
time
()
out1
=
f
(
filters
,
biasvals
,
img1d
)
out1
=
f
(
filters
,
biasvals
,
img1d
)
ttot
+=
time
.
time
()
-
ttime1
ttot
+=
time
.
time
()
-
ttime1
...
@@ -101,13 +101,13 @@ class TestSP(unittest.TestCase):
...
@@ -101,13 +101,13 @@ class TestSP(unittest.TestCase):
#downprop = function([kerns,input], vis, mode=mode)
#downprop = function([kerns,input], vis, mode=mode)
#visval = downprop(filters,img1d)
#visval = downprop(filters,img1d)
# test downward propagation -- reference implementation
# test downward propagation -- reference implementation
#pshape = (img1d.shape[0],n
umpy.prod(outshp[1:]),numpy
.prod(kshp))
#pshape = (img1d.shape[0],n
p.prod(outshp[1:]),np
.prod(kshp))
#patchstack = n
umpy
.zeros(pshape)
#patchstack = n
p
.zeros(pshape)
# for bi in n
umpy
.arange(pshape[0]): # batch index
# for bi in n
p
.arange(pshape[0]): # batch index
#abspos = 0
#abspos = 0
# for outy in n
umpy
.arange(outshp[1]):
# for outy in n
p
.arange(outshp[1]):
# for outx in n
umpy
.arange(outshp[2]):
# for outx in n
p
.arange(outshp[2]):
# for ni in n
umpy
.arange(nkern):
# for ni in n
p
.arange(nkern):
# print 'filters[n,:].shape = ', filters[n,:].shape
# print 'filters[n,:].shape = ', filters[n,:].shape
# print 'out1[bi,abspos].shape =',out1[bi,abspos].shape
# print 'out1[bi,abspos].shape =',out1[bi,abspos].shape
#patchstack[bi,abspos,:] = filters[n,:]*out1[bi,abspos]
#patchstack[bi,abspos,:] = filters[n,:]*out1[bi,abspos]
...
@@ -115,13 +115,13 @@ class TestSP(unittest.TestCase):
...
@@ -115,13 +115,13 @@ class TestSP(unittest.TestCase):
#patchstack = patchstack.reshape(1,-1)
#patchstack = patchstack.reshape(1,-1)
# indices, indptr, spmat_shape, sptype, outshp = \
# indices, indptr, spmat_shape, sptype, outshp = \
# sp.convolution_indices.conv_eval(imshp,kshp,ss,conv_mode)
# sp.convolution_indices.conv_eval(imshp,kshp,ss,conv_mode)
#spmat = sparse.csc_matrix((n
umpy
.ones_like(indices),indices,indptr),spmat_shape)
#spmat = sparse.csc_matrix((n
p
.ones_like(indices),indices,indptr),spmat_shape)
#visref = n
umpy
.dot(patchstack, spmat.todense())
#visref = n
p
.dot(patchstack, spmat.todense())
# print 'visval = ', visval
# print 'visval = ', visval
# print 'visref = ', visref
# print 'visref = ', visref
#assert n
umpy
.all(visref==visval)
#assert n
p
.all(visref==visval)
# print '**** Convolution Profiling Results (',mode,') ****'
# print '**** Convolution Profiling Results (',mode,') ****'
...
@@ -143,10 +143,10 @@ class TestSP(unittest.TestCase):
...
@@ -143,10 +143,10 @@ class TestSP(unittest.TestCase):
# symbolic stuff
# symbolic stuff
kerns
=
[
tensor
.
dmatrix
(),
tensor
.
dmatrix
()]
kerns
=
[
tensor
.
dmatrix
(),
tensor
.
dmatrix
()]
input
=
tensor
.
dmatrix
()
input
=
tensor
.
dmatrix
()
rng
=
n
umpy
.
random
.
RandomState
(
3423489
)
rng
=
n
p
.
random
.
RandomState
(
3423489
)
# build actual input images
# 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
)
img1d
=
img2d
.
reshape
(
bsize
,
-
1
)
for
mode
in
(
'FAST_COMPILE'
,
'FAST_RUN'
):
for
mode
in
(
'FAST_COMPILE'
,
'FAST_RUN'
):
...
@@ -157,8 +157,8 @@ class TestSP(unittest.TestCase):
...
@@ -157,8 +157,8 @@ class TestSP(unittest.TestCase):
nkerns
[
0
],
input
,
imshp
,
ss
[
0
],
mode
=
conv_mode
)
nkerns
[
0
],
input
,
imshp
,
ss
[
0
],
mode
=
conv_mode
)
l1propup
=
function
([
kerns
[
0
],
input
],
l1hid
,
mode
=
mode
)
l1propup
=
function
([
kerns
[
0
],
input
],
l1hid
,
mode
=
mode
)
#l1kernvals = n
umpy.random.rand(nkerns[0],numpy
.prod(kshp[0]))
#l1kernvals = n
p.random.rand(nkerns[0],np
.prod(kshp[0]))
l1kernvals
=
n
umpy
.
arange
(
nkerns
[
0
]
*
numpy
.
prod
(
kshp
[
0
]))
.
reshape
(
nkerns
[
0
],
numpy
.
prod
(
kshp
[
0
]))
l1kernvals
=
n
p
.
arange
(
nkerns
[
0
]
*
np
.
prod
(
kshp
[
0
]))
.
reshape
(
nkerns
[
0
],
np
.
prod
(
kshp
[
0
]))
l1hidval
=
l1propup
(
l1kernvals
,
img1d
)
l1hidval
=
l1propup
(
l1kernvals
,
img1d
)
# actual values
# actual values
...
@@ -166,17 +166,17 @@ class TestSP(unittest.TestCase):
...
@@ -166,17 +166,17 @@ class TestSP(unittest.TestCase):
nkerns
[
1
],
l1hid
,
l1shp
,
ss
[
1
],
mode
=
conv_mode
)
nkerns
[
1
],
l1hid
,
l1shp
,
ss
[
1
],
mode
=
conv_mode
)
l2propup
=
function
([
kerns
[
1
],
l1hid
],
l2hid
,
mode
=
mode
)
l2propup
=
function
([
kerns
[
1
],
l1hid
],
l2hid
,
mode
=
mode
)
#l2kernvals = n
umpy.random.rand(nkerns[1],numpy
.prod(kshp[1])*nkerns[0])
#l2kernvals = n
p.random.rand(nkerns[1],np
.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
.
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
# 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
)
l2hidval
=
l2propup
(
l2kernvals
,
l1hidval
)
def
test_maxpool
(
self
):
def
test_maxpool
(
self
):
# generate flatted images
# generate flatted images
maxpoolshps
=
((
2
,
2
),
(
3
,
3
),
(
4
,
4
),
(
5
,
5
),
(
6
,
6
))
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
()
images
=
tensor
.
dmatrix
()
for
maxpoolshp
in
maxpoolshps
:
for
maxpoolshp
in
maxpoolshps
:
...
@@ -187,10 +187,10 @@ class TestSP(unittest.TestCase):
...
@@ -187,10 +187,10 @@ class TestSP(unittest.TestCase):
output_val
=
f
(
imval
.
reshape
(
imval
.
shape
[
0
],
-
1
))
output_val
=
f
(
imval
.
reshape
(
imval
.
shape
[
0
],
-
1
))
# numeric verification
# 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
[
2
]
//
maxpoolshp
[
0
],
imval
.
shape
[
3
]
//
maxpoolshp
[
1
]))
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
n
in
range
(
imval
.
shape
[
0
]):
for
k
in
range
(
imval
.
shape
[
1
]):
for
k
in
range
(
imval
.
shape
[
1
]):
...
@@ -198,9 +198,9 @@ class TestSP(unittest.TestCase):
...
@@ -198,9 +198,9 @@ class TestSP(unittest.TestCase):
for
j
in
range
(
imval
.
shape
[
3
]
//
maxpoolshp
[
1
]):
for
j
in
range
(
imval
.
shape
[
3
]
//
maxpoolshp
[
1
]):
ii
,
jj
=
i
*
maxpoolshp
[
0
],
j
*
maxpoolshp
[
1
]
ii
,
jj
=
i
*
maxpoolshp
[
0
],
j
*
maxpoolshp
[
1
]
patch
=
imval
[
n
,
k
,
ii
:
ii
+
maxpoolshp
[
0
],
jj
:
jj
+
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
)
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
):
def
mp
(
input
):
output
,
outshp
=
sp
.
max_pool
(
input
,
imval
.
shape
[
1
:],
maxpoolshp
)
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
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
import
unittest
import
unittest
import
theano
import
theano
import
numpy
import
numpy
as
np
import
scipy.sparse
as
sp
import
scipy.sparse
as
sp
from
theano
import
sparse
from
theano
import
sparse
...
...
theano/sparse/tests/test_basic.py
浏览文件 @
2b7ee2ec
...
@@ -4,7 +4,7 @@ import time
...
@@ -4,7 +4,7 @@ import time
import
unittest
import
unittest
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
import
numpy
import
numpy
as
np
from
six.moves
import
xrange
from
six.moves
import
xrange
try
:
try
:
import
scipy.sparse
as
sp
import
scipy.sparse
as
sp
...
@@ -83,8 +83,8 @@ def random_lil(shape, dtype, nnz):
...
@@ -83,8 +83,8 @@ def random_lil(shape, dtype, nnz):
huge
=
2
**
30
huge
=
2
**
30
for
k
in
range
(
nnz
):
for
k
in
range
(
nnz
):
# set non-zeros in random locations (row x, col y)
# set non-zeros in random locations (row x, col y)
idx
=
n
umpy
.
random
.
randint
(
1
,
huge
+
1
,
size
=
2
)
%
shape
idx
=
n
p
.
random
.
randint
(
1
,
huge
+
1
,
size
=
2
)
%
shape
value
=
n
umpy
.
random
.
rand
()
value
=
n
p
.
random
.
rand
()
# if dtype *int*, value will always be zeros!
# if dtype *int*, value will always be zeros!
if
"int"
in
dtype
:
if
"int"
in
dtype
:
value
=
int
(
value
*
100
)
value
=
int
(
value
*
100
)
...
@@ -136,23 +136,23 @@ def sparse_random_inputs(format, shape, n=1, out_dtype=None, p=0.5, gap=None,
...
@@ -136,23 +136,23 @@ def sparse_random_inputs(format, shape, n=1, out_dtype=None, p=0.5, gap=None,
assert
gap
[
0
]
>=
0
assert
gap
[
0
]
>=
0
def
_rand
():
def
_rand
():
where
=
n
umpy
.
random
.
binomial
(
1
,
p
,
size
=
shape
)
.
astype
(
'int8'
)
where
=
n
p
.
random
.
binomial
(
1
,
p
,
size
=
shape
)
.
astype
(
'int8'
)
if
out_dtype
in
sparse
.
discrete_dtypes
:
if
out_dtype
in
sparse
.
discrete_dtypes
:
if
not
gap
:
if
not
gap
:
value
=
n
umpy
.
random
.
randint
(
50
,
size
=
shape
)
value
=
n
p
.
random
.
randint
(
50
,
size
=
shape
)
elif
len
(
gap
)
==
2
:
elif
len
(
gap
)
==
2
:
value
=
n
umpy
.
random
.
randint
(
gap
[
0
],
gap
[
1
],
size
=
shape
)
value
=
n
p
.
random
.
randint
(
gap
[
0
],
gap
[
1
],
size
=
shape
)
else
:
else
:
value
=
n
umpy
.
random
.
randint
(
gap
[
0
],
size
=
shape
)
value
=
n
p
.
random
.
randint
(
gap
[
0
],
size
=
shape
)
else
:
else
:
if
not
gap
:
if
not
gap
:
value
=
n
umpy
.
random
.
random
(
shape
)
value
=
n
p
.
random
.
random
(
shape
)
elif
len
(
gap
)
==
2
:
elif
len
(
gap
)
==
2
:
a
,
b
=
gap
a
,
b
=
gap
value
=
a
+
n
umpy
.
random
.
random
(
shape
)
*
(
b
-
a
)
value
=
a
+
n
p
.
random
.
random
(
shape
)
*
(
b
-
a
)
else
:
else
:
value
=
n
umpy
.
random
.
random
(
shape
)
*
gap
[
0
]
value
=
n
p
.
random
.
random
(
shape
)
*
gap
[
0
]
return
(
where
*
value
)
.
astype
(
out_dtype
)
return
(
where
*
value
)
.
astype
(
out_dtype
)
variable
=
[
getattr
(
theano
.
sparse
,
format
+
'_matrix'
)(
dtype
=
out_dtype
)
variable
=
[
getattr
(
theano
.
sparse
,
format
+
'_matrix'
)(
dtype
=
out_dtype
)
...
@@ -169,13 +169,13 @@ def sparse_random_inputs(format, shape, n=1, out_dtype=None, p=0.5, gap=None,
...
@@ -169,13 +169,13 @@ def sparse_random_inputs(format, shape, n=1, out_dtype=None, p=0.5, gap=None,
for
idx
in
range
(
n
):
for
idx
in
range
(
n
):
assert
data
[
idx
]
.
nnz
>
1
,
(
assert
data
[
idx
]
.
nnz
>
1
,
(
"can't make a sparse matrix with explicit 0"
)
"can't make a sparse matrix with explicit 0"
)
d_idx
=
n
umpy
.
random
.
randint
(
data
[
idx
]
.
nnz
)
d_idx
=
n
p
.
random
.
randint
(
data
[
idx
]
.
nnz
)
data
[
idx
]
.
data
[
d_idx
]
=
0
data
[
idx
]
.
data
[
d_idx
]
=
0
# numpy 1.5.0 with scipy 0.9.0 have scipy.sparse.XXX_matrix return
# numpy 1.5.0 with scipy 0.9.0 have scipy.sparse.XXX_matrix return
# typenum 10(ulonglong) instead of 8(uint64) event if they are the same!
# typenum 10(ulonglong) instead of 8(uint64) event if they are the same!
# Theano don't like ulonglong type_num
# Theano don't like ulonglong type_num
dtype
=
n
umpy
.
dtype
(
out_dtype
)
# Convert into dtype object.
dtype
=
n
p
.
dtype
(
out_dtype
)
# Convert into dtype object.
if
data
[
0
]
.
dtype
.
num
!=
dtype
.
num
and
dtype
.
str
==
data
[
0
]
.
dtype
.
str
:
if
data
[
0
]
.
dtype
.
num
!=
dtype
.
num
and
dtype
.
str
==
data
[
0
]
.
dtype
.
str
:
data
[
0
]
.
data
=
theano
.
_asarray
(
data
[
0
]
.
data
,
out_dtype
)
data
[
0
]
.
data
=
theano
.
_asarray
(
data
[
0
]
.
data
,
out_dtype
)
assert
data
[
0
]
.
dtype
.
num
==
dtype
.
num
assert
data
[
0
]
.
dtype
.
num
==
dtype
.
num
...
@@ -423,7 +423,7 @@ class SparseInferShapeTester(utt.InferShapeTester):
...
@@ -423,7 +423,7 @@ class SparseInferShapeTester(utt.InferShapeTester):
[
x
+
y
],
[
x
+
y
],
[
sp
.
csr_matrix
(
random_lil
((
10
,
40
),
[
sp
.
csr_matrix
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
)),
config
.
floatX
,
3
)),
n
umpy
.
random
.
randn
(
10
,
40
)
.
astype
(
config
.
floatX
)],
n
p
.
random
.
randn
(
10
,
40
)
.
astype
(
config
.
floatX
)],
(
AddSD
,
sparse
.
opt
.
AddSD_ccode
))
(
AddSD
,
sparse
.
opt
.
AddSD_ccode
))
def
test_mul_ss
(
self
):
def
test_mul_ss
(
self
):
...
@@ -444,7 +444,7 @@ class SparseInferShapeTester(utt.InferShapeTester):
...
@@ -444,7 +444,7 @@ class SparseInferShapeTester(utt.InferShapeTester):
[
x
*
y
],
[
x
*
y
],
[
sp
.
csr_matrix
(
random_lil
((
10
,
40
),
[
sp
.
csr_matrix
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
)),
config
.
floatX
,
3
)),
n
umpy
.
random
.
randn
(
10
,
40
)
.
astype
(
config
.
floatX
)],
n
p
.
random
.
randn
(
10
,
40
)
.
astype
(
config
.
floatX
)],
MulSD
,
excluding
=
[
"local_mul_s_d"
])
MulSD
,
excluding
=
[
"local_mul_s_d"
])
def
test_remove0
(
self
):
def
test_remove0
(
self
):
...
@@ -518,7 +518,7 @@ class SparseInferShapeTester(utt.InferShapeTester):
...
@@ -518,7 +518,7 @@ class SparseInferShapeTester(utt.InferShapeTester):
x
=
tensor
.
matrix
()
x
=
tensor
.
matrix
()
self
.
_compile_and_check
([
x
],
self
.
_compile_and_check
([
x
],
[
csc_from_dense
(
x
)],
[
csc_from_dense
(
x
)],
[
n
umpy
.
random
.
randn
(
10
,
40
)
.
astype
(
[
n
p
.
random
.
randn
(
10
,
40
)
.
astype
(
config
.
floatX
)],
config
.
floatX
)],
csc_from_dense
.
__class__
)
csc_from_dense
.
__class__
)
...
@@ -531,9 +531,9 @@ class SparseInferShapeTester(utt.InferShapeTester):
...
@@ -531,9 +531,9 @@ class SparseInferShapeTester(utt.InferShapeTester):
self
.
_compile_and_check
(
self
.
_compile_and_check
(
[
x
,
vals
,
ilist
],
[
x
,
vals
,
ilist
],
[
out
],
[
out
],
[
n
umpy
.
zeros
((
40
,
10
),
dtype
=
config
.
floatX
),
[
n
p
.
zeros
((
40
,
10
),
dtype
=
config
.
floatX
),
n
umpy
.
random
.
randn
(
12
,
10
)
.
astype
(
config
.
floatX
),
n
p
.
random
.
randn
(
12
,
10
)
.
astype
(
config
.
floatX
),
n
umpy
.
random
.
randint
(
low
=
0
,
high
=
40
,
size
=
(
12
,))],
n
p
.
random
.
randint
(
low
=
0
,
high
=
40
,
size
=
(
12
,))],
ConstructSparseFromList
ConstructSparseFromList
)
)
...
@@ -565,8 +565,8 @@ class TestConstructSparseFromList(unittest.TestCase):
...
@@ -565,8 +565,8 @@ class TestConstructSparseFromList(unittest.TestCase):
assert
isinstance
(
g
.
owner
.
op
,
ConstructSparseFromList
)
assert
isinstance
(
g
.
owner
.
op
,
ConstructSparseFromList
)
# Test the sparse grad
# Test the sparse grad
valm
=
n
umpy
.
random
.
rand
(
5
,
4
)
.
astype
(
config
.
floatX
)
valm
=
n
p
.
random
.
rand
(
5
,
4
)
.
astype
(
config
.
floatX
)
valv
=
n
umpy
.
random
.
randint
(
0
,
5
,
10
)
valv
=
n
p
.
random
.
randint
(
0
,
5
,
10
)
m
=
theano
.
tensor
.
matrix
()
m
=
theano
.
tensor
.
matrix
()
shared_v
=
theano
.
shared
(
valv
)
shared_v
=
theano
.
shared
(
valv
)
...
@@ -603,21 +603,21 @@ class T_AddMul(unittest.TestCase):
...
@@ -603,21 +603,21 @@ class T_AddMul(unittest.TestCase):
def
testMulSS
(
self
):
def
testMulSS
(
self
):
self
.
_testSS
(
mul
,
self
.
_testSS
(
mul
,
n
umpy
.
array
([[
1.
,
0
],
[
3
,
0
],
[
0
,
6
]]),
n
p
.
array
([[
1.
,
0
],
[
3
,
0
],
[
0
,
6
]]),
n
umpy
.
array
([[
1.
,
2
],
[
3
,
0
],
[
0
,
6
]]))
n
p
.
array
([[
1.
,
2
],
[
3
,
0
],
[
0
,
6
]]))
def
testMulSD
(
self
):
def
testMulSD
(
self
):
self
.
_testSD
(
mul
,
self
.
_testSD
(
mul
,
n
umpy
.
array
([[
1.
,
0
],
[
3
,
0
],
[
0
,
6
]]),
n
p
.
array
([[
1.
,
0
],
[
3
,
0
],
[
0
,
6
]]),
n
umpy
.
array
([[
1.
,
2
],
[
3
,
0
],
[
0
,
6
]]))
n
p
.
array
([[
1.
,
2
],
[
3
,
0
],
[
0
,
6
]]))
def
testMulDS
(
self
):
def
testMulDS
(
self
):
self
.
_testDS
(
mul
,
self
.
_testDS
(
mul
,
n
umpy
.
array
([[
1.
,
0
],
[
3
,
0
],
[
0
,
6
]]),
n
p
.
array
([[
1.
,
0
],
[
3
,
0
],
[
0
,
6
]]),
n
umpy
.
array
([[
1.
,
2
],
[
3
,
0
],
[
0
,
6
]]))
n
p
.
array
([[
1.
,
2
],
[
3
,
0
],
[
0
,
6
]]))
def
_testSS
(
self
,
op
,
array1
=
n
umpy
.
array
([[
1.
,
0
],
[
3
,
0
],
[
0
,
6
]]),
def
_testSS
(
self
,
op
,
array1
=
n
p
.
array
([[
1.
,
0
],
[
3
,
0
],
[
0
,
6
]]),
array2
=
n
umpy
.
asarray
([[
0
,
2.
],
[
0
,
4
],
[
5
,
0
]])):
array2
=
n
p
.
asarray
([[
0
,
2.
],
[
0
,
4
],
[
5
,
0
]])):
for
mtype1
,
mtype2
in
product
(
_mtypes
,
_mtypes
):
for
mtype1
,
mtype2
in
product
(
_mtypes
,
_mtypes
):
for
dtype1
,
dtype2
in
[(
'float64'
,
'int8'
),
for
dtype1
,
dtype2
in
[(
'float64'
,
'int8'
),
(
'int8'
,
'float64'
),
(
'int8'
,
'float64'
),
...
@@ -643,19 +643,19 @@ class T_AddMul(unittest.TestCase):
...
@@ -643,19 +643,19 @@ class T_AddMul(unittest.TestCase):
val
=
eval_outputs
([
apb
])
val
=
eval_outputs
([
apb
])
self
.
assertTrue
(
val
.
shape
==
(
3
,
2
))
self
.
assertTrue
(
val
.
shape
==
(
3
,
2
))
if
op
is
add
:
if
op
is
add
:
self
.
assertTrue
(
n
umpy
.
all
(
val
.
todense
()
==
(
array1
+
array2
)))
self
.
assertTrue
(
n
p
.
all
(
val
.
todense
()
==
(
array1
+
array2
)))
if
dtype1
.
startswith
(
'float'
)
and
dtype2
.
startswith
(
'float'
):
if
dtype1
.
startswith
(
'float'
)
and
dtype2
.
startswith
(
'float'
):
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
False
)
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
False
)
elif
op
is
mul
:
elif
op
is
mul
:
self
.
assertTrue
(
n
umpy
.
all
(
val
.
todense
()
self
.
assertTrue
(
n
p
.
all
(
val
.
todense
()
==
(
array1
*
array2
)))
==
(
array1
*
array2
)))
if
dtype1
.
startswith
(
'float'
)
and
dtype2
.
startswith
(
'float'
):
if
dtype1
.
startswith
(
'float'
)
and
dtype2
.
startswith
(
'float'
):
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
False
)
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
False
)
def
_testSD
(
self
,
op
,
array1
=
n
umpy
.
array
([[
1.
,
0
],
[
3
,
0
],
[
0
,
6
]]),
def
_testSD
(
self
,
op
,
array1
=
n
p
.
array
([[
1.
,
0
],
[
3
,
0
],
[
0
,
6
]]),
array2
=
n
umpy
.
asarray
([[
0
,
2.
],
[
0
,
4
],
[
5
,
0
]])):
array2
=
n
p
.
asarray
([[
0
,
2.
],
[
0
,
4
],
[
5
,
0
]])):
for
mtype
in
_mtypes
:
for
mtype
in
_mtypes
:
for
a
in
[
n
umpy
.
array
(
array1
),
tensor
.
as_tensor_variable
(
array1
),
for
a
in
[
n
p
.
array
(
array1
),
tensor
.
as_tensor_variable
(
array1
),
theano
.
shared
(
array1
)]:
theano
.
shared
(
array1
)]:
for
dtype1
,
dtype2
in
[(
'float64'
,
'int8'
),
for
dtype1
,
dtype2
in
[(
'float64'
,
'int8'
),
(
'int8'
,
'float64'
),
(
'int8'
,
'float64'
),
...
@@ -675,9 +675,9 @@ class T_AddMul(unittest.TestCase):
...
@@ -675,9 +675,9 @@ class T_AddMul(unittest.TestCase):
self
.
assertTrue
(
val
.
shape
==
(
3
,
2
))
self
.
assertTrue
(
val
.
shape
==
(
3
,
2
))
if
op
is
add
:
if
op
is
add
:
self
.
assertTrue
(
_is_dense_variable
(
apb
))
self
.
assertTrue
(
_is_dense_variable
(
apb
))
self
.
assertTrue
(
n
umpy
.
all
(
val
==
(
array1
+
b
)))
self
.
assertTrue
(
n
p
.
all
(
val
==
(
array1
+
b
)))
ans
=
n
umpy
.
array
([[
1.
,
2
],
[
3
,
4
],
[
5
,
6
]])
ans
=
n
p
.
array
([[
1.
,
2
],
[
3
,
4
],
[
5
,
6
]])
self
.
assertTrue
(
n
umpy
.
all
(
val
==
ans
))
self
.
assertTrue
(
n
p
.
all
(
val
==
ans
))
if
isinstance
(
a
,
theano
.
Constant
):
if
isinstance
(
a
,
theano
.
Constant
):
a
=
a
.
data
a
=
a
.
data
if
getattr
(
a
,
'owner'
,
None
):
if
getattr
(
a
,
'owner'
,
None
):
...
@@ -686,8 +686,8 @@ class T_AddMul(unittest.TestCase):
...
@@ -686,8 +686,8 @@ class T_AddMul(unittest.TestCase):
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
True
)
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
True
)
elif
op
is
mul
:
elif
op
is
mul
:
self
.
assertTrue
(
_is_sparse_variable
(
apb
))
self
.
assertTrue
(
_is_sparse_variable
(
apb
))
self
.
assertTrue
(
n
umpy
.
all
(
val
.
todense
()
==
(
b
.
multiply
(
array1
))))
self
.
assertTrue
(
n
p
.
all
(
val
.
todense
()
==
(
b
.
multiply
(
array1
))))
self
.
assertTrue
(
n
umpy
.
all
(
val
.
todense
()
==
numpy
.
array
(
self
.
assertTrue
(
n
p
.
all
(
val
.
todense
()
==
np
.
array
(
[[
1
,
0
],
[
9
,
0
],
[
0
,
36
]])))
[[
1
,
0
],
[
9
,
0
],
[
0
,
36
]])))
if
isinstance
(
a
,
theano
.
Constant
):
if
isinstance
(
a
,
theano
.
Constant
):
a
=
a
.
data
a
=
a
.
data
...
@@ -696,10 +696,10 @@ class T_AddMul(unittest.TestCase):
...
@@ -696,10 +696,10 @@ class T_AddMul(unittest.TestCase):
if
dtype1
.
startswith
(
'float'
)
and
dtype2
.
startswith
(
'float'
):
if
dtype1
.
startswith
(
'float'
)
and
dtype2
.
startswith
(
'float'
):
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
False
)
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
False
)
def
_testDS
(
self
,
op
,
array1
=
n
umpy
.
array
([[
1.
,
0
],
[
3
,
0
],
[
0
,
6
]]),
def
_testDS
(
self
,
op
,
array1
=
n
p
.
array
([[
1.
,
0
],
[
3
,
0
],
[
0
,
6
]]),
array2
=
n
umpy
.
asarray
([[
0
,
2.
],
[
0
,
4
],
[
5
,
0
]])):
array2
=
n
p
.
asarray
([[
0
,
2.
],
[
0
,
4
],
[
5
,
0
]])):
for
mtype
in
_mtypes
:
for
mtype
in
_mtypes
:
for
b
in
[
n
umpy
.
asarray
(
array2
),
for
b
in
[
n
p
.
asarray
(
array2
),
tensor
.
as_tensor_variable
(
array2
),
tensor
.
as_tensor_variable
(
array2
),
theano
.
shared
(
array2
)]:
theano
.
shared
(
array2
)]:
for
dtype1
,
dtype2
in
[(
'float64'
,
'int8'
),
for
dtype1
,
dtype2
in
[(
'float64'
,
'int8'
),
...
@@ -718,18 +718,18 @@ class T_AddMul(unittest.TestCase):
...
@@ -718,18 +718,18 @@ class T_AddMul(unittest.TestCase):
self
.
assertTrue
(
val
.
shape
==
(
3
,
2
))
self
.
assertTrue
(
val
.
shape
==
(
3
,
2
))
if
op
is
add
:
if
op
is
add
:
self
.
assertTrue
(
_is_dense_variable
(
apb
))
self
.
assertTrue
(
_is_dense_variable
(
apb
))
self
.
assertTrue
(
n
umpy
.
all
(
val
==
(
a
+
array2
)))
self
.
assertTrue
(
n
p
.
all
(
val
==
(
a
+
array2
)))
ans
=
n
umpy
.
array
([[
1.
,
2
],
[
3
,
4
],
[
5
,
6
]])
ans
=
n
p
.
array
([[
1.
,
2
],
[
3
,
4
],
[
5
,
6
]])
self
.
assertTrue
(
n
umpy
.
all
(
val
==
ans
))
self
.
assertTrue
(
n
p
.
all
(
val
==
ans
))
if
isinstance
(
b
,
theano
.
Constant
):
if
isinstance
(
b
,
theano
.
Constant
):
b
=
b
.
data
b
=
b
.
data
if
dtype1
.
startswith
(
'float'
)
and
dtype2
.
startswith
(
'float'
):
if
dtype1
.
startswith
(
'float'
)
and
dtype2
.
startswith
(
'float'
):
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
True
)
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
True
)
elif
op
is
mul
:
elif
op
is
mul
:
self
.
assertTrue
(
_is_sparse_variable
(
apb
))
self
.
assertTrue
(
_is_sparse_variable
(
apb
))
ans
=
n
umpy
.
array
([[
1
,
0
],
[
9
,
0
],
[
0
,
36
]])
ans
=
n
p
.
array
([[
1
,
0
],
[
9
,
0
],
[
0
,
36
]])
self
.
assertTrue
(
n
umpy
.
all
(
val
.
todense
()
==
(
a
.
multiply
(
array2
))))
self
.
assertTrue
(
n
p
.
all
(
val
.
todense
()
==
(
a
.
multiply
(
array2
))))
self
.
assertTrue
(
n
umpy
.
all
(
val
.
todense
()
==
ans
))
self
.
assertTrue
(
n
p
.
all
(
val
.
todense
()
==
ans
))
if
isinstance
(
b
,
theano
.
Constant
):
if
isinstance
(
b
,
theano
.
Constant
):
b
=
b
.
data
b
=
b
.
data
if
dtype1
.
startswith
(
'float'
)
and
dtype2
.
startswith
(
'float'
):
if
dtype1
.
startswith
(
'float'
)
and
dtype2
.
startswith
(
'float'
):
...
@@ -742,7 +742,7 @@ class test_comparison(unittest.TestCase):
...
@@ -742,7 +742,7 @@ class test_comparison(unittest.TestCase):
# took from tensor basic_test.py
# took from tensor basic_test.py
def
_rand_ranged
(
self
,
min
,
max
,
shape
):
def
_rand_ranged
(
self
,
min
,
max
,
shape
):
return
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
)
*
(
max
-
min
)
+
min
,
return
n
p
.
asarray
(
np
.
random
.
rand
(
*
shape
)
*
(
max
-
min
)
+
min
,
dtype
=
config
.
floatX
)
dtype
=
config
.
floatX
)
tests
=
[
lambda
x
,
y
:
x
>
y
,
lambda
x
,
y
:
x
<
y
,
tests
=
[
lambda
x
,
y
:
x
>
y
,
lambda
x
,
y
:
x
<
y
,
...
@@ -768,7 +768,7 @@ class test_comparison(unittest.TestCase):
...
@@ -768,7 +768,7 @@ class test_comparison(unittest.TestCase):
m1
=
scipyType
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
))
m1
=
scipyType
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
))
m2
=
scipyType
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
))
m2
=
scipyType
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
))
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
(
m1
,
m2
)
.
data
,
testOp
(
m1
,
m2
)
.
data
))
self
.
assertTrue
(
n
p
.
array_equal
(
f
(
m1
,
m2
)
.
data
,
testOp
(
m1
,
m2
)
.
data
))
def
__generalized_sd_test
(
self
,
theanop
,
symbolicType
,
testOp
,
scipyType
):
def
__generalized_sd_test
(
self
,
theanop
,
symbolicType
,
testOp
,
scipyType
):
...
@@ -787,7 +787,7 @@ class test_comparison(unittest.TestCase):
...
@@ -787,7 +787,7 @@ class test_comparison(unittest.TestCase):
m1
=
scipyType
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
))
m1
=
scipyType
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
))
m2
=
self
.
_rand_ranged
(
1000
,
-
1000
,
[
10
,
40
])
m2
=
self
.
_rand_ranged
(
1000
,
-
1000
,
[
10
,
40
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
(
m1
,
m2
)
.
data
,
testOp
(
m1
,
m2
)
.
data
))
self
.
assertTrue
(
n
p
.
array_equal
(
f
(
m1
,
m2
)
.
data
,
testOp
(
m1
,
m2
)
.
data
))
def
__generalized_ds_test
(
self
,
theanop
,
symbolicType
,
testOp
,
scipyType
):
def
__generalized_ds_test
(
self
,
theanop
,
symbolicType
,
testOp
,
scipyType
):
...
@@ -806,7 +806,7 @@ class test_comparison(unittest.TestCase):
...
@@ -806,7 +806,7 @@ class test_comparison(unittest.TestCase):
m1
=
scipyType
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
))
m1
=
scipyType
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
))
m2
=
self
.
_rand_ranged
(
1000
,
-
1000
,
[
10
,
40
])
m2
=
self
.
_rand_ranged
(
1000
,
-
1000
,
[
10
,
40
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
(
m2
,
m1
)
.
data
,
testOp
(
m2
,
m1
)
.
data
))
self
.
assertTrue
(
n
p
.
array_equal
(
f
(
m2
,
m1
)
.
data
,
testOp
(
m2
,
m1
)
.
data
))
def
test_ss_csr_comparison
(
self
):
def
test_ss_csr_comparison
(
self
):
...
@@ -859,14 +859,14 @@ class test_comparison(unittest.TestCase):
...
@@ -859,14 +859,14 @@ class test_comparison(unittest.TestCase):
y
=
theano
.
tensor
.
matrix
()
y
=
theano
.
tensor
.
matrix
()
m1
=
sp
.
csc_matrix
((
2
,
2
),
dtype
=
theano
.
config
.
floatX
)
m1
=
sp
.
csc_matrix
((
2
,
2
),
dtype
=
theano
.
config
.
floatX
)
m2
=
n
umpy
.
asarray
([[
0
,
0
],
[
0
,
0
]],
dtype
=
theano
.
config
.
floatX
)
m2
=
n
p
.
asarray
([[
0
,
0
],
[
0
,
0
]],
dtype
=
theano
.
config
.
floatX
)
for
func
in
self
.
testsDic
:
for
func
in
self
.
testsDic
:
op
=
func
(
y
,
x
)
op
=
func
(
y
,
x
)
f
=
theano
.
function
([
y
,
x
],
op
)
f
=
theano
.
function
([
y
,
x
],
op
)
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
(
m2
,
m1
),
self
.
assertTrue
(
n
p
.
array_equal
(
f
(
m2
,
m1
),
self
.
testsDic
[
func
](
m2
,
m1
)))
self
.
testsDic
[
func
](
m2
,
m1
)))
...
@@ -876,7 +876,7 @@ class T_conversion(unittest.TestCase):
...
@@ -876,7 +876,7 @@ class T_conversion(unittest.TestCase):
if
0
:
if
0
:
def
test0
(
self
):
def
test0
(
self
):
a
=
tensor
.
as_tensor_variable
(
n
umpy
.
random
.
rand
(
5
))
a
=
tensor
.
as_tensor_variable
(
n
p
.
random
.
rand
(
5
))
s
=
csc_from_dense
(
a
)
s
=
csc_from_dense
(
a
)
val
=
eval_outputs
([
s
])
val
=
eval_outputs
([
s
])
self
.
assertTrue
(
str
(
val
.
dtype
)
==
'float64'
)
self
.
assertTrue
(
str
(
val
.
dtype
)
==
'float64'
)
...
@@ -884,7 +884,7 @@ class T_conversion(unittest.TestCase):
...
@@ -884,7 +884,7 @@ class T_conversion(unittest.TestCase):
if
0
:
if
0
:
def
test1
(
self
):
def
test1
(
self
):
a
=
tensor
.
as_tensor_variable
(
n
umpy
.
random
.
rand
(
5
))
a
=
tensor
.
as_tensor_variable
(
n
p
.
random
.
rand
(
5
))
s
=
csr_from_dense
(
a
)
s
=
csr_from_dense
(
a
)
val
=
eval_outputs
([
s
])
val
=
eval_outputs
([
s
])
self
.
assertTrue
(
str
(
val
.
dtype
)
==
'float64'
)
self
.
assertTrue
(
str
(
val
.
dtype
)
==
'float64'
)
...
@@ -898,7 +898,7 @@ class T_conversion(unittest.TestCase):
...
@@ -898,7 +898,7 @@ class T_conversion(unittest.TestCase):
d
=
dense_from_sparse
(
s
)
d
=
dense_from_sparse
(
s
)
val
=
eval_outputs
([
d
])
val
=
eval_outputs
([
d
])
self
.
assertTrue
(
str
(
val
.
dtype
)
==
s
.
dtype
)
self
.
assertTrue
(
str
(
val
.
dtype
)
==
s
.
dtype
)
self
.
assertTrue
(
n
umpy
.
all
(
val
[
0
]
==
[
1
,
0
,
0
,
0
,
0
]))
self
.
assertTrue
(
n
p
.
all
(
val
[
0
]
==
[
1
,
0
,
0
,
0
,
0
]))
def
test_todense
(
self
):
def
test_todense
(
self
):
# call sparse_var.todense()
# call sparse_var.todense()
...
@@ -908,7 +908,7 @@ class T_conversion(unittest.TestCase):
...
@@ -908,7 +908,7 @@ class T_conversion(unittest.TestCase):
d
=
s
.
toarray
()
d
=
s
.
toarray
()
val
=
eval_outputs
([
d
])
val
=
eval_outputs
([
d
])
self
.
assertTrue
(
str
(
val
.
dtype
)
==
s
.
dtype
)
self
.
assertTrue
(
str
(
val
.
dtype
)
==
s
.
dtype
)
self
.
assertTrue
(
n
umpy
.
all
(
val
[
0
]
==
[
1
,
0
,
0
,
0
,
0
]))
self
.
assertTrue
(
n
p
.
all
(
val
[
0
]
==
[
1
,
0
,
0
,
0
,
0
]))
@staticmethod
@staticmethod
def
check_format_ndim
(
format
,
ndim
):
def
check_format_ndim
(
format
,
ndim
):
...
@@ -923,8 +923,8 @@ class T_conversion(unittest.TestCase):
...
@@ -923,8 +923,8 @@ class T_conversion(unittest.TestCase):
c
=
d
.
sum
()
c
=
d
.
sum
()
g
=
tensor
.
grad
(
c
,
x
)
g
=
tensor
.
grad
(
c
,
x
)
f
=
theano
.
function
([
x
],
[
s
,
g
])
f
=
theano
.
function
([
x
],
[
s
,
g
])
f
(
n
umpy
.
array
(
0
,
dtype
=
config
.
floatX
,
ndmin
=
ndim
))
f
(
n
p
.
array
(
0
,
dtype
=
config
.
floatX
,
ndmin
=
ndim
))
f
(
n
umpy
.
array
(
7
,
dtype
=
config
.
floatX
,
ndmin
=
ndim
))
f
(
n
p
.
array
(
7
,
dtype
=
config
.
floatX
,
ndmin
=
ndim
))
def
test_format_ndim
(
self
):
def
test_format_ndim
(
self
):
for
format
in
'csc'
,
'csr'
:
for
format
in
'csc'
,
'csr'
:
...
@@ -972,10 +972,10 @@ class test_csm_properties(unittest.TestCase):
...
@@ -972,10 +972,10 @@ class test_csm_properties(unittest.TestCase):
data
,
indices
,
indptr
,
shape
=
f
(
spmat
)
data
,
indices
,
indptr
,
shape
=
f
(
spmat
)
assert
n
umpy
.
all
(
data
==
spmat
.
data
)
assert
n
p
.
all
(
data
==
spmat
.
data
)
assert
n
umpy
.
all
(
indices
==
spmat
.
indices
)
assert
n
p
.
all
(
indices
==
spmat
.
indices
)
assert
n
umpy
.
all
(
indptr
==
spmat
.
indptr
)
assert
n
p
.
all
(
indptr
==
spmat
.
indptr
)
assert
n
umpy
.
all
(
shape
==
spmat
.
shape
)
assert
n
p
.
all
(
shape
==
spmat
.
shape
)
class
test_csm
(
unittest
.
TestCase
):
class
test_csm
(
unittest
.
TestCase
):
...
@@ -991,7 +991,7 @@ class test_csm(unittest.TestCase):
...
@@ -991,7 +991,7 @@ class test_csm(unittest.TestCase):
spmat
=
sp_types
[
format
](
random_lil
((
4
,
3
),
dtype
,
3
))
spmat
=
sp_types
[
format
](
random_lil
((
4
,
3
),
dtype
,
3
))
verify_grad_sparse
(
lambda
x
:
CSM
(
format
)(
x
,
spmat
.
indices
,
verify_grad_sparse
(
lambda
x
:
CSM
(
format
)(
x
,
spmat
.
indices
,
spmat
.
indptr
,
n
umpy
.
asarray
(
spmat
.
shape
,
'int32'
)),
spmat
.
indptr
,
n
p
.
asarray
(
spmat
.
shape
,
'int32'
)),
[
spmat
.
data
],
structured
=
True
)
[
spmat
.
data
],
structured
=
True
)
def
test_csm_sparser
(
self
):
def
test_csm_sparser
(
self
):
...
@@ -1018,7 +1018,7 @@ class test_csm(unittest.TestCase):
...
@@ -1018,7 +1018,7 @@ class test_csm(unittest.TestCase):
spmat
=
sp_types
[
format
](
random_lil
((
4
,
3
),
dtype
,
3
))
spmat
=
sp_types
[
format
](
random_lil
((
4
,
3
),
dtype
,
3
))
res
=
f
(
spmat
.
data
,
spmat
.
indices
,
spmat
.
indptr
,
res
=
f
(
spmat
.
data
,
spmat
.
indices
,
spmat
.
indptr
,
n
umpy
.
asarray
(
spmat
.
shape
,
'int32'
))
n
p
.
asarray
(
spmat
.
shape
,
'int32'
))
assert
len
(
spmat
.
data
)
==
len
(
res
)
assert
len
(
spmat
.
data
)
==
len
(
res
)
...
@@ -1063,12 +1063,12 @@ class test_csm(unittest.TestCase):
...
@@ -1063,12 +1063,12 @@ class test_csm(unittest.TestCase):
spmat
=
sp_types
[
format
](
random_lil
((
4
,
3
),
dtype
,
3
))
spmat
=
sp_types
[
format
](
random_lil
((
4
,
3
),
dtype
,
3
))
res
=
f
(
spmat
.
data
,
spmat
.
indices
,
spmat
.
indptr
,
res
=
f
(
spmat
.
data
,
spmat
.
indices
,
spmat
.
indptr
,
n
umpy
.
asarray
(
spmat
.
shape
,
'int32'
))
n
p
.
asarray
(
spmat
.
shape
,
'int32'
))
assert
n
umpy
.
all
(
res
.
data
==
spmat
.
data
)
assert
n
p
.
all
(
res
.
data
==
spmat
.
data
)
assert
n
umpy
.
all
(
res
.
indices
==
spmat
.
indices
)
assert
n
p
.
all
(
res
.
indices
==
spmat
.
indices
)
assert
n
umpy
.
all
(
res
.
indptr
==
spmat
.
indptr
)
assert
n
p
.
all
(
res
.
indptr
==
spmat
.
indptr
)
assert
n
umpy
.
all
(
res
.
shape
==
spmat
.
shape
)
assert
n
p
.
all
(
res
.
shape
==
spmat
.
shape
)
class
test_structureddot
(
unittest
.
TestCase
):
class
test_structureddot
(
unittest
.
TestCase
):
...
@@ -1082,7 +1082,7 @@ class test_structureddot(unittest.TestCase):
...
@@ -1082,7 +1082,7 @@ class test_structureddot(unittest.TestCase):
# allocate a random sparse matrix
# allocate a random sparse matrix
spmat
=
sp
.
csc_matrix
(
random_lil
((
4
,
3
),
'float32'
,
3
))
spmat
=
sp
.
csc_matrix
(
random_lil
((
4
,
3
),
'float32'
,
3
))
mat
=
n
umpy
.
asarray
(
numpy
.
random
.
randn
(
3
,
2
),
'float32'
)
mat
=
n
p
.
asarray
(
np
.
random
.
randn
(
3
,
2
),
'float32'
)
verify_grad_sparse
(
structured_dot
,
[
spmat
,
mat
],
structured
=
True
)
verify_grad_sparse
(
structured_dot
,
[
spmat
,
mat
],
structured
=
True
)
...
@@ -1098,7 +1098,7 @@ class test_structureddot(unittest.TestCase):
...
@@ -1098,7 +1098,7 @@ class test_structureddot(unittest.TestCase):
# allocate a random sparse matrix
# allocate a random sparse matrix
spmat
=
sp
.
csr_matrix
(
random_lil
((
4
,
3
),
'float64'
,
3
))
spmat
=
sp
.
csr_matrix
(
random_lil
((
4
,
3
),
'float64'
,
3
))
mat
=
n
umpy
.
asarray
(
numpy
.
random
.
randn
(
3
,
2
),
'float64'
)
mat
=
n
p
.
asarray
(
np
.
random
.
randn
(
3
,
2
),
'float64'
)
verify_grad_sparse
(
structured_dot
,
[
spmat
,
mat
],
structured
=
True
)
verify_grad_sparse
(
structured_dot
,
[
spmat
,
mat
],
structured
=
True
)
...
@@ -1129,8 +1129,8 @@ class test_structureddot(unittest.TestCase):
...
@@ -1129,8 +1129,8 @@ class test_structureddot(unittest.TestCase):
# an intc vs. int32 bug.
# an intc vs. int32 bug.
# The lil makes an intc on my computer when sparse_dtype
# The lil makes an intc on my computer when sparse_dtype
# is int32.
# is int32.
spmat
.
dtype
=
n
umpy
.
dtype
(
sparse_dtype
)
spmat
.
dtype
=
n
p
.
dtype
(
sparse_dtype
)
mat
=
n
umpy
.
asarray
(
numpy
.
random
.
randn
(
N
,
K
)
*
9
,
mat
=
n
p
.
asarray
(
np
.
random
.
randn
(
N
,
K
)
*
9
,
dtype
=
dense_dtype
)
dtype
=
dense_dtype
)
# print 'DTYPES', sparse_dtype, dense_dtype
# print 'DTYPES', sparse_dtype, dense_dtype
# print 'sym types', a.type, b.type
# print 'sym types', a.type, b.type
...
@@ -1158,9 +1158,9 @@ class test_structureddot(unittest.TestCase):
...
@@ -1158,9 +1158,9 @@ class test_structureddot(unittest.TestCase):
spmat
=
sp
.
lil_matrix
((
4
,
6
),
dtype
=
'int64'
)
spmat
=
sp
.
lil_matrix
((
4
,
6
),
dtype
=
'int64'
)
for
i
in
range
(
5
):
for
i
in
range
(
5
):
# set non-zeros in random locations (row x, col y)
# set non-zeros in random locations (row x, col y)
x
=
n
umpy
.
floor
(
numpy
.
random
.
rand
()
*
spmat
.
shape
[
0
])
x
=
n
p
.
floor
(
np
.
random
.
rand
()
*
spmat
.
shape
[
0
])
y
=
n
umpy
.
floor
(
numpy
.
random
.
rand
()
*
spmat
.
shape
[
1
])
y
=
n
p
.
floor
(
np
.
random
.
rand
()
*
spmat
.
shape
[
1
])
spmat
[
x
,
y
]
=
n
umpy
.
random
.
rand
()
*
10
spmat
[
x
,
y
]
=
n
p
.
random
.
rand
()
*
10
spmat
=
sp
.
csc_matrix
(
spmat
)
spmat
=
sp
.
csc_matrix
(
spmat
)
images
=
tensor
.
Tensor
(
dtype
=
'float32'
,
images
=
tensor
.
Tensor
(
dtype
=
'float32'
,
...
@@ -1179,12 +1179,12 @@ class test_structureddot(unittest.TestCase):
...
@@ -1179,12 +1179,12 @@ class test_structureddot(unittest.TestCase):
sdcscpresent
=
True
sdcscpresent
=
True
assert
sdcscpresent
assert
sdcscpresent
kernvals
=
n
umpy
.
array
(
spmat
.
data
[:
spmat
.
size
])
kernvals
=
n
p
.
array
(
spmat
.
data
[:
spmat
.
size
])
# print 'kdtype', kernvals.dtype, kernvals.shape,
# print 'kdtype', kernvals.dtype, kernvals.shape,
# print kernvals.ndim, kernvals.dtype.num
# print kernvals.ndim, kernvals.dtype.num
# print 'type of kernvals = ', kernvals.dtype
# print 'type of kernvals = ', kernvals.dtype
bsize
=
3
bsize
=
3
imvals
=
1.0
*
n
umpy
.
array
(
numpy
.
arange
(
bsize
*
spmat
.
shape
[
1
])
.
\
imvals
=
1.0
*
n
p
.
array
(
np
.
arange
(
bsize
*
spmat
.
shape
[
1
])
.
\
reshape
(
bsize
,
spmat
.
shape
[
1
]),
reshape
(
bsize
,
spmat
.
shape
[
1
]),
dtype
=
'float32'
)
dtype
=
'float32'
)
outvals
=
f
(
kernvals
,
imvals
)
outvals
=
f
(
kernvals
,
imvals
)
...
@@ -1230,7 +1230,7 @@ class test_structureddot(unittest.TestCase):
...
@@ -1230,7 +1230,7 @@ class test_structureddot(unittest.TestCase):
(
400
,
3000
,
200
,
6000
),
(
400
,
3000
,
200
,
6000
),
]:
]:
spmat
=
sp
.
csc_matrix
(
random_lil
((
M
,
N
),
sparse_dtype
,
nnz
))
spmat
=
sp
.
csc_matrix
(
random_lil
((
M
,
N
),
sparse_dtype
,
nnz
))
mat
=
n
umpy
.
asarray
(
numpy
.
random
.
randn
(
N
,
K
),
dense_dtype
)
mat
=
n
p
.
asarray
(
np
.
random
.
randn
(
N
,
K
),
dense_dtype
)
theano_times
=
[]
theano_times
=
[]
scipy_times
=
[]
scipy_times
=
[]
for
i
in
xrange
(
5
):
for
i
in
xrange
(
5
):
...
@@ -1243,8 +1243,8 @@ class test_structureddot(unittest.TestCase):
...
@@ -1243,8 +1243,8 @@ class test_structureddot(unittest.TestCase):
theano_times
.
append
(
t1
-
t0
)
theano_times
.
append
(
t1
-
t0
)
scipy_times
.
append
(
t2
-
t1
)
scipy_times
.
append
(
t2
-
t1
)
theano_time
=
n
umpy
.
min
(
theano_times
)
theano_time
=
n
p
.
min
(
theano_times
)
scipy_time
=
n
umpy
.
min
(
scipy_times
)
scipy_time
=
n
p
.
min
(
scipy_times
)
speedup
=
scipy_time
/
theano_time
speedup
=
scipy_time
/
theano_time
# print scipy_times
# print scipy_times
...
@@ -1278,7 +1278,7 @@ class test_structureddot(unittest.TestCase):
...
@@ -1278,7 +1278,7 @@ class test_structureddot(unittest.TestCase):
(
400
,
3000
,
200
,
6000
),
(
400
,
3000
,
200
,
6000
),
]:
]:
spmat
=
sp
.
csr_matrix
(
random_lil
((
M
,
N
),
sparse_dtype
,
nnz
))
spmat
=
sp
.
csr_matrix
(
random_lil
((
M
,
N
),
sparse_dtype
,
nnz
))
mat
=
n
umpy
.
asarray
(
numpy
.
random
.
randn
(
N
,
K
),
dense_dtype
)
mat
=
n
p
.
asarray
(
np
.
random
.
randn
(
N
,
K
),
dense_dtype
)
t0
=
time
.
time
()
t0
=
time
.
time
()
theano_result
=
f
(
spmat
,
mat
)
theano_result
=
f
(
spmat
,
mat
)
t1
=
time
.
time
()
t1
=
time
.
time
()
...
@@ -1309,18 +1309,18 @@ class DotTests(utt.InferShapeTester):
...
@@ -1309,18 +1309,18 @@ class DotTests(utt.InferShapeTester):
utt
.
seed_rng
()
utt
.
seed_rng
()
self
.
x_csr
=
scipy
.
sparse
.
csr_matrix
(
self
.
x_csr
=
scipy
.
sparse
.
csr_matrix
(
n
umpy
.
random
.
binomial
(
1
,
0.5
,
x_size
),
dtype
=
theano
.
config
.
floatX
)
n
p
.
random
.
binomial
(
1
,
0.5
,
x_size
),
dtype
=
theano
.
config
.
floatX
)
self
.
x_csc
=
scipy
.
sparse
.
csc_matrix
(
self
.
x_csc
=
scipy
.
sparse
.
csc_matrix
(
n
umpy
.
random
.
binomial
(
1
,
0.5
,
x_size
),
dtype
=
theano
.
config
.
floatX
)
n
p
.
random
.
binomial
(
1
,
0.5
,
x_size
),
dtype
=
theano
.
config
.
floatX
)
self
.
y
=
n
umpy
.
asarray
(
numpy
.
random
.
uniform
(
-
1
,
1
,
y_size
),
self
.
y
=
n
p
.
asarray
(
np
.
random
.
uniform
(
-
1
,
1
,
y_size
),
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
self
.
y_csr
=
scipy
.
sparse
.
csr_matrix
(
self
.
y_csr
=
scipy
.
sparse
.
csr_matrix
(
n
umpy
.
random
.
binomial
(
1
,
0.5
,
y_size
),
dtype
=
theano
.
config
.
floatX
)
n
p
.
random
.
binomial
(
1
,
0.5
,
y_size
),
dtype
=
theano
.
config
.
floatX
)
self
.
y_csc
=
scipy
.
sparse
.
csc_matrix
(
self
.
y_csc
=
scipy
.
sparse
.
csc_matrix
(
n
umpy
.
random
.
binomial
(
1
,
0.5
,
y_size
),
dtype
=
theano
.
config
.
floatX
)
n
p
.
random
.
binomial
(
1
,
0.5
,
y_size
),
dtype
=
theano
.
config
.
floatX
)
self
.
v_10
=
n
umpy
.
asarray
(
numpy
.
random
.
uniform
(
-
1
,
1
,
10
),
self
.
v_10
=
n
p
.
asarray
(
np
.
random
.
uniform
(
-
1
,
1
,
10
),
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
self
.
v_100
=
n
umpy
.
asarray
(
numpy
.
random
.
uniform
(
-
1
,
1
,
100
),
self
.
v_100
=
n
p
.
asarray
(
np
.
random
.
uniform
(
-
1
,
1
,
100
),
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
def
test_csr_dense
(
self
):
def
test_csr_dense
(
self
):
...
@@ -1386,7 +1386,7 @@ class DotTests(utt.InferShapeTester):
...
@@ -1386,7 +1386,7 @@ class DotTests(utt.InferShapeTester):
# Test infer_shape
# Test infer_shape
f_a
=
theano
.
function
([
x
,
y
],
theano
.
sparse
.
dot
(
x
,
y
)
.
shape
)
f_a
=
theano
.
function
([
x
,
y
],
theano
.
sparse
.
dot
(
x
,
y
)
.
shape
)
f_b
=
lambda
x
,
y
:
(
x
*
y
)
.
shape
f_b
=
lambda
x
,
y
:
(
x
*
y
)
.
shape
assert
n
umpy
.
all
(
f_a
(
vx
,
vy
)
==
f_b
(
vx
,
vy
))
assert
n
p
.
all
(
f_a
(
vx
,
vy
)
==
f_b
(
vx
,
vy
))
topo
=
f_a
.
maker
.
fgraph
.
toposort
()
topo
=
f_a
.
maker
.
fgraph
.
toposort
()
if
theano
.
config
.
mode
!=
'FAST_COMPILE'
:
if
theano
.
config
.
mode
!=
'FAST_COMPILE'
:
nb
=
0
nb
=
0
...
@@ -1402,7 +1402,7 @@ class DotTests(utt.InferShapeTester):
...
@@ -1402,7 +1402,7 @@ class DotTests(utt.InferShapeTester):
a
=
sparse
.
csr_matrix
(
'a'
,
dtype
=
'float32'
)
a
=
sparse
.
csr_matrix
(
'a'
,
dtype
=
'float32'
)
b
=
cuda
.
float32_shared_constructor
(
b
=
cuda
.
float32_shared_constructor
(
n
umpy
.
random
.
rand
(
3
,
4
)
.
astype
(
'float32'
))
n
p
.
random
.
rand
(
3
,
4
)
.
astype
(
'float32'
))
d
=
sparse
.
dot
(
a
,
b
)
d
=
sparse
.
dot
(
a
,
b
)
f
=
theano
.
function
([
a
],
d
)
f
=
theano
.
function
([
a
],
d
)
...
@@ -1429,8 +1429,8 @@ class DotTests(utt.InferShapeTester):
...
@@ -1429,8 +1429,8 @@ class DotTests(utt.InferShapeTester):
y
=
m2
.
reshape
(
shape
=
(
2
,
4
,
9
),
ndim
=
3
)
y
=
m2
.
reshape
(
shape
=
(
2
,
4
,
9
),
ndim
=
3
)
f
=
theano
.
function
(
inputs
=
[
I
,
C
],
outputs
=
y
)
f
=
theano
.
function
(
inputs
=
[
I
,
C
],
outputs
=
y
)
i
=
n
umpy
.
asarray
([[
4
,
3
,
7
,
7
],
[
2
,
8
,
4
,
5
]],
dtype
=
intX
)
i
=
n
p
.
asarray
([[
4
,
3
,
7
,
7
],
[
2
,
8
,
4
,
5
]],
dtype
=
intX
)
a
=
n
umpy
.
asarray
(
numpy
.
random
.
randint
(
0
,
100
,
(
size
,
size
)),
a
=
n
p
.
asarray
(
np
.
random
.
randint
(
0
,
100
,
(
size
,
size
)),
dtype
=
intX
)
dtype
=
intX
)
f
(
i
,
a
)
f
(
i
,
a
)
...
@@ -1441,7 +1441,7 @@ class DotTests(utt.InferShapeTester):
...
@@ -1441,7 +1441,7 @@ class DotTests(utt.InferShapeTester):
# allocate a random sparse matrix
# allocate a random sparse matrix
spmat
=
sp
.
csr_matrix
(
random_lil
((
4
,
3
),
'float64'
,
3
))
spmat
=
sp
.
csr_matrix
(
random_lil
((
4
,
3
),
'float64'
,
3
))
mat
=
n
umpy
.
asarray
(
numpy
.
random
.
randn
(
2
,
4
),
'float64'
)
mat
=
n
p
.
asarray
(
np
.
random
.
randn
(
2
,
4
),
'float64'
)
def
buildgraph_T
(
mat
):
def
buildgraph_T
(
mat
):
return
Dot
()(
mat
,
spmat
)
return
Dot
()(
mat
,
spmat
)
...
@@ -1456,12 +1456,12 @@ class UsmmTests(unittest.TestCase):
...
@@ -1456,12 +1456,12 @@ class UsmmTests(unittest.TestCase):
y_size
=
(
100
,
200
)
y_size
=
(
100
,
200
)
z_size
=
(
x_size
[
0
],
y_size
[
1
])
z_size
=
(
x_size
[
0
],
y_size
[
1
])
self
.
rng
=
n
umpy
.
random
.
RandomState
(
seed
=
utt
.
fetch_seed
())
self
.
rng
=
n
p
.
random
.
RandomState
(
seed
=
utt
.
fetch_seed
())
self
.
x
=
n
umpy
.
asarray
(
self
.
rng
.
binomial
(
1
,
0.5
,
x_size
),
self
.
x
=
n
p
.
asarray
(
self
.
rng
.
binomial
(
1
,
0.5
,
x_size
),
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
self
.
y
=
n
umpy
.
asarray
(
self
.
rng
.
uniform
(
-
1
,
1
,
y_size
),
self
.
y
=
n
p
.
asarray
(
self
.
rng
.
uniform
(
-
1
,
1
,
y_size
),
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
self
.
z
=
n
umpy
.
asarray
(
self
.
rng
.
uniform
(
-
1
,
1
,
z_size
),
self
.
z
=
n
p
.
asarray
(
self
.
rng
.
uniform
(
-
1
,
1
,
z_size
),
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
# this is slow, but it's the only test for the op.
# this is slow, but it's the only test for the op.
...
@@ -1487,17 +1487,17 @@ class UsmmTests(unittest.TestCase):
...
@@ -1487,17 +1487,17 @@ class UsmmTests(unittest.TestCase):
x
=
mat
(
format1
,
'x'
,
dtype1
)
x
=
mat
(
format1
,
'x'
,
dtype1
)
y
=
mat
(
format2
,
'y'
,
dtype2
)
y
=
mat
(
format2
,
'y'
,
dtype2
)
a
=
theano
.
tensor
.
scalar
(
'a'
,
dtype
=
dtype3
)
a
=
theano
.
tensor
.
scalar
(
'a'
,
dtype
=
dtype3
)
z
=
theano
.
shared
(
n
umpy
.
asarray
(
self
.
z
,
dtype
=
dtype4
)
.
copy
())
z
=
theano
.
shared
(
n
p
.
asarray
(
self
.
z
,
dtype
=
dtype4
)
.
copy
())
f_b
=
lambda
z
,
a
,
x
,
y
:
z
-
a
*
(
x
*
y
)
f_b
=
lambda
z
,
a
,
x
,
y
:
z
-
a
*
(
x
*
y
)
x_data
=
n
umpy
.
asarray
(
self
.
x
,
dtype
=
dtype1
)
x_data
=
n
p
.
asarray
(
self
.
x
,
dtype
=
dtype1
)
if
format1
!=
'dense'
:
if
format1
!=
'dense'
:
x_data
=
as_sparse_format
(
x_data
,
format1
)
x_data
=
as_sparse_format
(
x_data
,
format1
)
y_data
=
n
umpy
.
asarray
(
self
.
y
,
dtype
=
dtype2
)
y_data
=
n
p
.
asarray
(
self
.
y
,
dtype
=
dtype2
)
if
format2
!=
'dense'
:
if
format2
!=
'dense'
:
y_data
=
as_sparse_format
(
y_data
,
format2
)
y_data
=
as_sparse_format
(
y_data
,
format2
)
a_data
=
n
umpy
.
asarray
(
1.5
,
dtype
=
dtype3
)
a_data
=
n
p
.
asarray
(
1.5
,
dtype
=
dtype3
)
z_data
=
n
umpy
.
asarray
(
self
.
z
,
dtype
=
dtype4
)
z_data
=
n
p
.
asarray
(
self
.
z
,
dtype
=
dtype4
)
f_b_out
=
f_b
(
z_data
,
a_data
,
x_data
,
y_data
)
f_b_out
=
f_b
(
z_data
,
a_data
,
x_data
,
y_data
)
...
@@ -1603,17 +1603,17 @@ class UsmmTests(unittest.TestCase):
...
@@ -1603,17 +1603,17 @@ class UsmmTests(unittest.TestCase):
x
=
mat
(
format1
,
'x'
,
dtype1
)
x
=
mat
(
format1
,
'x'
,
dtype1
)
y
=
mat
(
format2
,
'y'
,
dtype2
)
y
=
mat
(
format2
,
'y'
,
dtype2
)
a
=
theano
.
tensor
.
scalar
(
'a'
,
dtype
=
dtype3
)
a
=
theano
.
tensor
.
scalar
(
'a'
,
dtype
=
dtype3
)
z
=
theano
.
shared
(
n
umpy
.
asarray
(
self
.
z
,
dtype
=
dtype4
)
.
copy
())
z
=
theano
.
shared
(
n
p
.
asarray
(
self
.
z
,
dtype
=
dtype4
)
.
copy
())
f_b
=
lambda
z
,
a
,
x
,
y
:
z
-
a
*
(
x
*
y
)
f_b
=
lambda
z
,
a
,
x
,
y
:
z
-
a
*
(
x
*
y
)
x_data
=
n
umpy
.
asarray
(
self
.
x
,
dtype
=
dtype1
)
x_data
=
n
p
.
asarray
(
self
.
x
,
dtype
=
dtype1
)
if
format1
!=
'dense'
:
if
format1
!=
'dense'
:
x_data
=
as_sparse_format
(
x_data
,
format1
)
x_data
=
as_sparse_format
(
x_data
,
format1
)
y_data
=
n
umpy
.
asarray
(
self
.
y
,
dtype
=
dtype2
)
y_data
=
n
p
.
asarray
(
self
.
y
,
dtype
=
dtype2
)
if
format2
!=
'dense'
:
if
format2
!=
'dense'
:
y_data
=
as_sparse_format
(
y_data
,
format2
)
y_data
=
as_sparse_format
(
y_data
,
format2
)
a_data
=
n
umpy
.
asarray
(
1.5
,
dtype
=
dtype3
)
a_data
=
n
p
.
asarray
(
1.5
,
dtype
=
dtype3
)
z_data
=
n
umpy
.
asarray
(
self
.
z
,
dtype
=
dtype4
)
z_data
=
n
p
.
asarray
(
self
.
z
,
dtype
=
dtype4
)
f_b_out
=
f_b
(
z_data
,
a_data
,
x_data
,
y_data
)
f_b_out
=
f_b
(
z_data
,
a_data
,
x_data
,
y_data
)
...
@@ -1641,8 +1641,8 @@ class test_zeros_like(unittest.TestCase):
...
@@ -1641,8 +1641,8 @@ class test_zeros_like(unittest.TestCase):
def
test
(
self
):
def
test
(
self
):
x
=
theano
.
sparse
.
csr_matrix
()
x
=
theano
.
sparse
.
csr_matrix
()
f
=
theano
.
function
([
x
],
theano
.
sparse
.
sp_zeros_like
(
x
))
f
=
theano
.
function
([
x
],
theano
.
sparse
.
sp_zeros_like
(
x
))
vx
=
scipy
.
sparse
.
csr_matrix
(
n
umpy
.
asarray
(
vx
=
scipy
.
sparse
.
csr_matrix
(
n
p
.
asarray
(
n
umpy
.
random
.
binomial
(
1
,
0.5
,
(
100
,
100
)),
n
p
.
random
.
binomial
(
1
,
0.5
,
(
100
,
100
)),
dtype
=
theano
.
config
.
floatX
))
dtype
=
theano
.
config
.
floatX
))
fx
=
f
(
vx
)
fx
=
f
(
vx
)
...
@@ -1666,7 +1666,7 @@ def test_shape():
...
@@ -1666,7 +1666,7 @@ def test_shape():
a
=
SparseType
(
'csr'
,
dtype
=
sparse_dtype
)()
a
=
SparseType
(
'csr'
,
dtype
=
sparse_dtype
)()
f
=
theano
.
function
([
a
],
a
.
shape
)
f
=
theano
.
function
([
a
],
a
.
shape
)
assert
n
umpy
.
all
(
f
(
sp
.
csr_matrix
(
random_lil
((
100
,
10
),
sparse_dtype
,
3
)))
assert
n
p
.
all
(
f
(
sp
.
csr_matrix
(
random_lil
((
100
,
10
),
sparse_dtype
,
3
)))
==
(
100
,
10
))
==
(
100
,
10
))
if
theano
.
config
.
mode
!=
'FAST_COMPILE'
:
if
theano
.
config
.
mode
!=
'FAST_COMPILE'
:
topo
=
f
.
maker
.
fgraph
.
toposort
()
topo
=
f
.
maker
.
fgraph
.
toposort
()
...
@@ -1765,12 +1765,12 @@ class ColScaleCSCTester(utt.InferShapeTester):
...
@@ -1765,12 +1765,12 @@ class ColScaleCSCTester(utt.InferShapeTester):
for
format
in
sparse
.
sparse_formats
:
for
format
in
sparse
.
sparse_formats
:
variable
,
data
=
sparse_random_inputs
(
format
,
shape
=
(
8
,
10
))
variable
,
data
=
sparse_random_inputs
(
format
,
shape
=
(
8
,
10
))
variable
.
append
(
tensor
.
vector
())
variable
.
append
(
tensor
.
vector
())
data
.
append
(
n
umpy
.
random
.
random
(
10
)
.
astype
(
config
.
floatX
))
data
.
append
(
n
p
.
random
.
random
(
10
)
.
astype
(
config
.
floatX
))
f
=
theano
.
function
(
variable
,
self
.
op
(
*
variable
))
f
=
theano
.
function
(
variable
,
self
.
op
(
*
variable
))
tested
=
f
(
*
data
)
tested
=
f
(
*
data
)
x
,
s
=
data
[
0
]
.
toarray
(),
data
[
1
][
n
umpy
.
newaxis
,
:]
x
,
s
=
data
[
0
]
.
toarray
(),
data
[
1
][
n
p
.
newaxis
,
:]
expected
=
x
*
s
expected
=
x
*
s
assert
tested
.
format
==
format
assert
tested
.
format
==
format
...
@@ -1781,7 +1781,7 @@ class ColScaleCSCTester(utt.InferShapeTester):
...
@@ -1781,7 +1781,7 @@ class ColScaleCSCTester(utt.InferShapeTester):
(
'csr'
,
sparse
.
RowScaleCSC
)]:
(
'csr'
,
sparse
.
RowScaleCSC
)]:
variable
,
data
=
sparse_random_inputs
(
format
,
shape
=
(
8
,
10
))
variable
,
data
=
sparse_random_inputs
(
format
,
shape
=
(
8
,
10
))
variable
.
append
(
tensor
.
vector
())
variable
.
append
(
tensor
.
vector
())
data
.
append
(
n
umpy
.
random
.
random
(
10
)
.
astype
(
config
.
floatX
))
data
.
append
(
n
p
.
random
.
random
(
10
)
.
astype
(
config
.
floatX
))
self
.
_compile_and_check
(
variable
,
self
.
_compile_and_check
(
variable
,
[
self
.
op
(
*
variable
)],
[
self
.
op
(
*
variable
)],
...
@@ -1792,7 +1792,7 @@ class ColScaleCSCTester(utt.InferShapeTester):
...
@@ -1792,7 +1792,7 @@ class ColScaleCSCTester(utt.InferShapeTester):
for
format
in
sparse
.
sparse_formats
:
for
format
in
sparse
.
sparse_formats
:
variable
,
data
=
sparse_random_inputs
(
format
,
shape
=
(
8
,
10
))
variable
,
data
=
sparse_random_inputs
(
format
,
shape
=
(
8
,
10
))
variable
.
append
(
tensor
.
vector
())
variable
.
append
(
tensor
.
vector
())
data
.
append
(
n
umpy
.
random
.
random
(
10
)
.
astype
(
config
.
floatX
))
data
.
append
(
n
p
.
random
.
random
(
10
)
.
astype
(
config
.
floatX
))
verify_grad_sparse
(
self
.
op
,
data
,
structured
=
True
)
verify_grad_sparse
(
self
.
op
,
data
,
structured
=
True
)
...
@@ -1806,12 +1806,12 @@ class RowScaleCSCTester(utt.InferShapeTester):
...
@@ -1806,12 +1806,12 @@ class RowScaleCSCTester(utt.InferShapeTester):
for
format
in
sparse
.
sparse_formats
:
for
format
in
sparse
.
sparse_formats
:
variable
,
data
=
sparse_random_inputs
(
format
,
shape
=
(
8
,
10
))
variable
,
data
=
sparse_random_inputs
(
format
,
shape
=
(
8
,
10
))
variable
.
append
(
tensor
.
vector
())
variable
.
append
(
tensor
.
vector
())
data
.
append
(
n
umpy
.
random
.
random
(
8
)
.
astype
(
config
.
floatX
))
data
.
append
(
n
p
.
random
.
random
(
8
)
.
astype
(
config
.
floatX
))
f
=
theano
.
function
(
variable
,
self
.
op
(
*
variable
))
f
=
theano
.
function
(
variable
,
self
.
op
(
*
variable
))
tested
=
f
(
*
data
)
tested
=
f
(
*
data
)
x
,
s
=
data
[
0
]
.
toarray
(),
data
[
1
][:,
n
umpy
.
newaxis
]
x
,
s
=
data
[
0
]
.
toarray
(),
data
[
1
][:,
n
p
.
newaxis
]
expected
=
x
*
s
expected
=
x
*
s
assert
tested
.
format
==
format
assert
tested
.
format
==
format
...
@@ -1822,7 +1822,7 @@ class RowScaleCSCTester(utt.InferShapeTester):
...
@@ -1822,7 +1822,7 @@ class RowScaleCSCTester(utt.InferShapeTester):
(
'csr'
,
sparse
.
ColScaleCSC
)]:
(
'csr'
,
sparse
.
ColScaleCSC
)]:
variable
,
data
=
sparse_random_inputs
(
format
,
shape
=
(
8
,
10
))
variable
,
data
=
sparse_random_inputs
(
format
,
shape
=
(
8
,
10
))
variable
.
append
(
tensor
.
vector
())
variable
.
append
(
tensor
.
vector
())
data
.
append
(
n
umpy
.
random
.
random
(
8
)
.
astype
(
config
.
floatX
))
data
.
append
(
n
p
.
random
.
random
(
8
)
.
astype
(
config
.
floatX
))
self
.
_compile_and_check
(
variable
,
self
.
_compile_and_check
(
variable
,
[
self
.
op
(
*
variable
)],
[
self
.
op
(
*
variable
)],
...
@@ -1833,7 +1833,7 @@ class RowScaleCSCTester(utt.InferShapeTester):
...
@@ -1833,7 +1833,7 @@ class RowScaleCSCTester(utt.InferShapeTester):
for
format
in
sparse
.
sparse_formats
:
for
format
in
sparse
.
sparse_formats
:
variable
,
data
=
sparse_random_inputs
(
format
,
shape
=
(
8
,
10
))
variable
,
data
=
sparse_random_inputs
(
format
,
shape
=
(
8
,
10
))
variable
.
append
(
tensor
.
vector
())
variable
.
append
(
tensor
.
vector
())
data
.
append
(
n
umpy
.
random
.
random
(
8
)
.
astype
(
config
.
floatX
))
data
.
append
(
n
p
.
random
.
random
(
8
)
.
astype
(
config
.
floatX
))
verify_grad_sparse
(
self
.
op
,
data
,
structured
=
True
)
verify_grad_sparse
(
self
.
op
,
data
,
structured
=
True
)
...
@@ -1935,12 +1935,12 @@ class SquareDiagonalTester(utt.InferShapeTester):
...
@@ -1935,12 +1935,12 @@ class SquareDiagonalTester(utt.InferShapeTester):
for
format
in
sparse
.
sparse_formats
:
for
format
in
sparse
.
sparse_formats
:
for
size
in
range
(
5
,
9
):
for
size
in
range
(
5
,
9
):
variable
=
[
tensor
.
vector
()]
variable
=
[
tensor
.
vector
()]
data
=
[
n
umpy
.
random
.
random
(
size
)
.
astype
(
config
.
floatX
)]
data
=
[
n
p
.
random
.
random
(
size
)
.
astype
(
config
.
floatX
)]
f
=
theano
.
function
(
variable
,
self
.
op
(
*
variable
))
f
=
theano
.
function
(
variable
,
self
.
op
(
*
variable
))
tested
=
f
(
*
data
)
.
toarray
()
tested
=
f
(
*
data
)
.
toarray
()
expected
=
n
umpy
.
diag
(
*
data
)
expected
=
n
p
.
diag
(
*
data
)
utt
.
assert_allclose
(
expected
,
tested
)
utt
.
assert_allclose
(
expected
,
tested
)
assert
tested
.
dtype
==
expected
.
dtype
assert
tested
.
dtype
==
expected
.
dtype
assert
tested
.
shape
==
expected
.
shape
assert
tested
.
shape
==
expected
.
shape
...
@@ -1949,7 +1949,7 @@ class SquareDiagonalTester(utt.InferShapeTester):
...
@@ -1949,7 +1949,7 @@ class SquareDiagonalTester(utt.InferShapeTester):
for
format
in
sparse
.
sparse_formats
:
for
format
in
sparse
.
sparse_formats
:
for
size
in
range
(
5
,
9
):
for
size
in
range
(
5
,
9
):
variable
=
[
tensor
.
vector
()]
variable
=
[
tensor
.
vector
()]
data
=
[
n
umpy
.
random
.
random
(
size
)
.
astype
(
config
.
floatX
)]
data
=
[
n
p
.
random
.
random
(
size
)
.
astype
(
config
.
floatX
)]
self
.
_compile_and_check
(
variable
,
self
.
_compile_and_check
(
variable
,
[
self
.
op
(
*
variable
)],
[
self
.
op
(
*
variable
)],
...
@@ -1960,7 +1960,7 @@ class SquareDiagonalTester(utt.InferShapeTester):
...
@@ -1960,7 +1960,7 @@ class SquareDiagonalTester(utt.InferShapeTester):
for
format
in
sparse
.
sparse_formats
:
for
format
in
sparse
.
sparse_formats
:
for
size
in
range
(
5
,
9
):
for
size
in
range
(
5
,
9
):
variable
=
[
tensor
.
vector
()]
variable
=
[
tensor
.
vector
()]
data
=
[
n
umpy
.
random
.
random
(
size
)
.
astype
(
config
.
floatX
)]
data
=
[
n
p
.
random
.
random
(
size
)
.
astype
(
config
.
floatX
)]
verify_grad_sparse
(
verify_grad_sparse
(
self
.
op
,
self
.
op
,
...
@@ -2091,7 +2091,7 @@ class Remove0Tester(utt.InferShapeTester):
...
@@ -2091,7 +2091,7 @@ class Remove0Tester(utt.InferShapeTester):
assert
target
.
has_sorted_indices
assert
target
.
has_sorted_indices
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
mat
=
(
n
umpy
.
arange
(
12
)
+
1
)
.
reshape
((
4
,
3
))
mat
=
(
n
p
.
arange
(
12
)
+
1
)
.
reshape
((
4
,
3
))
mat
[
0
,
1
]
=
mat
[
1
,
0
]
=
mat
[
2
,
2
]
=
0
mat
[
0
,
1
]
=
mat
[
1
,
0
]
=
mat
[
2
,
2
]
=
0
x_csc
=
theano
.
sparse
.
csc_matrix
(
dtype
=
theano
.
config
.
floatX
)
x_csc
=
theano
.
sparse
.
csc_matrix
(
dtype
=
theano
.
config
.
floatX
)
...
@@ -2109,7 +2109,7 @@ class Remove0Tester(utt.InferShapeTester):
...
@@ -2109,7 +2109,7 @@ class Remove0Tester(utt.InferShapeTester):
self
.
op_class
)
self
.
op_class
)
def
test_grad
(
self
):
def
test_grad
(
self
):
mat
=
(
n
umpy
.
arange
(
9
)
+
1
)
.
reshape
((
3
,
3
))
mat
=
(
n
p
.
arange
(
9
)
+
1
)
.
reshape
((
3
,
3
))
mat
[
0
,
1
]
=
mat
[
1
,
0
]
=
mat
[
2
,
2
]
=
0
mat
[
0
,
1
]
=
mat
[
1
,
0
]
=
mat
[
2
,
2
]
=
0
mat_csc
=
sp
.
csc_matrix
(
mat
,
dtype
=
theano
.
config
.
floatX
)
mat_csc
=
sp
.
csc_matrix
(
mat
,
dtype
=
theano
.
config
.
floatX
)
...
@@ -2121,7 +2121,7 @@ class Remove0Tester(utt.InferShapeTester):
...
@@ -2121,7 +2121,7 @@ class Remove0Tester(utt.InferShapeTester):
class
Test_getitem
(
unittest
.
TestCase
):
class
Test_getitem
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
self
.
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
def
test_GetItemList
(
self
):
def
test_GetItemList
(
self
):
...
@@ -2152,7 +2152,7 @@ class Test_getitem(unittest.TestCase):
...
@@ -2152,7 +2152,7 @@ class Test_getitem(unittest.TestCase):
def
test_get_item_list_grad
(
self
):
def
test_get_item_list_grad
(
self
):
op
=
theano
.
sparse
.
basic
.
GetItemList
()
op
=
theano
.
sparse
.
basic
.
GetItemList
()
def
op_with_fixed_index
(
x
):
def
op_with_fixed_index
(
x
):
return
op
(
x
,
index
=
n
umpy
.
asarray
([
0
,
1
]))
return
op
(
x
,
index
=
n
p
.
asarray
([
0
,
1
]))
x
,
x_val
=
sparse_random_inputs
(
"csr"
,
(
4
,
5
))
x
,
x_val
=
sparse_random_inputs
(
"csr"
,
(
4
,
5
))
...
@@ -2174,8 +2174,8 @@ class Test_getitem(unittest.TestCase):
...
@@ -2174,8 +2174,8 @@ class Test_getitem(unittest.TestCase):
t_geta
=
fa
(
A
[
0
])
t_geta
=
fa
(
A
[
0
])
t_getb
=
fb
(
B
[
0
])
t_getb
=
fb
(
B
[
0
])
s_geta
=
n
umpy
.
asarray
(
scipy
.
sparse
.
csr_matrix
(
A
[
0
])[[
0
,
0
,
1
,
3
],
[
0
,
1
,
2
,
4
]])
s_geta
=
n
p
.
asarray
(
scipy
.
sparse
.
csr_matrix
(
A
[
0
])[[
0
,
0
,
1
,
3
],
[
0
,
1
,
2
,
4
]])
s_getb
=
n
umpy
.
asarray
(
scipy
.
sparse
.
csc_matrix
(
B
[
0
])[[
0
,
0
,
1
,
3
],
[
0
,
1
,
2
,
4
]])
s_getb
=
n
p
.
asarray
(
scipy
.
sparse
.
csc_matrix
(
B
[
0
])[[
0
,
0
,
1
,
3
],
[
0
,
1
,
2
,
4
]])
utt
.
assert_allclose
(
t_geta
,
s_geta
)
utt
.
assert_allclose
(
t_geta
,
s_geta
)
utt
.
assert_allclose
(
t_getb
,
s_getb
)
utt
.
assert_allclose
(
t_getb
,
s_getb
)
...
@@ -2194,7 +2194,7 @@ class Test_getitem(unittest.TestCase):
...
@@ -2194,7 +2194,7 @@ class Test_getitem(unittest.TestCase):
def
test_get_item_2lists_grad
(
self
):
def
test_get_item_2lists_grad
(
self
):
op
=
theano
.
sparse
.
basic
.
GetItem2Lists
()
op
=
theano
.
sparse
.
basic
.
GetItem2Lists
()
def
op_with_fixed_index
(
x
):
def
op_with_fixed_index
(
x
):
return
op
(
x
,
ind1
=
n
umpy
.
asarray
([
0
,
1
]),
ind2
=
numpy
.
asarray
([
2
,
3
]))
return
op
(
x
,
ind1
=
n
p
.
asarray
([
0
,
1
]),
ind2
=
np
.
asarray
([
2
,
3
]))
x
,
x_val
=
sparse_random_inputs
(
"csr"
,
(
4
,
5
))
x
,
x_val
=
sparse_random_inputs
(
"csr"
,
(
4
,
5
))
...
@@ -2241,7 +2241,7 @@ class Test_getitem(unittest.TestCase):
...
@@ -2241,7 +2241,7 @@ class Test_getitem(unittest.TestCase):
r1
=
f1
(
vx
,
m
,
n
,
p
,
q
)
r1
=
f1
(
vx
,
m
,
n
,
p
,
q
)
t1
=
vx
[
m
:
n
,
p
:
q
]
t1
=
vx
[
m
:
n
,
p
:
q
]
assert
r1
.
shape
==
t1
.
shape
assert
r1
.
shape
==
t1
.
shape
assert
n
umpy
.
all
(
t1
.
toarray
()
==
r1
.
toarray
())
assert
n
p
.
all
(
t1
.
toarray
()
==
r1
.
toarray
())
"""
"""
Important: based on a discussion with both Fred and James
Important: based on a discussion with both Fred and James
...
@@ -2254,25 +2254,25 @@ class Test_getitem(unittest.TestCase):
...
@@ -2254,25 +2254,25 @@ class Test_getitem(unittest.TestCase):
r2 = f2(vx, m, n, p)
r2 = f2(vx, m, n, p)
t2 = vx[m:n, p]
t2 = vx[m:n, p]
assert r2.shape == t2.shape
assert r2.shape == t2.shape
assert n
umpy
.all(t2.toarray() == r2.toarray())
assert n
p
.all(t2.toarray() == r2.toarray())
f3 = theano.function([x, a, b, c], x[a, b:c])
f3 = theano.function([x, a, b, c], x[a, b:c])
r3 = f3(vx, m, n, p)
r3 = f3(vx, m, n, p)
t3 = vx[m, n:p]
t3 = vx[m, n:p]
assert r3.shape == t3.shape
assert r3.shape == t3.shape
assert n
umpy
.all(t3.toarray() == r3.toarray())
assert n
p
.all(t3.toarray() == r3.toarray())
f5 = theano.function([x], x[1:2,3])
f5 = theano.function([x], x[1:2,3])
r5 = f5(vx)
r5 = f5(vx)
t5 = vx[1:2, 3]
t5 = vx[1:2, 3]
assert r5.shape == t5.shape
assert r5.shape == t5.shape
assert n
umpy
.all(r5.toarray() == t5.toarray())
assert n
p
.all(r5.toarray() == t5.toarray())
f7 = theano.function([x], x[50])
f7 = theano.function([x], x[50])
r7 = f7(vx)
r7 = f7(vx)
t7 = vx[50]
t7 = vx[50]
assert r7.shape == t7.shape
assert r7.shape == t7.shape
assert n
umpy
.all(r7.toarray() == t7.toarray())
assert n
p
.all(r7.toarray() == t7.toarray())
"""
"""
if
is_supported_version
:
if
is_supported_version
:
f4
=
theano
.
function
([
x
,
a
,
b
,
e
],
x
[
a
:
b
:
e
])
f4
=
theano
.
function
([
x
,
a
,
b
,
e
],
x
[
a
:
b
:
e
])
...
@@ -2283,7 +2283,7 @@ class Test_getitem(unittest.TestCase):
...
@@ -2283,7 +2283,7 @@ class Test_getitem(unittest.TestCase):
r4
=
f4
(
vx
,
m
,
n
)
r4
=
f4
(
vx
,
m
,
n
)
t4
=
vx
[
m
:
n
]
t4
=
vx
[
m
:
n
]
assert
r4
.
shape
==
t4
.
shape
assert
r4
.
shape
==
t4
.
shape
assert
n
umpy
.
all
(
t4
.
toarray
()
==
r4
.
toarray
())
assert
n
p
.
all
(
t4
.
toarray
()
==
r4
.
toarray
())
#-----------------------------------------------------------
#-----------------------------------------------------------
# test cases using int indexing instead of theano variable
# test cases using int indexing instead of theano variable
...
@@ -2291,7 +2291,7 @@ class Test_getitem(unittest.TestCase):
...
@@ -2291,7 +2291,7 @@ class Test_getitem(unittest.TestCase):
r6
=
f6
(
vx
)
r6
=
f6
(
vx
)
t6
=
vx
[
1
:
10
:
j
,
10
:
20
:
k
]
t6
=
vx
[
1
:
10
:
j
,
10
:
20
:
k
]
assert
r6
.
shape
==
t6
.
shape
assert
r6
.
shape
==
t6
.
shape
assert
n
umpy
.
all
(
r6
.
toarray
()
==
t6
.
toarray
())
assert
n
p
.
all
(
r6
.
toarray
()
==
t6
.
toarray
())
#----------------------------------------------------------
#----------------------------------------------------------
# test cases with indexing both with theano variable and int
# test cases with indexing both with theano variable and int
...
@@ -2304,13 +2304,13 @@ class Test_getitem(unittest.TestCase):
...
@@ -2304,13 +2304,13 @@ class Test_getitem(unittest.TestCase):
r8
=
f8
(
vx
,
m
,
n
)
r8
=
f8
(
vx
,
m
,
n
)
t8
=
vx
[
m
:
n
,
10
:
20
]
t8
=
vx
[
m
:
n
,
10
:
20
]
assert
r8
.
shape
==
t8
.
shape
assert
r8
.
shape
==
t8
.
shape
assert
n
umpy
.
all
(
r8
.
toarray
()
==
t8
.
toarray
())
assert
n
p
.
all
(
r8
.
toarray
()
==
t8
.
toarray
())
f9
=
theano
.
function
([
x
,
a
,
b
],
x
[
1
:
a
:
j
,
1
:
b
:
k
])
f9
=
theano
.
function
([
x
,
a
,
b
],
x
[
1
:
a
:
j
,
1
:
b
:
k
])
r9
=
f9
(
vx
,
p
,
q
)
r9
=
f9
(
vx
,
p
,
q
)
t9
=
vx
[
1
:
p
:
j
,
1
:
q
:
k
]
t9
=
vx
[
1
:
p
:
j
,
1
:
q
:
k
]
assert
r9
.
shape
==
t9
.
shape
assert
r9
.
shape
==
t9
.
shape
assert
n
umpy
.
all
(
r9
.
toarray
()
==
t9
.
toarray
())
assert
n
p
.
all
(
r9
.
toarray
()
==
t9
.
toarray
())
#-----------------------------------------------------------
#-----------------------------------------------------------
# Test mixing None and variables
# Test mixing None and variables
...
@@ -2318,13 +2318,13 @@ class Test_getitem(unittest.TestCase):
...
@@ -2318,13 +2318,13 @@ class Test_getitem(unittest.TestCase):
r10
=
f10
(
vx
,
p
,
q
)
r10
=
f10
(
vx
,
p
,
q
)
t10
=
vx
[:
p
,
:
q
]
t10
=
vx
[:
p
,
:
q
]
assert
r10
.
shape
==
t10
.
shape
assert
r10
.
shape
==
t10
.
shape
assert
n
umpy
.
all
(
r10
.
toarray
()
==
t10
.
toarray
())
assert
n
p
.
all
(
r10
.
toarray
()
==
t10
.
toarray
())
f11
=
theano
.
function
([
x
,
a
],
x
[:,
a
:])
f11
=
theano
.
function
([
x
,
a
],
x
[:,
a
:])
r11
=
f11
(
vx
,
p
)
r11
=
f11
(
vx
,
p
)
t11
=
vx
[:,
p
:]
t11
=
vx
[:,
p
:]
assert
r11
.
shape
==
t11
.
shape
assert
r11
.
shape
==
t11
.
shape
assert
n
umpy
.
all
(
r11
.
toarray
()
==
t11
.
toarray
())
assert
n
p
.
all
(
r11
.
toarray
()
==
t11
.
toarray
())
# Test that is work with shared variable
# Test that is work with shared variable
sx
=
theano
.
shared
(
vx
)
sx
=
theano
.
shared
(
vx
)
...
@@ -2332,7 +2332,7 @@ class Test_getitem(unittest.TestCase):
...
@@ -2332,7 +2332,7 @@ class Test_getitem(unittest.TestCase):
r12
=
f12
(
p
)
r12
=
f12
(
p
)
t12
=
vx
[:,
p
:]
t12
=
vx
[:,
p
:]
assert
r12
.
shape
==
t12
.
shape
assert
r12
.
shape
==
t12
.
shape
assert
n
umpy
.
all
(
r12
.
toarray
()
==
t12
.
toarray
())
assert
n
p
.
all
(
r12
.
toarray
()
==
t12
.
toarray
())
#------------------------------------------------------------
#------------------------------------------------------------
# Invalid things
# Invalid things
...
@@ -2381,25 +2381,25 @@ class Test_getitem(unittest.TestCase):
...
@@ -2381,25 +2381,25 @@ class Test_getitem(unittest.TestCase):
r1
=
f1
(
vx
,
10
,
10
)
r1
=
f1
(
vx
,
10
,
10
)
t1
=
vx
[
10
,
10
]
t1
=
vx
[
10
,
10
]
assert
r1
.
shape
==
t1
.
shape
assert
r1
.
shape
==
t1
.
shape
assert
n
umpy
.
all
(
t1
==
r1
)
assert
n
p
.
all
(
t1
==
r1
)
f2
=
theano
.
function
([
x
,
a
],
x
[
50
,
a
])
f2
=
theano
.
function
([
x
,
a
],
x
[
50
,
a
])
r2
=
f2
(
vx
,
m
)
r2
=
f2
(
vx
,
m
)
t2
=
vx
[
50
,
m
]
t2
=
vx
[
50
,
m
]
assert
r2
.
shape
==
t2
.
shape
assert
r2
.
shape
==
t2
.
shape
assert
n
umpy
.
all
(
t2
==
r2
)
assert
n
p
.
all
(
t2
==
r2
)
f3
=
theano
.
function
([
x
,
a
],
x
[
a
,
50
])
f3
=
theano
.
function
([
x
,
a
],
x
[
a
,
50
])
r3
=
f3
(
vx
,
m
)
r3
=
f3
(
vx
,
m
)
t3
=
vx
[
m
,
50
]
t3
=
vx
[
m
,
50
]
assert
r3
.
shape
==
t3
.
shape
assert
r3
.
shape
==
t3
.
shape
assert
n
umpy
.
all
(
t3
==
r3
)
assert
n
p
.
all
(
t3
==
r3
)
f4
=
theano
.
function
([
x
],
x
[
50
,
42
])
f4
=
theano
.
function
([
x
],
x
[
50
,
42
])
r4
=
f4
(
vx
)
r4
=
f4
(
vx
)
t4
=
vx
[
m
,
n
]
t4
=
vx
[
m
,
n
]
assert
r3
.
shape
==
t3
.
shape
assert
r3
.
shape
==
t3
.
shape
assert
n
umpy
.
all
(
t4
==
r4
)
assert
n
p
.
all
(
t4
==
r4
)
# Test that is work with shared variable
# Test that is work with shared variable
sx
=
theano
.
shared
(
vx
)
sx
=
theano
.
shared
(
vx
)
...
@@ -2407,7 +2407,7 @@ class Test_getitem(unittest.TestCase):
...
@@ -2407,7 +2407,7 @@ class Test_getitem(unittest.TestCase):
r1
=
f1
(
10
,
10
)
r1
=
f1
(
10
,
10
)
t1
=
vx
[
10
,
10
]
t1
=
vx
[
10
,
10
]
assert
r1
.
shape
==
t1
.
shape
assert
r1
.
shape
==
t1
.
shape
assert
n
umpy
.
all
(
t1
==
r1
)
assert
n
p
.
all
(
t1
==
r1
)
class
CastTester
(
utt
.
InferShapeTester
):
class
CastTester
(
utt
.
InferShapeTester
):
...
@@ -2573,8 +2573,8 @@ class AddSSDataTester(utt.InferShapeTester):
...
@@ -2573,8 +2573,8 @@ class AddSSDataTester(utt.InferShapeTester):
for
format
in
sparse
.
sparse_formats
:
for
format
in
sparse
.
sparse_formats
:
variable
=
getattr
(
theano
.
sparse
,
format
+
'_matrix'
)
variable
=
getattr
(
theano
.
sparse
,
format
+
'_matrix'
)
rand
=
n
umpy
.
array
(
rand
=
n
p
.
array
(
n
umpy
.
random
.
randint
(
1
,
4
,
size
=
(
3
,
4
))
-
1
,
n
p
.
random
.
randint
(
1
,
4
,
size
=
(
3
,
4
))
-
1
,
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
constant
=
as_sparse_format
(
rand
,
format
)
constant
=
as_sparse_format
(
rand
,
format
)
...
@@ -2834,7 +2834,7 @@ def structure_function(f, index=0):
...
@@ -2834,7 +2834,7 @@ def structure_function(f, index=0):
StructuredSigmoidTester
=
elemwise_checker
(
StructuredSigmoidTester
=
elemwise_checker
(
sparse
.
structured_sigmoid
,
sparse
.
structured_sigmoid
,
structure_function
(
lambda
x
:
1.0
/
(
1.0
+
n
umpy
.
exp
(
-
x
))),
structure_function
(
lambda
x
:
1.0
/
(
1.0
+
n
p
.
exp
(
-
x
))),
test_dtypes
=
[
m
for
m
in
sparse
.
all_dtypes
test_dtypes
=
[
m
for
m
in
sparse
.
all_dtypes
if
(
not
m
in
sparse
.
complex_dtypes
and
if
(
not
m
in
sparse
.
complex_dtypes
and
not
m
.
startswith
(
'uint'
))],
not
m
.
startswith
(
'uint'
))],
...
@@ -2843,83 +2843,83 @@ StructuredSigmoidTester = elemwise_checker(
...
@@ -2843,83 +2843,83 @@ StructuredSigmoidTester = elemwise_checker(
StructuredExpTester
=
elemwise_checker
(
StructuredExpTester
=
elemwise_checker
(
sparse
.
structured_exp
,
sparse
.
structured_exp
,
structure_function
(
n
umpy
.
exp
),
structure_function
(
n
p
.
exp
),
name
=
'StructuredExpTester'
)
name
=
'StructuredExpTester'
)
StructuredLogTester
=
elemwise_checker
(
StructuredLogTester
=
elemwise_checker
(
sparse
.
structured_log
,
sparse
.
structured_log
,
structure_function
(
n
umpy
.
log
),
structure_function
(
n
p
.
log
),
gap
=
(
0.5
,
10
),
gap
=
(
0.5
,
10
),
name
=
'StructuredLogTester'
)
name
=
'StructuredLogTester'
)
StructuredPowTester
=
elemwise_checker
(
StructuredPowTester
=
elemwise_checker
(
lambda
x
:
sparse
.
structured_pow
(
x
,
2
),
lambda
x
:
sparse
.
structured_pow
(
x
,
2
),
structure_function
(
lambda
x
:
n
umpy
.
power
(
x
,
2
)),
structure_function
(
lambda
x
:
n
p
.
power
(
x
,
2
)),
name
=
'StructuredPowTester'
)
name
=
'StructuredPowTester'
)
StructuredMinimumTester
=
elemwise_checker
(
StructuredMinimumTester
=
elemwise_checker
(
lambda
x
:
structured_minimum
(
x
,
2
),
lambda
x
:
structured_minimum
(
x
,
2
),
structure_function
(
lambda
x
:
n
umpy
.
minimum
(
x
,
2
)),
structure_function
(
lambda
x
:
n
p
.
minimum
(
x
,
2
)),
name
=
'StructuredMinimumTester'
)
name
=
'StructuredMinimumTester'
)
StructuredMaximumTester
=
elemwise_checker
(
StructuredMaximumTester
=
elemwise_checker
(
lambda
x
:
structured_maximum
(
x
,
2
),
lambda
x
:
structured_maximum
(
x
,
2
),
structure_function
(
lambda
x
:
n
umpy
.
maximum
(
x
,
2
)),
structure_function
(
lambda
x
:
n
p
.
maximum
(
x
,
2
)),
name
=
'StructuredMaximumTester'
)
name
=
'StructuredMaximumTester'
)
StructuredAddTester
=
elemwise_checker
(
StructuredAddTester
=
elemwise_checker
(
lambda
x
:
structured_add
(
x
,
2
),
lambda
x
:
structured_add
(
x
,
2
),
structure_function
(
lambda
x
:
n
umpy
.
add
(
x
,
2
)),
structure_function
(
lambda
x
:
n
p
.
add
(
x
,
2
)),
name
=
'StructuredAddTester'
)
name
=
'StructuredAddTester'
)
SinTester
=
elemwise_checker
(
SinTester
=
elemwise_checker
(
sparse
.
sin
,
sparse
.
sin
,
n
umpy
.
sin
)
n
p
.
sin
)
TanTester
=
elemwise_checker
(
TanTester
=
elemwise_checker
(
sparse
.
tan
,
sparse
.
tan
,
n
umpy
.
tan
,
n
p
.
tan
,
gap
=
(
-
1
,
1
))
gap
=
(
-
1
,
1
))
ArcsinTester
=
elemwise_checker
(
ArcsinTester
=
elemwise_checker
(
sparse
.
arcsin
,
sparse
.
arcsin
,
n
umpy
.
arcsin
,
n
p
.
arcsin
,
gap
=
(
-
1
,
1
),
gap
=
(
-
1
,
1
),
gap_grad
=
(
-
0.99
,
0.99
))
gap_grad
=
(
-
0.99
,
0.99
))
ArctanTester
=
elemwise_checker
(
ArctanTester
=
elemwise_checker
(
sparse
.
arctan
,
sparse
.
arctan
,
n
umpy
.
arctan
)
n
p
.
arctan
)
SinhTester
=
elemwise_checker
(
SinhTester
=
elemwise_checker
(
sparse
.
sinh
,
sparse
.
sinh
,
n
umpy
.
sinh
)
n
p
.
sinh
)
ArcsinhTester
=
elemwise_checker
(
ArcsinhTester
=
elemwise_checker
(
sparse
.
arcsinh
,
sparse
.
arcsinh
,
n
umpy
.
arcsinh
,
n
p
.
arcsinh
,
gap
=
(
-
1
,
1
))
gap
=
(
-
1
,
1
))
TanhTester
=
elemwise_checker
(
TanhTester
=
elemwise_checker
(
sparse
.
tanh
,
sparse
.
tanh
,
n
umpy
.
tanh
,
n
p
.
tanh
,
gap
=
(
-
1
,
1
))
gap
=
(
-
1
,
1
))
ArctanhTester
=
elemwise_checker
(
ArctanhTester
=
elemwise_checker
(
sparse
.
arctanh
,
sparse
.
arctanh
,
n
umpy
.
arctanh
,
n
p
.
arctanh
,
gap
=
(
-
0.9
,
1
),
gap
=
(
-
0.9
,
1
),
gap_grad
=
(
-
0.9
,
0.95
))
gap_grad
=
(
-
0.9
,
0.95
))
RintTester
=
elemwise_checker
(
RintTester
=
elemwise_checker
(
sparse
.
rint
,
sparse
.
rint
,
n
umpy
.
rint
,
n
p
.
rint
,
grad_test
=
False
,
grad_test
=
False
,
test_dtypes
=
sparse
.
float_dtypes
)
test_dtypes
=
sparse
.
float_dtypes
)
SgnTester
=
elemwise_checker
(
SgnTester
=
elemwise_checker
(
sparse
.
sgn
,
sparse
.
sgn
,
n
umpy
.
sign
,
n
p
.
sign
,
grad_test
=
False
,
grad_test
=
False
,
test_dtypes
=
[
m
for
m
in
sparse
.
all_dtypes
test_dtypes
=
[
m
for
m
in
sparse
.
all_dtypes
if
(
not
m
in
sparse
.
complex_dtypes
and
if
(
not
m
in
sparse
.
complex_dtypes
and
...
@@ -2927,43 +2927,43 @@ SgnTester = elemwise_checker(
...
@@ -2927,43 +2927,43 @@ SgnTester = elemwise_checker(
CeilTester
=
elemwise_checker
(
CeilTester
=
elemwise_checker
(
sparse
.
ceil
,
sparse
.
ceil
,
n
umpy
.
ceil
,
n
p
.
ceil
,
grad_test
=
False
,
grad_test
=
False
,
test_dtypes
=
[
m
for
m
in
sparse
.
all_dtypes
test_dtypes
=
[
m
for
m
in
sparse
.
all_dtypes
if
not
m
in
sparse
.
complex_dtypes
])
if
not
m
in
sparse
.
complex_dtypes
])
FloorTester
=
elemwise_checker
(
FloorTester
=
elemwise_checker
(
sparse
.
floor
,
sparse
.
floor
,
n
umpy
.
floor
,
n
p
.
floor
,
grad_test
=
False
,
grad_test
=
False
,
test_dtypes
=
[
m
for
m
in
sparse
.
all_dtypes
test_dtypes
=
[
m
for
m
in
sparse
.
all_dtypes
if
not
m
in
sparse
.
complex_dtypes
])
if
not
m
in
sparse
.
complex_dtypes
])
Log1pTester
=
elemwise_checker
(
Log1pTester
=
elemwise_checker
(
sparse
.
log1p
,
sparse
.
log1p
,
n
umpy
.
log1p
,
n
p
.
log1p
,
gap
=
(
0.5
,
10
))
gap
=
(
0.5
,
10
))
Expm1Tester
=
elemwise_checker
(
Expm1Tester
=
elemwise_checker
(
sparse
.
expm1
,
sparse
.
expm1
,
n
umpy
.
expm1
)
n
p
.
expm1
)
Deg2radTester
=
elemwise_checker
(
Deg2radTester
=
elemwise_checker
(
sparse
.
deg2rad
,
sparse
.
deg2rad
,
n
umpy
.
deg2rad
,
n
p
.
deg2rad
,
test_dtypes
=
[
m
for
m
in
sparse
.
all_dtypes
test_dtypes
=
[
m
for
m
in
sparse
.
all_dtypes
if
not
m
in
sparse
.
complex_dtypes
])
if
not
m
in
sparse
.
complex_dtypes
])
Rad2degTester
=
elemwise_checker
(
Rad2degTester
=
elemwise_checker
(
sparse
.
rad2deg
,
sparse
.
rad2deg
,
n
umpy
.
rad2deg
,
n
p
.
rad2deg
,
test_dtypes
=
[
m
for
m
in
sparse
.
all_dtypes
test_dtypes
=
[
m
for
m
in
sparse
.
all_dtypes
if
not
m
in
sparse
.
complex_dtypes
])
if
not
m
in
sparse
.
complex_dtypes
])
TruncTester
=
elemwise_checker
(
TruncTester
=
elemwise_checker
(
sparse
.
trunc
,
sparse
.
trunc
,
n
umpy
.
trunc
,
n
p
.
trunc
,
test_dtypes
=
[
m
for
m
in
sparse
.
all_dtypes
test_dtypes
=
[
m
for
m
in
sparse
.
all_dtypes
if
not
m
in
sparse
.
complex_dtypes
])
if
not
m
in
sparse
.
complex_dtypes
])
...
@@ -2974,12 +2974,12 @@ SqrTester = elemwise_checker(
...
@@ -2974,12 +2974,12 @@ SqrTester = elemwise_checker(
SqrtTester
=
elemwise_checker
(
SqrtTester
=
elemwise_checker
(
sparse
.
sqrt
,
sparse
.
sqrt
,
n
umpy
.
sqrt
,
n
p
.
sqrt
,
gap
=
(
0
,
10
))
gap
=
(
0
,
10
))
ConjTester
=
elemwise_checker
(
ConjTester
=
elemwise_checker
(
sparse
.
conj
,
sparse
.
conj
,
n
umpy
.
conj
,
n
p
.
conj
,
grad_test
=
False
)
grad_test
=
False
)
...
@@ -2994,7 +2994,7 @@ class MulSVTester(unittest.TestCase):
...
@@ -2994,7 +2994,7 @@ class MulSVTester(unittest.TestCase):
for
format
in
[
'csr'
,
'csc'
]:
for
format
in
[
'csr'
,
'csc'
]:
for
dtype
in
[
'float32'
,
'float64'
]:
for
dtype
in
[
'float32'
,
'float64'
]:
spmat
=
sp_types
[
format
](
random_lil
((
4
,
3
),
dtype
,
3
))
spmat
=
sp_types
[
format
](
random_lil
((
4
,
3
),
dtype
,
3
))
mat
=
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
3
),
dtype
=
dtype
)
mat
=
n
p
.
asarray
(
np
.
random
.
rand
(
3
),
dtype
=
dtype
)
verify_grad_sparse
(
mul_s_v
,
verify_grad_sparse
(
mul_s_v
,
[
spmat
,
mat
],
[
spmat
,
mat
],
...
@@ -3011,7 +3011,7 @@ class MulSVTester(unittest.TestCase):
...
@@ -3011,7 +3011,7 @@ class MulSVTester(unittest.TestCase):
f
=
theano
.
function
([
x
,
y
],
mul_s_v
(
x
,
y
))
f
=
theano
.
function
([
x
,
y
],
mul_s_v
(
x
,
y
))
spmat
=
sp_types
[
format
](
random_lil
((
4
,
3
),
dtype
,
3
))
spmat
=
sp_types
[
format
](
random_lil
((
4
,
3
),
dtype
,
3
))
mat
=
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
3
),
dtype
=
dtype
)
mat
=
n
p
.
asarray
(
np
.
random
.
rand
(
3
),
dtype
=
dtype
)
out
=
f
(
spmat
,
mat
)
out
=
f
(
spmat
,
mat
)
...
@@ -3029,7 +3029,7 @@ class StructuredAddSVTester(unittest.TestCase):
...
@@ -3029,7 +3029,7 @@ class StructuredAddSVTester(unittest.TestCase):
for
format
in
[
'csr'
,
'csc'
]:
for
format
in
[
'csr'
,
'csc'
]:
for
dtype
in
[
'float32'
,
'float64'
]:
for
dtype
in
[
'float32'
,
'float64'
]:
spmat
=
sp_types
[
format
](
random_lil
((
4
,
3
),
dtype
,
3
))
spmat
=
sp_types
[
format
](
random_lil
((
4
,
3
),
dtype
,
3
))
mat
=
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
3
),
dtype
=
dtype
)
mat
=
n
p
.
asarray
(
np
.
random
.
rand
(
3
),
dtype
=
dtype
)
verify_grad_sparse
(
structured_add_s_v
,
verify_grad_sparse
(
structured_add_s_v
,
[
spmat
,
mat
],
[
spmat
,
mat
],
...
@@ -3047,8 +3047,8 @@ class StructuredAddSVTester(unittest.TestCase):
...
@@ -3047,8 +3047,8 @@ class StructuredAddSVTester(unittest.TestCase):
spmat
=
sp_types
[
format
](
random_lil
((
4
,
3
),
dtype
,
3
))
spmat
=
sp_types
[
format
](
random_lil
((
4
,
3
),
dtype
,
3
))
spones
=
spmat
.
copy
()
spones
=
spmat
.
copy
()
spones
.
data
=
n
umpy
.
ones_like
(
spones
.
data
)
spones
.
data
=
n
p
.
ones_like
(
spones
.
data
)
mat
=
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
3
),
dtype
=
dtype
)
mat
=
n
p
.
asarray
(
np
.
random
.
rand
(
3
),
dtype
=
dtype
)
out
=
f
(
spmat
,
mat
)
out
=
f
(
spmat
,
mat
)
...
@@ -3076,7 +3076,7 @@ class TrueDotTester(utt.InferShapeTester):
...
@@ -3076,7 +3076,7 @@ class TrueDotTester(utt.InferShapeTester):
tested
=
f
(
*
data
)
tested
=
f
(
*
data
)
x
,
y
=
[
m
.
toarray
()
for
m
in
data
]
x
,
y
=
[
m
.
toarray
()
for
m
in
data
]
expected
=
n
umpy
.
dot
(
x
,
y
)
expected
=
n
p
.
dot
(
x
,
y
)
assert
tested
.
format
==
format
assert
tested
.
format
==
format
assert
tested
.
dtype
==
expected
.
dtype
assert
tested
.
dtype
==
expected
.
dtype
...
@@ -3098,7 +3098,7 @@ class TrueDotTester(utt.InferShapeTester):
...
@@ -3098,7 +3098,7 @@ class TrueDotTester(utt.InferShapeTester):
f
=
theano
.
function
(
variable
,
self
.
op
(
*
variable
))
f
=
theano
.
function
(
variable
,
self
.
op
(
*
variable
))
tested
=
f
(
*
data
)
tested
=
f
(
*
data
)
expected
=
n
umpy
.
dot
(
data
[
0
]
.
toarray
(),
data
[
1
])
expected
=
n
p
.
dot
(
data
[
0
]
.
toarray
(),
data
[
1
])
assert
tested
.
format
==
format
assert
tested
.
format
==
format
assert
tested
.
dtype
==
expected
.
dtype
assert
tested
.
dtype
==
expected
.
dtype
...
@@ -3146,11 +3146,11 @@ class SamplingDotTester(utt.InferShapeTester):
...
@@ -3146,11 +3146,11 @@ class SamplingDotTester(utt.InferShapeTester):
x
=
[
tensor
.
matrix
()
for
t
in
range
(
2
)]
x
=
[
tensor
.
matrix
()
for
t
in
range
(
2
)]
x
.
append
(
sparse
.
csr_matrix
())
x
.
append
(
sparse
.
csr_matrix
())
# unsquare shape
# unsquare shape
a
=
[
n
umpy
.
array
(
numpy
.
random
.
randint
(
1
,
6
,
size
=
(
4
,
3
))
-
1
,
a
=
[
n
p
.
array
(
np
.
random
.
randint
(
1
,
6
,
size
=
(
4
,
3
))
-
1
,
dtype
=
theano
.
config
.
floatX
),
dtype
=
theano
.
config
.
floatX
),
n
umpy
.
array
(
numpy
.
random
.
randint
(
1
,
6
,
size
=
(
5
,
3
))
-
1
,
n
p
.
array
(
np
.
random
.
randint
(
1
,
6
,
size
=
(
5
,
3
))
-
1
,
dtype
=
theano
.
config
.
floatX
),
dtype
=
theano
.
config
.
floatX
),
n
umpy
.
array
(
numpy
.
random
.
randint
(
1
,
3
,
size
=
(
4
,
5
))
-
1
,
n
p
.
array
(
np
.
random
.
randint
(
1
,
3
,
size
=
(
4
,
5
))
-
1
,
dtype
=
theano
.
config
.
floatX
)
dtype
=
theano
.
config
.
floatX
)
]
]
a
[
2
]
=
sp
.
csr_matrix
(
a
[
2
])
a
[
2
]
=
sp
.
csr_matrix
(
a
[
2
])
...
@@ -3166,7 +3166,7 @@ class SamplingDotTester(utt.InferShapeTester):
...
@@ -3166,7 +3166,7 @@ class SamplingDotTester(utt.InferShapeTester):
tested
=
f
(
*
self
.
a
)
tested
=
f
(
*
self
.
a
)
x
,
y
,
p
=
self
.
a
x
,
y
,
p
=
self
.
a
expected
=
p
.
multiply
(
n
umpy
.
dot
(
x
,
y
.
T
))
expected
=
p
.
multiply
(
n
p
.
dot
(
x
,
y
.
T
))
utt
.
assert_allclose
(
as_ndarray
(
expected
),
tested
.
toarray
())
utt
.
assert_allclose
(
as_ndarray
(
expected
),
tested
.
toarray
())
assert
tested
.
format
==
'csr'
assert
tested
.
format
==
'csr'
...
@@ -3198,7 +3198,7 @@ test_shared_options = theano.tensor.tests.test_sharedvar.makeSharedTester(
...
@@ -3198,7 +3198,7 @@ test_shared_options = theano.tensor.tests.test_sharedvar.makeSharedTester(
internal_type_
=
scipy
.
sparse
.
csc_matrix
,
internal_type_
=
scipy
.
sparse
.
csc_matrix
,
test_internal_type_
=
scipy
.
sparse
.
issparse
,
test_internal_type_
=
scipy
.
sparse
.
issparse
,
theano_fct_
=
lambda
a
:
dense_from_sparse
(
a
*
2.
),
theano_fct_
=
lambda
a
:
dense_from_sparse
(
a
*
2.
),
ref_fct_
=
lambda
a
:
n
umpy
.
asarray
((
a
*
2
)
.
todense
()),
ref_fct_
=
lambda
a
:
n
p
.
asarray
((
a
*
2
)
.
todense
()),
cast_value_
=
scipy
.
sparse
.
csr_matrix
,
cast_value_
=
scipy
.
sparse
.
csr_matrix
,
name
=
'test_shared_options'
,
name
=
'test_shared_options'
,
)
)
...
...
theano/sparse/tests/test_opt.py
浏览文件 @
2b7ee2ec
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
import
numpy
import
numpy
as
np
try
:
try
:
import
scipy.sparse
as
sp
import
scipy.sparse
as
sp
import
scipy.sparse
import
scipy.sparse
...
@@ -157,14 +157,14 @@ def test_local_dense_from_sparse_sparse_from_dense():
...
@@ -157,14 +157,14 @@ def test_local_dense_from_sparse_sparse_from_dense():
def
test_sd_csc
():
def
test_sd_csc
():
A
=
sp
.
rand
(
4
,
5
,
density
=
0.60
,
format
=
'csc'
,
dtype
=
n
umpy
.
float32
)
A
=
sp
.
rand
(
4
,
5
,
density
=
0.60
,
format
=
'csc'
,
dtype
=
n
p
.
float32
)
b
=
n
umpy
.
random
.
rand
(
5
,
2
)
.
astype
(
numpy
.
float32
)
b
=
n
p
.
random
.
rand
(
5
,
2
)
.
astype
(
np
.
float32
)
target
=
A
*
b
target
=
A
*
b
a_val
=
theano
.
tensor
.
as_tensor_variable
(
A
.
data
)
a_val
=
theano
.
tensor
.
as_tensor_variable
(
A
.
data
)
a_ind
=
theano
.
tensor
.
as_tensor_variable
(
A
.
indices
)
a_ind
=
theano
.
tensor
.
as_tensor_variable
(
A
.
indices
)
a_ptr
=
theano
.
tensor
.
as_tensor_variable
(
A
.
indptr
)
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
)
b
=
theano
.
tensor
.
as_tensor_variable
(
b
)
res
=
theano
.
sparse
.
opt
.
sd_csc
(
a_val
,
a_ind
,
a_ptr
,
nrows
,
b
)
.
eval
()
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
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
import
unittest
import
unittest
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
import
numpy
import
numpy
as
np
try
:
try
:
import
scipy.sparse
as
sp
import
scipy.sparse
as
sp
except
ImportError
:
except
ImportError
:
...
@@ -30,7 +30,7 @@ class PoissonTester(utt.InferShapeTester):
...
@@ -30,7 +30,7 @@ class PoissonTester(utt.InferShapeTester):
for
format
in
sparse
.
sparse_formats
:
for
format
in
sparse
.
sparse_formats
:
variable
=
getattr
(
theano
.
sparse
,
format
+
'_matrix'
)
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
)
dtype
=
theano
.
config
.
floatX
)
x
[
format
]
=
variable
()
x
[
format
]
=
variable
()
...
@@ -50,7 +50,7 @@ class PoissonTester(utt.InferShapeTester):
...
@@ -50,7 +50,7 @@ class PoissonTester(utt.InferShapeTester):
assert
tested
.
format
==
format
assert
tested
.
format
==
format
assert
tested
.
dtype
==
self
.
a
[
format
]
.
dtype
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
assert
tested
.
shape
==
self
.
a
[
format
]
.
shape
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
...
@@ -67,7 +67,7 @@ class BinomialTester(utt.InferShapeTester):
...
@@ -67,7 +67,7 @@ class BinomialTester(utt.InferShapeTester):
shape
=
tensor
.
lvector
()
shape
=
tensor
.
lvector
()
_n
=
5
_n
=
5
_p
=
.
25
_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
]
_inputs
=
[
_n
,
_p
,
_shape
]
_inputs
=
[
_n
,
_p
,
_shape
]
...
@@ -88,7 +88,7 @@ class BinomialTester(utt.InferShapeTester):
...
@@ -88,7 +88,7 @@ class BinomialTester(utt.InferShapeTester):
assert
tested
.
shape
==
tuple
(
self
.
_shape
)
assert
tested
.
shape
==
tuple
(
self
.
_shape
)
assert
tested
.
format
==
sp_format
assert
tested
.
format
==
sp_format
assert
tested
.
dtype
==
o_type
assert
tested
.
dtype
==
o_type
assert
n
umpy
.
allclose
(
numpy
.
floor
(
tested
.
todense
()),
assert
n
p
.
allclose
(
np
.
floor
(
tested
.
todense
()),
tested
.
todense
())
tested
.
todense
())
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
...
@@ -103,7 +103,7 @@ class BinomialTester(utt.InferShapeTester):
...
@@ -103,7 +103,7 @@ class BinomialTester(utt.InferShapeTester):
class
MultinomialTester
(
utt
.
InferShapeTester
):
class
MultinomialTester
(
utt
.
InferShapeTester
):
p
=
sparse
.
csr_matrix
()
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.1
,
0.2
,
0.3
,
0.4
],
[
0.0
,
1.0
,
0.0
,
0.0
],
[
0.0
,
1.0
,
0.0
,
0.0
],
[
0.3
,
0.3
,
0.0
,
0.4
]],
[
0.3
,
0.3
,
0.0
,
0.4
]],
...
@@ -120,16 +120,16 @@ class MultinomialTester(utt.InferShapeTester):
...
@@ -120,16 +120,16 @@ class MultinomialTester(utt.InferShapeTester):
_n
=
5
_n
=
5
tested
=
f
(
self
.
_p
,
_n
)
tested
=
f
(
self
.
_p
,
_n
)
assert
tested
.
shape
==
self
.
_p
.
shape
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
assert
tested
[
2
,
1
]
==
_n
n
=
tensor
.
lvector
()
n
=
tensor
.
lvector
()
f
=
theano
.
function
([
self
.
p
,
n
],
multinomial
(
n
,
self
.
p
))
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
)
tested
=
f
(
self
.
_p
,
_n
)
assert
tested
.
shape
==
self
.
_p
.
shape
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
]
assert
tested
[
2
,
1
]
==
_n
[
2
]
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
...
...
theano/sparse/tests/test_utils.py
浏览文件 @
2b7ee2ec
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
import
numpy
import
numpy
as
np
import
theano.sparse
import
theano.sparse
if
not
theano
.
sparse
.
enable_sparse
:
if
not
theano
.
sparse
.
enable_sparse
:
raise
SkipTest
(
'Optional package sparse disabled'
)
raise
SkipTest
(
'Optional package sparse disabled'
)
...
@@ -11,21 +11,21 @@ from theano.sparse.tests.test_basic import as_sparse_format
...
@@ -11,21 +11,21 @@ from theano.sparse.tests.test_basic import as_sparse_format
def
test_hash_from_sparse
():
def
test_hash_from_sparse
():
hashs
=
[]
hashs
=
[]
rng
=
n
umpy
.
random
.
rand
(
5
,
5
)
rng
=
n
p
.
random
.
rand
(
5
,
5
)
for
format
in
[
'csc'
,
'csr'
]:
for
format
in
[
'csc'
,
'csr'
]:
rng
=
as_sparse_format
(
rng
,
format
)
rng
=
as_sparse_format
(
rng
,
format
)
for
data
in
[[[
-
2
]],
[[
-
1
]],
[[
0
]],
[[
1
]],
[[
2
]],
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
# 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
# Same data buffer and shapes but different strides
n
umpy
.
arange
(
25
)
.
reshape
(
5
,
5
),
n
p
.
arange
(
25
)
.
reshape
(
5
,
5
),
n
umpy
.
arange
(
25
)
.
reshape
(
5
,
5
)
.
T
,
n
p
.
arange
(
25
)
.
reshape
(
5
,
5
)
.
T
,
# Same data buffer, shapes and strides
# Same data buffer, shapes and strides
# but different dtypes
# but different dtypes
n
umpy
.
zeros
((
5
,
5
),
dtype
=
"uint32"
),
n
p
.
zeros
((
5
,
5
),
dtype
=
"uint32"
),
n
umpy
.
zeros
((
5
,
5
),
dtype
=
"int32"
),
n
p
.
zeros
((
5
,
5
),
dtype
=
"int32"
),
# Test slice
# Test slice
rng
,
rng
[
1
:],
rng
[:
4
],
rng
[
1
:
3
],
rng
,
rng
[
1
:],
rng
[:
4
],
rng
[
1
:
3
],
# Don't test step as they are not supported by sparse
# Don't test step as they are not supported by sparse
...
...
theano/sparse/type.py
浏览文件 @
2b7ee2ec
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
try
:
try
:
import
scipy.sparse
import
scipy.sparse
imported_scipy
=
True
imported_scipy
=
True
...
@@ -20,7 +20,7 @@ def _is_sparse(x):
...
@@ -20,7 +20,7 @@ def _is_sparse(x):
True iff x is a L{scipy.sparse.spmatrix} (and not a L{numpy.ndarray}).
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 "
raise
NotImplementedError
(
"this function should only be called on "
"sparse.scipy.sparse.spmatrix or "
"sparse.scipy.sparse.spmatrix or "
"numpy.ndarray, not,"
,
x
)
"numpy.ndarray, not,"
,
x
)
...
@@ -107,12 +107,12 @@ class SparseType(gof.Type):
...
@@ -107,12 +107,12 @@ class SparseType(gof.Type):
return
(
SparseType
.
may_share_memory
(
a
,
b
.
data
)
or
return
(
SparseType
.
may_share_memory
(
a
,
b
.
data
)
or
SparseType
.
may_share_memory
(
a
,
b
.
indices
)
or
SparseType
.
may_share_memory
(
a
,
b
.
indices
)
or
SparseType
.
may_share_memory
(
a
,
b
.
indptr
))
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
a
,
b
=
b
,
a
if
_is_sparse
(
a
)
and
isinstance
(
b
,
n
umpy
.
ndarray
):
if
_is_sparse
(
a
)
and
isinstance
(
b
,
n
p
.
ndarray
):
if
(
n
umpy
.
may_share_memory
(
a
.
data
,
b
)
or
if
(
n
p
.
may_share_memory
(
a
.
data
,
b
)
or
n
umpy
.
may_share_memory
(
a
.
indices
,
b
)
or
n
p
.
may_share_memory
(
a
.
indices
,
b
)
or
n
umpy
.
may_share_memory
(
a
.
indptr
,
b
)):
n
p
.
may_share_memory
(
a
.
indptr
,
b
)):
# currently we can't share memory with a.shape as it is a tuple
# currently we can't share memory with a.shape as it is a tuple
return
True
return
True
return
False
return
False
...
@@ -168,8 +168,8 @@ class SparseType(gof.Type):
...
@@ -168,8 +168,8 @@ class SparseType(gof.Type):
obj
.
indices
.
size
,
obj
.
indptr
.
size
,
obj
.
nnz
)
obj
.
indices
.
size
,
obj
.
indptr
.
size
,
obj
.
nnz
)
def
get_size
(
self
,
shape_info
):
def
get_size
(
self
,
shape_info
):
return
(
shape_info
[
1
]
*
n
umpy
.
dtype
(
self
.
dtype
)
.
itemsize
+
return
(
shape_info
[
1
]
*
n
p
.
dtype
(
self
.
dtype
)
.
itemsize
+
(
shape_info
[
2
]
+
shape_info
[
3
])
*
n
umpy
.
dtype
(
'int32'
)
.
itemsize
)
(
shape_info
[
2
]
+
shape_info
[
3
])
*
n
p
.
dtype
(
'int32'
)
.
itemsize
)
# Register SparseType's C code for ViewOp.
# Register SparseType's C code for ViewOp.
theano
.
compile
.
register_view_op_c_code
(
theano
.
compile
.
register_view_op_c_code
(
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论