Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f06f537a
提交
f06f537a
authored
11月 07, 2016
作者:
kvmanohar22
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modified numpy imports to one common form
上级
1db72747
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
37 行增加
和
37 行删除
+37
-37
basic.py
theano/sparse/basic.py
+26
-26
opt.py
theano/sparse/opt.py
+2
-2
type.py
theano/sparse/type.py
+9
-9
没有找到文件。
theano/sparse/basic.py
浏览文件 @
f06f537a
...
...
@@ -12,7 +12,7 @@ from __future__ import absolute_import, print_function, division
import
sys
import
numpy
import
numpy
as
np
from
numpy.lib.stride_tricks
import
as_strided
from
six
import
integer_types
from
six.moves
import
xrange
...
...
@@ -86,11 +86,11 @@ def _is_dense(x):
L{numpy.ndarray}).
"""
if
not
isinstance
(
x
,
(
scipy
.
sparse
.
spmatrix
,
n
umpy
.
ndarray
)):
if
not
isinstance
(
x
,
(
scipy
.
sparse
.
spmatrix
,
n
p
.
ndarray
)):
raise
NotImplementedError
(
"this function should only be called on "
"sparse.scipy.sparse.spmatrix or "
"numpy.ndarray, not,"
,
x
)
return
isinstance
(
x
,
n
umpy
.
ndarray
)
return
isinstance
(
x
,
n
p
.
ndarray
)
# Wrapper type
...
...
@@ -205,8 +205,8 @@ def sp_zeros_like(x):
# TODO: don't restrict to CSM formats
_
,
_
,
indptr
,
shape
=
csm_properties
(
x
)
return
CSM
(
format
=
x
.
format
)(
data
=
n
umpy
.
array
([],
dtype
=
x
.
type
.
dtype
),
indices
=
n
umpy
.
array
([],
dtype
=
'int32'
),
return
CSM
(
format
=
x
.
format
)(
data
=
n
p
.
array
([],
dtype
=
x
.
type
.
dtype
),
indices
=
n
p
.
array
([],
dtype
=
'int32'
),
indptr
=
tensor
.
zeros_like
(
indptr
),
shape
=
shape
)
...
...
@@ -293,9 +293,9 @@ class _sparse_py_operators:
args
=
args
,
if
len
(
args
)
==
2
:
scalar_arg_1
=
(
n
umpy
.
isscalar
(
args
[
0
])
or
scalar_arg_1
=
(
n
p
.
isscalar
(
args
[
0
])
or
getattr
(
args
[
0
],
'type'
,
None
)
==
tensor
.
iscalar
)
scalar_arg_2
=
(
n
umpy
.
isscalar
(
args
[
1
])
or
scalar_arg_2
=
(
n
p
.
isscalar
(
args
[
1
])
or
getattr
(
args
[
1
],
'type'
,
None
)
==
tensor
.
iscalar
)
if
scalar_arg_1
and
scalar_arg_2
:
ret
=
get_item_scalar
(
self
,
args
)
...
...
@@ -554,17 +554,17 @@ class CSM(gof.Op):
data
=
tensor
.
as_tensor_variable
(
data
)
if
not
isinstance
(
indices
,
gof
.
Variable
):
indices_
=
n
umpy
.
asarray
(
indices
)
indices_
=
n
p
.
asarray
(
indices
)
indices_32
=
theano
.
_asarray
(
indices
,
dtype
=
'int32'
)
assert
(
indices_
==
indices_32
)
.
all
()
indices
=
indices_32
if
not
isinstance
(
indptr
,
gof
.
Variable
):
indptr_
=
n
umpy
.
asarray
(
indptr
)
indptr_
=
n
p
.
asarray
(
indptr
)
indptr_32
=
theano
.
_asarray
(
indptr
,
dtype
=
'int32'
)
assert
(
indptr_
==
indptr_32
)
.
all
()
indptr
=
indptr_32
if
not
isinstance
(
shape
,
gof
.
Variable
):
shape_
=
n
umpy
.
asarray
(
shape
)
shape_
=
n
p
.
asarray
(
shape
)
shape_32
=
theano
.
_asarray
(
shape
,
dtype
=
'int32'
)
assert
(
shape_
==
shape_32
)
.
all
()
shape
=
shape_32
...
...
@@ -606,7 +606,7 @@ class CSM(gof.Op):
if
self
.
format
==
'csc'
:
out
[
0
]
=
scipy
.
sparse
.
csc_matrix
((
data
,
indices
.
copy
(),
indptr
.
copy
()),
n
umpy
.
asarray
(
shape
),
copy
=
False
)
n
p
.
asarray
(
shape
),
copy
=
False
)
else
:
assert
self
.
format
==
'csr'
out
[
0
]
=
scipy
.
sparse
.
csr_matrix
((
data
,
indices
.
copy
(),
...
...
@@ -729,8 +729,8 @@ class CSMGrad(gof.op.Op):
else
:
sp_dim
=
x_shape
[
0
]
g_row
=
n
umpy
.
zeros
(
sp_dim
,
dtype
=
g_data
.
dtype
)
gout_data
=
n
umpy
.
zeros
(
x_data
.
shape
,
dtype
=
node
.
outputs
[
0
]
.
dtype
)
g_row
=
n
p
.
zeros
(
sp_dim
,
dtype
=
g_data
.
dtype
)
gout_data
=
n
p
.
zeros
(
x_data
.
shape
,
dtype
=
node
.
outputs
[
0
]
.
dtype
)
for
i
in
range
(
len
(
x_indptr
)
-
1
):
for
j_ptr
in
range
(
g_indptr
[
i
],
g_indptr
[
i
+
1
]):
...
...
@@ -1100,7 +1100,7 @@ class GetItem2Lists(gof.op.Op):
x
=
inp
[
0
]
ind1
=
inp
[
1
]
ind2
=
inp
[
2
]
out
[
0
]
=
n
umpy
.
asarray
(
x
[
ind1
,
ind2
])
.
flatten
()
out
[
0
]
=
n
p
.
asarray
(
x
[
ind1
,
ind2
])
.
flatten
()
"""
Here scipy returns the corresponding elements in a matrix which isn't
what we are aiming for. Using asarray and flatten, out[0] becomes an
...
...
@@ -1244,7 +1244,7 @@ class GetItem2d(gof.op.Op):
elif
((
isinstance
(
ind
,
gof
.
Variable
)
and
getattr
(
ind
,
'ndim'
,
-
1
)
==
0
)
or
n
umpy
.
isscalar
(
ind
)):
n
p
.
isscalar
(
ind
)):
raise
NotImplementedError
(
'Theano has no sparse vector'
+
'Use X[a:b, c:d], X[a:b, c:c+1] or X[a:b] instead.'
)
...
...
@@ -1653,9 +1653,9 @@ class SpSum(gof.op.Op):
(
x
,)
=
inputs
(
z
,)
=
outputs
if
self
.
axis
is
None
:
z
[
0
]
=
n
umpy
.
asarray
(
x
.
sum
())
z
[
0
]
=
n
p
.
asarray
(
x
.
sum
())
else
:
z
[
0
]
=
n
umpy
.
asarray
(
x
.
sum
(
self
.
axis
))
.
ravel
()
z
[
0
]
=
n
p
.
asarray
(
x
.
sum
(
self
.
axis
))
.
ravel
()
def
grad
(
self
,
inputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -2540,7 +2540,7 @@ class __ComparisonOpSD(gof.op.Op):
assert
x
.
shape
==
y
.
shape
assert
_is_dense
(
y
)
o
=
self
.
comparison
(
x
,
y
)
.
astype
(
'uint8'
)
o
=
n
umpy
.
asarray
(
o
)
o
=
n
p
.
asarray
(
o
)
out
[
0
]
=
o
def
infer_shape
(
self
,
node
,
ins_shapes
):
...
...
@@ -3382,7 +3382,7 @@ class TrueDot(gof.op.Op):
# 'ushort', 'intc', 'uintc', 'longlong', 'ulonglong', 'single',
# 'double', 'longdouble', 'csingle', 'cdouble', 'clongdouble']
# But ulonglong is uint64 on x86-64, but with a different typenum!
if
rval
.
dtype
.
num
!=
n
umpy
.
dtype
(
str
(
rval
.
dtype
))
.
num
:
if
rval
.
dtype
.
num
!=
n
p
.
dtype
(
str
(
rval
.
dtype
))
.
num
:
assert
str
(
rval
.
dtype
)
==
node
.
outputs
[
0
]
.
dtype
# Create a view with the expected typenum.
format
=
node
.
outputs
[
0
]
.
type
.
format
...
...
@@ -3509,7 +3509,7 @@ class StructuredDot(gof.Op):
# dot of an NxM sparse matrix, with a Mx1 dense matrix, returns vector
# not matrix
if
variable
.
ndim
==
1
:
variable
=
n
umpy
.
expand_dims
(
variable
,
1
)
variable
=
n
p
.
expand_dims
(
variable
,
1
)
elif
variable
.
ndim
!=
2
:
raise
Exception
(
'Output of structured dot should be a matrix '
'(ndim=2)'
)
...
...
@@ -3622,7 +3622,7 @@ class StructuredDotGradCSC(gof.Op):
def
perform
(
self
,
node
,
inputs
,
outputs
):
(
a_indices
,
a_indptr
,
b
,
g_ab
)
=
inputs
(
out
,)
=
outputs
g_a_data
=
n
umpy
.
zeros
(
a_indices
.
shape
,
dtype
=
g_ab
.
dtype
)
g_a_data
=
n
p
.
zeros
(
a_indices
.
shape
,
dtype
=
g_ab
.
dtype
)
for
j
in
xrange
(
len
(
a_indptr
)
-
1
):
ind0
=
a_indptr
[
j
]
ind1
=
a_indptr
[
j
+
1
]
...
...
@@ -3631,7 +3631,7 @@ class StructuredDotGradCSC(gof.Op):
# Depending on the type of g_ab and b (sparse or dense),
# the following dot product can result in a scalar or
# a (1, 1) sparse matrix.
dot_val
=
n
umpy
.
dot
(
g_ab
[
i
],
b
[
j
]
.
T
)
dot_val
=
n
p
.
dot
(
g_ab
[
i
],
b
[
j
]
.
T
)
if
isinstance
(
dot_val
,
scipy
.
sparse
.
spmatrix
):
dot_val
=
dot_val
[
0
,
0
]
g_a_data
[
i_idx
]
=
dot_val
...
...
@@ -3752,7 +3752,7 @@ class StructuredDotGradCSR(gof.Op):
def
perform
(
self
,
node
,
inputs
,
outputs
):
(
a_indices
,
a_indptr
,
b
,
g_ab
)
=
inputs
(
out
,)
=
outputs
g_a_data
=
n
umpy
.
zeros
(
a_indices
.
shape
,
dtype
=
g_ab
.
dtype
)
g_a_data
=
n
p
.
zeros
(
a_indices
.
shape
,
dtype
=
g_ab
.
dtype
)
for
i
in
xrange
(
len
(
a_indptr
)
-
1
):
# loop over rows
ind0
=
a_indptr
[
i
]
ind1
=
a_indptr
[
i
+
1
]
...
...
@@ -3763,7 +3763,7 @@ class StructuredDotGradCSR(gof.Op):
# Depending on the type of g_ab and b (sparse or dense),
# the following dot product can result in a scalar or
# a (1, 1) sparse matrix.
dot_val
=
n
umpy
.
dot
(
g_ab
[
i
],
b
[
j
]
.
T
)
dot_val
=
n
p
.
dot
(
g_ab
[
i
],
b
[
j
]
.
T
)
if
isinstance
(
dot_val
,
scipy
.
sparse
.
spmatrix
):
dot_val
=
dot_val
[
0
,
0
]
g_a_data
[
j_idx
]
=
dot_val
...
...
@@ -3910,7 +3910,7 @@ class SamplingDot(gof.op.Op):
if
not
_is_sparse
(
p
):
raise
TypeError
(
p
)
out
[
0
]
=
p
.
__class__
(
p
.
multiply
(
n
umpy
.
dot
(
x
,
y
.
T
)))
out
[
0
]
=
p
.
__class__
(
p
.
multiply
(
n
p
.
dot
(
x
,
y
.
T
)))
def
grad
(
self
,
inputs
,
gout
):
(
x
,
y
,
p
)
=
inputs
...
...
@@ -4243,7 +4243,7 @@ class ConstructSparseFromList(gof.Op):
out
,
=
out_
rows
,
cols
=
values
.
shape
assert
rows
==
len
(
ilist
)
indptr
=
n
umpy
.
arange
(
cols
+
1
)
*
rows
indptr
=
n
p
.
arange
(
cols
+
1
)
*
rows
indices
=
as_strided
(
ilist
,
strides
=
(
0
,
ilist
.
strides
[
0
]),
shape
=
(
cols
,
ilist
.
shape
[
0
]))
.
flatten
()
...
...
theano/sparse/opt.py
浏览文件 @
f06f537a
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
scipy
import
theano
...
...
@@ -879,7 +879,7 @@ local_usmm = gof.opt.PatternSub(
(
theano
.
tensor
.
sub
,
'z'
,
(
theano
.
tensor
.
mul
,
{
'pattern'
:
'alpha'
,
'constraint'
:
lambda
expr
:
(
n
umpy
.
all
(
expr
.
type
.
broadcastable
)
and
'constraint'
:
lambda
expr
:
(
n
p
.
all
(
expr
.
type
.
broadcastable
)
and
theano
.
config
.
blas
.
ldflags
)},
(
sparse
.
_dot
,
'x'
,
'y'
))),
(
usmm
,
(
theano
.
tensor
.
neg
,
'alpha'
),
'x'
,
'y'
,
'z'
))
...
...
theano/sparse/type.py
浏览文件 @
f06f537a
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
try
:
import
scipy.sparse
imported_scipy
=
True
...
...
@@ -20,7 +20,7 @@ def _is_sparse(x):
True iff x is a L{scipy.sparse.spmatrix} (and not a L{numpy.ndarray}).
"""
if
not
isinstance
(
x
,
(
scipy
.
sparse
.
spmatrix
,
n
umpy
.
ndarray
,
tuple
,
list
)):
if
not
isinstance
(
x
,
(
scipy
.
sparse
.
spmatrix
,
n
p
.
ndarray
,
tuple
,
list
)):
raise
NotImplementedError
(
"this function should only be called on "
"sparse.scipy.sparse.spmatrix or "
"numpy.ndarray, not,"
,
x
)
...
...
@@ -107,12 +107,12 @@ class SparseType(gof.Type):
return
(
SparseType
.
may_share_memory
(
a
,
b
.
data
)
or
SparseType
.
may_share_memory
(
a
,
b
.
indices
)
or
SparseType
.
may_share_memory
(
a
,
b
.
indptr
))
if
_is_sparse
(
b
)
and
isinstance
(
a
,
n
umpy
.
ndarray
):
if
_is_sparse
(
b
)
and
isinstance
(
a
,
n
p
.
ndarray
):
a
,
b
=
b
,
a
if
_is_sparse
(
a
)
and
isinstance
(
b
,
n
umpy
.
ndarray
):
if
(
n
umpy
.
may_share_memory
(
a
.
data
,
b
)
or
n
umpy
.
may_share_memory
(
a
.
indices
,
b
)
or
n
umpy
.
may_share_memory
(
a
.
indptr
,
b
)):
if
_is_sparse
(
a
)
and
isinstance
(
b
,
n
p
.
ndarray
):
if
(
n
p
.
may_share_memory
(
a
.
data
,
b
)
or
n
p
.
may_share_memory
(
a
.
indices
,
b
)
or
n
p
.
may_share_memory
(
a
.
indptr
,
b
)):
# currently we can't share memory with a.shape as it is a tuple
return
True
return
False
...
...
@@ -168,8 +168,8 @@ class SparseType(gof.Type):
obj
.
indices
.
size
,
obj
.
indptr
.
size
,
obj
.
nnz
)
def
get_size
(
self
,
shape_info
):
return
(
shape_info
[
1
]
*
n
umpy
.
dtype
(
self
.
dtype
)
.
itemsize
+
(
shape_info
[
2
]
+
shape_info
[
3
])
*
n
umpy
.
dtype
(
'int32'
)
.
itemsize
)
return
(
shape_info
[
1
]
*
n
p
.
dtype
(
self
.
dtype
)
.
itemsize
+
(
shape_info
[
2
]
+
shape_info
[
3
])
*
n
p
.
dtype
(
'int32'
)
.
itemsize
)
# Register SparseType's C code for ViewOp.
theano
.
compile
.
register_view_op_c_code
(
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论