Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
83032266
提交
83032266
authored
7月 07, 2014
作者:
Tanjay94
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added grad to GetItemList and its test.
上级
4934189c
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
67 行增加
和
5 行删除
+67
-5
ops.py
theano/sandbox/linalg/ops.py
+1
-1
basic.py
theano/sparse/basic.py
+55
-3
test_basic.py
theano/sparse/tests/test_basic.py
+11
-1
没有找到文件。
theano/sandbox/linalg/ops.py
浏览文件 @
83032266
...
...
@@ -1407,7 +1407,7 @@ def norm(x,ord):
elif
ord
==
-
1
:
return
tensor
.
min
(
tensor
.
sum
(
abs
(
x
),
0
))
else
:
raise
ValueError
(
0
)
raise
ValueError
()
elif
ndim
>
2
:
raise
NotImplementedError
(
"We don't support norm witn ndim > 2"
)
...
...
theano/sparse/basic.py
浏览文件 @
83032266
...
...
@@ -18,7 +18,7 @@ from theano.gof.python25 import all
from
theano.gradient
import
DisconnectedType
from
theano.sparse.utils
import
hash_from_sparse
import
theano.tests.unittest_tools
as
utt
from
theano.gradient
import
grad_not_implemented
from
theano.gradient
import
grad_not_implemented
,
grad_undefined
from
theano.sparse.type
import
SparseType
,
_is_sparse
from
numpy.lib.stride_tricks
import
as_strided
...
...
@@ -1007,13 +1007,15 @@ class GetItemList(gof.op.Op):
def
__hash__
(
self
):
return
hash
(
type
(
self
))
def
infer_shape
(
self
,
node
,
shapes
):
return
[(
shapes
[
1
][
0
],
shapes
[
0
][
1
])]
def
make_node
(
self
,
x
,
index
):
x
=
as_sparse_variable
(
x
)
assert
x
.
format
in
[
"csr"
,
"csc"
]
ind
=
tensor
.
as_tensor_variable
(
index
)
assert
ind
.
ndim
==
1
assert
'int'
in
ind
.
dtype
return
gof
.
Apply
(
self
,
[
x
,
ind
],
[
x
.
type
()])
...
...
@@ -1023,13 +1025,63 @@ class GetItemList(gof.op.Op):
assert
_is_sparse
(
x
)
out
[
0
]
=
x
[
indices
]
def
grad
(
self
,
inputs
,
g_outputs
):
x
,
indices
=
inputs
gout
,
=
g_outputs
return
[
GetItemListGrad
(
self
)(
x
,
indices
,
gout
),
grad_undefined
(
self
,
1
,
indices
,
"No gradient for this input"
)]
def
__str__
(
self
):
return
self
.
__class__
.
__name__
get_item_list
=
GetItemList
()
# Indexing
class
GetItemListGrad
(
gof
.
op
.
Op
):
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
))
def
__hash__
(
self
):
return
hash
(
type
(
self
))
def
infer_shape
(
self
,
node
,
shapes
):
return
[(
shapes
[
0
][
0
],
shapes
[
0
][
1
])]
def
make_node
(
self
,
x
,
index
,
gz
):
x
=
as_sparse_variable
(
x
)
gz
=
as_sparse_variable
(
gz
)
assert
x
.
format
in
[
"csr"
,
"csc"
]
assert
gz
.
format
in
[
"csr"
,
"csc"
]
ind
=
tensor
.
as_tensor_variable
(
index
)
assert
ind
.
ndim
==
1
return
gof
.
Apply
(
self
,
[
x
,
ind
,
gz
],
[
x
.
type
()])
def
perform
(
self
,
node
,
inp
,
(
out
,
)):
x
=
inp
[
0
]
indices
=
inp
[
1
]
gz
=
inp
[
2
]
if
x
.
format
in
[
"csr"
]:
y
=
scipy
.
sparse
.
csr_matrix
((
x
.
shape
[
0
],
x
.
shape
[
1
]))
else
:
y
=
scipy
.
sparse
.
csc_matrix
((
x
.
shape
[
0
],
x
.
shape
[
1
]))
z
=
0
for
i
in
indices
:
y
[
i
]
=
gz
[
z
]
z
=
z
+
1
out
[
0
]
=
y
def
__str__
(
self
):
return
self
.
__class__
.
__name__
get_item_list_grad
=
GetItemListGrad
()
class
GetItem2d
(
gof
.
op
.
Op
):
# See doc in instance of this Op or function after this class definition.
def
__eq__
(
self
,
other
):
...
...
theano/sparse/tests/test_basic.py
浏览文件 @
83032266
...
...
@@ -6,6 +6,7 @@ import numpy
try
:
import
scipy.sparse
as
sp
import
scipy.sparse
from
scipy.sparse
import
csr_matrix
except
ImportError
:
pass
# The variable enable_sparse will be used to disable the test file.
...
...
@@ -31,7 +32,7 @@ from theano.sparse import (
AddSS
,
AddSD
,
MulSS
,
MulSD
,
Transpose
,
Neg
,
Remove0
,
add
,
mul
,
structured_dot
,
transpose
,
csc_from_dense
,
csr_from_dense
,
dense_from_sparse
,
Dot
,
Usmm
,
sp_ones_like
,
GetItemScalar
,
Dot
,
Usmm
,
sp_ones_like
,
GetItemScalar
,
GetItemList
,
SparseFromDense
,
Cast
,
cast
,
HStack
,
VStack
,
AddSSData
,
add_s_s_data
,
structured_minimum
,
structured_maximum
,
structured_add
,
...
...
@@ -2046,6 +2047,15 @@ class Test_getitem(unittest.TestCase):
self
.
assertRaises
(
IndexError
,
f
,
A
[
0
])
def
test_get_item_list_grad
(
self
):
op
=
theano
.
sparse
.
basic
.
GetItemList
()
def
op_with_fixed_index
(
x
):
return
op
(
x
,
index
=
numpy
.
asarray
([
0
,
1
]))
x
,
x_val
=
sparse_random_inputs
(
"csr"
,
(
4
,
5
),
out_dtype
=
"float64"
)
verify_grad_sparse
(
op_with_fixed_index
,
x_val
)
def
test_GetItem2D
(
self
):
sparse_formats
=
(
'csc'
,
'csr'
)
for
format
in
sparse_formats
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论