Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
8a6bdc91
提交
8a6bdc91
authored
5月 12, 2014
作者:
Hengjean
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added equal and notEqual operation between sparse matrices
上级
daf7fc95
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
227 行增加
和
1 行删除
+227
-1
basic.py
theano/sparse/basic.py
+169
-0
test_basic.py
theano/sparse/tests/test_basic.py
+58
-1
没有找到文件。
theano/sparse/basic.py
浏览文件 @
8a6bdc91
...
@@ -284,6 +284,25 @@ class _sparse_py_operators:
...
@@ -284,6 +284,25 @@ class _sparse_py_operators:
def
__rmul__
(
left
,
right
):
def
__rmul__
(
left
,
right
):
return
mul
(
left
,
right
)
return
mul
(
left
,
right
)
# comparison operators
def
__lt__
(
self
,
other
):
pass
def
__le__
(
self
,
other
):
pass
def
__gt__
(
self
,
other
):
pass
def
__ge__
(
self
,
other
):
pass
def
__ne__
(
self
,
other
):
pass
# extra pseudo-operator symbols
# extra pseudo-operator symbols
def
__dot__
(
left
,
right
):
def
__dot__
(
left
,
right
):
...
@@ -2149,6 +2168,156 @@ def mul(x, y):
...
@@ -2149,6 +2168,156 @@ def mul(x, y):
raise
NotImplementedError
()
raise
NotImplementedError
()
class
EqualSS
(
gof
.
op
.
Op
):
"""
:param x:first compared sparse matrix
:param y:second compared sparse matrix
:return: x==y
"""
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
))
def
__hash__
(
self
):
return
hash
(
type
(
self
))
def
make_node
(
self
,
x
,
y
):
x
=
as_sparse_variable
(
x
)
y
=
as_sparse_variable
(
y
)
if
x
.
type
.
format
!=
y
.
type
.
format
:
raise
NotImplementedError
()
return
gof
.
Apply
(
self
,
[
x
,
y
],
[
SparseType
(
dtype
=
'uint8'
,
format
=
x
.
type
.
format
)
.
make_variable
()])
def
perform
(
self
,
node
,
(
x
,
y
),
(
out
,
)):
assert
_is_sparse
(
x
)
and
_is_sparse
(
y
)
assert
x
.
shape
==
y
.
shape
out
[
0
]
=
(
x
==
y
)
.
astype
(
'uint8'
)
def
infer_shape
(
self
,
node
,
ins_shapes
):
return
[
ins_shapes
[
0
]]
def
__str__
(
self
):
return
self
.
__class__
.
__name__
equal_s_s
=
EqualSS
()
def
equal
(
x
,
y
):
"""
Add two matrices, the two of which are sparse.
:param x: A matrix variable.
:param y: A matrix variable.
:return: `x` == `y`
:note: At least one of `x` and `y` must be a sparse matrix.
"""
if
hasattr
(
x
,
'getnnz'
):
x
=
as_sparse_variable
(
x
)
if
hasattr
(
y
,
'getnnz'
):
y
=
as_sparse_variable
(
y
)
if
not
isinstance
(
x
,
theano
.
Variable
):
x
=
theano
.
tensor
.
as_tensor_variable
(
x
)
if
not
isinstance
(
y
,
theano
.
Variable
):
y
=
theano
.
tensor
.
as_tensor_variable
(
y
)
x_is_sparse_variable
=
_is_sparse_variable
(
x
)
y_is_sparse_variable
=
_is_sparse_variable
(
y
)
assert
x_is_sparse_variable
or
y_is_sparse_variable
if
x_is_sparse_variable
and
y_is_sparse_variable
:
return
equal_s_s
(
x
,
y
)
elif
x_is_sparse_variable
and
not
y_is_sparse_variable
:
raise
NotImplementedError
()
elif
y_is_sparse_variable
and
not
x_is_sparse_variable
:
raise
NotImplementedError
()
else
:
raise
NotImplementedError
()
class
NotEqualSS
(
gof
.
op
.
Op
):
"""
:param x:first compared sparse matrix
:param y:second compared sparse matrix
:return: x==y
"""
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
))
def
__hash__
(
self
):
return
hash
(
type
(
self
))
def
make_node
(
self
,
x
,
y
):
x
=
as_sparse_variable
(
x
)
y
=
as_sparse_variable
(
y
)
if
x
.
type
.
format
!=
y
.
type
.
format
:
raise
NotImplementedError
()
return
gof
.
Apply
(
self
,
[
x
,
y
],
[
SparseType
(
dtype
=
'uint8'
,
format
=
x
.
type
.
format
)
.
make_variable
()])
def
perform
(
self
,
node
,
(
x
,
y
),
(
out
,
)):
assert
_is_sparse
(
x
)
and
_is_sparse
(
y
)
assert
x
.
shape
==
y
.
shape
out
[
0
]
=
(
x
!=
y
)
.
astype
(
'uint8'
)
def
infer_shape
(
self
,
node
,
ins_shapes
):
return
[
ins_shapes
[
0
]]
def
__str__
(
self
):
return
self
.
__class__
.
__name__
not_equal_s_s
=
NotEqualSS
()
def
notEqual
(
x
,
y
):
"""
Add two matrices, the two of which are sparse.
:param x: A matrix variable.
:param y: A matrix variable.
:return: `x` == `y`
:note: At least one of `x` and `y` must be a sparse matrix.
"""
if
hasattr
(
x
,
'getnnz'
):
x
=
as_sparse_variable
(
x
)
if
hasattr
(
y
,
'getnnz'
):
y
=
as_sparse_variable
(
y
)
if
not
isinstance
(
x
,
theano
.
Variable
):
x
=
theano
.
tensor
.
as_tensor_variable
(
x
)
if
not
isinstance
(
y
,
theano
.
Variable
):
y
=
theano
.
tensor
.
as_tensor_variable
(
y
)
x_is_sparse_variable
=
_is_sparse_variable
(
x
)
y_is_sparse_variable
=
_is_sparse_variable
(
y
)
assert
x_is_sparse_variable
or
y_is_sparse_variable
if
x_is_sparse_variable
and
y_is_sparse_variable
:
return
not_equal_s_s
(
x
,
y
)
elif
x_is_sparse_variable
and
not
y_is_sparse_variable
:
raise
NotImplementedError
()
elif
y_is_sparse_variable
and
not
x_is_sparse_variable
:
raise
NotImplementedError
()
else
:
raise
NotImplementedError
()
class
HStack
(
gof
.
op
.
Op
):
class
HStack
(
gof
.
op
.
Op
):
"""Stack sparse matrices horizontally (column wise).
"""Stack sparse matrices horizontally (column wise).
...
...
theano/sparse/tests/test_basic.py
浏览文件 @
8a6bdc91
...
@@ -40,7 +40,7 @@ from theano.sparse import (
...
@@ -40,7 +40,7 @@ from theano.sparse import (
Diag
,
diag
,
SquareDiagonal
,
square_diagonal
,
Diag
,
diag
,
SquareDiagonal
,
square_diagonal
,
EnsureSortedIndices
,
ensure_sorted_indices
,
clean
,
EnsureSortedIndices
,
ensure_sorted_indices
,
clean
,
ConstructSparseFromList
,
construct_sparse_from_list
,
ConstructSparseFromList
,
construct_sparse_from_list
,
TrueDot
,
true_dot
)
TrueDot
,
true_dot
,
equal
,
notEqual
)
# Probability distributions are currently tested in test_sp2.py
# Probability distributions are currently tested in test_sp2.py
#from theano.sparse import (
#from theano.sparse import (
...
@@ -647,6 +647,63 @@ class T_AddMul(unittest.TestCase):
...
@@ -647,6 +647,63 @@ class T_AddMul(unittest.TestCase):
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
False
)
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
False
)
class
test_comparison
(
unittest
.
TestCase
):
def
setUp
(
self
):
utt
.
seed_rng
()
def
test_equalss_csr
(
self
):
x
=
sparse
.
csr_matrix
()
y
=
sparse
.
csr_matrix
()
equality
=
equal
(
x
,
y
)
f
=
theano
.
function
([
x
,
y
],
equality
)
m1
=
sp
.
csr_matrix
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
))
m2
=
sp
.
csr_matrix
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
))
self
.
assertTrue
(
numpy
.
array_equal
(
f
(
m1
,
m2
)
.
data
,
(
m1
==
m2
)
.
data
))
def
test_equalss_csc
(
self
):
x
=
sparse
.
csc_matrix
()
y
=
sparse
.
csc_matrix
()
equality
=
equal
(
x
,
y
)
f
=
theano
.
function
([
x
,
y
],
equality
)
m1
=
sp
.
csc_matrix
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
))
m2
=
sp
.
csc_matrix
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
))
self
.
assertTrue
(
numpy
.
array_equal
(
f
(
m1
,
m2
)
.
data
,
(
m1
==
m2
)
.
data
))
def
test_not_equalss_csr
(
self
):
x
=
sparse
.
csr_matrix
()
y
=
sparse
.
csr_matrix
()
unequality
=
notEqual
(
x
,
y
)
f
=
theano
.
function
([
x
,
y
],
unequality
)
m1
=
sp
.
csr_matrix
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
))
m2
=
sp
.
csr_matrix
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
))
self
.
assertTrue
(
numpy
.
array_equal
(
f
(
m1
,
m2
)
.
data
,
(
m1
!=
m2
)
.
data
))
def
test_not_equalss_csc
(
self
):
x
=
sparse
.
csc_matrix
()
y
=
sparse
.
csc_matrix
()
unequality
=
notEqual
(
x
,
y
)
f
=
theano
.
function
([
x
,
y
],
unequality
)
m1
=
sp
.
csc_matrix
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
))
m2
=
sp
.
csc_matrix
(
random_lil
((
10
,
40
),
config
.
floatX
,
3
))
self
.
assertTrue
(
numpy
.
array_equal
(
f
(
m1
,
m2
)
.
data
,
(
m1
!=
m2
)
.
data
))
class
T_conversion
(
unittest
.
TestCase
):
class
T_conversion
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
utt
.
seed_rng
()
utt
.
seed_rng
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论