Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
ea16231b
提交
ea16231b
authored
1月 12, 2012
作者:
Valentin Bisson
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
CCW#37: Reviewed Remove0 sparse op.
上级
e898dbf9
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
49 行增加
和
23 行删除
+49
-23
sp.py
theano/sparse/sandbox/sp.py
+17
-22
test_sp.py
theano/sparse/sandbox/test_sp.py
+32
-1
没有找到文件。
theano/sparse/sandbox/sp.py
浏览文件 @
ea16231b
...
...
@@ -222,32 +222,27 @@ class Remove0(Op):
"""
Remove explicit zeros from a sparse matrix, and resort indices
"""
def
make_node
(
self
,
x
):
return
gof
.
Apply
(
self
,
[
x
],
[
x
.
type
()])
def
perform
(
self
,
node
,
(
x
,),
(
z
,)):
if
x
.
format
!=
'csc'
:
raise
TypeError
(
'Remove0 only works on csc matrices'
)
M
,
N
=
x
.
shape
data
=
x
.
data
i
ndices
=
x
.
indices
indptr
=
x
.
indptr
def
__init__
(
self
,
inplace
=
False
,
*
args
,
**
kwargs
):
Op
.
__init__
(
self
,
*
args
,
**
kwargs
)
self
.
inplace
=
inplace
i
f
self
.
inplace
:
self
.
destroy_map
=
{
0
:
[
0
]}
#TODO: try using ndarrays and then prune() on the result
new_data
=
[]
new_indices
=
[]
new_indptr
=
[
0
]
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
and
self
.
inplace
==
other
.
inplace
for
j
in
xrange
(
0
,
N
):
for
i_idx
in
xrange
(
indptr
[
j
],
indptr
[
j
+
1
]):
if
data
[
i_idx
]
!=
0
:
new_data
.
append
(
data
[
i_idx
])
new_indices
.
append
(
indices
[
i_idx
])
new_indptr
.
append
(
len
(
new_indices
))
def
make_node
(
self
,
x
):
return
gof
.
Apply
(
self
,
[
x
],
[
x
.
type
()])
z
[
0
]
=
sparse
.
csc_matrix
((
new_data
,
new_indices
,
new_indptr
),
(
M
,
N
))
def
perform
(
self
,
node
,
(
x
,),
(
z
,)):
if
self
.
inplace
:
c
=
x
else
:
c
=
x
.
copy
()
c
.
eliminate_zeros
()
z
[
0
]
=
c
return
def
grad
(
self
,
(
x
,),
(
gz
,)):
return
[
gz
]
...
...
theano/sparse/sandbox/test_sp.py
浏览文件 @
ea16231b
...
...
@@ -16,7 +16,6 @@ from theano import function, tensor
import
theano
from
theano.sparse.sandbox
import
sp
from
theano.tests
import
unittest_tools
as
utt
from
theano.sparse.tests.test_basic
import
random_lil
class
TestSP
(
unittest
.
TestCase
):
...
...
@@ -426,6 +425,36 @@ class TestSP(unittest.TestCase):
#utt.verify_grad(SpSum(axis=None), [x_val])
print
'ok'
def
test_remove0
():
print
print
'test_remove0()'
import
scipy
import
numpy
as
N
configs
=
[
# structure type, numpy matching class, optimized
(
'csc'
,
scipy
.
sparse
.
csc_matrix
),
(
'csr'
,
scipy
.
sparse
.
csr_matrix
),
]
for
format
,
matrix_class
in
configs
:
print
'config: format=
\'
%(format)
s
\'
, matrix_class=
%(matrix_class)
s'
%
locals
()
# real
origin
=
N
.
arange
(
9
)
.
reshape
((
3
,
3
))
.
astype
(
theano
.
config
.
floatX
)
with0
=
matrix_class
(
origin
)
.
astype
(
theano
.
config
.
floatX
)
with0
[
0
,
1
]
=
with0
[
1
,
0
]
=
with0
[
2
,
2
]
=
0
# symbolic
x
=
theano
.
sparse
.
SparseType
(
format
=
format
,
dtype
=
theano
.
config
.
floatX
)()
f
=
theano
.
function
([
x
],
sp
.
Remove0
()(
x
))
# checking
with0
.
eliminate_zeros
()
# makes sense to change its name
target
=
with0
assert
(
f
(
with0
)
.
todense
()
==
target
.
todense
())
.
all
()
def
test_diagonal
():
for
K
in
1
,
5
:
...
...
@@ -532,6 +561,8 @@ def test_col_scale():
print
>>
sys
.
stderr
,
"WARNING: skipping gradient test because verify_grad doesn't support sparse arguments"
if
__name__
==
'__main__'
:
test_remove0
()
exit
()
if
1
:
testcase
=
TestSP
suite
=
unittest
.
TestLoader
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论