Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
441d5a7f
提交
441d5a7f
authored
8月 14, 2012
作者:
nouiz
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #853 from bouchnic/cast
NEWS: Add astype to SparseVariable (Nicolas B.)
上级
212b5fe8
c3227a52
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
60 行增加
和
63 行删除
+60
-63
basic.py
theano/sparse/basic.py
+6
-0
test_basic.py
theano/sparse/tests/test_basic.py
+54
-63
没有找到文件。
theano/sparse/basic.py
浏览文件 @
441d5a7f
...
...
@@ -267,6 +267,9 @@ class _sparse_py_operators:
T
=
property
(
lambda
self
:
transpose
(
self
),
doc
=
"Return aliased transpose of self (read-only)"
)
def
astype
(
self
,
dtype
):
return
cast
(
self
,
dtype
)
def
__neg__
(
self
):
return
neg
(
self
)
...
...
@@ -1031,6 +1034,9 @@ ccast = Cast('complex64')
zcast
=
Cast
(
'complex128'
)
def
cast
(
variable
,
dtype
):
return
Cast
(
dtype
)(
variable
)
#
# Conversion
#
...
...
theano/sparse/tests/test_basic.py
浏览文件 @
441d5a7f
...
...
@@ -33,7 +33,7 @@ from theano.sparse import (
csc_from_dense
,
csr_from_dense
,
dense_from_sparse
,
Dot
,
Usmm
,
UsmmCscDense
,
sp_ones_like
,
GetItemScalar
,
SparseFromDense
,
Cast
,
HStack
,
VStack
,
AddSSData
,
add_s_s_data
,
Cast
,
cast
,
HStack
,
VStack
,
AddSSData
,
add_s_s_data
,
Poisson
,
poisson
,
Binomial
,
Multinomial
,
multinomial
,
structured_sigmoid
,
structured_exp
,
structured_log
,
structured_pow
,
structured_minimum
,
structured_maximum
,
structured_add
,
...
...
@@ -1934,75 +1934,66 @@ class Test_getitem(unittest.TestCase):
assert
numpy
.
all
(
t1
==
r1
)
class
TestCast
(
utt
.
InferShapeTester
):
compatible_types
=
(
tensor
.
int_dtypes
+
tensor
.
continuous_dtypes
)
x_csc
=
[
theano
.
sparse
.
csc_matrix
(
dtype
=
t
)
for
t
in
compatible_types
]
x_csr
=
[
theano
.
sparse
.
csr_matrix
(
dtype
=
t
)
for
t
in
compatible_types
]
indptr
=
numpy
.
array
([
0
,
2
,
3
,
6
])
indices
=
numpy
.
array
([
0
,
2
,
2
,
0
,
1
,
2
])
data
=
numpy
.
array
([
1
,
2
,
3
,
4
,
5
,
6
])
properties
=
(
data
,
indices
,
indptr
)
class
CastTester
(
utt
.
InferShapeTester
):
def
setUp
(
self
):
super
(
TestCast
,
self
)
.
setUp
()
self
.
op_class
=
Cast
super
(
CastTester
,
self
)
.
setUp
()
def
test_cast
(
self
):
cast_csc
=
dict
([
(
x
,
[
theano
.
function
([
x
],
Cast
(
t
)(
x
))
for
t
in
self
.
compatible_types
])
for
x
in
self
.
x_csc
])
cast_csr
=
dict
([
(
x
,
[
theano
.
function
([
x
],
Cast
(
t
)(
x
))
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
)
.
copy
()
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
for
format
in
sparse
.
sparse_formats
:
for
i_dtype
in
sparse
.
all_dtypes
:
for
o_dtype
in
sparse
.
all_dtypes
:
(
variable
,
),
(
data
,
)
=
sparse_random_inputs
(
format
,
shape
=
(
4
,
7
),
out_dtype
=
i_dtype
)
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
],
[
Cast
(
t
)(
x
)],
[
a
],
self
.
op_class
)
func
=
theano
.
function
([
variable
],
cast
(
variable
,
o_dtype
))
cls
=
theano
.
function
([
variable
],
Cast
(
o_dtype
)(
variable
))
prop
=
theano
.
function
([
variable
],
variable
.
astype
(
o_dtype
))
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
],
[
Cast
(
t
)(
x
)],
[
a
],
self
.
op_class
)
t_func
,
t_cls
,
t_prop
=
func
(
data
),
cls
(
data
),
prop
(
data
)
expected
=
data
.
toarray
()
.
astype
(
o_dtype
)
assert
t_func
.
format
==
format
assert
t_cls
.
format
==
format
assert
t_prop
.
format
==
format
t_func
=
t_func
.
toarray
()
t_cls
=
t_cls
.
toarray
()
t_prop
=
t_prop
.
toarray
()
assert
numpy
.
allclose
(
t_func
,
expected
)
assert
numpy
.
allclose
(
t_cls
,
expected
)
assert
numpy
.
allclose
(
t_prop
,
expected
)
def
test_infer_shape
(
self
):
for
format
in
sparse
.
sparse_formats
:
for
i_dtype
in
sparse
.
all_dtypes
:
for
o_dtype
in
sparse
.
all_dtypes
:
variable
,
data
=
sparse_random_inputs
(
format
,
shape
=
(
4
,
7
),
out_dtype
=
i_dtype
)
self
.
_compile_and_check
(
variable
,
[
Cast
(
o_dtype
)(
*
variable
)],
data
,
Cast
)
def
test_grad
(
self
):
for
dtype
in
tensor
.
float_dtypes
:
for
t
in
tensor
.
float_dtypes
:
eps
=
None
if
t
==
'float32'
:
eps
=
7e-4
a
=
sp
.
csc_matrix
(
self
.
properties
,
dtype
=
dtype
)
verify_grad_sparse
(
Cast
(
t
),
[
a
],
eps
=
eps
)
for
dtype
in
tensor
.
float_dtypes
:
for
t
in
tensor
.
float_dtypes
:
eps
=
None
if
t
==
'float32'
:
eps
=
7e-4
a
=
sp
.
csr_matrix
(
self
.
properties
,
dtype
=
dtype
)
verify_grad_sparse
(
Cast
(
t
),
[
a
],
eps
=
eps
)
for
format
in
sparse
.
sparse_formats
:
for
i_dtype
in
sparse
.
float_dtypes
:
for
o_dtype
in
tensor
.
float_dtypes
:
_
,
data
=
sparse_random_inputs
(
format
,
shape
=
(
4
,
7
),
out_dtype
=
i_dtype
)
eps
=
None
if
o_dtype
==
'float32'
:
eps
=
7e-4
verify_grad_sparse
(
Cast
(
o_dtype
),
data
,
eps
=
eps
)
class
_HVStackTester
(
utt
.
InferShapeTester
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论