Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
56888c31
提交
56888c31
authored
7月 06, 2012
作者:
Nicolas Bouchard
提交者:
Frederic
7月 06, 2012
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move tests of sp_sum.
上级
85c30d7b
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
70 行增加
和
69 行删除
+70
-69
basic.py
theano/sparse/basic.py
+3
-3
test_sp.py
theano/sparse/sandbox/test_sp.py
+0
-66
test_basic.py
theano/sparse/tests/test_basic.py
+67
-0
没有找到文件。
theano/sparse/basic.py
浏览文件 @
56888c31
...
...
@@ -1287,11 +1287,11 @@ neg = Neg()
class
SpSum
(
gof
.
op
.
Op
):
"""
TODO: rewrite
"""TODO: rewrite
Scale each columns of a sparse matrix by the
corresponding element of a dense vector
"""
axis
=
None
sparse_grad
=
False
...
...
@@ -1351,7 +1351,7 @@ class SpSum(gof.op.Op):
if
self
.
axis
==
0
:
if
x
.
format
==
'csc'
:
z
[
0
]
=
numpy
.
asarray
(
x
.
sum
(
axis
=
self
.
axis
))
.
reshape
(
(
x
.
shape
[
1
],))
(
x
.
shape
[
1
],
))
else
:
z
[
0
]
=
numpy
.
asarray
(
x
.
asformat
(
x
.
format
)
.
sum
(
axis
=
self
.
axis
))
.
reshape
((
x
.
shape
[
1
],))
...
...
theano/sparse/sandbox/test_sp.py
浏览文件 @
56888c31
...
...
@@ -362,72 +362,6 @@ class TestSP(unittest.TestCase):
# symbolic stuff
utt
.
verify_grad
(
d
,
[
kvals
])
def
test_sp_sum
(
self
):
from
theano.sparse
import
SpSum
# TODO: test both grad.
rng
=
numpy
.
random
.
RandomState
(
42
)
from
theano.sparse.basic
import
SparseFromDense
,
DenseFromSparse
cases
=
[(
"csc"
,
scipy
.
sparse
.
csc_matrix
),
(
"csr"
,
scipy
.
sparse
.
csr_matrix
)]
for
format
,
cast
in
cases
:
#print 'format: %(format)s' % locals()
x
=
theano
.
sparse
.
SparseType
(
format
=
format
,
dtype
=
theano
.
config
.
floatX
)()
x_data
=
numpy
.
arange
(
20
)
.
reshape
(
5
,
4
)
.
astype
(
theano
.
config
.
floatX
)
# Sum on all axis
#print 'sum on all axis...'
z
=
theano
.
sparse
.
sandbox
.
sp
.
sp_sum
(
x
)
assert
z
.
type
.
broadcastable
==
()
f
=
theano
.
function
([
x
],
z
)
x_val
=
cast
(
x_data
)
out
=
f
(
x_val
)
expected
=
x_val
.
sum
()
assert
out
==
expected
# Sum on axis 0
#print 'sum on axis 0...'
z
=
theano
.
sparse
.
sandbox
.
sp
.
sp_sum
(
x
,
axis
=
0
)
assert
z
.
type
.
broadcastable
==
(
False
,)
f
=
theano
.
function
([
x
],
z
)
x_val
=
cast
(
x_data
)
out
=
f
(
x_val
)
expected
=
x_val
.
sum
(
axis
=
0
)
assert
(
out
==
expected
)
.
all
()
# Sum on axis 1
#print 'sum on axis 1...'
z
=
theano
.
sparse
.
sandbox
.
sp
.
sp_sum
(
x
,
axis
=
1
)
assert
z
.
type
.
broadcastable
==
(
False
,)
f
=
theano
.
function
([
x
],
z
)
x_val
=
cast
(
x_data
)
out
=
f
(
x_val
)
expected
=
numpy
.
asarray
(
x_val
.
sum
(
axis
=
1
))
.
reshape
(
x_val
.
shape
[
0
])
assert
(
out
==
expected
)
.
all
()
# Sparse gradient on Sum on all axis
# unfinished, and suspended until verify_grad get fixed
if
False
:
# print 'grad on sum on all axis...'
def
fun
(
x
):
## verify_grad does not handle sparse data, so here's some casting as a workaround.
# x is a dense matrix: make it sparse
sparse_var
=
SparseFromDense
(
format
)(
x
)
# apply op
dense_sum
=
theano
.
sparse
.
sandbox
.
sp
.
SpSum
(
axis
=
None
,
sparse_grad
=
False
)(
sparse_var
)
return
dense_sum
# cast back to dense so that verify_grad can work
dense_sum
=
theano
.
sparse
.
DenseFromSparse
()(
sparse_sum
)
return
dense_sum
x_val
=
x_data
.
copy
()
# print type(x_val)
import
pdb
;
pdb
.
set_trace
()
tensor
.
verify_grad
(
fun
,
[
x_val
],
rng
=
rng
)
#utt.verify_grad(SpSum(axis=None), [x_val])
# print 'ok'
def
test_diag
():
m
=
theano
.
sparse
.
csc_matrix
()
...
...
theano/sparse/tests/test_basic.py
浏览文件 @
56888c31
...
...
@@ -1329,6 +1329,73 @@ def test_size():
check
()
def
test_sp_sum
():
from
theano.sparse
import
SpSum
# TODO: test both grad.
rng
=
numpy
.
random
.
RandomState
(
42
)
from
theano.sparse.basic
import
SparseFromDense
,
DenseFromSparse
cases
=
[(
"csc"
,
scipy
.
sparse
.
csc_matrix
),
(
"csr"
,
scipy
.
sparse
.
csr_matrix
)]
for
format
,
cast
in
cases
:
#print 'format: %(format)s' % locals()
x
=
theano
.
sparse
.
SparseType
(
format
=
format
,
dtype
=
theano
.
config
.
floatX
)()
x_data
=
numpy
.
arange
(
20
)
.
reshape
(
5
,
4
)
.
astype
(
theano
.
config
.
floatX
)
# Sum on all axis
#print 'sum on all axis...'
z
=
theano
.
sparse
.
sp_sum
(
x
)
assert
z
.
type
.
broadcastable
==
()
f
=
theano
.
function
([
x
],
z
)
x_val
=
cast
(
x_data
)
out
=
f
(
x_val
)
expected
=
x_val
.
sum
()
assert
out
==
expected
# Sum on axis 0
#print 'sum on axis 0...'
z
=
theano
.
sparse
.
sp_sum
(
x
,
axis
=
0
)
assert
z
.
type
.
broadcastable
==
(
False
,)
f
=
theano
.
function
([
x
],
z
)
x_val
=
cast
(
x_data
)
out
=
f
(
x_val
)
expected
=
x_val
.
sum
(
axis
=
0
)
assert
(
out
==
expected
)
.
all
()
# Sum on axis 1
#print 'sum on axis 1...'
z
=
theano
.
sparse
.
sp_sum
(
x
,
axis
=
1
)
assert
z
.
type
.
broadcastable
==
(
False
,)
f
=
theano
.
function
([
x
],
z
)
x_val
=
cast
(
x_data
)
out
=
f
(
x_val
)
expected
=
numpy
.
asarray
(
x_val
.
sum
(
axis
=
1
))
.
reshape
(
x_val
.
shape
[
0
])
assert
(
out
==
expected
)
.
all
()
# Sparse gradient on Sum on all axis
# unfinished, and suspended until verify_grad get fixed
if
False
:
# print 'grad on sum on all axis...'
def
fun
(
x
):
## verify_grad does not handle sparse data, so here's some casting as a workaround.
# x is a dense matrix: make it sparse
sparse_var
=
SparseFromDense
(
format
)(
x
)
# apply op
dense_sum
=
theano
.
sparse
.
SpSum
(
axis
=
None
,
sparse_grad
=
False
)(
sparse_var
)
return
dense_sum
# cast back to dense so that verify_grad can work
dense_sum
=
theano
.
sparse
.
DenseFromSparse
()(
sparse_sum
)
return
dense_sum
x_val
=
x_data
.
copy
()
# print type(x_val)
import
pdb
;
pdb
.
set_trace
()
tensor
.
verify_grad
(
fun
,
[
x_val
],
rng
=
rng
)
#utt.verify_grad(SpSum(axis=None), [x_val])
# print 'ok'
class
Remove0Tester
(
utt
.
InferShapeTester
):
def
setUp
(
self
):
super
(
Remove0Tester
,
self
)
.
setUp
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论