Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
933eeb94
提交
933eeb94
authored
11月 23, 2011
作者:
Valentin Bisson
提交者:
Frederic
12月 02, 2011
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implemented non-sparse grad.
上级
2babd2d6
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
20 行增加
和
34 行删除
+20
-34
sp.py
theano/sparse/sandbox/sp.py
+11
-23
test_sp.py
theano/sparse/sandbox/test_sp.py
+9
-11
没有找到文件。
theano/sparse/sandbox/sp.py
浏览文件 @
933eeb94
...
...
@@ -31,7 +31,7 @@ class SpSum(Op):
:param sparse_grad: if True, this instance ignores the gradient on matrix elements which are implicitly 0.
"""
super
(
SpSum
,
self
)
.
__init__
()
self
.
axis
=
axis
self
.
axis
=
axis
self
.
sparse_grad
=
sparse_grad
if
self
.
axis
not
in
(
None
,
0
,
1
):
raise
ValueError
(
'illegal value for self.axis'
)
...
...
@@ -54,9 +54,8 @@ class SpSum(Op):
# At least for small matrices (5x5), the .sum() method of a csc matrix returns a dense matrix
# as the result whether axis is 0 or 1... weird!
###
#make sure a sparse type has been given in input
assert
isinstance
(
x
.
type
,
theano
.
sparse
.
SparseType
)
b
=
()
b
=
()
if
self
.
axis
is
not
None
:
b
=
(
False
,)
z
=
tensor
.
tensor
(
broadcastable
=
b
,
dtype
=
x
.
dtype
)
...
...
@@ -76,7 +75,7 @@ class SpSum(Op):
if
self
.
axis
is
None
:
z
[
0
]
=
numpy
.
asarray
(
x
.
sum
())
else
:
s
=
set
(
xrange
(
len
(
x
.
shape
)))
-
set
([
self
.
axis
])
s
=
set
(
xrange
(
len
(
x
.
shape
)))
-
set
([
self
.
axis
])
myreshape
=
map
((
lambda
i
:
x
.
shape
[
i
]),
s
)
if
x
.
format
not
in
(
'csc'
,
'csr'
):
x
=
x
.
asformat
(
x
.
format
)
...
...
@@ -95,31 +94,20 @@ class SpSum(Op):
# z[0] = numpy.asarray(x.asformat(x.format).sum(axis=self.axis)).reshape((x.shape[0],))
def
grad
(
self
,(
x
,),
(
gz
,)):
print
'grad (sparse:
%
s):'
%
self
.
sparse_grad
,
x
,
gz
if
self
.
sparse_grad
:
if
self
.
axis
is
None
:
return
[
gz
*
theano
.
sparse
.
sp_ones_like
(
x
)]
r
=
gz
*
theano
.
sparse
.
sp_ones_like
(
x
)
elif
self
.
axis
==
0
:
return
col_scale
(
theano
.
sparse
.
sp_ones_like
(
x
),
gz
)
r
=
col_scale
(
theano
.
sparse
.
sp_ones_like
(
x
),
gz
)
elif
self
.
axis
==
1
:
return
row_scale
(
theano
.
sparse
.
sp_ones_like
(
x
),
gz
)
else
:
assert
False
else
:
# if sparse_grad is False, this instance does not ignore the
# gradient on matrix elements which are implicitly 0,
# and return a dense version of the data.
if
self
.
axis
is
None
:
return
[
gz
*
tensor
.
ones_like
(
theano
.
sparse
.
dense_from_sparse
(
x
))]
elif
self
.
axis
==
0
:
raise
NotImplementedError
(
'non sparse grad, axis=0'
)
pass
elif
self
.
axis
==
1
:
raise
NotImplementedError
(
'non sparse grad, axis=1'
)
pass
r
=
row_scale
(
theano
.
sparse
.
sp_ones_like
(
x
),
gz
)
else
:
assert
False
if
not
self
.
sparse_grad
:
r
=
theano
.
sparse
.
dense_from_sparse
(
r
)
return
[
r
]
def
sp_sum
(
x
,
axis
=
None
,
sparse_grad
=
False
):
return
SpSum
(
axis
,
sparse_grad
)(
x
)
...
...
theano/sparse/sandbox/test_sp.py
浏览文件 @
933eeb94
...
...
@@ -372,21 +372,19 @@ class TestSP(unittest.TestCase):
# TODO: test both grad.
rng
=
numpy
.
random
.
RandomState
(
42
)
from
theano.sparse.basic
import
SparseFromDense
,
DenseFromSparse
for
format
,
cast
in
[(
"csc"
,
scipy
.
sparse
.
csc_matrix
),
(
"csr"
,
scipy
.
sparse
.
csr_matrix
)]:
cases
=
[(
"csc"
,
scipy
.
sparse
.
csc_matrix
),
(
"csr"
,
scipy
.
sparse
.
csr_matrix
)]
print
'format:
%(format)
s'
%
locals
()
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
)
#print 'x_data:',x_data
#print 'x_data.sum():',x_data.sum()
# Sum on all axis
print
'sum on all axis...'
z
=
theano
.
sparse
.
sandbox
.
sp
.
sp_sum
(
x
)
assert
z
.
type
.
broadcastable
==
()
assert
z
.
type
.
broadcastable
==
()
f
=
theano
.
function
([
x
],
z
)
x_val
=
cast
(
x_data
)
out
=
f
(
x_val
)
...
...
@@ -396,17 +394,17 @@ class TestSP(unittest.TestCase):
# Sum on axis 0
print
'sum on axis 0...'
z
=
theano
.
sparse
.
sandbox
.
sp
.
sp_sum
(
x
,
axis
=
0
)
assert
z
.
type
.
broadcastable
==
(
False
,)
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
)
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
,)
assert
z
.
type
.
broadcastable
==
(
False
,)
f
=
theano
.
function
([
x
],
z
)
x_val
=
cast
(
x_data
)
out
=
f
(
x_val
)
...
...
@@ -414,7 +412,7 @@ class TestSP(unittest.TestCase):
assert
(
out
==
expected
)
.
all
()
# Sparse gradient on Sum on all axis
#
suspended until som estuff get fixed =/
#
unfinished, and suspended until verify_grad get fixed
if
False
:
print
'grad on sum on all axis...'
def
fun
(
x
):
...
...
@@ -422,7 +420,7 @@ class TestSP(unittest.TestCase):
# 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
)
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
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论