Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
2eccc507
提交
2eccc507
authored
1月 27, 2013
作者:
Vivek Kulkarni
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Taken care of all comments
上级
e68ccc58
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
67 行增加
和
72 行删除
+67
-72
basic.py
theano/sparse/basic.py
+66
-0
basic.py
theano/tensor/basic.py
+1
-72
没有找到文件。
theano/sparse/basic.py
浏览文件 @
2eccc507
...
@@ -20,6 +20,7 @@ from theano.sparse.utils import hash_from_sparse
...
@@ -20,6 +20,7 @@ from theano.sparse.utils import hash_from_sparse
import
theano.tests.unittest_tools
as
utt
import
theano.tests.unittest_tools
as
utt
from
theano.gradient
import
grad_not_implemented
from
theano.gradient
import
grad_not_implemented
from
theano.sparse.type
import
SparseType
,
_is_sparse
from
theano.sparse.type
import
SparseType
,
_is_sparse
from
numpy.lib.stride_tricks
import
as_strided
sparse_formats
=
[
'csc'
,
'csr'
]
sparse_formats
=
[
'csc'
,
'csr'
]
...
@@ -3290,3 +3291,68 @@ class Usmm(gof.op.Op):
...
@@ -3290,3 +3291,68 @@ class Usmm(gof.op.Op):
out
[
0
]
=
rval
out
[
0
]
=
rval
usmm
=
Usmm
()
usmm
=
Usmm
()
class
ConstructSparseFromList
(
gof
.
Op
):
"""Constructs a sparse matrix out of a list of 2-D matrix rows"""
def
__hash__
(
self
):
return
hash
((
type
(
self
)))
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
))
def
__str__
(
self
):
return
self
.
__class__
.
__name__
def
make_node
(
self
,
x
,
y
,
ilist
):
x_
=
theano
.
tensor
.
as_tensor_variable
(
x
)
y_
=
theano
.
tensor
.
as_tensor_variable
(
y
)
ilist_
=
theano
.
tensor
.
as_tensor_variable
(
ilist
)
if
ilist_
.
type
.
dtype
[:
3
]
not
in
(
'int'
,
'uin'
):
raise
TypeError
(
'index must be integers'
)
if
ilist_
.
type
.
ndim
!=
1
:
raise
TypeError
(
'index must be vector'
)
if
x_
.
type
.
ndim
==
0
:
raise
TypeError
(
'cannot index into a scalar'
)
if
y_
.
type
.
ndim
>
x_
.
type
.
ndim
:
raise
TypeError
(
'cannot construct sparse matrix as dimensions differ'
)
return
gof
.
Apply
(
self
,
[
x_
,
y_
,
ilist_
],
[
theano
.
sparse
.
csc_matrix
(
dtype
=
x
.
dtype
)])
def
perform
(
self
,
node
,
inp
,
out_
):
x
,
values
,
idx
=
inp
out
,
=
out_
rows
,
cols
=
values
.
shape
assert
rows
==
len
(
idx
)
indptr
=
numpy
.
arange
(
cols
+
1
)
*
rows
indices
=
as_strided
(
idx
,
strides
=
(
0
,
idx
.
strides
[
0
]),
shape
=
(
cols
,
idx
.
shape
[
0
]))
.
flatten
()
data
=
values
.
T
.
flatten
()
out
[
0
]
=
scipy
.
sparse
.
csc_matrix
((
data
,
indices
,
indptr
),
shape
=
x
.
shape
,
dtype
=
x
.
dtype
)
def
infer_shape
(
self
,
node
,
ishapes
):
x
,
y
,
ilist
=
ishapes
return
[
x
]
def
R_op
(
self
,
inputs
,
eval_points
):
if
None
in
eval_points
[:
2
]:
return
[
None
]
return
self
.
make_node
(
eval_points
[
0
],
eval_points
[
1
],
*
inputs
[
2
:])
.
outputs
def
connection_pattern
(
self
,
node
):
rval
=
[[
True
],
[
True
],
[
False
]]
return
rval
def
grad
(
self
,
inputs
,
grads
):
g_output
,
=
grads
x
,
y
=
inputs
[:
2
]
idx_list
=
inputs
[
2
:]
gx
=
g_output
gy
=
theano
.
tensor
.
advanced_subtensor1
(
g_output
,
*
idx_list
)
return
[
gx
,
gy
]
+
[
DisconnectedType
()()]
*
len
(
idx_list
)
theano/tensor/basic.py
浏览文件 @
2eccc507
...
@@ -6523,7 +6523,7 @@ class AdvancedSubtensor1(Op):
...
@@ -6523,7 +6523,7 @@ class AdvancedSubtensor1(Op):
gz
,
=
grads
gz
,
=
grads
assert
len
(
inputs
)
==
2
assert
len
(
inputs
)
==
2
rval1
=
[
ConstructSparse
()(
inputs
[
0
],
gz
,
inputs
[
1
])]
rval1
=
[
theano
.
sparse
.
ConstructSparseFromList
()(
inputs
[
0
],
gz
,
inputs
[
1
])]
return
rval1
+
[
DisconnectedType
()()]
*
(
len
(
inputs
)
-
1
)
return
rval1
+
[
DisconnectedType
()()]
*
(
len
(
inputs
)
-
1
)
def
R_op
(
self
,
inputs
,
eval_points
):
def
R_op
(
self
,
inputs
,
eval_points
):
...
@@ -6535,77 +6535,6 @@ class AdvancedSubtensor1(Op):
...
@@ -6535,77 +6535,6 @@ class AdvancedSubtensor1(Op):
x
,
ilist
=
ishapes
x
,
ilist
=
ishapes
return
[
ilist
+
x
[
1
:]]
return
[
ilist
+
x
[
1
:]]
class
ConstructSparse
(
Op
):
"""Constructs a sparse matrix out of a list of 2-D matrix rows"""
def
__init__
(
self
):
import
scipy.sparse
as
ssparse
from
numpy.lib.stride_tricks
import
as_strided
self
.
m_ssparse
=
ssparse
self
.
m_as_strided
=
as_strided
def
__hash__
(
self
):
return
hash
((
type
(
self
)))
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
))
def
__str__
(
self
):
return
self
.
__class__
.
__name__
def
make_node
(
self
,
x
,
y
,
ilist
):
x_
=
as_tensor_variable
(
x
)
y_
=
as_tensor_variable
(
y
)
ilist_
=
as_tensor_variable
(
ilist
)
if
ilist_
.
type
.
dtype
[:
3
]
not
in
(
'int'
,
'uin'
):
raise
TypeError
(
'index must be integers'
)
if
ilist_
.
type
.
ndim
!=
1
:
raise
TypeError
(
'index must be vector'
)
if
x_
.
type
.
ndim
==
0
:
raise
TypeError
(
'cannot index into a scalar'
)
if
y_
.
type
.
ndim
>
x_
.
type
.
ndim
:
raise
TypeError
(
'cannot construct sparse matrix as dimensions differ'
)
return
Apply
(
self
,
[
x_
,
y_
,
ilist_
],
[
theano
.
sparse
.
csc_matrix
(
dtype
=
x
.
dtype
)])
def
perform
(
self
,
node
,
inp
,
out_
):
x
,
values
,
idx
=
inp
out
,
=
out_
rows
,
cols
=
values
.
shape
assert
rows
==
len
(
idx
)
indptr
=
numpy
.
arange
(
cols
+
1
)
*
rows
indices
=
self
.
m_as_strided
(
idx
,
strides
=
(
0
,
idx
.
strides
[
0
]),
shape
=
(
cols
,
idx
.
shape
[
0
]))
.
flatten
()
data
=
values
.
T
.
flatten
()
out
[
0
]
=
self
.
m_ssparse
.
csc_matrix
((
data
,
indices
,
indptr
),
shape
=
x
.
shape
,
dtype
=
x
.
dtype
)
def
infer_shape
(
self
,
node
,
ishapes
):
x
,
y
,
ilist
=
ishapes
return
[
x
]
def
R_op
(
self
,
inputs
,
eval_points
):
if
None
in
eval_points
[:
2
]:
return
[
None
]
return
self
.
make_node
(
eval_points
[
0
],
eval_points
[
1
],
*
inputs
[
2
:])
.
outputs
def
connection_pattern
(
self
,
node
):
rval
=
[[
True
],
[
True
],
[
False
]]
return
rval
def
grad
(
self
,
inputs
,
grads
):
g_output
,
=
grads
x
,
y
=
inputs
[:
2
]
idx_list
=
inputs
[
2
:]
gx
=
g_output
gy
=
advanced_subtensor1
(
g_output
,
*
idx_list
)
return
[
gx
,
gy
]
+
[
DisconnectedType
()()]
*
len
(
idx_list
)
advanced_subtensor1
=
AdvancedSubtensor1
()
advanced_subtensor1
=
AdvancedSubtensor1
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论