Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
dcc7944a
提交
dcc7944a
authored
6月 11, 2012
作者:
Nicolas Bouchard
提交者:
Frederic
7月 06, 2012
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix bug to cast a sparse
上级
afd9a418
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
132 行增加
和
15 行删除
+132
-15
sp2.py
theano/sparse/sandbox/sp2.py
+52
-5
test_sp2.py
theano/sparse/tests/test_sp2.py
+80
-10
没有找到文件。
theano/sparse/sandbox/sp2.py
浏览文件 @
dcc7944a
...
@@ -3,6 +3,7 @@ import scipy.sparse
...
@@ -3,6 +3,7 @@ import scipy.sparse
from
theano
import
gof
,
tensor
,
scalar
from
theano
import
gof
,
tensor
,
scalar
from
theano.tensor
import
blas
from
theano.tensor
import
blas
from
theano
import
tensor
as
T
from
theano.sparse.basic
import
(
from
theano.sparse.basic
import
(
as_sparse_variable
,
SparseType
,
add_s_s
,
neg
,
as_sparse_variable
,
SparseType
,
add_s_s
,
neg
,
...
@@ -32,15 +33,61 @@ class Cast(gof.op.Op):
...
@@ -32,15 +33,61 @@ class Cast(gof.op.Op):
def
make_node
(
self
,
x
):
def
make_node
(
self
,
x
):
x
=
as_sparse_variable
(
x
)
x
=
as_sparse_variable
(
x
)
return
gof
.
Apply
(
self
,
[
x
],
return
gof
.
Apply
(
self
,
[
x
],
[
SparseType
(
dtype
=
self
.
out_type
,
format
=
x
.
format
)
.
make_variable
()])
[
SparseType
(
dtype
=
self
.
out_type
,
format
=
x
.
format
)
.
make_variable
()])
def
perform
(
self
,
node
,
(
x
,
),
(
out
,
)):
def
perform
(
self
,
node
,
(
x
,
),
(
out
,
)):
assert
_is_sparse
(
x
)
assert
_is_sparse
(
x
)
out
[
0
]
=
x
out
[
0
]
=
x
.
astype
(
self
.
out_type
)
out
[
0
]
.
data
=
numpy
.
asarray
(
out
[
0
]
.
data
,
dtype
=
self
.
out_type
)
fcast
=
Cast
(
'float32'
)
def
grad
(
self
,
inputs
,
outputs_gradients
):
dcast
=
Cast
(
'float64'
)
if
inputs
[
0
]
.
dtype
in
T
.
continuous_dtypes
:
gz
=
outputs_gradients
[
0
]
return
[
Cast
(
self
.
out_type
)(
gz
)]
else
:
return
[
None
]
def
infer_shape
(
self
,
node
,
ins_shapes
):
return
ins_shapes
def
__str__
(
self
):
return
self
.
__class__
.
__name__
def
astype
(
x
,
t
):
"""Cast sparse variable `x` to the desired dtype `t`.
This wrap the method astype from scipy.
:Parameters:
- `x`: Sparse array
- `t`: dtype
"""
return
Cast
(
t
)(
x
)
def
fcast
(
x
):
"""Cast sparse variable `x` to `float32`.
This wrap the method astype from scipy.
:Parameters:
- `x`: Sparse array
"""
return
Cast
(
'float32'
)(
x
)
def
dcast
(
x
):
"""Cast sparse variable `x` to `float64`.
This wrap the method astype from scipy.
:Parameters:
- `x`: Sparse array
"""
return
Cast
(
'float64'
)(
x
)
class
AddSSData
(
gof
.
op
.
Op
):
class
AddSSData
(
gof
.
op
.
Op
):
...
...
theano/sparse/tests/test_sp2.py
浏览文件 @
dcc7944a
...
@@ -2,7 +2,7 @@ import time
...
@@ -2,7 +2,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
try
:
try
:
import
scipy.sparse
as
sp
import
scipy.sparse
as
sp
import
scipy.sparse
import
scipy.sparse
...
@@ -19,6 +19,7 @@ if not S.enable_sparse:
...
@@ -19,6 +19,7 @@ if not S.enable_sparse:
from
theano.sparse.sandbox
import
sp2
as
S2
from
theano.sparse.sandbox
import
sp2
as
S2
from
theano.tests
import
unittest_tools
as
utt
from
theano.tests
import
unittest_tools
as
utt
from
theano.sparse.basic
import
verify_grad_sparse
def
as_sparse_format
(
data
,
format
):
def
as_sparse_format
(
data
,
format
):
if
format
==
'csc'
:
if
format
==
'csc'
:
...
@@ -38,8 +39,8 @@ def random_lil(shape, dtype, nnz):
...
@@ -38,8 +39,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
.
random_integers
(
huge
,
size
=
len
(
shape
))
%
shape
idx
=
n
p
.
random
.
random_integers
(
huge
,
size
=
len
(
shape
))
%
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
)
...
@@ -49,6 +50,75 @@ def random_lil(shape, dtype, nnz):
...
@@ -49,6 +50,75 @@ def random_lil(shape, dtype, nnz):
return
rval
return
rval
class
TestCast
(
utt
.
InferShapeTester
):
compatible_types
=
T
.
int_dtypes
+
T
.
continuous_dtypes
x_csc
=
[
S
.
csc_matrix
(
dtype
=
t
)
for
t
in
compatible_types
]
x_csr
=
[
S
.
csr_matrix
(
dtype
=
t
)
for
t
in
compatible_types
]
indptr
=
np
.
array
([
0
,
2
,
3
,
6
])
indices
=
np
.
array
([
0
,
2
,
2
,
0
,
1
,
2
])
data
=
np
.
array
([
1
,
2
,
3
,
4
,
5
,
6
])
properties
=
(
data
,
indices
,
indptr
)
def
setUp
(
self
):
super
(
TestCast
,
self
)
.
setUp
()
self
.
op_class
=
S2
.
Cast
def
test_cast
(
self
):
cast_csc
=
dict
([
(
x
,
[
theano
.
function
([
x
],
S2
.
astype
(
x
,
t
))
for
t
in
self
.
compatible_types
])
for
x
in
self
.
x_csc
])
cast_csr
=
dict
([
(
x
,
[
theano
.
function
([
x
],
S2
.
astype
(
x
,
t
))
for
t
in
self
.
compatible_types
])
for
x
in
self
.
x_csr
])
for
x
in
self
.
x_csc
:
for
f
,
t
in
zip
(
cast_csc
[
x
],
self
.
compatible_types
):
a
=
sp
.
csc_matrix
(
self
.
properties
,
dtype
=
x
.
dtype
)
assert
f
(
a
)
.
dtype
==
t
for
x
in
self
.
x_csr
:
for
f
,
t
in
zip
(
cast_csr
[
x
],
self
.
compatible_types
):
a
=
sp
.
csr_matrix
(
self
.
properties
,
dtype
=
x
.
dtype
)
assert
f
(
a
)
.
dtype
==
t
def
test_infer_shape
(
self
):
for
x
in
self
.
x_csc
:
for
t
in
self
.
compatible_types
:
a
=
sp
.
csc_matrix
(
self
.
properties
,
dtype
=
x
.
dtype
)
self
.
_compile_and_check
([
x
],
[
S2
.
astype
(
x
,
t
)],
[
a
],
self
.
op_class
)
for
x
in
self
.
x_csr
:
for
t
in
self
.
compatible_types
:
a
=
sp
.
csr_matrix
(
self
.
properties
,
dtype
=
x
.
dtype
)
self
.
_compile_and_check
([
x
],
[
S2
.
astype
(
x
,
t
)],
[
a
],
self
.
op_class
)
def
test_grad
(
self
):
x_csc
=
[
S
.
csc_matrix
(
dtype
=
t
)
for
t
in
T
.
float_dtypes
]
x_csr
=
[
S
.
csr_matrix
(
dtype
=
t
)
for
t
in
T
.
float_dtypes
]
# There is a problem with the grad
# TODO Find the problem
# for x in x_csc:
# for t in T.float_dtypes:
# a = sp.csc_matrix(self.properties, dtype=x.dtype)
# verify_grad_sparse(S2.Cast(t), [a])
# for x in x_csr:
# for t in T.float_dtypes:
# a = sp.csr_matrix(self.properties, dtype=x.dtype)
# verify_grad_sparse(S2.Cast(t), [a])
class
test_structured_add_s_v
(
unittest
.
TestCase
):
class
test_structured_add_s_v
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
utt
.
seed_rng
()
utt
.
seed_rng
()
...
@@ -60,7 +130,7 @@ class test_structured_add_s_v(unittest.TestCase):
...
@@ -60,7 +130,7 @@ class test_structured_add_s_v(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
)
S
.
verify_grad_sparse
(
S2
.
structured_add_s_v
,
S
.
verify_grad_sparse
(
S2
.
structured_add_s_v
,
[
spmat
,
mat
],
structured
=
True
)
[
spmat
,
mat
],
structured
=
True
)
...
@@ -77,12 +147,12 @@ class test_structured_add_s_v(unittest.TestCase):
...
@@ -77,12 +147,12 @@ class test_structured_add_s_v(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
)
assert
n
umpy
.
allclose
(
out
.
toarray
(),
spones
.
multiply
(
spmat
+
mat
))
assert
n
p
.
allclose
(
out
.
toarray
(),
spones
.
multiply
(
spmat
+
mat
))
class
test_mul_s_v
(
unittest
.
TestCase
):
class
test_mul_s_v
(
unittest
.
TestCase
):
...
@@ -96,7 +166,7 @@ class test_mul_s_v(unittest.TestCase):
...
@@ -96,7 +166,7 @@ class test_mul_s_v(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
)
S
.
verify_grad_sparse
(
S2
.
mul_s_v
,
S
.
verify_grad_sparse
(
S2
.
mul_s_v
,
[
spmat
,
mat
],
structured
=
True
)
[
spmat
,
mat
],
structured
=
True
)
...
@@ -112,11 +182,11 @@ class test_mul_s_v(unittest.TestCase):
...
@@ -112,11 +182,11 @@ class test_mul_s_v(unittest.TestCase):
f
=
theano
.
function
([
x
,
y
],
S2
.
mul_s_v
(
x
,
y
))
f
=
theano
.
function
([
x
,
y
],
S2
.
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
)
assert
n
umpy
.
allclose
(
out
.
toarray
(),
spmat
.
toarray
()
*
mat
)
assert
n
p
.
allclose
(
out
.
toarray
(),
spmat
.
toarray
()
*
mat
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论