Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f3d9bc9c
提交
f3d9bc9c
authored
1月 31, 2012
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pep8
上级
e31cdfbb
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
42 行增加
和
34 行删除
+42
-34
basic.py
theano/sparse/basic.py
+42
-34
没有找到文件。
theano/sparse/basic.py
浏览文件 @
f3d9bc9c
...
@@ -644,52 +644,56 @@ class SparseFromDense(gof.op.Op):
...
@@ -644,52 +644,56 @@ class SparseFromDense(gof.op.Op):
csr_from_dense
=
SparseFromDense
(
'csr'
)
csr_from_dense
=
SparseFromDense
(
'csr'
)
csc_from_dense
=
SparseFromDense
(
'csc'
)
csc_from_dense
=
SparseFromDense
(
'csc'
)
# Indexing
# Indexing
class
GetItem2d
(
gof
.
op
.
Op
):
class
GetItem2d
(
gof
.
op
.
Op
):
"""
"""
Implement a subtensor of sparse variable and that return a sparse matrix.
Implement a subtensor of sparse variable and that return a sparse matrix.
If you want to take only one element of a sparse matrix see the
class GetItemScalar
If you want to take only one element of a sparse matrix see the
that return a tensor scalar.
class GetItemScalar
that return a tensor scalar.
:note:
:note:
that subtensor selection always returns a matrix so
that subtensor selection always returns a matrix so indexing with [a:b, c:d] is forced.
indexing with [a:b, c:d] is forced. If one index is a scalar,
If one index is a scalar,
e.g. x[a:b, c] and x[a, b:c], generate an error. Use instead
e.g. x[a:b, c] and x[a, b:c], generate an error. Use instead
x[a:b, c:c+1] and x[a:a+1, b:c].
x[a:b, c:c+1] and x[a:a+1, b:c].
The above indexing methods are not supported because the rval would be a sparse
matrix rather than a sparse vector, which is a deviation from numpy indexing rule.
The above indexing methods are not supported because the rval
This decision is made largely for keeping the consistency between numpy and theano.
would be a sparse matrix rather than a sparse vector, which is a
Subjected to modification when sparse vector is supported.
deviation from numpy indexing rule. This decision is made largely
for keeping the consistency between numpy and theano. Subjected
to modification when sparse vector is supported.
"""
"""
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
))
return
(
type
(
self
)
==
type
(
other
))
def
__hash__
(
self
):
def
__hash__
(
self
):
return
hash
(
type
(
self
))
return
hash
(
type
(
self
))
# Fred:Too complicated for now. If you need it, look at the Subtensor.infer_shape.
# Fred:Too complicated for now. If you need it, look at
# the Subtensor.infer_shape.
# def infer_shape(self, node, i0_shapes):
# def infer_shape(self, node, i0_shapes):
# return i0_shapes
# return i0_shapes
def
make_node
(
self
,
x
,
index
):
def
make_node
(
self
,
x
,
index
):
x
=
as_sparse_variable
(
x
)
x
=
as_sparse_variable
(
x
)
assert
len
(
index
)
in
[
1
,
2
]
assert
len
(
index
)
in
[
1
,
2
]
input_op
=
[
x
]
input_op
=
[
x
]
for
ind
in
index
:
for
ind
in
index
:
if
isinstance
(
ind
,
slice
):
if
isinstance
(
ind
,
slice
):
# in case of slice is written in theano variable
# in case of slice is written in theano variable
start
=
ind
.
start
start
=
ind
.
start
stop
=
ind
.
stop
stop
=
ind
.
stop
# in case of slice is written in python int
# in case of slice is written in python int
if
isinstance
(
start
,
int
):
if
isinstance
(
start
,
int
):
start
=
theano
.
tensor
.
constant
(
start
)
start
=
theano
.
tensor
.
constant
(
start
)
if
isinstance
(
stop
,
int
):
if
isinstance
(
stop
,
int
):
stop
=
theano
.
tensor
.
constant
(
stop
)
stop
=
theano
.
tensor
.
constant
(
stop
)
#in case of indexing using python int
#in case of indexing using python int
#elif isinstance(ind,int):
#elif isinstance(ind,int):
# start = theano.tensor.constant(ind)
# start = theano.tensor.constant(ind)
...
@@ -697,47 +701,50 @@ class GetItem2d(gof.op.Op):
...
@@ -697,47 +701,50 @@ class GetItem2d(gof.op.Op):
#elif ind.ndim == 0:
#elif ind.ndim == 0:
# start = ind
# start = ind
# stop = ind + 1
# stop = ind + 1
else
:
else
:
raise
NotImplemented
(
'Theano has no sparse vector'
+
raise
NotImplemented
(
'Use X[a:b,c:d], X[a:b,c:c+1] or X[a:b] instead.'
)
'Theano has no sparse vector'
+
'Use X[a:b,c:d], X[a:b,c:c+1] or X[a:b] instead.'
)
input_op
+=
[
start
,
stop
]
input_op
+=
[
start
,
stop
]
if
len
(
index
)
==
1
:
if
len
(
index
)
==
1
:
i
=
theano
.
gof
.
Constant
(
theano
.
gof
.
generic
,
None
)
i
=
theano
.
gof
.
Constant
(
theano
.
gof
.
generic
,
None
)
input_op
+=
[
i
,
i
]
input_op
+=
[
i
,
i
]
return
gof
.
Apply
(
self
,
input_op
,
[
x
.
type
()])
return
gof
.
Apply
(
self
,
input_op
,
[
x
.
type
()])
def
perform
(
self
,
node
,
(
x
,
start1
,
stop1
,
start2
,
stop2
),
(
out
,
)):
def
perform
(
self
,
node
,
(
x
,
start1
,
stop1
,
start2
,
stop2
),
(
out
,
)):
assert
_is_sparse
(
x
)
assert
_is_sparse
(
x
)
out
[
0
]
=
x
[
start1
:
stop1
,
start2
:
stop2
]
out
[
0
]
=
x
[
start1
:
stop1
,
start2
:
stop2
]
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
__class__
.
__name__
return
self
.
__class__
.
__name__
get_item_2d
=
GetItem2d
()
get_item_2d
=
GetItem2d
()
class
GetItemScalar
(
gof
.
op
.
Op
):
class
GetItemScalar
(
gof
.
op
.
Op
):
"""
"""
Implement a subtensor of a sparse variable that take two scalar as index and return a scalar
Implement a subtensor of a sparse variable that take two scalar as
index and return a scalar
:see: GetItem2d to return more then one element.
:see: GetItem2d to return more then one element.
"""
"""
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
))
return
(
type
(
self
)
==
type
(
other
))
def
__hash__
(
self
):
def
__hash__
(
self
):
return
hash
(
type
(
self
))
return
hash
(
type
(
self
))
def
infer_shape
(
self
,
node
,
i0_shapes
):
def
infer_shape
(
self
,
node
,
i0_shapes
):
return
[()]
return
[()]
def
make_node
(
self
,
x
,
index
):
def
make_node
(
self
,
x
,
index
):
x
=
as_sparse_variable
(
x
)
x
=
as_sparse_variable
(
x
)
assert
len
(
index
)
==
2
assert
len
(
index
)
==
2
input_op
=
[
x
]
input_op
=
[
x
]
for
ind
in
index
:
for
ind
in
index
:
if
isinstance
(
ind
,
slice
):
if
isinstance
(
ind
,
slice
):
...
@@ -747,7 +754,7 @@ class GetItemScalar(gof.op.Op):
...
@@ -747,7 +754,7 @@ class GetItemScalar(gof.op.Op):
elif
isinstance
(
ind
,
int
):
elif
isinstance
(
ind
,
int
):
ind
=
theano
.
tensor
.
constant
(
ind
)
ind
=
theano
.
tensor
.
constant
(
ind
)
input_op
+=
[
ind
]
input_op
+=
[
ind
]
# in case of indexing using theano variable
# in case of indexing using theano variable
elif
ind
.
ndim
==
0
:
elif
ind
.
ndim
==
0
:
input_op
+=
[
ind
]
input_op
+=
[
ind
]
...
@@ -755,18 +762,19 @@ class GetItemScalar(gof.op.Op):
...
@@ -755,18 +762,19 @@ class GetItemScalar(gof.op.Op):
raise
NotImplemented
()
raise
NotImplemented
()
return
gof
.
Apply
(
self
,
input_op
,
[
tensor
.
scalar
(
dtype
=
x
.
dtype
)])
return
gof
.
Apply
(
self
,
input_op
,
[
tensor
.
scalar
(
dtype
=
x
.
dtype
)])
def
perform
(
self
,
node
,
(
x
,
ind1
,
ind2
),
(
out
,
)):
def
perform
(
self
,
node
,
(
x
,
ind1
,
ind2
),
(
out
,
)):
assert
_is_sparse
(
x
)
assert
_is_sparse
(
x
)
out
[
0
]
=
x
[
ind1
,
ind2
]
out
[
0
]
=
x
[
ind1
,
ind2
]
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
__class__
.
__name__
return
self
.
__class__
.
__name__
get_item_scalar
=
GetItemScalar
()
get_item_scalar
=
GetItemScalar
()
# Linear Algebra
# Linear Algebra
class
Transpose
(
gof
.
op
.
Op
):
class
Transpose
(
gof
.
op
.
Op
):
format_map
=
{
'csr'
:
'csc'
,
format_map
=
{
'csr'
:
'csc'
,
'csc'
:
'csr'
}
'csc'
:
'csr'
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论