Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
13a8641c
提交
13a8641c
authored
4月 17, 2008
作者:
turian@grenat.iro.umontreal.ca
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Renamed SparseR to SparseResult
上级
fac5027f
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
25 行增加
和
25 行删除
+25
-25
sparse.py
sparse.py
+25
-25
没有找到文件。
sparse.py
浏览文件 @
13a8641c
...
...
@@ -18,23 +18,23 @@ import tensor
def
assparse
(
sp
,
**
kwargs
):
"""
Wrapper around SparseR constructor.
Wrapper around SparseR
esult
constructor.
@param sp: A sparse matrix. assparse reads dtype and format properties
out of this sparse matrix.
@return: SparseR version of sp.
@return: SparseR
esult
version of sp.
@todo Verify that sp is sufficiently sparse, and raise a warning if it is not
"""
if
isinstance
(
sp
,
SparseR
):
if
isinstance
(
sp
,
SparseR
esult
):
return
sp
else
:
# @todo Verify that sp is sufficiently sparse, and raise a
# warning if it is not
rval
=
SparseR
(
str
(
sp
.
dtype
),
sp
.
format
,
**
kwargs
)
rval
=
SparseR
esult
(
str
(
sp
.
dtype
),
sp
.
format
,
**
kwargs
)
rval
.
data
=
sp
return
rval
class
SparseR
(
gof
.
result
.
Result
):
class
SparseR
esult
(
gof
.
result
.
Result
):
"""
Attribute:
format - a string identifying the type of sparsity
...
...
@@ -58,26 +58,26 @@ class SparseR(gof.result.Result):
Fundamental way to do create a sparse node.
@param dtype: Type of numbers in the matrix.
@param format: The sparse storage strategy.
@return An empty SparseR instance.
@return An empty SparseR
esult
instance.
"""
gof
.
Result
.
__init__
(
self
,
**
kwargs
)
if
dtype
in
SparseR
.
dtype_set
:
if
dtype
in
SparseR
esult
.
dtype_set
:
self
.
_dtype
=
dtype
assert
isinstance
(
format
,
str
)
#print format, type(format), SparseR
.format_cls.keys(), format in SparseR
.format_cls
if
format
in
SparseR
.
format_cls
:
#print format, type(format), SparseR
esult.format_cls.keys(), format in SparseResult
.format_cls
if
format
in
SparseR
esult
.
format_cls
:
self
.
_format
=
format
else
:
raise
NotImplementedError
(
'unsupported format "
%
s" not in list'
%
format
,
SparseR
.
format_cls
.
keys
())
raise
NotImplementedError
(
'unsupported format "
%
s" not in list'
%
format
,
SparseR
esult
.
format_cls
.
keys
())
def
filter
(
self
,
value
):
if
isinstance
(
value
,
SparseR
.
format_cls
[
self
.
format
])
\
if
isinstance
(
value
,
SparseR
esult
.
format_cls
[
self
.
format
])
\
and
value
.
dtype
==
self
.
dtype
:
return
value
#print 'pass-through failed', type(value)
sp
=
SparseR
.
format_cls
[
self
.
format
](
value
)
sp
=
SparseR
esult
.
format_cls
[
self
.
format
](
value
)
if
str
(
sp
.
dtype
)
!=
self
.
dtype
:
raise
NotImplementedError
()
if
sp
.
format
!=
self
.
format
:
...
...
@@ -86,9 +86,9 @@ class SparseR(gof.result.Result):
def
__copy__
(
self
):
if
self
.
name
is
not
None
:
rval
=
SparseR
(
self
.
_dtype
,
self
.
_format
,
name
=
self
.
name
)
rval
=
SparseR
esult
(
self
.
_dtype
,
self
.
_format
,
name
=
self
.
name
)
else
:
rval
=
SparseR
(
self
.
_dtype
,
self
.
_format
)
rval
=
SparseR
esult
(
self
.
_dtype
,
self
.
_format
)
rval
.
data
=
copy
.
copy
(
self
.
data
)
return
rval
...
...
@@ -126,11 +126,11 @@ class SparseFromDense(gof.op.Op):
else
:
self
.
inputs
=
[
tensor
.
astensor
(
x
),
gof
.
result
.
PythonResult
()]
self
.
inputs
[
1
]
.
data
=
format
self
.
outputs
=
[
SparseR
(
x
.
dtype
,
self
.
inputs
[
1
]
.
data
)]
self
.
outputs
=
[
SparseR
esult
(
x
.
dtype
,
self
.
inputs
[
1
]
.
data
)]
def
impl
(
self
,
x
,
fmt
):
# this would actually happen anyway when we try to assign to
# self.outputs[0].data, but that seems hackish -JB
return
SparseR
.
format_cls
[
fmt
](
x
)
return
SparseR
esult
.
format_cls
[
fmt
](
x
)
def
grad
(
self
,
(
x
,
fmt
),
gz
):
return
dense_from_sparse
(
gz
)
sparse_from_dense
=
gof
.
op
.
constructor
(
SparseFromDense
)
...
...
@@ -145,7 +145,7 @@ class Transpose(gof.op.Op):
gof
.
op
.
Op
.
__init__
(
self
,
**
kwargs
)
x
=
assparse
(
x
)
self
.
inputs
=
[
x
]
self
.
outputs
=
[
SparseR
(
x
.
dtype
,
Transpose
.
format_map
[
x
.
format
])]
self
.
outputs
=
[
SparseR
esult
(
x
.
dtype
,
Transpose
.
format_map
[
x
.
format
])]
def
impl
(
self
,
x
):
return
x
.
transpose
()
def
grad
(
self
,
x
,
gz
):
...
...
@@ -161,7 +161,7 @@ class AddSS(gof.op.Op): #add two sparse matrices
raise
NotImplementedError
()
if
x
.
format
!=
y
.
format
:
raise
NotImplementedError
()
self
.
outputs
=
[
SparseR
(
x
.
dtype
,
x
.
format
)]
self
.
outputs
=
[
SparseR
esult
(
x
.
dtype
,
x
.
format
)]
def
impl
(
self
,
x
,
y
):
return
x
+
y
def
grad
(
self
,
(
x
,
y
),
gz
):
...
...
@@ -174,14 +174,14 @@ class Dot(gof.op.Op):
grad_preserves_dense - a boolean flags [default: True].
grad_preserves_dense controls whether gradients with respect to inputs
are converted to dense matrices when the corresponding input y is
dense (not in a L{SparseR} wrapper). This is generally a good idea
dense (not in a L{SparseR
esult
} wrapper). This is generally a good idea
when L{Dot} is in the middle of a larger graph, because the types
of gy will match that of y. This conversion might be inefficient if
the gradients are graph outputs though, hence this mask.
"""
def
__init__
(
self
,
x
,
y
,
grad_preserves_dense
=
True
):
"""
Because of trickiness of implementing, we assume that the left argument x is SparseR (not dense)
Because of trickiness of implementing, we assume that the left argument x is SparseR
esult
(not dense)
"""
if
x
.
dtype
!=
y
.
dtype
:
raise
NotImplementedError
()
...
...
@@ -195,7 +195,7 @@ class Dot(gof.op.Op):
raise
NotImplementedError
()
self
.
inputs
=
[
x
,
y
]
# Need to convert? e.g. assparse
self
.
outputs
=
[
SparseR
(
x
.
dtype
,
myformat
)]
self
.
outputs
=
[
SparseR
esult
(
x
.
dtype
,
myformat
)]
self
.
grad_preserves_dense
=
grad_preserves_dense
def
perform
(
self
):
"""
...
...
@@ -205,8 +205,8 @@ class Dot(gof.op.Op):
self
.
outputs
[
0
]
.
data
=
self
.
inputs
[
0
]
.
data
.
dot
(
self
.
inputs
[
1
]
.
data
)
def
grad
(
self
,
(
x
,
y
),
(
gz
,)):
rval
=
[
dot
(
gz
,
y
.
T
),
dot
(
x
.
T
,
gz
)]
assert
isinstance
(
self
.
inputs
[
0
],
SparseR
)
if
not
isinstance
(
self
.
inputs
[
1
],
SparseR
):
assert
isinstance
(
self
.
inputs
[
0
],
SparseR
esult
)
if
not
isinstance
(
self
.
inputs
[
1
],
SparseR
esult
):
if
self
.
grad_preserves_dense
:
rval
[
1
]
=
dense_from_sparse
(
rval
[
1
])
return
rval
...
...
@@ -222,8 +222,8 @@ def dot(x, y, grad_preserves_dense=True):
if
hasattr
(
x
,
'getnnz'
):
x
=
assparse
(
x
)
if
hasattr
(
y
,
'getnnz'
):
y
=
assparse
(
y
)
x_is_sparse
=
isinstance
(
x
,
SparseR
)
y_is_sparse
=
isinstance
(
y
,
SparseR
)
x_is_sparse
=
isinstance
(
x
,
SparseR
esult
)
y_is_sparse
=
isinstance
(
y
,
SparseR
esult
)
if
not
x_is_sparse
and
not
y_is_sparse
:
raise
TypeError
()
if
x_is_sparse
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论